4 #include "jasper/jasper.h"
16 # define ENC_JPEG2000 enc_jpeg2000
19 # define ENC_JPEG2000 enc_jpeg2000__
21 # define ENC_JPEG2000 enc_jpeg2000_
26 int ENC_JPEG2000(unsigned char *cin
,g2int
*pwidth
,g2int
*pheight
,g2int
*pnbits
,
27 g2int
*ltype
, g2int
*ratio
, g2int
*retry
, char *outjpc
,
29 /*$$$ SUBPROGRAM DOCUMENTATION BLOCK
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:
42 * 2004-07-20 GIlbert - Added retry argument/option to allow option of
43 * increasing the maximum number of guard bits to the
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)
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
68 * outjpc - Output encoded JPEG2000 code stream
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.
78 * Requires JasPer Software version 1.500.4 or 1.700.2
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
;
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);
106 Set lossy compression options, if requested.
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.
126 image
.brx_
=(uint_fast32_t)width
;
127 image
.bry_
=(uint_fast32_t)height
;
130 image
.brx_
=(jas_image_coord_t
)width
;
131 image
.bry_
=(jas_image_coord_t
)height
;
136 image
.colormodel_
=JAS_IMAGE_CM_GRAY
; /* grayscale Image */
139 image
.clrspc_
=JAS_CLRSPC_SGRAY
; /* grayscale Image */
149 cmpt
.width_
=(uint_fast32_t)width
;
150 cmpt
.height_
=(uint_fast32_t)height
;
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
);
159 cmpt
.cps_
=(nbits
+7)/8;
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
174 jpcstream
=jas_stream_memopen(outjpc
,(int)(*jpclen
));
179 ier
=jpc_encode(&image
,jpcstream
,opts
);
181 printf(" jpc_encode return = %d \n",ier
);
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