Class: Mikunyan::Decoder::ImageDecoder

Inherits:
Object
  • Object
show all
Defined in:
lib/mikunyan/decoders/image_decoder.rb

Overview

Class for image decoding tools

Class Method Summary collapse

Class Method Details

.create_astc_file(object) ⇒ String?

Create ASTC file data from ObjectValue

Parameters:

Returns:

  • (String, nil)

    created file



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/mikunyan/decoders/image_decoder.rb', line 487

def self.create_astc_file(object)
  astc_list = {
    48 => 4, 49 => 5, 50 => 6, 51 => 8, 52 => 10, 53 => 12,
    54 => 4, 55 => 5, 56 => 6, 57 => 8, 58 => 10, 59 => 12,
    66 => 4, 67 => 5, 68 => 6, 69 => 8, 70 => 10, 71 => 12
  }
  width = object['m_Width']&.value
  height = object['m_Height']&.value
  fmt = object['m_TextureFormat']&.value
  bin = object['image data']&.value
  return unless width && height && fmt && bin && astc_list[fmt]

  bin = object['m_StreamData']&.value if bin.empty?
  return unless bin

  header = [0x13, 0xab, 0xa1, 0x5c, astc_list[fmt], astc_list[fmt], 1].pack('C*')
  header << [width].pack('V').byteslice(0, 3)
  header << [height].pack('V').byteslice(0, 3)
  header << [1, 0, 0].pack('C*')
  header + bin
end

.decode_a8(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from A8 binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



122
123
124
# File 'lib/mikunyan/decoders/image_decoder.rb', line 122

def self.decode_a8(width, height, bin)
  ChunkyPNG::Image.from_rgb_stream(width, height, DecodeHelper.decode_a8(bin, width * height)).flip
end

.decode_argb32(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from ARGB32 binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



174
175
176
177
178
179
180
181
# File 'lib/mikunyan/decoders/image_decoder.rb', line 174

def self.decode_argb32(width, height, bin)
  mem = String.new(capacity: width * height * 4)
  (width * height).times do |i|
    c = BinUtils.get_int32_be(bin, i * 4)
    BinUtils.append_int32_be!(mem, ((c & 0x00ffffff) << 8) | ((c & 0xff000000) >> 24))
  end
  ChunkyPNG::Image.from_rgba_stream(width, height, mem).flip
end

.decode_argb4444(width, height, bin, endian = :big) ⇒ ChunkyPNG::Image

Decode image from ARGB4444 binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

  • endian (Symbol) (defaults to: :big)

    endianness of binary

Returns:

  • (ChunkyPNG::Image)

    decoded image



141
142
143
144
145
146
147
148
149
# File 'lib/mikunyan/decoders/image_decoder.rb', line 141

def self.decode_argb4444(width, height, bin, endian = :big)
  mem = String.new(capacity: width * height * 4)
  (width * height).times do |i|
    c = endian == :little ? BinUtils.get_int16_le(bin, i * 2) : BinUtils.get_int16_be(bin, i * 2)
    c = ((c & 0x0f00) << 16) | ((c & 0x00f0) << 12) | ((c & 0x000f) << 8) | ((c & 0xf000) >> 12)
    BinUtils.append_int32_be!(mem, c << 4 | c)
  end
  ChunkyPNG::Image.from_rgba_stream(width, height, mem).flip
end

.decode_astc(width, height, blocksize, bin) ⇒ ChunkyPNG::Image

Decode image from ASTC compressed binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • blocksize (Integer)

    block size

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



457
458
459
460
# File 'lib/mikunyan/decoders/image_decoder.rb', line 457

def self.decode_astc(width, height, blocksize, bin)
  ChunkyPNG::Image.from_rgba_stream(width, height,
                                    DecodeHelper.decode_astc(bin, width, height, blocksize, blocksize))
end

.decode_bgra32(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from BGRA32 binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



239
240
241
242
243
244
245
246
# File 'lib/mikunyan/decoders/image_decoder.rb', line 239

def self.decode_bgra32(width, height, bin)
  mem = String.new(capacity: width * height * 4)
  (width * height).times do |i|
    c = BinUtils.get_int32_le(bin, i * 4)
    BinUtils.append_int32_be!(mem, ((c & 0x00ffffff) << 8) | ((c & 0xff000000) >> 24))
  end
  ChunkyPNG::Image.from_rgba_stream(width, height, mem).flip
end

.decode_crunched(width, height, bin) ⇒ ChunkyPNG::Image?

Decode image from crunched texture binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image, nil)

    decoded image



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/mikunyan/decoders/image_decoder.rb', line 467

def self.decode_crunched(width, height, bin)
  file = Mikunyan::DecodeHelper::CrunchStream.new(bin)
  level_info = file.level_info(0)
  case level_info.format
  when Mikunyan::DecodeHelper::CrunchStream::Format::DXT1
    decode_dxt1(width, height, file.unpack_level(0))
  when Mikunyan::DecodeHelper::CrunchStream::Format::DXT5
    decode_dxt5(width, height, file.unpack_level(0))
  when Mikunyan::DecodeHelper::CrunchStream::Format::ETC1
    decode_etc1(width, height, file.unpack_level(0))
  when Mikunyan::DecodeHelper::CrunchStream::Format::ETC2
    decode_etc2rgb(width, height, file.unpack_level(0))
  when Mikunyan::DecodeHelper::CrunchStream::Format::ETC2A
    decode_etc2rgba8(width, height, file.unpack_level(0))
  end
end

.decode_dxt1(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from DXT1 compressed binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



354
355
356
# File 'lib/mikunyan/decoders/image_decoder.rb', line 354

def self.decode_dxt1(width, height, bin)
  ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_dxt1(bin, width, height))
end

.decode_dxt5(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from DXT5 compressed binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



363
364
365
# File 'lib/mikunyan/decoders/image_decoder.rb', line 363

def self.decode_dxt5(width, height, bin)
  ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_dxt5(bin, width, height))
end

.decode_eacr(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from EAC R11 compressed binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



393
394
395
# File 'lib/mikunyan/decoders/image_decoder.rb', line 393

def self.decode_eacr(width, height, bin)
  ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_eacr(bin, width, height))
end

.decode_eacrg(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from EAC RG11 compressed binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



411
412
413
# File 'lib/mikunyan/decoders/image_decoder.rb', line 411

def self.decode_eacrg(width, height, bin)
  ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_eacrg(bin, width, height))
end

.decode_eacsr(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from EAC Signed R11 compressed binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



402
403
404
# File 'lib/mikunyan/decoders/image_decoder.rb', line 402

def self.decode_eacsr(width, height, bin)
  ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_eacsr(bin, width, height))
end

.decode_eacsrg(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from EAC Signed RG11 compressed binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



420
421
422
# File 'lib/mikunyan/decoders/image_decoder.rb', line 420

def self.decode_eacsrg(width, height, bin)
  ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_eacsrg(bin, width, height))
end

.decode_etc1(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from ETC1 compressed binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



384
385
386
# File 'lib/mikunyan/decoders/image_decoder.rb', line 384

def self.decode_etc1(width, height, bin)
  ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_etc1(bin, width, height))
end

.decode_etc2rgb(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from ETC2 compressed binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



429
430
431
# File 'lib/mikunyan/decoders/image_decoder.rb', line 429

def self.decode_etc2rgb(width, height, bin)
  ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_etc2(bin, width, height))
end

.decode_etc2rgba1(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from ETC2 Alpha1 compressed binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



438
439
440
# File 'lib/mikunyan/decoders/image_decoder.rb', line 438

def self.decode_etc2rgba1(width, height, bin)
  ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_etc2a1(bin, width, height))
end

.decode_etc2rgba8(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from ETC2 Alpha8 compressed binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



447
448
449
# File 'lib/mikunyan/decoders/image_decoder.rb', line 447

def self.decode_etc2rgba8(width, height, bin)
  ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_etc2a8(bin, width, height))
end

.decode_object(object) ⇒ ChunkyPNG::Image?

Decode image from Mikunyan::ObjectValue

Parameters:

Returns:

  • (ChunkyPNG::Image, nil)

    decoded image



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
# File 'lib/mikunyan/decoders/image_decoder.rb', line 19

def self.decode_object(object)
  return nil unless object.is_a?(ObjectValue)

  endian = object.endian
  width = object['m_Width']&.value
  height = object['m_Height']&.value
  bin = object['image data']&.value
  fmt = object['m_TextureFormat']&.value
  return nil unless width && height && bin && fmt

  bin = object['m_StreamData']&.value if bin.empty?
  return nil unless bin

  case fmt
  when 1 # Alpha8
    decode_a8(width, height, bin)
  when 2 # ARGB4444
    decode_argb4444(width, height, bin, endian)
  when 3 # RGB24
    decode_rgb24(width, height, bin)
  when 4 # RGBA32
    decode_rgba32(width, height, bin)
  when 5 # ARGB32
    decode_argb32(width, height, bin)
  when 7 # RGB565
    decode_rgb565(width, height, bin, endian)
  when 9 # R16
    decode_r16(width, height, bin)
  when 10 # DXT1
    decode_dxt1(width, height, bin)
  when 12 # DXT5
    decode_dxt5(width, height, bin)
  when 13 # RGBA4444
    decode_rgba4444(width, height, bin, endian)
  when 14 # BGRA32
    decode_bgra32(width, height, bin)
  when 15 # RHalf
    decode_rhalf(width, height, bin, endian)
  when 16 # RGHalf
    decode_rghalf(width, height, bin, endian)
  when 17 # RGBAHalf
    decode_rgbahalf(width, height, bin, endian)
  when 18 # RFloat
    decode_rfloat(width, height, bin, endian)
  when 19 # RGFloat
    decode_rgfloat(width, height, bin, endian)
  when 20 # RGBAFloat
    decode_rgbafloat(width, height, bin, endian)
  # when 21 # YUY2
  when 22 # RGB9e5Float
    decode_rgb9e5float(width, height, bin, endian)
  # when 24 # BC6H
  # when 25 # BC7
  # when 26 # BC4
  # when 27 # BC5
  when 28, 29, 64, 65 # DXT1Crunched, DXT5Crunched, ETC_RGB4Crunched, ETC2_RGBA8Crunched
    decode_crunched(width, height, bin)
  when 30, 31, -127 # PVRTC_RGB2, PVRTC_RGBA2, PVRTC_2BPP_RGBA
    decode_pvrtc1(width, height, bin, 2)
  when 32, 33 # PVRTC_RGB4, PVRTC_RGBA4
    decode_pvrtc1(width, height, bin, 4)
  when 34 # ETC_RGB4
    decode_etc1(width, height, bin)
  when 41 # EAC_R
    decode_eacr(width, height, bin)
  when 42 # EAC_R_SIGNED
    decode_eacsr(width, height, bin)
  when 43 # EAC_RG
    decode_eacrg(width, height, bin)
  when 44 # EAC_RG_SIGNED
    decode_eacsrg(width, height, bin)
  when 45 # ETC2_RGB
    decode_etc2rgb(width, height, bin)
  when 46 # ETC2_RGBA1
    decode_etc2rgba1(width, height, bin)
  when 47 # ETC2_RGBA8
    decode_etc2rgba8(width, height, bin)
  when 48, 54, 66 # ASTC_RGB_4x4, ASTC_RGBA_4x4, ASTC_HDR_4x4
    decode_astc(width, height, 4, bin)
  when 49, 55, 67 # ASTC_RGB_5x5, ASTC_RGBA_5x5, ASTC_HDR_5x5
    decode_astc(width, height, 5, bin)
  when 50, 56, 68 # ASTC_RGB_6x6, ASTC_RGBA_6x6, ASTC_HDR_6x6
    decode_astc(width, height, 6, bin)
  when 51, 57, 69 # ASTC_RGB_8x8, ASTC_RGBA_8x8, ASTC_HDR_8x8
    decode_astc(width, height, 8, bin)
  when 52, 58, 70 # ASTC_10x10, ASTC_RGBA_10x10, ASTC_HDR_10x10
    decode_astc(width, height, 10, bin)
  when 53, 59, 71 # ASTC_RGB_12x12, ASTC_RGBA_12x12, ASTC_HDR_12x12
    decode_astc(width, height, 12, bin)
  # when 60 # ETC_RGB4_3DS
  # when 61 # ETC_RGBA8_3DS
  when 62 # RG16
    decode_rg16(width, height, bin)
  when 63 # R8
    decode_r8(width, height, bin)
  end
end

.decode_pvrtc1(width, height, bin, bpp) ⇒ ChunkyPNG::Image

Decode image from PVRTC1 compressed binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

  • bpp (Integer)

    bit per pixel (2 or 4)

Returns:

  • (ChunkyPNG::Image)

    decoded image



373
374
375
376
377
# File 'lib/mikunyan/decoders/image_decoder.rb', line 373

def self.decode_pvrtc1(width, height, bin, bpp)
  raise 'bpp of PVRTC1 must be 2 or 4' unless [2, 4].include?(bpp)

  ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_pvrtc1(bin, width, height, bpp == 2))
end

.decode_r16(width, height, bin, endian = :big) ⇒ ChunkyPNG::Image

Decode image from R16 binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

  • endian (Symbol) (defaults to: :big)

    endianness of binary

Returns:

  • (ChunkyPNG::Image)

    decoded image



200
201
202
203
# File 'lib/mikunyan/decoders/image_decoder.rb', line 200

def self.decode_r16(width, height, bin, endian = :big)
  ChunkyPNG::Image.from_rgb_stream(width, height,
                                   DecodeHelper.decode_r16(bin, width * height, endian == :big)).flip
end

.decode_r8(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from R8 binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



131
132
133
# File 'lib/mikunyan/decoders/image_decoder.rb', line 131

def self.decode_r8(width, height, bin)
  ChunkyPNG::Image.from_rgb_stream(width, height, DecodeHelper.decode_r8(bin, width * height)).flip
end

.decode_rfloat(width, height, bin, endian = :big) ⇒ ChunkyPNG::Image

Decode image from R float binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

  • endian (Symbol) (defaults to: :big)

    endianness of binary

Returns:

  • (ChunkyPNG::Image)

    decoded image



307
308
309
310
311
312
313
314
315
# File 'lib/mikunyan/decoders/image_decoder.rb', line 307

def self.decode_rfloat(width, height, bin, endian = :big)
  mem = String.new(capacity: width * height * 3)
  unpackstr = endian == :little ? 'e' : 'g'
  (width * height).times do |i|
    c = f2i(bin.byteslice(i * 4, 4).unpack1(unpackstr))
    BinUtils.append_int8!(mem, c, c, c)
  end
  ChunkyPNG::Image.from_rgb_stream(width, height, mem).flip
end

.decode_rg16(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from RG16 binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



226
227
228
229
230
231
232
# File 'lib/mikunyan/decoders/image_decoder.rb', line 226

def self.decode_rg16(width, height, bin)
  mem = String.new(capacity: width * height * 3)
  (width * height).times do |i|
    BinUtils.append_int16_int8_be!(mem, BinUtils.get_int16_be(bin, i * 2), 0)
  end
  ChunkyPNG::Image.from_rgb_stream(width, height, mem).flip
end

.decode_rgb24(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from RGB24 binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



156
157
158
# File 'lib/mikunyan/decoders/image_decoder.rb', line 156

def self.decode_rgb24(width, height, bin)
  ChunkyPNG::Image.from_rgb_stream(width, height, bin).flip
end

.decode_rgb565(width, height, bin, endian = :big) ⇒ ChunkyPNG::Image

Decode image from RGB565 binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

  • endian (Symbol) (defaults to: :big)

    endianness of binary

Returns:

  • (ChunkyPNG::Image)

    decoded image



189
190
191
192
# File 'lib/mikunyan/decoders/image_decoder.rb', line 189

def self.decode_rgb565(width, height, bin, endian = :big)
  ChunkyPNG::Image.from_rgb_stream(width, height,
                                   DecodeHelper.decode_rgb565(bin, width * height, endian == :big)).flip
end

.decode_rgb9e5float(width, height, bin, endian = :big) ⇒ ChunkyPNG::Image

Decode image from RGB9e5 binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

  • endian (Symbol) (defaults to: :big)

    endianness of binary

Returns:

  • (ChunkyPNG::Image)

    decoded image



254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/mikunyan/decoders/image_decoder.rb', line 254

def self.decode_rgb9e5float(width, height, bin, endian = :big)
  mem = String.new(capacity: width * height * 3)
  (width * height).times do |i|
    n = endian == :little ? BinUtils.get_int32_le(bin, i * 4) : BinUtils.get_int32_be(bin, i * 4)
    b = n >> 18 & 0x1ff
    g = n >> 9 & 0x1ff
    r = n & 0x1ff
    scale = n >> 27 & 0x1f
    scale = 2**(scale - 24)
    BinUtils.append_int8!(mem, f2i(r * scale), f2i(g * scale), f2i(b * scale))
  end
  ChunkyPNG::Image.from_rgb_stream(width, height, mem).flip
end

.decode_rgba32(width, height, bin) ⇒ ChunkyPNG::Image

Decode image from RGBA32 binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

Returns:

  • (ChunkyPNG::Image)

    decoded image



165
166
167
# File 'lib/mikunyan/decoders/image_decoder.rb', line 165

def self.decode_rgba32(width, height, bin)
  ChunkyPNG::Image.from_rgba_stream(width, height, bin).flip
end

.decode_rgba4444(width, height, bin, endian = :big) ⇒ ChunkyPNG::Image

Decode image from RGBA4444 binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

  • endian (Symbol) (defaults to: :big)

    endianness of binary

Returns:

  • (ChunkyPNG::Image)

    decoded image



211
212
213
214
215
216
217
218
219
# File 'lib/mikunyan/decoders/image_decoder.rb', line 211

def self.decode_rgba4444(width, height, bin, endian = :big)
  mem = String.new(capacity: width * height * 4)
  (width * height).times do |i|
    c = endian == :little ? BinUtils.get_int16_le(bin, i * 2) : BinUtils.get_int16_be(bin, i * 2)
    c = ((c & 0xf000) << 12) | ((c & 0x0f00) << 8) | ((c & 0x00f0) << 4) | (c & 0x000f)
    BinUtils.append_int32_be!(mem, c << 4 | c)
  end
  ChunkyPNG::Image.from_rgba_stream(width, height, mem).flip
end

.decode_rgbafloat(width, height, bin, endian = :big) ⇒ ChunkyPNG::Image

Decode image from RGBA float binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

  • endian (Symbol) (defaults to: :big)

    endianness of binary

Returns:

  • (ChunkyPNG::Image)

    decoded image



339
340
341
342
343
344
345
346
347
# File 'lib/mikunyan/decoders/image_decoder.rb', line 339

def self.decode_rgbafloat(width, height, bin, endian = :big)
  mem = String.new(capacity: width * height * 4)
  unpackstr = endian == :little ? 'e4' : 'g4'
  (width * height).times do |i|
    r, g, b, a = bin.byteslice(i * 16, 16).unpack(unpackstr)
    BinUtils.append_int8!(mem, f2i(r), f2i(g), f2i(b), f2i(a))
  end
  ChunkyPNG::Image.from_rgba_stream(width, height, mem).flip
end

.decode_rgbahalf(width, height, bin, endian = :big) ⇒ ChunkyPNG::Image

Decode image from RGBA Half-float binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

  • endian (Symbol) (defaults to: :big)

    endianness of binary

Returns:

  • (ChunkyPNG::Image)

    decoded image



296
297
298
299
# File 'lib/mikunyan/decoders/image_decoder.rb', line 296

def self.decode_rgbahalf(width, height, bin, endian = :big)
  ChunkyPNG::Image.from_rgba_stream(width, height,
                                    DecodeHelper.decode_rgbahalf(bin, width * height, endian == :big)).flip
end

.decode_rgfloat(width, height, bin, endian = :big) ⇒ ChunkyPNG::Image

Decode image from RG float binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

  • endian (Symbol) (defaults to: :big)

    endianness of binary

Returns:

  • (ChunkyPNG::Image)

    decoded image



323
324
325
326
327
328
329
330
331
# File 'lib/mikunyan/decoders/image_decoder.rb', line 323

def self.decode_rgfloat(width, height, bin, endian = :big)
  mem = String.new(capacity: width * height * 3)
  unpackstr = endian == :little ? 'e2' : 'g2'
  (width * height).times do |i|
    r, g = bin.byteslice(i * 8, 8).unpack(unpackstr)
    BinUtils.append_int8!(mem, f2i(r), f2i(g), 0)
  end
  ChunkyPNG::Image.from_rgb_stream(width, height, mem).flip
end

.decode_rghalf(width, height, bin, endian = :big) ⇒ ChunkyPNG::Image

Decode image from RG Half-float binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

  • endian (Symbol) (defaults to: :big)

    endianness of binary

Returns:

  • (ChunkyPNG::Image)

    decoded image



285
286
287
288
# File 'lib/mikunyan/decoders/image_decoder.rb', line 285

def self.decode_rghalf(width, height, bin, endian = :big)
  ChunkyPNG::Image.from_rgb_stream(width, height,
                                   DecodeHelper.decode_rghalf(bin, width * height, endian == :big)).flip
end

.decode_rhalf(width, height, bin, endian = :big) ⇒ ChunkyPNG::Image

Decode image from R Half-float binary

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

  • bin (String)

    binary to decode

  • endian (Symbol) (defaults to: :big)

    endianness of binary

Returns:

  • (ChunkyPNG::Image)

    decoded image



274
275
276
277
# File 'lib/mikunyan/decoders/image_decoder.rb', line 274

def self.decode_rhalf(width, height, bin, endian = :big)
  ChunkyPNG::Image.from_rgb_stream(width, height,
                                   DecodeHelper.decode_rhalf(bin, width * height, endian == :big)).flip
end

.f2i(val) ⇒ Object

0.0,1.0

-> [0,255]



510
511
512
513
514
# File 'lib/mikunyan/decoders/image_decoder.rb', line 510

def self.f2i(val)
  return 0 unless val.finite?

  (val * 255).round.clamp(0, 255)
end