5 #include "jasper/jasper.h"
7 #endif /* USE_JPEG2000 */
16 #if defined _UNDERSCORE
17 #define enc_jpeg2000 enc_jpeg2000_
18 #elif defined _DOUBLEUNDERSCORE
19 #define enc_jpeg2000 enc_jpeg2000__
23 int enc_jpeg2000(unsigned char *cin
,g2int
*pwidth
,g2int
*pheight
,g2int
*pnbits
,
24 g2int
*ltype
, g2int
*ratio
, g2int
*retry
, char *outjpc
,
26 /*$$$ SUBPROGRAM DOCUMENTATION BLOCK
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:
39 * 2004-07-20 GIlbert - Added retry argument/option to allow option of
40 * increasing the maximum number of guard bits to the
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)
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
65 * outjpc - Output encoded JPEG2000 code stream
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.
75 * Requires JasPer Software version 1.500.4 or 1.700.2
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
;
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);
105 * Set lossy compression options, if requested.
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.
125 image
.brx_
=(uint_fast32_t)width
;
126 image
.bry_
=(uint_fast32_t)height
;
129 image
.brx_
=(jas_image_coord_t
)width
;
130 image
.bry_
=(jas_image_coord_t
)height
;
135 image
.colormodel_
=JAS_IMAGE_CM_GRAY
; /* grayscale Image */
138 image
.clrspc_
=JAS_CLRSPC_SGRAY
; /* grayscale Image */
142 * Does not seem to be needed, and throws a compiler error
151 cmpt
.width_
=(uint_fast32_t)width
;
152 cmpt
.height_
=(uint_fast32_t)height
;
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
);
161 cmpt
.cps_
=(nbits
+7)/8;
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
176 jpcstream
=jas_stream_memopen(outjpc
,(int)(*jpclen
));
181 ier
=jpc_encode(&image
,jpcstream
,opts
);
183 printf(" jpc_encode return = %d \n",ier
);
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 */