updated top-level README and version_decl for V4.4.2 (#1795)
[WRF.git] / external / io_grib2 / g2lib / enc_jpeg2000.c
blob93f8baee75199f0f8bd13c61a9838d5e9af54793
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "jasper/jasper.h"
5 #define JAS_1_700_2
6 #include "proto.h"
8 #ifdef __64BIT__
9 typedef int g2int;
10 #else
11 typedef long g2int;
12 #endif
14 #ifndef CRAY
15 # ifdef NOUNDERSCORE
16 # define ENC_JPEG2000 enc_jpeg2000
17 # else
18 # ifdef F2CSTYLE
19 # define ENC_JPEG2000 enc_jpeg2000__
20 # else
21 # define ENC_JPEG2000 enc_jpeg2000_
22 # endif
23 # endif
24 #endif
26 int ENC_JPEG2000(unsigned char *cin,g2int *pwidth,g2int *pheight,g2int *pnbits,
27 g2int *ltype, g2int *ratio, g2int *retry, char *outjpc,
28 g2int *jpclen)
29 /*$$$ SUBPROGRAM DOCUMENTATION BLOCK
30 * . . . .
31 * SUBPROGRAM: enc_jpeg2000 Encodes JPEG2000 code stream
32 * PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-12-02
34 * ABSTRACT: This Function encodes a grayscale image into a JPEG2000 code stream
35 * specified in the JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1)
36 * using JasPer Software version 1.500.4 (or 1.700.2 ) written by the
37 * University of British Columbia, Image Power Inc, and others.
38 * JasPer is available at http://www.ece.uvic.ca/~mdadams/jasper/.
40 * PROGRAM HISTORY LOG:
41 * 2002-12-02 Gilbert
42 * 2004-07-20 GIlbert - Added retry argument/option to allow option of
43 * increasing the maximum number of guard bits to the
44 * JPEG2000 algorithm.
46 * USAGE: int enc_jpeg2000(unsigned char *cin,g2int *pwidth,g2int *pheight,
47 * g2int *pnbits, g2int *ltype, g2int *ratio,
48 * g2int *retry, char *outjpc, g2int *jpclen)
50 * INPUT ARGUMENTS:
51 * cin - Packed matrix of Grayscale image values to encode.
52 * pwidth - Pointer to width of image
53 * pheight - Pointer to height of image
54 * pnbits - Pointer to depth (in bits) of image. i.e number of bits
55 * used to hold each data value
56 * ltype - Pointer to indicator of lossless or lossy compression
57 * = 1, for lossy compression
58 * != 1, for lossless compression
59 * ratio - Pointer to target compression ratio. (ratio:1)
60 * Used only when *ltype == 1.
61 * retry - Pointer to option type.
62 * 1 = try increasing number of guard bits
63 * otherwise, no additional options
64 * jpclen - Number of bytes allocated for new JPEG2000 code stream in
65 * outjpc.
67 * INPUT ARGUMENTS:
68 * outjpc - Output encoded JPEG2000 code stream
70 * RETURN VALUES :
71 * > 0 = Length in bytes of encoded JPEG2000 code stream
72 * -3 = Error decode jpeg2000 code stream.
73 * -5 = decoded image had multiple color components.
74 * Only grayscale is expected.
76 * REMARKS:
78 * Requires JasPer Software version 1.500.4 or 1.700.2
80 * ATTRIBUTES:
81 * LANGUAGE: C
82 * MACHINE: IBM SP
84 *$$$*/
86 int ier,rwcnt;
87 jas_image_t image;
88 jas_stream_t *jpcstream,*istream;
89 jas_image_cmpt_t cmpt,*pcmpt;
90 #define MAXOPTSSIZE 1024
91 char opts[MAXOPTSSIZE];
93 g2int width,height,nbits;
94 width=*pwidth;
95 height=*pheight;
96 nbits=*pnbits;
98 printf(" enc_jpeg2000:width %ld\n",width);
99 printf(" enc_jpeg2000:height %ld\n",height);
100 printf(" enc_jpeg2000:nbits %ld\n",nbits);
101 printf(" enc_jpeg2000:jpclen %ld\n",*jpclen);
103 /* jas_init(); */
106 Set lossy compression options, if requested.
108 if ( *ltype != 1 ) {
109 opts[0]=(char)0;
111 else {
112 snprintf(opts,MAXOPTSSIZE,"mode=real\nrate=%f",1.0/(float)*ratio);
114 if ( *retry == 1 ) { /* option to increase number of guard bits */
115 strcat(opts,"\nnumgbits=4");
117 /*printf("SAGopts: %s\n",opts); */
120 Initialize the JasPer image structure describing the grayscale
121 image to encode into the JPEG2000 code stream.
123 image.tlx_=0;
124 image.tly_=0;
125 #ifdef JAS_1_500_4
126 image.brx_=(uint_fast32_t)width;
127 image.bry_=(uint_fast32_t)height;
128 #endif
129 #ifdef JAS_1_700_2
130 image.brx_=(jas_image_coord_t)width;
131 image.bry_=(jas_image_coord_t)height;
132 #endif
133 image.numcmpts_=1;
134 image.maxcmpts_=1;
135 #ifdef JAS_1_500_4
136 image.colormodel_=JAS_IMAGE_CM_GRAY; /* grayscale Image */
137 #endif
138 #ifdef JAS_1_700_2
139 image.clrspc_=JAS_CLRSPC_SGRAY; /* grayscale Image */
140 image.cmprof_=0;
141 #endif
142 image.inmem_=1;
144 cmpt.tlx_=0;
145 cmpt.tly_=0;
146 cmpt.hstep_=1;
147 cmpt.vstep_=1;
148 #ifdef JAS_1_500_4
149 cmpt.width_=(uint_fast32_t)width;
150 cmpt.height_=(uint_fast32_t)height;
151 #endif
152 #ifdef JAS_1_700_2
153 cmpt.width_=(jas_image_coord_t)width;
154 cmpt.height_=(jas_image_coord_t)height;
155 cmpt.type_=JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y);
156 #endif
157 cmpt.prec_=nbits;
158 cmpt.sgnd_=0;
159 cmpt.cps_=(nbits+7)/8;
161 pcmpt=&cmpt;
162 image.cmpts_=&pcmpt;
165 Open a JasPer stream containing the input grayscale values
167 istream=jas_stream_memopen((char *)cin,height*width*cmpt.cps_);
168 cmpt.stream_=istream;
171 Open an output stream that will contain the encoded jpeg2000
172 code stream.
174 jpcstream=jas_stream_memopen(outjpc,(int)(*jpclen));
177 Encode image.
179 ier=jpc_encode(&image,jpcstream,opts);
180 if ( ier != 0 ) {
181 printf(" jpc_encode return = %d \n",ier);
182 return -3;
185 Clean up JasPer work structures.
187 rwcnt=jpcstream->rwcnt_;
188 ier=jas_stream_close(istream);
189 ier=jas_stream_close(jpcstream);
191 Return size of jpeg2000 code stream
193 return (rwcnt);