Skip to content

Commit 476105c

Browse files
Enable g2int and int outputs for jpeg decoding with OpenJPEG (#591)
* create static function int_dec_jpeg2000 to decenc_openjpeg.c to handle g2int and int outputs * typos * format * more format issues * Update decenc_openjpeg.c * address void expression warning * enable tests for openjpeg
1 parent 5f79509 commit 476105c

2 files changed

Lines changed: 73 additions & 35 deletions

File tree

src/decenc_openjpeg.c

Lines changed: 73 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,14 @@ opj_stream_create_default_memory_stream(opj_memory_stream *memoryStream, OPJ_BOO
243243
* PROGRAM HISTORY LOG:
244244
* - 2002-12-02 Gilbert
245245
* - 2016-06-08 Jovic
246+
* - 2025-03-07 Stahl | added functionality to handle g2int and int outputs
246247
*
247248
* @param injpc Input JPEG2000 code stream.
248249
* @param bufsize Length (in bytes) of the input JPEG2000 code stream.
249-
* @param outfld Output matrix of grayscale image values.
250+
* @param outfld Pointer to either int or g2int array, already
251+
* allocated, that gets the unpacked data.
252+
* @param out_is_g2int Non-zero if the output array is of type g2int
253+
* (i.e. 64-bit ints), zero if output is an int array (32-bits).
250254
*
251255
* @return
252256
* - 0 Successful decode
@@ -255,37 +259,10 @@ opj_stream_create_default_memory_stream(opj_memory_stream *memoryStream, OPJ_BOO
255259
*
256260
* @note Requires OpenJPEG Version 2.
257261
*
258-
* @author Alyson Stahl
262+
* @author Stephen Gilbert, Jovic, Stahl
259263
*/
260-
int
261-
g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld)
262-
{
263-
return dec_jpeg2000(injpc, bufsize, (g2int *)outfld);
264-
}
265-
266-
/**
267-
* This Function decodes a JPEG2000 code stream specified in the
268-
* JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using OpenJPEG.
269-
*
270-
* PROGRAM HISTORY LOG:
271-
* - 2002-12-02 Gilbert
272-
* - 2016-06-08 Jovic
273-
*
274-
* @param injpc Input JPEG2000 code stream.
275-
* @param bufsize Length (in bytes) of the input JPEG2000 code stream.
276-
* @param outfld Output matrix of grayscale image values.
277-
*
278-
* @return
279-
* - 0 Successful decode
280-
* - -3 Error decode jpeg2000 code stream.
281-
* - -5 decoded image had multiple color components. Only grayscale is expected.
282-
*
283-
* @note Requires OpenJPEG Version 2.
284-
*
285-
* @author Stephen Gilbert, Jovic
286-
*/
287-
int
288-
dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
264+
static int
265+
int_dec_jpeg2000(char *injpc, g2int bufsize, void *outfld, int out_is_g2int)
289266
{
290267
int iret = 0;
291268
OPJ_INT32 mask;
@@ -348,8 +325,16 @@ dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
348325

349326
mask = (1 << image->comps[0].prec) - 1;
350327

351-
for (unsigned int i = 0; i < image->comps[0].w * image->comps[0].h; i++)
352-
outfld[i] = (g2int)(image->comps[0].data[i] & mask);
328+
if (out_is_g2int)
329+
{
330+
for (unsigned int i = 0; i < image->comps[0].w * image->comps[0].h; i++)
331+
((g2int *)outfld)[i] = (g2int)(image->comps[0].data[i] & mask);
332+
}
333+
else
334+
{
335+
for (unsigned int i = 0; i < image->comps[0].w * image->comps[0].h; i++)
336+
((int *)outfld)[i] = (int)(image->comps[0].data[i] & mask);
337+
}
353338

354339
if (!opj_end_decompress(codec, stream))
355340
{
@@ -369,6 +354,61 @@ dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
369354
return iret;
370355
}
371356

357+
/**
358+
* This Function decodes a JPEG2000 code stream specified in the
359+
* JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using OpenJPEG.
360+
*
361+
* PROGRAM HISTORY LOG:
362+
* - 2002-12-02 Gilbert
363+
* - 2016-06-08 Jovic
364+
*
365+
* @param injpc Input JPEG2000 code stream.
366+
* @param bufsize Length (in bytes) of the input JPEG2000 code stream.
367+
* @param outfld Output matrix of grayscale image values.
368+
*
369+
* @return
370+
* - 0 Successful decode
371+
* - -3 Error decode jpeg2000 code stream.
372+
* - -5 decoded image had multiple color components. Only grayscale is expected.
373+
*
374+
* @note Requires OpenJPEG Version 2.
375+
*
376+
* @author Alyson Stahl
377+
*/
378+
int
379+
g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld)
380+
{
381+
return int_dec_jpeg2000(injpc, bufsize, outfld, 0);
382+
}
383+
384+
/**
385+
* This Function decodes a JPEG2000 code stream specified in the
386+
* JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using OpenJPEG.
387+
*
388+
* PROGRAM HISTORY LOG:
389+
* - 2002-12-02 Gilbert
390+
* - 2016-06-08 Jovic
391+
* - 2025-03-07 Stahl | moved code to int_dec_jpeg2000() function to handle int outputs
392+
*
393+
* @param injpc Input JPEG2000 code stream.
394+
* @param bufsize Length (in bytes) of the input JPEG2000 code stream.
395+
* @param outfld Output matrix of grayscale image values.
396+
*
397+
* @return
398+
* - 0 Successful decode
399+
* - -3 Error decode jpeg2000 code stream.
400+
* - -5 decoded image had multiple color components. Only grayscale is expected.
401+
*
402+
* @note Requires OpenJPEG Version 2.
403+
*
404+
* @author Stephen Gilbert, Jovic, Stahl
405+
*/
406+
int
407+
dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
408+
{
409+
return int_dec_jpeg2000(injpc, bufsize, outfld, 1);
410+
}
411+
372412
/**
373413
* This Function encodes a grayscale image into a JPEG2000 code stream
374414
* specified in the JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1)

tests/tst_jpeg.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ main()
2020

2121
printf("Testing JPEG functions.\n");
2222
/* g2c_set_log_level(10); */
23-
#ifdef USE_JPEG2000
2423
printf("Testing enc_jpeg2000()/dec_jpeg2000() call...");
2524
{
2625
unsigned char data[DATA_LEN] = {1, 2, 3, 4};
@@ -72,7 +71,6 @@ main()
7271
}
7372
}
7473
printf("ok!\n");
75-
#endif
7674
{
7775
g2int height = 2, width = 2;
7876
g2int len = PACKED_LEN, ndpts = DATA_LEN;

0 commit comments

Comments
 (0)