Skip to content

Commit 6280c6d

Browse files
committed
resolved 16bit JPEG2000 encoding and decoding (#1436)
1 parent 079cf1b commit 6280c6d

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

libheif/plugins/decoder_openjpeg.cc

+7-4
Original file line numberDiff line numberDiff line change
@@ -376,15 +376,18 @@ struct heif_error openjpeg_decode_image(void* decoder_raw, struct heif_image** o
376376
// TODO: a SIMD implementation to convert int32 to uint8 would speed this up
377377
// https://stackoverflow.com/questions/63774643/how-to-convert-uint32-to-uint8-using-simd-but-not-avx512
378378

379-
if (stride == cwidth) {
380-
for (int i = 0; i < cwidth * cheight; i++) {
381-
p[i] = (uint8_t) opj_comp.data[i];
379+
if (bit_depth <= 8) {
380+
for (int y = 0; y < cheight; y++) {
381+
for (int x = 0; x < cwidth; x++) {
382+
p[y * stride + x] = (uint8_t) opj_comp.data[y * cwidth + x];
383+
}
382384
}
383385
}
384386
else {
387+
uint16_t* p16 = (uint16_t*)p;
385388
for (int y = 0; y < cheight; y++) {
386389
for (int x = 0; x < cwidth; x++) {
387-
p[y * stride + x] = (uint8_t) opj_comp.data[y * cwidth + x];
390+
p16[y * stride/2 + x] = (uint16_t) opj_comp.data[y * cwidth + x];
388391
}
389392
}
390393
}

libheif/plugins/encoder_openjpeg.cc

+16-3
Original file line numberDiff line numberDiff line change
@@ -527,14 +527,27 @@ struct heif_error opj_encode_image(void* encoder_raw, const struct heif_image* i
527527
for (int comp = 0; comp < band_count; comp++) {
528528
int stride;
529529
const uint8_t* p = heif_image_get_plane_readonly(image, channels[comp], &stride);
530+
int bpp = heif_image_get_bits_per_pixel(image, channels[comp]);
530531

531532
int cwidth = component_params[comp].w;
532533
int cheight = component_params[comp].h;
533534

534535
// Note: obj_image data is 32bit integer
535-
for (int y = 0; y < cheight; y++) {
536-
for (int x = 0; x < cwidth; x++) {
537-
opj_image->comps[comp].data[y * cwidth + x] = p[y * stride + x];
536+
537+
if (bpp <= 8) {
538+
for (int y = 0; y < cheight; y++) {
539+
for (int x = 0; x < cwidth; x++) {
540+
opj_image->comps[comp].data[y * cwidth + x] = p[y * stride + x];
541+
}
542+
}
543+
}
544+
else {
545+
const uint16_t* p16 = (const uint16_t*)p;
546+
547+
for (int y = 0; y < cheight; y++) {
548+
for (int x = 0; x < cwidth; x++) {
549+
opj_image->comps[comp].data[y * cwidth + x] = p16[y * stride/2 + x];
550+
}
538551
}
539552
}
540553
}

0 commit comments

Comments
 (0)