source: trunk/WRF.COMMON/WRFV2/external/io_grib2/g2lib/enc_jpeg2000.c

Last change on this file was 11, checked in by aslmd, 14 years ago

spiga@svn-planeto:ajoute le modele meso-echelle martien

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