Skip to content

Commit 12c3d9d

Browse files
author
Grok Compression
committed
compress: 16 bit path - phase 1
1 parent e867e93 commit 12c3d9d

20 files changed

Lines changed: 1367 additions & 165 deletions

File tree

src/lib/codec/apps/GrkCompress.cpp

Lines changed: 15 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ static void validateCinema(const std::string& arg, uint16_t profile, grk_cparame
264264
parameters->numgbits = (profile == GRK_PROFILE_CINEMA_2K) ? 1 : 2;
265265
}
266266

267+
template<typename T>
267268
static grk_image* loadInputImage(const char* filename, grk_cparameters* parameters)
268269
{
269270
grk_image* image = nullptr;
@@ -278,47 +279,47 @@ static grk_image* loadInputImage(const char* filename, grk_cparameters* paramete
278279
switch(fmt)
279280
{
280281
case GRK_FMT_PGX: {
281-
PGXFormat<int32_t> pgx;
282+
PGXFormat<T> pgx;
282283
image = pgx.readImage(filename, parameters);
283284
}
284285
break;
285286
case GRK_FMT_PXM: {
286-
PNMFormat<int32_t> pnm(false);
287+
PNMFormat<T> pnm(false);
287288
image = pnm.readImage(filename, parameters);
288289
}
289290
break;
290291
case GRK_FMT_BMP: {
291-
BMPFormat<int32_t> bmp;
292+
BMPFormat<T> bmp;
292293
image = bmp.readImage(filename, parameters);
293294
}
294295
break;
295296
#ifdef GROK_HAVE_LIBTIFF
296297
case GRK_FMT_TIF: {
297-
TIFFFormat<int32_t> tif;
298+
TIFFFormat<T> tif;
298299
image = tif.readImage(filename, parameters);
299300
}
300301
break;
301302
#endif
302303
case GRK_FMT_RAW: {
303-
RAWFormat<int32_t> raw(true);
304+
RAWFormat<T> raw(true);
304305
image = raw.readImage(filename, parameters);
305306
}
306307
break;
307308
case GRK_FMT_RAWL: {
308-
RAWFormat<int32_t> raw(false);
309+
RAWFormat<T> raw(false);
309310
image = raw.readImage(filename, parameters);
310311
}
311312
break;
312313
#ifdef GROK_HAVE_LIBPNG
313314
case GRK_FMT_PNG: {
314-
PNGFormat<int32_t> png;
315+
PNGFormat<T> png;
315316
image = png.readImage(filename, parameters);
316317
}
317318
break;
318319
#endif
319320
#ifdef GROK_HAVE_LIBJPEG
320321
case GRK_FMT_JPG: {
321-
JPEGFormat<int32_t> jpeg;
322+
JPEGFormat<T> jpeg;
322323
image = jpeg.readImage(filename, parameters);
323324
}
324325
break;
@@ -400,7 +401,8 @@ int GrkCompress::main(int argc, const char** argv, grk_image* in_image, grk_stre
400401
initParams.parameters.rate_control_algorithm = rate_control_algorithm;
401402
initParams.parameters.decod_format = GRK_FMT_UNK;
402403

403-
grk_image* image = loadInputImage(filePath.string().c_str(), &initParams.parameters);
404+
grk_image* image =
405+
loadInputImage<int32_t>(filePath.string().c_str(), &initParams.parameters);
404406
if(!image)
405407
{
406408
spdlog::warn("Skipping unreadable file: {}", filePath.filename().string());
@@ -2070,102 +2072,11 @@ static uint64_t pluginCompressCallback(grk_plugin_compress_user_callback_info* i
20702072
}
20712073
}
20722074
/* decode the source image */
2073-
switch(info->compressor_parameters->decod_format)
2075+
image = loadInputImage<int32_t>(info->input_file_name, info->compressor_parameters);
2076+
if(!image)
20742077
{
2075-
case GRK_FMT_PGX: {
2076-
PGXFormat<int32_t> pgx;
2077-
image = pgx.readImage(info->input_file_name, info->compressor_parameters);
2078-
if(!image)
2079-
{
2080-
spdlog::error("Unable to load pgx file");
2081-
goto cleanup;
2082-
}
2083-
}
2084-
break;
2085-
2086-
case GRK_FMT_PXM: {
2087-
PNMFormat<int32_t> pnm(false);
2088-
image = pnm.readImage(info->input_file_name, info->compressor_parameters);
2089-
if(!image)
2090-
{
2091-
spdlog::error("Unable to load pnm file");
2092-
goto cleanup;
2093-
}
2094-
}
2095-
break;
2096-
2097-
case GRK_FMT_BMP: {
2098-
BMPFormat<int32_t> bmp;
2099-
image = bmp.readImage(info->input_file_name, info->compressor_parameters);
2100-
if(!image)
2101-
{
2102-
spdlog::error("Unable to load bmp file");
2103-
goto cleanup;
2104-
}
2105-
}
2106-
break;
2107-
2108-
#ifdef GROK_HAVE_LIBTIFF
2109-
case GRK_FMT_TIF: {
2110-
TIFFFormat<int32_t> tif;
2111-
image = tif.readImage(info->input_file_name, info->compressor_parameters);
2112-
if(!image)
2113-
goto cleanup;
2114-
}
2115-
break;
2116-
#endif /* GROK_HAVE_LIBTIFF */
2117-
2118-
case GRK_FMT_RAW: {
2119-
RAWFormat<int32_t> raw(true);
2120-
image = raw.readImage(info->input_file_name, info->compressor_parameters);
2121-
if(!image)
2122-
{
2123-
spdlog::error("Unable to load raw file");
2124-
goto cleanup;
2125-
}
2126-
}
2127-
break;
2128-
2129-
case GRK_FMT_RAWL: {
2130-
RAWFormat<int32_t> raw(false);
2131-
image = raw.readImage(info->input_file_name, info->compressor_parameters);
2132-
if(!image)
2133-
{
2134-
spdlog::error("Unable to load raw file");
2135-
goto cleanup;
2136-
}
2137-
}
2138-
break;
2139-
2140-
#ifdef GROK_HAVE_LIBPNG
2141-
case GRK_FMT_PNG: {
2142-
PNGFormat<int32_t> png;
2143-
image = png.readImage(info->input_file_name, info->compressor_parameters);
2144-
if(!image)
2145-
{
2146-
spdlog::error("Unable to load png file");
2147-
goto cleanup;
2148-
}
2149-
}
2150-
break;
2151-
#endif /* GROK_HAVE_LIBPNG */
2152-
2153-
#ifdef GROK_HAVE_LIBJPEG
2154-
case GRK_FMT_JPG: {
2155-
JPEGFormat<int32_t> jpeg;
2156-
image = jpeg.readImage(info->input_file_name, info->compressor_parameters);
2157-
if(!image)
2158-
{
2159-
spdlog::error("Unable to load jpeg file");
2160-
goto cleanup;
2161-
}
2162-
}
2163-
break;
2164-
#endif /* GROK_HAVE_LIBPNG */
2165-
default:
2166-
spdlog::error("Input file format {} is not supported",
2167-
convertFileFmtToString(info->compressor_parameters->decod_format));
2168-
goto cleanup;
2078+
spdlog::error("Unable to load input file");
2079+
goto cleanup;
21692080
}
21702081

21712082
/* Can happen if input file is TIFF or PNG

src/lib/codec/common/convert.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,11 @@ void convertToOutput(const uint8_t* src, T* dest, size_t w, bool invert)
201201
for(i = 0; i < (w & ~(size_t)1U); i += 2U)
202202
{
203203
uint8_t val = *src++;
204-
dest[i + 0] = INV(sign_extend<T>(val >> 4, 32 - 4), 0xF, invert);
205-
dest[i + 1] = INV(sign_extend<T>(val & 0xF, 32 - 4), 0xF, invert);
204+
dest[i + 0] = INV(sign_extend<T>(val >> 4, sizeof(T) * 8 - 4), 0xF, invert);
205+
dest[i + 1] = INV(sign_extend<T>(val & 0xF, sizeof(T) * 8 - 4), 0xF, invert);
206206
}
207207
if(w & 1U)
208-
dest[i + 0] = INV(sign_extend<T>((*src++) >> 4, 32 - 4), 0xF, invert);
208+
dest[i + 0] = INV(sign_extend<T>((*src++) >> 4, sizeof(T) * 8 - 4), 0xF, invert);
209209
}
210210
else
211211
{
@@ -368,22 +368,22 @@ void convertToOutput(const uint8_t* src, T* dest, size_t w, bool invert)
368368
uint32_t val3 = *src++;
369369
uint32_t val4 = *src++;
370370

371-
dest[i + 0] =
372-
sign_extend<T>(INV((T)((val0 << 2) | (val1 >> 6)), INV_MASK_10, invert), 32 - 10);
371+
dest[i + 0] = sign_extend<T>(INV((T)((val0 << 2) | (val1 >> 6)), INV_MASK_10, invert),
372+
sizeof(T) * 8 - 10);
373373
dest[i + 1] = sign_extend<T>(
374-
INV((T)(((val1 & 0x3FU) << 4) | (val2 >> 4)), INV_MASK_10, invert), 32 - 10);
374+
INV((T)(((val1 & 0x3FU) << 4) | (val2 >> 4)), INV_MASK_10, invert), sizeof(T) * 8 - 10);
375375
dest[i + 2] = sign_extend<T>(
376-
INV((T)(((val2 & 0xFU) << 6) | (val3 >> 2)), INV_MASK_10, invert), 32 - 10);
377-
dest[i + 3] =
378-
sign_extend<T>(INV((T)(((val3 & 0x3U) << 8) | val4), INV_MASK_10, invert), 32 - 10);
376+
INV((T)(((val2 & 0xFU) << 6) | (val3 >> 2)), INV_MASK_10, invert), sizeof(T) * 8 - 10);
377+
dest[i + 3] = sign_extend<T>(INV((T)(((val3 & 0x3U) << 8) | val4), INV_MASK_10, invert),
378+
sizeof(T) * 8 - 10);
379379
}
380380
if(w & 3U)
381381
{
382382
uint32_t val0 = *src++;
383383
uint32_t val1 = *src++;
384384
w = w & 3U;
385-
dest[i + 0] =
386-
sign_extend<T>(INV((T)((val0 << 2) | (val1 >> 6)), INV_MASK_10, invert), 32 - 10);
385+
dest[i + 0] = sign_extend<T>(INV((T)((val0 << 2) | (val1 >> 6)), INV_MASK_10, invert),
386+
sizeof(T) * 8 - 10);
387387

388388
if(w > 1U)
389389
{
@@ -482,17 +482,17 @@ void convertToOutput(const uint8_t* src, T* dest, size_t w, bool invert)
482482
uint32_t val1 = *src++;
483483
uint32_t val2 = *src++;
484484

485-
dest[i + 0] =
486-
sign_extend<T>(INV((T)((val0 << 4) | (val1 >> 4)), INV_MASK_12, invert), 32 - 12);
487-
dest[i + 1] =
488-
sign_extend<T>(INV((T)(((val1 & 0xFU) << 8) | val2), INV_MASK_12, invert), 32 - 12);
485+
dest[i + 0] = sign_extend<T>(INV((T)((val0 << 4) | (val1 >> 4)), INV_MASK_12, invert),
486+
sizeof(T) * 8 - 12);
487+
dest[i + 1] = sign_extend<T>(INV((T)(((val1 & 0xFU) << 8) | val2), INV_MASK_12, invert),
488+
sizeof(T) * 8 - 12);
489489
}
490490
if(w & 1U)
491491
{
492492
uint32_t val0 = *src++;
493493
uint32_t val1 = *src++;
494-
dest[i + 0] =
495-
sign_extend<T>(INV((T)((val0 << 4) | (val1 >> 4)), INV_MASK_12, invert), 32 - 12);
494+
dest[i + 0] = sign_extend<T>(INV((T)((val0 << 4) | (val1 >> 4)), INV_MASK_12, invert),
495+
sizeof(T) * 8 - 12);
496496
}
497497
}
498498
else

src/lib/codec/formats/BMPFormat.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,8 @@ grk_image* BMPFormat<T>::readImage(const std::string& fname, grk_cparameters* pa
952952
auto img_comp = cmptparm + i;
953953
img_comp->prec = (numcmpts == 1U) ? (uint8_t)infoHeader_.biBitCount : 8U;
954954
img_comp->sgnd = false;
955+
if constexpr(sizeof(T) == 2)
956+
img_comp->data_type = GRK_INT_16;
955957
img_comp->dx = parameters->subsampling_dx;
956958
img_comp->dy = parameters->subsampling_dy;
957959
img_comp->w = grk::ceildiv<uint32_t>((uint32_t)infoHeader_.biWidth, img_comp->dx);

src/lib/codec/formats/JPEGFormat.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ grk_image* JPEGFormat<T>::jpegtoimage(const char* filename, grk_cparameters* par
235235
for(int j = 0; j < cinfo.output_components; j++)
236236
{
237237
cmptparm[j].prec = (uint8_t)bps;
238+
if constexpr(sizeof(T) == 2)
239+
cmptparm[j].data_type = GRK_INT_16;
238240
cmptparm[j].dx = 1;
239241
cmptparm[j].dy = 1;
240242
cmptparm[j].w = w;

src/lib/codec/formats/PGXFormat.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ grk_image* pgxtoimage(const char* filename, const grk_cparameters* parameters)
149149
: cmptparm.y0 + (uint32_t)(h - 1) * parameters->subsampling_dy + 1;
150150
cmptparm.sgnd = sign;
151151
cmptparm.prec = (uint8_t)prec;
152+
if constexpr(sizeof(T) == 2)
153+
cmptparm.data_type = GRK_INT_16;
152154
cmptparm.dx = parameters->subsampling_dx;
153155
cmptparm.dy = parameters->subsampling_dy;
154156

@@ -165,7 +167,7 @@ grk_image* pgxtoimage(const char* filename, const grk_cparameters* parameters)
165167

166168
/* set image data */
167169
stride_diff = image->comps->stride - w;
168-
shift = (uint8_t)(32 - prec);
170+
shift = (uint8_t)(sizeof(T) * 8 - prec);
169171
dest = (T*)image->comps->data;
170172
for(uint32_t j = 0; j < h; ++j)
171173
{

src/lib/codec/formats/PNGFormat.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ bool PNGFormat<T>::writeHeader(void)
311311
spdlog::error("Can't allocate memory for PNG row");
312312
goto beach;
313313
}
314-
row32s_ = (T*)malloc((size_t)image_->comps[0].w * (size_t)nr_comp_ * sizeof(int32_t));
314+
row32s_ = (T*)malloc((size_t)image_->comps[0].w * (size_t)nr_comp_ * sizeof(T));
315315
if(row32s_ == nullptr)
316316
{
317317
spdlog::error("Can't allocate memory for interleaved 32s row");
@@ -565,6 +565,8 @@ grk_image* PNGFormat<T>::do_decode(grk_cparameters* params)
565565
auto img_comp = cmptparm + i;
566566
img_comp->prec = (uint8_t)bit_depth;
567567
img_comp->sgnd = false;
568+
if constexpr(sizeof(T) == 2)
569+
img_comp->data_type = GRK_INT_16;
568570
img_comp->dx = params->subsampling_dx;
569571
img_comp->dy = params->subsampling_dy;
570572
img_comp->w = grk::ceildiv<uint32_t>(width, img_comp->dx);

src/lib/codec/formats/PNMFormat.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,8 @@ grk_image* PNMFormat<T>::readImage(const grk_cparameters* parameters)
10661066
{
10671067
cmptparm[i].prec = prec;
10681068
cmptparm[i].sgnd = false;
1069+
if constexpr(sizeof(T) == 2)
1070+
cmptparm[i].data_type = GRK_INT_16;
10691071
cmptparm[i].dx = subsampling_dx;
10701072
cmptparm[i].dy = subsampling_dy;
10711073
cmptparm[i].w = w;

src/lib/codec/formats/RAWFormat.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ grk_image* RAWFormat<T>::readImage(const char* filename, grk_cparameters* parame
321321
memset(cmptparm + i, 0, sizeof(grk_image_comp));
322322
cmptparm[i].prec = raw_cp->prec;
323323
cmptparm[i].sgnd = raw_cp->sgnd;
324+
if constexpr(sizeof(T) == 2)
325+
cmptparm[i].data_type = GRK_INT_16;
324326
cmptparm[i].dx = (uint8_t)(subsampling_dx * raw_cp->comps[i].dx);
325327
cmptparm[i].dy = (uint8_t)(subsampling_dy * raw_cp->comps[i].dy);
326328
cmptparm[i].w = w;

src/lib/codec/formats/TIFFFormat.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,8 @@ grk_image* TIFFFormat<T>::readImage(const std::string& filename, grk_cparameters
20752075
auto img_comp = &cmptparm[j];
20762076
memset(img_comp, 0, sizeof(grk_image_comp));
20772077
img_comp->prec = (uint8_t)tiBps;
2078+
if constexpr(sizeof(T) == 2)
2079+
img_comp->data_type = GRK_INT_16;
20782080
bool chroma = (j == 1 || j == 2);
20792081
img_comp->dx = chroma ? (uint8_t)chroma_subsample_x : 1;
20802082
img_comp->dy = chroma ? (uint8_t)chroma_subsample_y : 1;

0 commit comments

Comments
 (0)