Small updates to JPEG loader

- Remove some unnecessary nesting
- Add frame type (SOF0, et al) to metadata if `.return_metadata` is used
This commit is contained in:
Jeroen van Rijn
2025-09-09 18:34:19 +02:00
parent 6b18f8df63
commit cd3069b16b
2 changed files with 774 additions and 762 deletions
+15 -13
View File
@@ -642,22 +642,23 @@ JFXX_Extension_Code :: enum u8 {
} }
JPEG_Marker :: enum u8 { JPEG_Marker :: enum u8 {
SOF0 = 0xC0, SOF0 = 0xC0, // Baseline sequential DCT
SOF1 = 0xC1, SOF1 = 0xC1, // Extended sequential DCT
SOF2 = 0xC2, SOF2 = 0xC2, // Progressive DCT
SOF3 = 0xC3, SOF3 = 0xC3, // Lossless (sequential)
SOF5 = 0xC5, // Differential sequential DCT
SOF6 = 0xC6, // Differential progressive DCT
SOF7 = 0xC7, // Differential lossless (sequential)
SOF9 = 0xC9, // Extended sequential DCT, Arithmetic coding
SOF10 = 0xCA, // Progressive DCT, Arithmetic coding
SOF11 = 0xCB, // Lossless (sequential), Arithmetic coding
SOF13 = 0xCD, // Differential sequential DCT, Arithmetic coding
SOF14 = 0xCE, // Differential progressive DCT, Arithmetic coding
SOF15 = 0xCF, // Differential lossless (sequential), Arithmetic coding
DHT = 0xC4, DHT = 0xC4,
SOF5 = 0xC5,
SOF6 = 0xC6,
SOF7 = 0xC7,
JPG = 0xC8, JPG = 0xC8,
SOF9 = 0xC9,
SOF10 = 0xCA,
SOF11 = 0xCB,
DAC = 0xCC, DAC = 0xCC,
SOF13 = 0xCD,
SOF14 = 0xCE,
SOF15 = 0xCF,
RST0 = 0xD0, RST0 = 0xD0,
RST1 = 0xD1, RST1 = 0xD1,
RST2 = 0xD2, RST2 = 0xD2,
@@ -713,6 +714,7 @@ JPEG_Info :: struct {
jfxx_app0: Maybe(JFXX_APP0), jfxx_app0: Maybe(JFXX_APP0),
comments: [dynamic]string, comments: [dynamic]string,
exif: [dynamic]Exif, exif: [dynamic]Exif,
frame_type: JPEG_Marker,
} }
// Function to help with image buffer calculations // Function to help with image buffer calculations
+12 -2
View File
@@ -219,8 +219,10 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
defer delete(blocks) defer delete(blocks)
loop: for { loop: for {
// Loop until we find 0xFF.
first = compress.read_u8(ctx) or_return first = compress.read_u8(ctx) or_return
if first == 0xFF { (first == 0xFF) or_continue
marker := cast(image.JPEG_Marker)compress.read_u8(ctx) or_return marker := cast(image.JPEG_Marker)compress.read_u8(ctx) or_return
if expect_EOI && marker != .EOI { if expect_EOI && marker != .EOI {
return img, .Extra_Data_After_SOS return img, .Extra_Data_After_SOS
@@ -582,6 +584,11 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
return img, .Invalid_Number_Of_Channels return img, .Invalid_Number_Of_Channels
} }
if img.metadata != nil {
info := img.metadata.(^image.JPEG_Info)
info.frame_type = marker
}
mcu_width = (img.width + 7) / BLOCK_SIZE mcu_width = (img.width + 7) / BLOCK_SIZE
mcu_height = (img.height + 7) / BLOCK_SIZE mcu_height = (img.height + 7) / BLOCK_SIZE
block_width = mcu_width block_width = mcu_width
@@ -661,6 +668,10 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
case .SOF14: // Differential progressive DCT, Arithmetic coding case .SOF14: // Differential progressive DCT, Arithmetic coding
fallthrough fallthrough
case .SOF15: // Differential lossless (sequential), Arithmetic coding case .SOF15: // Differential lossless (sequential), Arithmetic coding
if img.metadata != nil {
info := img.metadata.(^image.JPEG_Info)
info.frame_type = marker
}
return img, .Unsupported_Frame_Type return img, .Unsupported_Frame_Type
case .SOS: case .SOS:
if img.channels == 0 && img.depth == 0 && img.width == 0 && img.height == 0 { if img.channels == 0 && img.depth == 0 && img.width == 0 && img.height == 0 {
@@ -1055,7 +1066,6 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
compress.read_slice_from_memory(ctx, cast(int)length) or_return compress.read_slice_from_memory(ctx, cast(int)length) or_return
} }
} }
}
return return
} }