Merge branch 'release-v4.6.0'
[WPS.git] / ungrib / src / ngl / g2 / dec_jpeg2000.c
blob2f9df244ae9515329737ca8f61d79edabfce98d3
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #ifdef USE_JPEG2000
5 #include "jasper/jasper.h"
6 #define JAS_1_700_2
7 #endif /* USE_JPEG2000 */
10 #ifdef __64BIT__
11 typedef int g2int;
12 #else
13 typedef long g2int;
14 #endif
16 #if defined _UNDERSCORE
17 #define dec_jpeg2000 dec_jpeg2000_
18 #elif defined _DOUBLEUNDERSCORE
19 #define dec_jpeg2000 dec_jpeg2000__
20 #endif
22 int dec_jpeg2000(char *injpc,g2int *bufsize,g2int *outfld)
23 /*$$$ SUBPROGRAM DOCUMENTATION BLOCK
24 * . . . .
25 * SUBPROGRAM: dec_jpeg2000 Decodes JPEG2000 code stream
26 * PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-12-02
28 * ABSTRACT: This Function decodes a JPEG2000 code stream specified in the
29 * JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using JasPer
30 * Software version 1.500.4 (or 1.700.2) written by the University of British
31 * Columbia and Image Power Inc, and others.
32 * JasPer is available at http://www.ece.uvic.ca/~mdadams/jasper/.
34 * PROGRAM HISTORY LOG:
35 * 2002-12-02 Gilbert
37 * USAGE: int dec_jpeg2000(char *injpc,g2int *bufsize,g2int *outfld)
39 * INPUT ARGUMENTS:
40 * injpc - Input JPEG2000 code stream.
41 * bufsize - Length (in bytes) of the input JPEG2000 code stream.
43 * INPUT ARGUMENTS:
44 * outfld - Output matrix of grayscale image values.
46 * RETURN VALUES :
47 * 0 = Successful decode
48 * -3 = Error decode jpeg2000 code stream.
49 * -5 = decoded image had multiple color components.
50 * Only grayscale is expected.
52 * REMARKS:
54 * Requires JasPer Software version 1.500.4 or 1.700.2
56 * ATTRIBUTES:
57 * LANGUAGE: C
58 * MACHINE: IBM SP
60 *$$$*/
63 #ifdef USE_JPEG2000
64 int ier;
65 g2int i,j,k,n;
66 jas_image_t *image=0;
67 jas_stream_t *jpcstream,*istream;
68 jas_image_cmpt_t cmpt,*pcmpt;
69 char *opts=0;
70 jas_matrix_t *data;
72 /* jas_init(); */
74 /*
75 * Create jas_stream_t containing input JPEG200 codestream in memory.
76 */
78 jpcstream=jas_stream_memopen(injpc,*bufsize);
80 /*
81 * Decode JPEG200 codestream into jas_image_t structure.
82 */
83 image=jpc_decode(jpcstream,opts);
84 if ( image == 0 ) {
85 printf(" jpc_decode return = %d \n",ier);
86 return -3;
89 pcmpt=image->cmpts_[0];
91 printf(" SAGOUT DECODE:\n");
92 printf(" tlx %d \n",image->tlx_);
93 printf(" tly %d \n",image->tly_);
94 printf(" brx %d \n",image->brx_);
95 printf(" bry %d \n",image->bry_);
96 printf(" numcmpts %d \n",image->numcmpts_);
97 printf(" maxcmpts %d \n",image->maxcmpts_);
98 #ifdef JAS_1_500_4
99 printf(" colormodel %d \n",image->colormodel_);
100 #endif
101 #ifdef JAS_1_700_2
102 printf(" colorspace %d \n",image->clrspc_);
103 #endif
104 printf(" inmem %d \n",image->inmem_);
105 printf(" COMPONENT:\n");
106 printf(" tlx %d \n",pcmpt->tlx_);
107 printf(" tly %d \n",pcmpt->tly_);
108 printf(" hstep %d \n",pcmpt->hstep_);
109 printf(" vstep %d \n",pcmpt->vstep_);
110 printf(" width %d \n",pcmpt->width_);
111 printf(" height %d \n",pcmpt->height_);
112 printf(" prec %d \n",pcmpt->prec_);
113 printf(" sgnd %d \n",pcmpt->sgnd_);
114 printf(" cps %d \n",pcmpt->cps_);
115 #ifdef JAS_1_700_2
116 printf(" type %d \n",pcmpt->type_);
117 #endif
120 /* Expecting jpeg2000 image to be grayscale only.
121 * No color components.
123 if (image->numcmpts_ != 1 ) {
124 printf("dec_jpeg2000: Found color image. Grayscale expected.\n");
125 return (-5);
129 * Create a data matrix of grayscale image values decoded from
130 * the jpeg2000 codestream.
132 data=jas_matrix_create(jas_image_height(image), jas_image_width(image));
133 jas_image_readcmpt(image,0,0,0,jas_image_width(image),
134 jas_image_height(image),data);
136 * Copy data matrix to output integer array.
138 k=0;
139 for (i=0;i<pcmpt->height_;i++)
140 for (j=0;j<pcmpt->width_;j++)
141 outfld[k++]=data->rows_[i][j];
143 * Clean up JasPer work structures.
145 jas_matrix_destroy(data);
146 ier=jas_stream_close(jpcstream);
147 jas_image_destroy(image);
149 #endif /* USE_JPEG2000 */
150 return 0;