| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2023, Nicolas Ramz <nicolas.ramz@gmail.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 10:01:40 +02:00
										 |  |  | #include <AK/ByteReader.h>
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  | #include <AK/Debug.h>
 | 
					
						
							|  |  |  | #include <AK/Endian.h>
 | 
					
						
							|  |  |  | #include <AK/FixedArray.h>
 | 
					
						
							| 
									
										
										
										
											2023-09-13 12:17:58 +02:00
										 |  |  | #include <AK/IntegralMath.h>
 | 
					
						
							| 
									
										
										
										
											2023-12-21 00:17:37 -05:00
										 |  |  | #include <LibCompress/PackBitsDecoder.h>
 | 
					
						
							| 
									
										
										
										
											2023-11-20 17:06:45 +00:00
										 |  |  | #include <LibGfx/FourCC.h>
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  | #include <LibGfx/ImageFormats/ILBMLoader.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Gfx { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct IFFHeader { | 
					
						
							|  |  |  |     FourCC form; | 
					
						
							|  |  |  |     BigEndian<u32> file_size; | 
					
						
							|  |  |  |     FourCC format; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static_assert(AssertSize<IFFHeader, 12>()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct Chunk { | 
					
						
							|  |  |  |     FourCC type; | 
					
						
							|  |  |  |     ReadonlyBytes data; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum class CompressionType : u8 { | 
					
						
							|  |  |  |     None = 0, | 
					
						
							| 
									
										
										
										
											2023-10-30 16:33:20 +00:00
										 |  |  |     ByteRun = 1, | 
					
						
							|  |  |  |     __Count | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum class MaskType : u8 { | 
					
						
							|  |  |  |     None = 0, | 
					
						
							|  |  |  |     HasMask = 1, | 
					
						
							|  |  |  |     HasTransparentColor = 2, | 
					
						
							| 
									
										
										
										
											2023-10-30 16:33:20 +00:00
										 |  |  |     HasLasso = 3, | 
					
						
							|  |  |  |     __Count | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 10:01:40 +02:00
										 |  |  | enum class ViewportMode : u32 { | 
					
						
							|  |  |  |     EHB = 0x80, | 
					
						
							|  |  |  |     HAM = 0x800 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-08 13:50:53 +01:00
										 |  |  | enum class Format : u8 { | 
					
						
							|  |  |  |     // Amiga interleaved format
 | 
					
						
							|  |  |  |     ILBM = 0, | 
					
						
							|  |  |  |     // PC-DeluxePaint chunky format
 | 
					
						
							|  |  |  |     PBM = 1 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 10:01:40 +02:00
										 |  |  | AK_ENUM_BITWISE_OPERATORS(ViewportMode); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  | struct ChunkHeader { | 
					
						
							|  |  |  |     FourCC chunk_type; | 
					
						
							|  |  |  |     BigEndian<u32> chunk_size; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct BMHDHeader { | 
					
						
							|  |  |  |     BigEndian<u16> width; | 
					
						
							|  |  |  |     BigEndian<u16> height; | 
					
						
							|  |  |  |     BigEndian<i16> x; | 
					
						
							|  |  |  |     BigEndian<i16> y; | 
					
						
							|  |  |  |     u8 planes; | 
					
						
							|  |  |  |     MaskType mask; | 
					
						
							|  |  |  |     CompressionType compression; | 
					
						
							|  |  |  |     u8 pad; | 
					
						
							|  |  |  |     BigEndian<u16> transparent_color; | 
					
						
							|  |  |  |     u8 x_aspect; | 
					
						
							|  |  |  |     u8 y_aspect; | 
					
						
							|  |  |  |     BigEndian<u16> page_width; | 
					
						
							|  |  |  |     BigEndian<u16> page_height; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static_assert(sizeof(BMHDHeader) == 20); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct ILBMLoadingContext { | 
					
						
							|  |  |  |     enum class State { | 
					
						
							|  |  |  |         NotDecoded = 0, | 
					
						
							|  |  |  |         HeaderDecoded, | 
					
						
							|  |  |  |         BitmapDecoded | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     State state { State::NotDecoded }; | 
					
						
							|  |  |  |     ReadonlyBytes data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // points to current chunk
 | 
					
						
							|  |  |  |     ReadonlyBytes chunks_cursor; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // max number of bytes per plane row
 | 
					
						
							|  |  |  |     u16 pitch; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 10:01:40 +02:00
										 |  |  |     ViewportMode viewport_mode; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Vector<Color> color_table; | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 12:17:58 +02:00
										 |  |  |     // number of bits needed to describe current palette
 | 
					
						
							|  |  |  |     u8 cmap_bits; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |     RefPtr<Gfx::Bitmap> bitmap; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     BMHDHeader bm_header; | 
					
						
							| 
									
										
										
										
											2023-12-08 13:50:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Format format; | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ErrorOr<void> decode_iff_ilbm_header(ILBMLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (context.state >= ILBMLoadingContext::State::HeaderDecoded) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (context.data.size() < sizeof(IFFHeader)) | 
					
						
							|  |  |  |         return Error::from_string_literal("Missing IFF header"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto& header = *bit_cast<IFFHeader const*>(context.data.data()); | 
					
						
							| 
									
										
										
										
											2023-12-08 13:50:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (header.form != FourCC("FORM") || (header.format != FourCC("ILBM") && header.format != FourCC("PBM "))) | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |         return Error::from_string_literal("Invalid IFF-ILBM header"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-08 13:50:53 +01:00
										 |  |  |     context.format = header.format == FourCC("ILBM") ? Format::ILBM : Format::PBM; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 10:01:40 +02:00
										 |  |  | static ErrorOr<Vector<Color>> decode_cmap_chunk(Chunk cmap_chunk) | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     size_t const size = cmap_chunk.data.size() / 3; | 
					
						
							| 
									
										
										
										
											2023-09-13 10:01:40 +02:00
										 |  |  |     Vector<Color> color_table; | 
					
						
							|  |  |  |     TRY(color_table.try_ensure_capacity(size)); | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (size_t i = 0; i < size; ++i) { | 
					
						
							| 
									
										
										
										
											2023-09-13 10:01:40 +02:00
										 |  |  |         color_table.unchecked_append(Color(cmap_chunk.data[i * 3], cmap_chunk.data[(i * 3) + 1], cmap_chunk.data[(i * 3) + 2])); | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return color_table; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ErrorOr<RefPtr<Gfx::Bitmap>> chunky_to_bitmap(ILBMLoadingContext& context, ByteBuffer const& chunky) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto const width = context.bm_header.width; | 
					
						
							|  |  |  |     auto const height = context.bm_header.height; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     RefPtr<Gfx::Bitmap> bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, { width, height })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dbgln_if(ILBM_DEBUG, "created Bitmap {}x{}", width, height); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-15 16:20:16 +01:00
										 |  |  |     // - For 24bit pictures: the chunky buffer contains 3 bytes (R,G,B) per pixel
 | 
					
						
							|  |  |  |     // - For indexed colored pictures: chunky buffer contains a single byte per pixel
 | 
					
						
							|  |  |  |     u8 pixel_size = AK::max(1, context.bm_header.planes / 8); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |     for (int row = 0; row < height; ++row) { | 
					
						
							| 
									
										
										
										
											2023-09-13 12:17:58 +02:00
										 |  |  |         // Keep color: in HAM mode, current color
 | 
					
						
							|  |  |  |         // may be based on previous color instead of coming from
 | 
					
						
							|  |  |  |         // the palette.
 | 
					
						
							|  |  |  |         Color color = Color::Black; | 
					
						
							| 
									
										
										
										
											2023-12-15 16:20:16 +01:00
										 |  |  |         for (int col = 0; col < width; col++) { | 
					
						
							|  |  |  |             size_t index = (width * row * pixel_size) + (col * pixel_size); | 
					
						
							|  |  |  |             if (context.bm_header.planes == 24) { | 
					
						
							|  |  |  |                 color = Color(chunky[index], chunky[index + 1], chunky[index + 2]); | 
					
						
							|  |  |  |             } else if (chunky[index] < context.color_table.size()) { | 
					
						
							|  |  |  |                 color = context.color_table[chunky[index]]; | 
					
						
							| 
									
										
										
										
											2024-01-05 17:11:05 +01:00
										 |  |  |                 if (context.bm_header.mask == MaskType::HasTransparentColor && chunky[index] == context.bm_header.transparent_color) | 
					
						
							|  |  |  |                     color = color.with_alpha(0); | 
					
						
							| 
									
										
										
										
											2023-09-13 12:17:58 +02:00
										 |  |  |             } else if (has_flag(context.viewport_mode, ViewportMode::HAM)) { | 
					
						
							|  |  |  |                 // Get the control bit which will tell use how current pixel should be calculated
 | 
					
						
							| 
									
										
										
										
											2023-12-15 16:20:16 +01:00
										 |  |  |                 u8 control = (chunky[index] >> context.cmap_bits) & 0x3; | 
					
						
							| 
									
										
										
										
											2023-09-13 12:17:58 +02:00
										 |  |  |                 // Since we only have (cmap_bits - 2) bits to define the component,
 | 
					
						
							|  |  |  |                 // we need to pad it to 8 bits.
 | 
					
						
							| 
									
										
										
										
											2023-12-15 16:20:16 +01:00
										 |  |  |                 u8 component = (chunky[index] % context.color_table.size()) << (8 - context.cmap_bits); | 
					
						
							| 
									
										
										
										
											2023-09-13 12:17:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if (control == 1) { | 
					
						
							|  |  |  |                     color.set_blue(component); | 
					
						
							|  |  |  |                 } else if (control == 2) { | 
					
						
							|  |  |  |                     color.set_red(component); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     color.set_green(component); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 return Error::from_string_literal("Color map index out of bounds but HAM bit not set"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             bitmap->set_pixel(col, row, color); | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dbgln_if(ILBM_DEBUG, "filled Bitmap"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return bitmap; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ErrorOr<ByteBuffer> planar_to_chunky(ReadonlyBytes bitplanes, ILBMLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     dbgln_if(ILBM_DEBUG, "planar_to_chunky"); | 
					
						
							|  |  |  |     u16 pitch = context.pitch; | 
					
						
							|  |  |  |     u16 width = context.bm_header.width; | 
					
						
							|  |  |  |     u16 height = context.bm_header.height; | 
					
						
							|  |  |  |     u8 planes = context.bm_header.planes; | 
					
						
							| 
									
										
										
										
											2023-12-15 16:20:16 +01:00
										 |  |  |     size_t buffer_size = static_cast<size_t>(width) * height; | 
					
						
							|  |  |  |     // If planes number is 24 we'll store R,G,B components so buffer needs to be 3 times width*height
 | 
					
						
							|  |  |  |     // otherwise we'll store a single 8bit index to the CMAP.
 | 
					
						
							|  |  |  |     if (planes == 24) | 
					
						
							|  |  |  |         buffer_size *= 3; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto chunky = TRY(ByteBuffer::create_zeroed(buffer_size)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     u8 const pixel_size = AK::max(1, planes / 8); | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (u16 y = 0; y < height; y++) { | 
					
						
							| 
									
										
										
										
											2023-12-02 12:37:23 +02:00
										 |  |  |         size_t scanline = static_cast<size_t>(y) * width; | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |         for (u8 p = 0; p < planes; p++) { | 
					
						
							| 
									
										
										
										
											2023-12-15 16:20:16 +01:00
										 |  |  |             u8 const plane_mask = 1 << (p % 8); | 
					
						
							| 
									
										
										
										
											2023-10-30 18:51:54 +00:00
										 |  |  |             size_t offset_base = (pitch * planes * y) + (p * pitch); | 
					
						
							| 
									
										
										
										
											2024-01-05 17:09:29 +01:00
										 |  |  |             if (offset_base + pitch > bitplanes.size()) | 
					
						
							| 
									
										
										
										
											2023-10-30 18:51:54 +00:00
										 |  |  |                 return Error::from_string_literal("Malformed bitplane data"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |             for (u16 i = 0; i < pitch; i++) { | 
					
						
							| 
									
										
										
										
											2023-10-30 18:51:54 +00:00
										 |  |  |                 u8 bit = bitplanes[offset_base + i]; | 
					
						
							| 
									
										
										
										
											2023-12-15 16:20:16 +01:00
										 |  |  |                 u8 rgb_shift = p / 8; | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 for (u8 b = 0; b < 8; b++) { | 
					
						
							|  |  |  |                     u8 mask = 1 << (7 - b); | 
					
						
							|  |  |  |                     // get current plane
 | 
					
						
							|  |  |  |                     if (bit & mask) { | 
					
						
							|  |  |  |                         u16 x = (i * 8) + b; | 
					
						
							| 
									
										
										
										
											2024-01-10 17:45:52 +01:00
										 |  |  |                         size_t offset = (scanline * pixel_size) + (x * pixel_size) + rgb_shift; | 
					
						
							|  |  |  |                         // Only throw an error if we would actually attempt to write
 | 
					
						
							|  |  |  |                         // outside of the chunky buffer. Some apps like PPaint produce
 | 
					
						
							|  |  |  |                         // malformed bitplane data but files are still accepted by most readers
 | 
					
						
							|  |  |  |                         // since they do not cause writing past the chunky buffer.
 | 
					
						
							|  |  |  |                         if (offset >= chunky.size()) { | 
					
						
							|  |  |  |                             return Error::from_string_literal("Malformed bitplane data"); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         chunky[offset] |= plane_mask; | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-15 16:20:16 +01:00
										 |  |  |     dbgln_if(ILBM_DEBUG, "planar_to_chunky: end"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |     return chunky; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ErrorOr<ByteBuffer> uncompress_byte_run(ReadonlyBytes data, ILBMLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto length = data.size(); | 
					
						
							|  |  |  |     dbgln_if(ILBM_DEBUG, "uncompress_byte_run pitch={} size={}", context.pitch, data.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-30 16:18:12 +00:00
										 |  |  |     size_t plane_data_size = context.pitch * context.bm_header.height * context.bm_header.planes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // The maximum run length of this compression method is 127 bytes, so the uncompressed size
 | 
					
						
							|  |  |  |     // cannot be more than 127 times the size of the chunk we are decompressing.
 | 
					
						
							|  |  |  |     if (plane_data_size > NumericLimits<u32>::max() || ceil_div(plane_data_size, 127ul) > length) | 
					
						
							|  |  |  |         return Error::from_string_literal("Uncompressed data size too large"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-21 00:17:37 -05:00
										 |  |  |     auto plane_data = TRY(Compress::PackBits::decode_all(data, plane_data_size)); | 
					
						
							| 
									
										
										
										
											2023-10-30 16:18:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |     return plane_data; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 10:01:40 +02:00
										 |  |  | static ErrorOr<void> extend_ehb_palette(ILBMLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     dbgln_if(ILBM_DEBUG, "need to extend palette"); | 
					
						
							|  |  |  |     for (size_t i = 0; i < 32; ++i) { | 
					
						
							|  |  |  |         auto const color = context.color_table[i]; | 
					
						
							|  |  |  |         TRY(context.color_table.try_append(color.darkened())); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 12:17:58 +02:00
										 |  |  | static ErrorOr<void> reduce_ham_palette(ILBMLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     u8 bits = context.cmap_bits; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dbgln_if(ILBM_DEBUG, "reduce palette planes={} bits={}", context.bm_header.planes, context.cmap_bits); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (bits > context.bm_header.planes) { | 
					
						
							|  |  |  |         dbgln_if(ILBM_DEBUG, "need to reduce palette"); | 
					
						
							|  |  |  |         bits -= (bits - context.bm_header.planes) + 2; | 
					
						
							|  |  |  |         // bits shouldn't theorically be less than 4 bits in HAM mode.
 | 
					
						
							|  |  |  |         if (bits < 4) | 
					
						
							|  |  |  |             return Error::from_string_literal("Error while reducing CMAP for HAM: bits too small"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         context.color_table.resize((context.color_table.size() >> bits)); | 
					
						
							|  |  |  |         context.cmap_bits = bits; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  | static ErrorOr<void> decode_body_chunk(Chunk body_chunk, ILBMLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     dbgln_if(ILBM_DEBUG, "decode_body_chunk {}", body_chunk.data.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 10:46:15 +02:00
										 |  |  |     ByteBuffer pixel_data; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |     if (context.bm_header.compression == CompressionType::ByteRun) { | 
					
						
							|  |  |  |         auto plane_data = TRY(uncompress_byte_run(body_chunk.data, context)); | 
					
						
							| 
									
										
										
										
											2023-12-08 13:50:53 +01:00
										 |  |  |         if (context.format == Format::ILBM) | 
					
						
							|  |  |  |             pixel_data = TRY(planar_to_chunky(plane_data, context)); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             pixel_data = plane_data; | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2023-12-08 13:50:53 +01:00
										 |  |  |         if (context.format == Format::ILBM) | 
					
						
							|  |  |  |             pixel_data = TRY(planar_to_chunky(body_chunk.data, context)); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             pixel_data = TRY(ByteBuffer::copy(body_chunk.data.data(), body_chunk.data.size())); | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 12:17:58 +02:00
										 |  |  |     // Some files already have 64 colors defined in the palette,
 | 
					
						
							|  |  |  |     // maybe for upward compatibility with 256 colors software/hardware.
 | 
					
						
							|  |  |  |     // DPaint 4 & previous files only have 32 colors so the
 | 
					
						
							| 
									
										
										
										
											2023-09-13 10:01:40 +02:00
										 |  |  |     // palette needs to be extended only for these files.
 | 
					
						
							|  |  |  |     if (has_flag(context.viewport_mode, ViewportMode::EHB) && context.color_table.size() < 64) { | 
					
						
							|  |  |  |         TRY(extend_ehb_palette(context)); | 
					
						
							| 
									
										
										
										
											2023-09-13 12:17:58 +02:00
										 |  |  |     } else if (has_flag(context.viewport_mode, ViewportMode::HAM)) { | 
					
						
							|  |  |  |         TRY(reduce_ham_palette(context)); | 
					
						
							| 
									
										
										
										
											2023-09-13 10:01:40 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 10:46:15 +02:00
										 |  |  |     context.bitmap = TRY(chunky_to_bitmap(context, pixel_data)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ErrorOr<Chunk> decode_iff_chunk_header(ReadonlyBytes chunks) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (chunks.size() < sizeof(ChunkHeader)) | 
					
						
							|  |  |  |         return Error::from_string_literal("Not enough data for IFF chunk header"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto const& header = *bit_cast<ChunkHeader const*>(chunks.data()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (chunks.size() < sizeof(ChunkHeader) + header.chunk_size) | 
					
						
							|  |  |  |         return Error::from_string_literal("Not enough data for IFF chunk"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return Chunk { header.chunk_type, { chunks.data() + sizeof(ChunkHeader), header.chunk_size } }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ErrorOr<Chunk> decode_iff_advance_chunk(ReadonlyBytes& chunks) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto chunk = TRY(decode_iff_chunk_header(chunks)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     chunks = chunks.slice(sizeof(ChunkHeader) + chunk.data.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // add padding if needed
 | 
					
						
							|  |  |  |     if (chunk.data.size() % 2 != 0) { | 
					
						
							|  |  |  |         if (chunks.is_empty()) | 
					
						
							|  |  |  |             return Error::from_string_literal("Missing data for padding byte"); | 
					
						
							|  |  |  |         if (*chunks.data() != 0) | 
					
						
							|  |  |  |             return Error::from_string_literal("Padding byte is not 0"); | 
					
						
							|  |  |  |         chunks = chunks.slice(1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return chunk; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ErrorOr<void> decode_iff_chunks(ILBMLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto& chunks = context.chunks_cursor; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dbgln_if(ILBM_DEBUG, "decode_iff_chunks"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (!chunks.is_empty()) { | 
					
						
							|  |  |  |         auto chunk = TRY(decode_iff_advance_chunk(chunks)); | 
					
						
							|  |  |  |         if (chunk.type == FourCC("CMAP")) { | 
					
						
							| 
									
										
										
										
											2023-09-13 12:17:58 +02:00
										 |  |  |             // Some files (HAM mainly) have CMAP chunks larger than the planes they advertise: I'm not sure
 | 
					
						
							|  |  |  |             // why but we should not return an error in this case.
 | 
					
						
							| 
									
										
										
										
											2023-10-30 16:35:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |             context.color_table = TRY(decode_cmap_chunk(chunk)); | 
					
						
							| 
									
										
										
										
											2023-09-13 12:17:58 +02:00
										 |  |  |             context.cmap_bits = AK::ceil_log2(context.color_table.size()); | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |         } else if (chunk.type == FourCC("BODY")) { | 
					
						
							| 
									
										
										
										
											2023-12-15 16:20:16 +01:00
										 |  |  |             if (context.color_table.is_empty() && context.bm_header.planes != 24) | 
					
						
							|  |  |  |                 return Error::from_string_literal("Decoding indexed BODY chunk without a color map is not currently supported"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Apparently 32bit ilbm files exist: but I wasn't able to find any,
 | 
					
						
							|  |  |  |             // nor is it documented anywhere, so let's make it clear it's not supported.
 | 
					
						
							|  |  |  |             if (context.bm_header.planes != 24 && context.bm_header.planes > 8) | 
					
						
							|  |  |  |                 return Error::from_string_literal("Invalid number of bitplanes"); | 
					
						
							| 
									
										
										
										
											2023-10-30 16:24:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |             TRY(decode_body_chunk(chunk, context)); | 
					
						
							|  |  |  |             context.state = ILBMLoadingContext::State::BitmapDecoded; | 
					
						
							|  |  |  |         } else if (chunk.type == FourCC("CRNG")) { | 
					
						
							|  |  |  |             dbgln_if(ILBM_DEBUG, "Chunk:CRNG"); | 
					
						
							| 
									
										
										
										
											2023-09-13 10:01:40 +02:00
										 |  |  |         } else if (chunk.type == FourCC("CAMG")) { | 
					
						
							|  |  |  |             context.viewport_mode = static_cast<ViewportMode>(AK::convert_between_host_and_big_endian(ByteReader::load32(chunk.data.data()))); | 
					
						
							|  |  |  |             dbgln_if(ILBM_DEBUG, "Chunk:CAMG, Viewport={}, EHB={}, HAM={}", (u32)context.viewport_mode, has_flag(context.viewport_mode, ViewportMode::EHB), has_flag(context.viewport_mode, ViewportMode::HAM)); | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-16 18:18:22 +01:00
										 |  |  |     if (context.state != ILBMLoadingContext::State::BitmapDecoded) | 
					
						
							|  |  |  |         return Error::from_string_literal("Missing body chunk"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ErrorOr<void> decode_bmhd_chunk(ILBMLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     context.chunks_cursor = context.data.slice(sizeof(IFFHeader)); | 
					
						
							|  |  |  |     auto first_chunk = TRY(decode_iff_advance_chunk(context.chunks_cursor)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (first_chunk.type != FourCC("BMHD")) | 
					
						
							|  |  |  |         return Error::from_string_literal("IFFImageDecoderPlugin: Invalid chunk type, expected BMHD"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-12 20:52:19 +01:00
										 |  |  |     if (first_chunk.data.size() < sizeof(BMHDHeader)) | 
					
						
							|  |  |  |         return Error::from_string_literal("IFFImageDecoderPlugin: Not enough data for header chunk"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |     context.bm_header = *bit_cast<BMHDHeader const*>(first_chunk.data.data()); | 
					
						
							| 
									
										
										
										
											2023-10-30 16:33:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (context.bm_header.mask >= MaskType::__Count) | 
					
						
							|  |  |  |         return Error::from_string_literal("IFFImageDecoderPlugin: Unsupported mask type"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (context.bm_header.compression >= CompressionType::__Count) | 
					
						
							|  |  |  |         return Error::from_string_literal("IFFImageDecoderPlugin: Unsupported compression type"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 16:24:01 +02:00
										 |  |  |     context.pitch = ceil_div((u16)context.bm_header.width, (u16)16) * 2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context.state = ILBMLoadingContext::State::HeaderDecoded; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dbgln_if(ILBM_DEBUG, "IFFImageDecoderPlugin: BMHD: {}x{} ({},{}), p={}, m={}, c={}", | 
					
						
							|  |  |  |         context.bm_header.width, | 
					
						
							|  |  |  |         context.bm_header.height, | 
					
						
							|  |  |  |         context.bm_header.x, | 
					
						
							|  |  |  |         context.bm_header.y, | 
					
						
							|  |  |  |         context.bm_header.planes, | 
					
						
							|  |  |  |         to_underlying(context.bm_header.mask), | 
					
						
							|  |  |  |         to_underlying(context.bm_header.compression)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ILBMImageDecoderPlugin::ILBMImageDecoderPlugin(ReadonlyBytes data, NonnullOwnPtr<ILBMLoadingContext> context) | 
					
						
							|  |  |  |     : m_context(move(context)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_context->data = data; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ILBMImageDecoderPlugin::~ILBMImageDecoderPlugin() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | IntSize ILBMImageDecoderPlugin::size() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return IntSize { m_context->bm_header.width, m_context->bm_header.height }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool ILBMImageDecoderPlugin::sniff(ReadonlyBytes data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ILBMLoadingContext context; | 
					
						
							|  |  |  |     context.data = data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return !decode_iff_ilbm_header(context).is_error(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> ILBMImageDecoderPlugin::create(ReadonlyBytes data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto context = TRY(try_make<ILBMLoadingContext>()); | 
					
						
							|  |  |  |     auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) ILBMImageDecoderPlugin(data, move(context)))); | 
					
						
							|  |  |  |     TRY(decode_iff_ilbm_header(*plugin->m_context)); | 
					
						
							|  |  |  |     TRY(decode_bmhd_chunk(*plugin->m_context)); | 
					
						
							|  |  |  |     return plugin; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<ImageFrameDescriptor> ILBMImageDecoderPlugin::frame(size_t index, Optional<IntSize>) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (index > 0) | 
					
						
							|  |  |  |         return Error::from_string_literal("ILBMImageDecoderPlugin: frame index must be 0"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_context->state < ILBMLoadingContext::State::BitmapDecoded) | 
					
						
							|  |  |  |         TRY(decode_iff_chunks(*m_context)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     VERIFY(m_context->bitmap); | 
					
						
							|  |  |  |     return ImageFrameDescriptor { m_context->bitmap, 0 }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |