ungrib build
[WPS.git] / ungrib / src / ngl / g2 / enc_jpeg2000.c
blob2be6c062bff25073422fbffa5528fbd7b95be101
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 */
9 #ifdef __64BIT__
10 typedef int g2int;
11 #else
12 typedef long g2int;
13 #endif
16 #if defined _UNDERSCORE
17 #define enc_jpeg2000 enc_jpeg2000_
18 #elif defined _DOUBLEUNDERSCORE
19 #define enc_jpeg2000 enc_jpeg2000__
20 #endif
23 int enc_jpeg2000(unsigned char *cin,g2int *pwidth,g2int *pheight,g2int *pnbits,
24 g2int *ltype, g2int *ratio, g2int *retry, char *outjpc,
25 g2int *jpclen)
26 /*$$$ SUBPROGRAM DOCUMENTATION BLOCK
27 * . . . .
28 * SUBPROGRAM: enc_jpeg2000 Encodes JPEG2000 code stream
29 * PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-12-02
31 * ABSTRACT: This Function encodes a grayscale image into a JPEG2000 code stream
32 * specified in the JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1)
33 * using JasPer Software version 1.500.4 (or 1.700.2 ) written by the
34 * University of British Columbia, Image Power Inc, and others.
35 * JasPer is available at http://www.ece.uvic.ca/~mdadams/jasper/.
37 * PROGRAM HISTORY LOG:
38 * 2002-12-02 Gilbert
39 * 2004-07-20 GIlbert - Added retry argument/option to allow option of
40 * increasing the maximum number of guard bits to the
41 * JPEG2000 algorithm.
43 * USAGE: int enc_jpeg2000(unsigned char *cin,g2int *pwidth,g2int *pheight,
44 * g2int *pnbits, g2int *ltype, g2int *ratio,
45 * g2int *retry, char *outjpc, g2int *jpclen)
47 * INPUT ARGUMENTS:
48 * cin - Packed matrix of Grayscale image values to encode.
49 * pwidth - Pointer to width of image
50 * pheight - Pointer to height of image
51 * pnbits - Pointer to depth (in bits) of image. i.e number of bits
52 * used to hold each data value
53 * ltype - Pointer to indicator of lossless or lossy compression
54 * = 1, for lossy compression
55 * != 1, for lossless compression
56 * ratio - Pointer to target compression ratio. (ratio:1)
57 * Used only when *ltype == 1.
58 * retry - Pointer to option type.
59 * 1 = try increasing number of guard bits
60 * otherwise, no additional options
61 * jpclen - Number of bytes allocated for new JPEG2000 code stream in
62 * outjpc.
64 * INPUT ARGUMENTS:
65 * outjpc - Output encoded JPEG2000 code stream
67 * RETURN VALUES :
68 * > 0 = Length in bytes of encoded JPEG2000 code stream
69 * -3 = Error decode jpeg2000 code stream.
70 * -5 = decoded image had multiple color components.
71 * Only grayscale is expected.
73 * REMARKS:
75 * Requires JasPer Software version 1.500.4 or 1.700.2
77 * ATTRIBUTES:
78 * LANGUAGE: C
79 * MACHINE: IBM SP
81 *$$$*/
83 int rwcnt = 0;
84 #ifdef USE_JPEG2000
85 int ier ;
86 jas_image_t image;
87 jas_stream_t *jpcstream,*istream;
88 jas_image_cmpt_t cmpt,*pcmpt;
89 #define MAXOPTSSIZE 1024
90 char opts[MAXOPTSSIZE];
92 g2int width,height,nbits;
93 width=*pwidth;
94 height=*pheight;
95 nbits=*pnbits;
97 printf(" enc_jpeg2000:width %ld\n",width);
98 printf(" enc_jpeg2000:height %ld\n",height);
99 printf(" enc_jpeg2000:nbits %ld\n",nbits);
100 printf(" enc_jpeg2000:jpclen %ld\n",*jpclen);
102 /* jas_init(); */
105 * Set lossy compression options, if requested.
107 if ( *ltype != 1 ) {
108 opts[0]=(char)0;
110 else {
111 snprintf(opts,MAXOPTSSIZE,"mode=real\nrate=%f",1.0/(float)*ratio);
113 if ( *retry == 1 ) { /* option to increase number of guard bits */
114 strcat(opts,"\nnumgbits=4");
116 /* printf("SAGopts: %s\n",opts); */
119 * Initialize the JasPer image structure describing the grayscale
120 * image to encode into the JPEG2000 code stream.
122 image.tlx_=0;
123 image.tly_=0;
124 #ifdef JAS_1_500_4
125 image.brx_=(uint_fast32_t)width;
126 image.bry_=(uint_fast32_t)height;
127 #endif
128 #ifdef JAS_1_700_2
129 image.brx_=(jas_image_coord_t)width;
130 image.bry_=(jas_image_coord_t)height;
131 #endif
132 image.numcmpts_=1;
133 image.maxcmpts_=1;
134 #ifdef JAS_1_500_4
135 image.colormodel_=JAS_IMAGE_CM_GRAY; /* grayscale Image */
136 #endif
137 #ifdef JAS_1_700_2
138 image.clrspc_=JAS_CLRSPC_SGRAY; /* grayscale Image */
139 image.cmprof_=0;
140 #endif
142 * Does not seem to be needed, and throws a compiler error
143 * image.inmem_=1;
146 cmpt.tlx_=0;
147 cmpt.tly_=0;
148 cmpt.hstep_=1;
149 cmpt.vstep_=1;
150 #ifdef JAS_1_500_4
151 cmpt.width_=(uint_fast32_t)width;
152 cmpt.height_=(uint_fast32_t)height;
153 #endif
154 #ifdef JAS_1_700_2
155 cmpt.width_=(jas_image_coord_t)width;
156 cmpt.height_=(jas_image_coord_t)height;
157 cmpt.type_=JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y);
158 #endif
159 cmpt.prec_=nbits;
160 cmpt.sgnd_=0;
161 cmpt.cps_=(nbits+7)/8;
163 pcmpt=&cmpt;
164 image.cmpts_=&pcmpt;
167 * Open a JasPer stream containing the input grayscale values
169 istream=jas_stream_memopen((char *)cin,height*width*cmpt.cps_);
170 cmpt.stream_=istream;
173 * Open an output stream that will contain the encoded jpeg2000
174 * code stream.
176 jpcstream=jas_stream_memopen(outjpc,(int)(*jpclen));
179 * Encode image.
181 ier=jpc_encode(&image,jpcstream,opts);
182 if ( ier != 0 ) {
183 printf(" jpc_encode return = %d \n",ier);
184 return -3;
187 * Clean up JasPer work structures.
189 rwcnt=jpcstream->rwcnt_;
190 ier=jas_stream_close(istream);
191 ier=jas_stream_close(jpcstream);
193 * Return size of jpeg2000 code stream
195 #endif /* USE_JPEG2000 */
196 return (rwcnt);