Skip to content

Instantly share code, notes, and snippets.

@nico
Last active May 8, 2024 13:30
Show Gist options
  • Save nico/4f2d7e4b5a153293b6866b0abefbe5e7 to your computer and use it in GitHub Desktop.
Save nico/4f2d7e4b5a153293b6866b0abefbe5e7 to your computer and use it in GitHub Desktop.
NEVER_INLINE static ErrorOr<void> write_image_data(LittleEndianOutputBitStream& bit_stream, Bitmap const& bitmap, bool all_pixels_are_opaque)
{
for (ARGB32 pixel : bitmap) {
u8 a = pixel >> 24;
u8 r = pixel >> 16;
u8 g = pixel >> 8;
u8 b = pixel;
// We wrote a huffman table that gives every symbol 8 bits. That means we can write the image data
// out uncompressed –- but we do need to reverse the bit order of the bytes.
TRY(bit_stream.write_bits(Compress::reverse8_lookup_table[g], 8u));
TRY(bit_stream.write_bits(Compress::reverse8_lookup_table[r], 8u));
TRY(bit_stream.write_bits(Compress::reverse8_lookup_table[b], 8u));
// If all pixels are opaque, we wrote a one-element huffman table for alpha, which needs 0 bits per element.
if (!all_pixels_are_opaque)
TRY(bit_stream.write_bits(Compress::reverse8_lookup_table[a], 8u));
}
return {};
}
=>
+0x00 sub sp, sp, #0x80
+0x04 stp x26, x25, [sp, #0x30]
+0x08 stp x24, x23, [sp, #0x40]
+0x0c stp x22, x21, [sp, #0x50]
+0x10 stp x20, x19, [sp, #0x60]
+0x14 stp x29, x30, [sp, #0x70]
+0x18 add x29, sp, #0x70
+0x1c mov x19, x8
+0x20 adrp x8, 220 ; 0xdc000
+0x24 ldr x8, [x8, #0x750]
+0x28 ldr x8, [x8]
+0x2c str x8, [sp, #0x28]
+0x30 ldur x8, [x1, #0x4]
+0x34 lsr x9, x8, #32
+0x38 ldr w10, [x1, #0xc]
+0x3c mul w9, w10, w9
+0x40 cmp w9, #0x0
+0x44 b.le "Gfx::write_image_data(AK::LittleEndianOutputBitStream&, Gfx::Bitmap const&, bool)+0x174"
+0x48 asr x8, x8, #32
+0x4c ldr x9, [x1, #0x18]
+0x50 mul x22, x9, x8
+0x54 cbz x22, "Gfx::write_image_data(AK::LittleEndianOutputBitStream&, Gfx::Bitmap const&, bool)+0x128"
+0x58 mov x20, x2
+0x5c mov x21, x0
+0x60 ldr x23, [x1, #0x10]
+0x64 adrp x24, 67 ; 0x43000
+0x68 add x24, x24, #0x9a7
+0x6c b "Gfx::write_image_data(AK::LittleEndianOutputBitStream&, Gfx::Bitmap const&, bool)+0x7c"
+0x70 add x23, x23, #0x4
+0x74 subs x22, x22, #0x4
+0x78 b.eq "Gfx::write_image_data(AK::LittleEndianOutputBitStream&, Gfx::Bitmap const&, bool)+0x128"
+0x7c ldr w25, [x23]
+0x80 ubfx x8, x25, #8, #8
+0x84 ldrb w1, [x24, x8]
+0x88 add x8, sp, #0x8
+0x8c mov x0, x21
+0x90 mov w2, #0x8
+0x94 bl "AK::ErrorOr<void, AK::Error> AK::LittleEndianOutputBitStream::write_bits<unsigned char>(unsigned char, unsigned long)"
+0x98 ldrb w8, [sp, #0x20]
+0x9c cbnz w8, "Gfx::write_image_data(AK::LittleEndianOutputBitStream&, Gfx::Bitmap const&, bool)+0x104"
+0xa0 ubfx x8, x25, #16, #8
+0xa4 ldrb w1, [x24, x8]
+0xa8 add x8, sp, #0x8
+0xac mov x0, x21
+0xb0 mov w2, #0x8
+0xb4 bl "AK::ErrorOr<void, AK::Error> AK::LittleEndianOutputBitStream::write_bits<unsigned char>(unsigned char, unsigned long)"
+0xb8 ldrb w8, [sp, #0x20]
+0xbc cbnz w8, "Gfx::write_image_data(AK::LittleEndianOutputBitStream&, Gfx::Bitmap const&, bool)+0x104"
+0xc0 and x8, x25, #0xff
+0xc4 ldrb w1, [x24, x8]
+0xc8 add x8, sp, #0x8
+0xcc mov x0, x21
+0xd0 mov w2, #0x8
+0xd4 bl "AK::ErrorOr<void, AK::Error> AK::LittleEndianOutputBitStream::write_bits<unsigned char>(unsigned char, unsigned long)"
+0xd8 ldrb w8, [sp, #0x20]
+0xdc cbnz w8, "Gfx::write_image_data(AK::LittleEndianOutputBitStream&, Gfx::Bitmap const&, bool)+0x104"
+0xe0 tbnz w20, #0x0, "Gfx::write_image_data(AK::LittleEndianOutputBitStream&, Gfx::Bitmap const&, bool)+0x70"
+0xe4 lsr x8, x25, #24
+0xe8 ldrb w1, [x24, x8]
+0xec add x8, sp, #0x8
+0xf0 mov x0, x21
+0xf4 mov w2, #0x8
+0xf8 bl "AK::ErrorOr<void, AK::Error> AK::LittleEndianOutputBitStream::write_bits<unsigned char>(unsigned char, unsigned long)"
+0xfc ldrb w8, [sp, #0x20]
+0x100 cbz w8, "Gfx::write_image_data(AK::LittleEndianOutputBitStream&, Gfx::Bitmap const&, bool)+0x70"
+0x104 cmp w8, #0x1
+0x108 b.ne "Gfx::write_image_data(AK::LittleEndianOutputBitStream&, Gfx::Bitmap const&, bool)+0x168"
+0x10c ldur q0, [sp, #0x8]
+0x110 str q0, [x19]
+0x114 ldr x8, [sp, #0x18]
+0x118 str x8, [x19, #0x10]
+0x11c mov w8, #0x1
+0x120 strb w8, [x19, #0x18]
+0x124 b "Gfx::write_image_data(AK::LittleEndianOutputBitStream&, Gfx::Bitmap const&, bool)+0x134"
+0x128 stp xzr, xzr, [x19]
+0x12c strb wzr, [x19, #0x18]
+0x130 str xzr, [x19, #0x10]
+0x134 ldr x8, [sp, #0x28]
+0x138 adrp x9, 220 ; 0xdc000
+0x13c ldr x9, [x9, #0x750]
+0x140 ldr x9, [x9]
+0x144 cmp x9, x8
+0x148 b.ne "Gfx::write_image_data(AK::LittleEndianOutputBitStream&, Gfx::Bitmap const&, bool)+0x180"
+0x14c ldp x29, x30, [sp, #0x70]
+0x150 ldp x20, x19, [sp, #0x60]
+0x154 ldp x22, x21, [sp, #0x50]
+0x158 ldp x24, x23, [sp, #0x40]
+0x15c ldp x26, x25, [sp, #0x30]
+0x160 add sp, sp, #0x80
+0x164 ret
+0x168 adrp x0, 192 ; 0xc0000
+0x16c add x0, x0, #0x69b
+0x170 bl "DYLD-STUB$$ak_verification_failed"
+0x174 adrp x0, 193 ; 0xc1000
+0x178 add x0, x0, #0x4b3
+0x17c bl "DYLD-STUB$$ak_verification_failed"
+0x180 bl "DYLD-STUB$$__stack_chk_fail"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment