ENH: Fixing compilation warnings on gcc 4.4.
[openjpeg.git] / test_V2_tile_handling / test_encoder.c
1 /*
2  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 #define USE_OPJ_DEPRECATED
27
28 #include "openjpeg.h"
29 #include "stdlib.h"
30
31 /* set this macro to enable profiling for the given test */
32 /* warning : in order to be effective, openjpeg must have been built with profiling enabled !! */
33 //#define _PROFILE
34
35 #ifdef WIN32
36 #include "windows.h" // needed for rand() function
37 #endif
38
39
40 #define NUM_COMPS                       3
41 #define IMAGE_WIDTH                     2000
42 #define IMAGE_HEIGHT            2000
43 #define TILE_WIDTH                      1000
44 #define TILE_HEIGHT                     1000
45 #define COMP_PREC                       8
46 #define OUTPUT_FILE                     "test.j2k"
47
48 /* -------------------------------------------------------------------------- */
49
50 /**
51 sample error callback expecting a FILE* client object
52 */
53 void error_callback_file(const char *msg, void *client_data) {
54         FILE *stream = (FILE*)client_data;
55         fprintf(stream, "[ERROR] %s", msg);
56 }
57 /**
58 sample warning callback expecting a FILE* client object
59 */
60 void warning_callback_file(const char *msg, void *client_data) {
61         FILE *stream = (FILE*)client_data;
62         fprintf(stream, "[WARNING] %s", msg);
63 }
64 /**
65 sample error debug callback expecting no client object
66 */
67 void error_callback(const char *msg, void *client_data) {
68         (void)client_data;
69         fprintf(stdout, "[ERROR] %s", msg);
70 }
71 /**
72 sample warning debug callback expecting no client object
73 */
74 void warning_callback(const char *msg, void *client_data) {
75         (void)client_data;
76         fprintf(stdout, "[WARNING] %s", msg);
77 }
78 /**
79 sample debug callback expecting no client object
80 */
81 void info_callback(const char *msg, void *client_data) {
82         (void)client_data;
83         fprintf(stdout, "[INFO] %s", msg);
84 }
85
86 /* -------------------------------------------------------------------------- */
87
88 int main ()
89 {
90         opj_cparameters_t l_param;
91         opj_codec_t * l_codec;
92         opj_image_t * l_image;
93         opj_image_cmptparm_t l_params [NUM_COMPS];
94         FILE * l_file;
95         opj_stream_t * l_stream;
96         OPJ_UINT32 l_nb_tiles = (IMAGE_WIDTH/TILE_WIDTH) * (IMAGE_HEIGHT/TILE_HEIGHT);
97         OPJ_UINT32 l_data_size = TILE_WIDTH * TILE_HEIGHT * NUM_COMPS * (COMP_PREC/8);
98
99 #ifdef USING_MCT
100         const OPJ_FLOAT32 l_mct [] = 
101         {
102                 1 , 0 , 0 ,
103                 0 , 1 , 0 ,
104                 0 , 0 , 1
105         };
106
107         const OPJ_INT32 l_offsets [] = 
108         {
109                 128 , 128 , 128
110         };
111 #endif
112
113         opj_image_cmptparm_t * l_current_param_ptr;
114         OPJ_UINT32 i;
115         OPJ_BYTE *l_data;
116
117         PROFINIT();
118         l_data = (OPJ_BYTE*) malloc(TILE_WIDTH * TILE_HEIGHT * NUM_COMPS * (COMP_PREC/8) * sizeof(OPJ_BYTE));
119         
120         fprintf(stdout, "Encoding random values -> keep in mind that this is very hard to compress\n");
121         for
122                 (i=0;i<l_data_size;++i)
123         {
124                 l_data[i] = rand();
125         }
126         
127         opj_set_default_encoder_parameters(&l_param);
128         /** you may here add custom encoding parameters */
129         /* rate specifications */
130         /** number of quality layers in the stream */
131         l_param.tcp_numlayers = 1;
132         l_param.cp_fixed_quality = 1;
133         l_param.tcp_distoratio[0] = 20;
134         /* is using others way of calculation */
135         /* l_param.cp_disto_alloc = 1 or l_param.cp_fixed_alloc = 1 */
136         /* l_param.tcp_rates[0] = ... */
137         
138
139         /* tile definitions parameters */
140         /* position of the tile grid aligned with the image */
141         l_param.cp_tx0 = 0;
142         l_param.cp_ty0 = 0;
143         /* tile size, we are using tile based encoding */
144         l_param.tile_size_on = true;
145         l_param.cp_tdx = TILE_WIDTH;
146         l_param.cp_tdy = TILE_HEIGHT;
147         
148         /* use irreversible encoding ?*/
149         l_param.irreversible = 1;
150
151         /* do not bother with mct, the rsiz is set when calling opj_set_MCT*/
152         /*l_param.cp_rsiz = STD_RSIZ;*/
153         
154         /* no cinema */
155         /*l_param.cp_cinema = 0;*/
156         
157         /* no not bother using SOP or EPH markers, do not use custom size precinct */
158         /* number of precincts to specify */
159         /* l_param.csty = 0;*/
160         /* l_param.res_spec = ... */
161         /* l_param.prch_init[i] = .. */
162         /* l_param.prcw_init[i] = .. */
163
164         
165         /* do not use progression order changes */
166         /*l_param.numpocs = 0;*/
167         /* l_param.POC[i].... */
168
169         /* do not restrain the size for a component.*/
170         /* l_param.max_comp_size = 0; */
171         
172         /** block encoding style for each component, do not use at the moment */
173         /** J2K_CCP_CBLKSTY_TERMALL, J2K_CCP_CBLKSTY_LAZY, J2K_CCP_CBLKSTY_VSC, J2K_CCP_CBLKSTY_SEGSYM, J2K_CCP_CBLKSTY_RESET */
174         /* l_param.mode = 0;*/
175
176         /** number of resolutions */
177         l_param.numresolution = 6;
178
179         /** progression order to use*/
180         /** LRCP, RLCP, RPCL, PCRL, CPRL */
181         l_param.prog_order = LRCP;
182         
183         /** no "region" of interest, more precisally component */
184         /* l_param.roi_compno = -1; */
185         /* l_param.roi_shift = 0; */
186         
187         /* we are not using multiple tile parts for a tile. */
188         /* l_param.tp_on = 0; */
189         /* l_param.tp_flag = 0; */      
190         
191         /* if we are using mct */
192 #ifdef USING_MCT
193         opj_set_MCT(&l_param,l_mct,l_offsets,NUM_COMPS);
194 #endif
195
196         
197         /* image definition */
198         l_current_param_ptr = l_params;
199         for
200                 (i=0;i<NUM_COMPS;++i)
201         {
202                 /* do not bother bpp useless */
203                 /*l_current_param_ptr->bpp = COMP_PREC;*/
204                 l_current_param_ptr->dx = 1;
205                 l_current_param_ptr->dy = 1;
206                 l_current_param_ptr->h = IMAGE_HEIGHT;
207                 l_current_param_ptr->sgnd = 0;
208                 l_current_param_ptr->prec = COMP_PREC;
209                 l_current_param_ptr->w = IMAGE_WIDTH;
210                 l_current_param_ptr->x0 = 0;
211                 l_current_param_ptr->y0 = 0;
212                 ++l_current_param_ptr;
213         }
214
215         l_codec = opj_create_compress(CODEC_J2K);
216         if
217                 (! l_codec)
218         {
219                 return 1;
220         }
221
222         /* catch events using our callbacks and give a local context */         
223         opj_set_info_handler(l_codec, info_callback,00);
224         opj_set_warning_handler(l_codec, warning_callback,00);
225         opj_set_error_handler(l_codec, error_callback,00);
226         
227         l_image = opj_image_tile_create(NUM_COMPS,l_params,CLRSPC_SRGB);
228         if
229                 (! l_image)
230         {
231                 opj_destroy_codec(l_codec);
232                 return 1;
233         }
234         l_image->x0 = 0;
235         l_image->y0 = 0;
236         l_image->x1 = IMAGE_WIDTH;
237         l_image->y1 = IMAGE_HEIGHT;
238         l_image->color_space = CLRSPC_SRGB;
239         
240         if
241                 (! opj_setup_encoder(l_codec,&l_param,l_image))
242         {
243                 opj_destroy_codec(l_codec);
244                 opj_image_destroy(l_image);
245                 return 1;
246         }
247         
248         l_file = fopen(OUTPUT_FILE,"wb");
249         if
250                 (! l_file)
251         {
252                 opj_destroy_codec(l_codec);
253                 opj_image_destroy(l_image);
254                 return 1;
255         }
256
257         l_stream = opj_stream_create_default_file_stream(l_file,false);
258
259         if
260                 (! opj_start_compress(l_codec,l_image,l_stream))
261         {
262                 opj_stream_destroy(l_stream);
263                 fclose(l_file);
264                 opj_destroy_codec(l_codec);
265                 opj_image_destroy(l_image);
266                 return 1;
267         }
268         for
269                 (i=0;i<l_nb_tiles;++i)
270         {
271                 if
272                         (! opj_write_tile(l_codec,i,l_data,l_data_size,l_stream))
273                 {
274                         opj_stream_destroy(l_stream);
275                         fclose(l_file);
276                         opj_destroy_codec(l_codec);
277                         opj_image_destroy(l_image);
278                         return 1;
279                 }
280         }
281         if
282                 (! opj_end_compress(l_codec,l_stream))
283         {
284                 opj_stream_destroy(l_stream);
285                 fclose(l_file);
286                 opj_destroy_codec(l_codec);
287                 opj_image_destroy(l_image);
288                 return 1;
289         }
290         opj_stream_destroy(l_stream);
291         fclose(l_file);
292         opj_destroy_codec(l_codec);
293         opj_image_destroy(l_image);
294
295         // Print profiling
296         PROFPRINT();
297
298         return 0;
299 }
300
301
302
303
304
305