Skip to content

Commit

Permalink
PNG reader: implement loading HDR image as SDR (#971)
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Oct 18, 2023
1 parent f4f185b commit b051d31
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions examples/decoder_png.cc
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,22 @@ InputImage loadPNG(const char* filename, int output_bit_depth)
}
}
else {
err = heif_image_create((int) width, (int) height,
heif_colorspace_RGB,
has_alpha ?
heif_chroma_interleaved_RRGGBBAA_LE :
heif_chroma_interleaved_RRGGBB_LE,
&image);
if (output_bit_depth == 8) {
err = heif_image_create((int) width, (int) height,
heif_colorspace_RGB,
has_alpha ?
heif_chroma_interleaved_RGBA :
heif_chroma_interleaved_RGB,
&image);
}
else {
err = heif_image_create((int) width, (int) height,
heif_colorspace_RGB,
has_alpha ?
heif_chroma_interleaved_RRGGBBAA_LE :
heif_chroma_interleaved_RRGGBB_LE,
&image);
}
(void) err;

int bdShift = 16 - output_bit_depth;
Expand All @@ -367,16 +377,32 @@ InputImage loadPNG(const char* filename, int output_bit_depth)
int stride;
uint8_t* p_out = (uint8_t*) heif_image_get_plane(image, heif_channel_interleaved, &stride);

for (uint32_t y = 0; y < height; y++) {
uint8_t* p = row_pointers[y];
if (output_bit_depth==8) {
// convert HDR to SDR

for (uint32_t y = 0; y < height; y++) {
uint8_t* p = row_pointers[y];

uint32_t nVal = (has_alpha ? 4 : 3) * width;
uint32_t nVal = (has_alpha ? 4 : 3) * width;

for (uint32_t x = 0; x < nVal; x++) {
uint16_t v = (uint16_t) (((p[0] << 8) | p[1]) >> bdShift);
p_out[2 * x + y * stride + 1] = (uint8_t) (v >> 8);
p_out[2 * x + y * stride + 0] = (uint8_t) (v & 0xFF);
p += 2;
for (uint32_t x = 0; x < nVal; x++) {
p_out[x + y * stride] = p[0];
p+=2;
}
}
}
else {
for (uint32_t y = 0; y < height; y++) {
uint8_t* p = row_pointers[y];

uint32_t nVal = (has_alpha ? 4 : 3) * width;

for (uint32_t x = 0; x < nVal; x++) {
uint16_t v = (uint16_t) (((p[0] << 8) | p[1]) >> bdShift);
p_out[2 * x + y * stride + 1] = (uint8_t) (v >> 8);
p_out[2 * x + y * stride + 0] = (uint8_t) (v & 0xFF);
p += 2;
}
}
}
}
Expand Down

0 comments on commit b051d31

Please sign in to comment.