Merge pull request #1025 from Kelimion/png_info

Change PNG's img.sidecar to ^Info, make img.depth an int.
This commit is contained in:
Jeroen van Rijn
2021-06-20 18:40:46 +02:00
committed by GitHub
3 changed files with 12 additions and 9 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ Image :: struct {
width: int, width: int,
height: int, height: int,
channels: int, channels: int,
depth: u8, depth: int,
pixels: bytes.Buffer, pixels: bytes.Buffer,
/* /*
Some image loaders/writers can return/take an optional background color. Some image loaders/writers can return/take an optional background color.
+3 -3
View File
@@ -26,12 +26,12 @@ main :: proc() {
if err != nil { if err != nil {
fmt.printf("Trying to read PNG file %v returned %v\n", file, err); fmt.printf("Trying to read PNG file %v returned %v\n", file, err);
} else { } else {
v: png.Info; v: ^png.Info;
ok: bool; ok: bool;
fmt.printf("Image: %vx%vx%v, %v-bit.\n", img.width, img.height, img.channels, img.depth); fmt.printf("Image: %vx%vx%v, %v-bit.\n", img.width, img.height, img.channels, img.depth);
if v, ok = img.sidecar.(png.Info); ok { if v, ok = img.sidecar.(^png.Info); ok {
// Handle ancillary chunks as you wish. // Handle ancillary chunks as you wish.
// We provide helper functions for a few types. // We provide helper functions for a few types.
for c in v.chunks { for c in v.chunks {
@@ -195,7 +195,7 @@ write_image_as_ppm :: proc(filename: string, image: ^image.Image) -> (success: b
defer close(fd); defer close(fd);
write_string(fd, write_string(fd,
fmt.tprintf("P6\n%v %v\n%v\n", width, height, (1 << depth -1)), fmt.tprintf("P6\n%v %v\n%v\n", width, height, (1 << uint(depth) - 1)),
); );
if channels == 3 { if channels == 3 {
+8 -5
View File
@@ -392,7 +392,10 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
img = new(Image); img = new(Image);
} }
img.sidecar = nil; info: ^Info;
if img.sidecar == nil {
info = new(Info);
}
ctx := &compress.Context{ ctx := &compress.Context{
input = stream, input = stream,
@@ -413,7 +416,8 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
e: io.Error; e: io.Error;
header: IHDR; header: IHDR;
info: Info;
img.sidecar = info;
info.chunks.allocator = context.temp_allocator; info.chunks.allocator = context.temp_allocator;
// State to ensure correct chunk ordering. // State to ensure correct chunk ordering.
@@ -462,12 +466,12 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
// Color image without a palette // Color image without a palette
img.channels = 3; img.channels = 3;
final_image_channels = 3; final_image_channels = 3;
img.depth = header.bit_depth; img.depth = int(header.bit_depth);
} else { } else {
// Grayscale // Grayscale
img.channels = 1; img.channels = 1;
final_image_channels = 1; final_image_channels = 1;
img.depth = header.bit_depth; img.depth = int(header.bit_depth);
} }
if .Alpha in header.color_type { if .Alpha in header.color_type {
@@ -522,7 +526,6 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
// If we only want image metadata and don't want the pixel data, we can early out. // If we only want image metadata and don't want the pixel data, we can early out.
if .return_metadata not_in options && .do_not_decompress_image in options { if .return_metadata not_in options && .do_not_decompress_image in options {
img.channels = final_image_channels; img.channels = final_image_channels;
img.sidecar = info;
return img, nil; return img, nil;
} }
// There must be at least 1 IDAT, contiguous if more. // There must be at least 1 IDAT, contiguous if more.