image_blp/convert/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
mod dxtn;
pub mod error;
mod jpeg;
mod mipmap;
mod palette;
mod raw1;
mod raw3;

use crate::types::*;
pub use ::image::imageops::FilterType;
use ::image::DynamicImage;
use dxtn::*;
pub use error::Error;
use jpeg::*;
use raw1::*;
use raw3::*;
use std::fmt;
pub use texpresso::Algorithm as DxtAlgorithm;

/// Convert from parsed raw BLP image to useful [DynamicImage]
pub fn blp_to_image(image: &BlpImage, mipmap_level: usize) -> Result<DynamicImage, Error> {
    match &image.content {
        BlpContent::Raw1(content) => raw1_to_image(&image.header, content, mipmap_level),
        BlpContent::Raw3(content) => raw3_to_image(&image.header, content, mipmap_level),
        BlpContent::Jpeg(content) => jpeg_to_image(content, mipmap_level),
        BlpContent::Dxt1(content) => dxtn_to_image(&image.header, content, mipmap_level),
        BlpContent::Dxt3(content) => dxtn_to_image(&image.header, content, mipmap_level),
        BlpContent::Dxt5(content) => dxtn_to_image(&image.header, content, mipmap_level),
    }
}

/// A way to specify [image_to_blp] which BLP type you want to
/// get in a result.
#[derive(Clone, PartialEq, Eq)]
pub enum BlpTarget {
    /// BLP0 format variation. War3 RoC Beta builds. External
    /// mipmaps.
    Blp0(BlpOldFormat),
    /// BLP1 format variation. War3 TFT usual textures. Internal
    /// mipmaps.
    Blp1(BlpOldFormat),
    /// BLP2 format variation. WoW usual textures. Internal
    /// mipmaps.
    Blp2(Blp2Format),
}

impl Default for BlpTarget {
    fn default() -> Self {
        BlpTarget::Blp1(Default::default())
    }
}

impl fmt::Display for BlpTarget {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BlpTarget::Blp0(format) => write!(f, "BLP0 {}", format),
            BlpTarget::Blp1(format) => write!(f, "BLP1 {}", format),
            BlpTarget::Blp2(format) => write!(f, "BLP2 {}", format),
        }
    }
}

/// Encoding options for BLP0 and BLP1 formats.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BlpOldFormat {
    /// Paletted 256 colors image with/without alpha.  
    Raw1 { alpha_bits: AlphaBits },
    /// JPEG encoding with/without alpha.
    Jpeg { has_alpha: bool },
}

impl Default for BlpOldFormat {
    fn default() -> Self {
        BlpOldFormat::Jpeg { has_alpha: true }
    }
}

impl fmt::Display for BlpOldFormat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BlpOldFormat::Raw1 { alpha_bits } => write!(f, "Palleted image with {}", alpha_bits),
            BlpOldFormat::Jpeg { has_alpha } => {
                if *has_alpha {
                    write!(f, "Jpeg image with alpha")
                } else {
                    write!(f, "Jpeg image without alpha")
                }
            }
        }
    }
}

/// Allowed alpha bits values for Raw1 encoding
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum AlphaBits {
    /// No alpha channel. 0 bits.
    NoAlpha,
    /// 1 bit. Pixel is transparent or opaque.
    Bit1,
    /// 4 bits per pixel.
    Bit4,
    /// 8 bits per pixel.
    #[default]
    Bit8,
}

impl fmt::Display for AlphaBits {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AlphaBits::NoAlpha => write!(f, "no alpha"),
            AlphaBits::Bit1 => write!(f, "1 bit alpha"),
            AlphaBits::Bit4 => write!(f, "4 bits alpha"),
            AlphaBits::Bit8 => write!(f, "8 bits alpha"),
        }
    }
}

impl From<AlphaBits> for u32 {
    fn from(value: AlphaBits) -> u32 {
        match value {
            AlphaBits::NoAlpha => 0,
            AlphaBits::Bit1 => 1,
            AlphaBits::Bit4 => 4,
            AlphaBits::Bit8 => 8,
        }
    }
}

impl From<AlphaBits> for u8 {
    fn from(value: AlphaBits) -> u8 {
        match value {
            AlphaBits::NoAlpha => 0,
            AlphaBits::Bit1 => 1,
            AlphaBits::Bit4 => 4,
            AlphaBits::Bit8 => 8,
        }
    }
}

/// BLP2 format compression options.
#[derive(Clone, PartialEq, Eq)]
pub enum Blp2Format {
    /// Paletted 256 colors image with/without alpha.  
    Raw1 { alpha_bits: AlphaBits },
    /// RGBA bitmap
    Raw3,
    /// JPEG encoded image. Although, it is never used in real files.
    Jpeg { has_alpha: bool },
    /// ST3C compression, type with 1 bit alpha or 0 bit alpha.
    Dxt1 {
        has_alpha: bool,
        /// Compression speed/quality setting
        compress_algorithm: DxtAlgorithm,
    },
    /// ST3C compression, type with paletted alpha.
    Dxt3 {
        has_alpha: bool,
        /// Compression speed/quality setting
        compress_algorithm: DxtAlgorithm,
    },
    /// ST3C compression, type with interpolated alpha.
    Dxt5 {
        has_alpha: bool,
        /// Compression speed/quality setting
        compress_algorithm: DxtAlgorithm,
    },
}

impl Default for Blp2Format {
    fn default() -> Self {
        Blp2Format::Dxt5 {
            has_alpha: true,
            compress_algorithm: Default::default(),
        }
    }
}

impl fmt::Display for Blp2Format {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Blp2Format::Raw1 { alpha_bits } => write!(f, "Palleted image with {}", alpha_bits),
            Blp2Format::Raw3 => write!(f, "RGBA raw data"),
            Blp2Format::Jpeg { has_alpha } => {
                if *has_alpha {
                    write!(f, "Jpeg image with alpha")
                } else {
                    write!(f, "Jpeg image without alpha")
                }
            }
            Blp2Format::Dxt1 {
                has_alpha,
                compress_algorithm,
            } => {
                let compress_str = match *compress_algorithm {
                    DxtAlgorithm::RangeFit => "fast/low quality",
                    DxtAlgorithm::ClusterFit => "slow/high quality",
                    DxtAlgorithm::IterativeClusterFit => "very slow/best quality",
                };
                if *has_alpha {
                    write!(f, "DXT1 image with alpha and compression {}", compress_str)
                } else {
                    write!(
                        f,
                        "DXT1 image without alpha and compression {}",
                        compress_str
                    )
                }
            }
            Blp2Format::Dxt3 {
                has_alpha,
                compress_algorithm,
            } => {
                let compress_str = match *compress_algorithm {
                    DxtAlgorithm::RangeFit => "fast/low quality",
                    DxtAlgorithm::ClusterFit => "slow/high quality",
                    DxtAlgorithm::IterativeClusterFit => "very slow/best quality",
                };
                if *has_alpha {
                    write!(f, "DXT3 image with alpha and compression {}", compress_str)
                } else {
                    write!(
                        f,
                        "DXT3 image without alpha and compression {}",
                        compress_str
                    )
                }
            }
            Blp2Format::Dxt5 {
                has_alpha,
                compress_algorithm,
            } => {
                let compress_str = match *compress_algorithm {
                    DxtAlgorithm::RangeFit => "fast/low quality",
                    DxtAlgorithm::ClusterFit => "slow/high quality",
                    DxtAlgorithm::IterativeClusterFit => "very slow/best quality",
                };
                if *has_alpha {
                    write!(f, "DXT5 image with alpha and compression {}", compress_str)
                } else {
                    write!(
                        f,
                        "DXT5 image without alpha and compression {}",
                        compress_str
                    )
                }
            }
        }
    }
}

/// Convert from unpacked pixels into BLP image ready for writing down
pub fn image_to_blp(
    image: DynamicImage,
    make_mipmaps: bool,
    target: BlpTarget,
    mipmap_filter: FilterType,
) -> Result<BlpImage, Error> {
    if image.width() > BLP_MAX_WIDTH {
        return Err(Error::WidthTooLarge(image.width()));
    }
    if image.height() > BLP_MAX_HEIGHT {
        return Err(Error::HeightTooLarge(image.height()));
    }

    match target {
        BlpTarget::Blp0(format) => match format {
            BlpOldFormat::Raw1 { alpha_bits } => {
                let header = BlpHeader {
                    version: BlpVersion::Blp0,
                    content: BlpContentTag::Direct,
                    flags: BlpFlags::Old {
                        alpha_bits: alpha_bits.into(),
                        extra: 4,
                        has_mipmaps: if make_mipmaps { 1 } else { 0 },
                    },
                    width: image.width(),
                    height: image.height(),
                    mipmap_locator: MipmapLocator::External,
                };
                let blp_raw1 =
                    image_to_raw1(image, alpha_bits.into(), make_mipmaps, mipmap_filter)?;
                Ok(BlpImage {
                    header,
                    content: BlpContent::Raw1(blp_raw1),
                })
            }
            BlpOldFormat::Jpeg { has_alpha } => {
                let alpha_bits = if has_alpha { 8 } else { 0 };
                let blp_jpeg = image_to_jpeg(&image, make_mipmaps, alpha_bits, mipmap_filter)?;
                Ok(BlpImage {
                    header: BlpHeader {
                        version: BlpVersion::Blp0,
                        content: BlpContentTag::Jpeg,
                        flags: BlpFlags::Old {
                            alpha_bits: alpha_bits as u32,
                            extra: 5,
                            has_mipmaps: if make_mipmaps { 1 } else { 0 },
                        },
                        width: image.width(),
                        height: image.height(),
                        mipmap_locator: MipmapLocator::External,
                    },
                    content: BlpContent::Jpeg(blp_jpeg),
                })
            }
        },
        BlpTarget::Blp1(format) => match format {
            BlpOldFormat::Raw1 { alpha_bits } => {
                let width = image.width();
                let height = image.height();
                let blp_raw1 =
                    image_to_raw1(image, alpha_bits.into(), make_mipmaps, mipmap_filter)?;
                let header = BlpHeader {
                    version: BlpVersion::Blp1,
                    content: BlpContentTag::Direct,
                    flags: BlpFlags::Old {
                        alpha_bits: alpha_bits.into(),
                        extra: 4,
                        has_mipmaps: if make_mipmaps { 1 } else { 0 },
                    },
                    width,
                    height,
                    mipmap_locator: blp_raw1.mipmap_locator(BlpVersion::Blp1),
                };
                Ok(BlpImage {
                    header,
                    content: BlpContent::Raw1(blp_raw1),
                })
            }
            BlpOldFormat::Jpeg { has_alpha } => {
                let alpha_bits = if has_alpha { 8 } else { 0 };
                let blp_jpeg = image_to_jpeg(&image, make_mipmaps, alpha_bits, mipmap_filter)?;
                Ok(BlpImage {
                    header: BlpHeader {
                        version: BlpVersion::Blp1,
                        content: BlpContentTag::Jpeg,
                        flags: BlpFlags::Old {
                            alpha_bits: alpha_bits as u32,
                            extra: 5,
                            has_mipmaps: if make_mipmaps { 1 } else { 0 },
                        },
                        width: image.width(),
                        height: image.height(),
                        mipmap_locator: blp_jpeg.mipmap_locator(BlpVersion::Blp1),
                    },
                    content: BlpContent::Jpeg(blp_jpeg),
                })
            }
        },
        BlpTarget::Blp2(format) => match format {
            Blp2Format::Raw1 { alpha_bits } => {
                let width = image.width();
                let height = image.height();
                let blp_raw1 =
                    image_to_raw1(image, alpha_bits.into(), make_mipmaps, mipmap_filter)?;
                let header = BlpHeader {
                    version: BlpVersion::Blp2,
                    content: BlpContentTag::Direct,
                    flags: BlpFlags::Blp2 {
                        compression: Compression::Raw1,
                        alpha_bits: alpha_bits.into(),
                        alpha_type: 0,
                        has_mipmaps: if make_mipmaps { 1 } else { 0 },
                    },
                    width,
                    height,
                    mipmap_locator: blp_raw1.mipmap_locator(BlpVersion::Blp2),
                };
                Ok(BlpImage {
                    header,
                    content: BlpContent::Raw1(blp_raw1),
                })
            }
            Blp2Format::Raw3 => {
                let width = image.width();
                let height = image.height();
                let blp_raw3 = image_to_raw3(image, make_mipmaps, mipmap_filter)?;
                Ok(BlpImage {
                    header: BlpHeader {
                        version: BlpVersion::Blp2,
                        content: BlpContentTag::Direct,
                        flags: BlpFlags::Blp2 {
                            compression: Compression::Raw3,
                            alpha_bits: 8,
                            alpha_type: 0,
                            has_mipmaps: if make_mipmaps { 1 } else { 0 },
                        },
                        width,
                        height,
                        mipmap_locator: blp_raw3.mipmap_locator(BlpVersion::Blp2),
                    },
                    content: BlpContent::Raw3(blp_raw3),
                })
            }
            Blp2Format::Jpeg { has_alpha } => {
                let alpha_bits = if has_alpha { 8 } else { 0 };
                let blp_jpeg = image_to_jpeg(&image, make_mipmaps, alpha_bits, mipmap_filter)?;
                Ok(BlpImage {
                    header: BlpHeader {
                        version: BlpVersion::Blp2,
                        content: BlpContentTag::Jpeg,
                        flags: BlpFlags::Blp2 {
                            compression: Compression::Jpeg,
                            alpha_bits: 8,
                            alpha_type: 0,
                            has_mipmaps: if make_mipmaps { 1 } else { 0 },
                        },
                        width: image.width(),
                        height: image.height(),
                        mipmap_locator: blp_jpeg.mipmap_locator(BlpVersion::Blp2),
                    },
                    content: BlpContent::Jpeg(blp_jpeg),
                })
            }
            Blp2Format::Dxt1 {
                has_alpha,
                compress_algorithm,
            } => {
                let width = image.width();
                let height = image.height();
                let alpha_bits = if has_alpha { 1 } else { 0 };
                let blp_dxtn = image_to_dxtn(
                    image,
                    DxtnFormat::Dxt1,
                    make_mipmaps,
                    mipmap_filter,
                    compress_algorithm,
                )?;
                Ok(BlpImage {
                    header: BlpHeader {
                        version: BlpVersion::Blp2,
                        content: BlpContentTag::Direct,
                        flags: BlpFlags::Blp2 {
                            compression: Compression::Dxtc,
                            alpha_bits,
                            alpha_type: 0,
                            has_mipmaps: if make_mipmaps { 1 } else { 0 },
                        },
                        width,
                        height,
                        mipmap_locator: blp_dxtn.mipmap_locator(BlpVersion::Blp2),
                    },
                    content: BlpContent::Dxt1(blp_dxtn),
                })
            }
            Blp2Format::Dxt3 {
                has_alpha,
                compress_algorithm,
            } => {
                let width = image.width();
                let height = image.height();
                let alpha_bits = if has_alpha { 8 } else { 0 };
                let blp_dxtn = image_to_dxtn(
                    image,
                    DxtnFormat::Dxt3,
                    make_mipmaps,
                    mipmap_filter,
                    compress_algorithm,
                )?;
                Ok(BlpImage {
                    header: BlpHeader {
                        version: BlpVersion::Blp2,
                        content: BlpContentTag::Direct,
                        flags: BlpFlags::Blp2 {
                            compression: Compression::Dxtc,
                            alpha_bits,
                            alpha_type: 1,
                            has_mipmaps: if make_mipmaps { 1 } else { 0 },
                        },
                        width,
                        height,
                        mipmap_locator: blp_dxtn.mipmap_locator(BlpVersion::Blp2),
                    },
                    content: BlpContent::Dxt3(blp_dxtn),
                })
            }
            Blp2Format::Dxt5 {
                has_alpha,
                compress_algorithm,
            } => {
                let width = image.width();
                let height = image.height();
                let alpha_bits = if has_alpha { 8 } else { 0 };
                let blp_dxtn = image_to_dxtn(
                    image,
                    DxtnFormat::Dxt5,
                    make_mipmaps,
                    mipmap_filter,
                    compress_algorithm,
                )?;
                Ok(BlpImage {
                    header: BlpHeader {
                        version: BlpVersion::Blp2,
                        content: BlpContentTag::Direct,
                        flags: BlpFlags::Blp2 {
                            compression: Compression::Dxtc,
                            alpha_bits,
                            alpha_type: 7,
                            has_mipmaps: if make_mipmaps { 1 } else { 0 },
                        },
                        width,
                        height,
                        mipmap_locator: blp_dxtn.mipmap_locator(BlpVersion::Blp2),
                    },
                    content: BlpContent::Dxt5(blp_dxtn),
                })
            }
        },
    }
}