[trunk] move to the new API for function opj_stream_create_default_file_stream. Use...
[openjpeg.git] / tests / test_tile_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
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <math.h>
31
32 #include "opj_config.h"
33 #include "openjpeg.h"
34 #include "stdlib.h"
35
36 /* -------------------------------------------------------------------------- */
37
38 /**
39 sample error callback expecting a FILE* client object
40 */
41 static void error_callback_file(const char *msg, void *client_data) {
42         FILE *stream = (FILE*)client_data;
43         fprintf(stream, "[ERROR] %s", msg);
44 }
45 /**
46 sample warning callback expecting a FILE* client object
47 */
48 static void warning_callback_file(const char *msg, void *client_data) {
49         FILE *stream = (FILE*)client_data;
50         fprintf(stream, "[WARNING] %s", msg);
51 }
52 /**
53 sample error debug callback expecting no client object
54 */
55 static void error_callback(const char *msg, void *client_data) {
56         (void)client_data;
57         fprintf(stdout, "[ERROR] %s", msg);
58 }
59 /**
60 sample warning debug callback expecting no client object
61 */
62 static void warning_callback(const char *msg, void *client_data) {
63         (void)client_data;
64         fprintf(stdout, "[WARNING] %s", msg);
65 }
66 /**
67 sample debug callback expecting no client object
68 */
69 static void info_callback(const char *msg, void *client_data) {
70         (void)client_data;
71         fprintf(stdout, "[INFO] %s", msg);
72 }
73
74 /* -------------------------------------------------------------------------- */
75
76 #define NUM_COMPS_MAX 4
77 int main (int argc, char *argv[])
78 {
79         opj_cparameters_t l_param;
80         opj_codec_t * l_codec;
81         opj_image_t * l_image;
82         opj_image_cmptparm_t l_params [NUM_COMPS_MAX];
83         opj_stream_t * l_stream;
84         OPJ_UINT32 l_nb_tiles;
85         OPJ_UINT32 l_data_size;
86         unsigned char len;
87
88 #ifdef USING_MCT
89         const OPJ_FLOAT32 l_mct [] =
90         {
91                 1 , 0 , 0 ,
92                 0 , 1 , 0 ,
93                 0 , 0 , 1
94         };
95
96         const OPJ_INT32 l_offsets [] =
97         {
98                 128 , 128 , 128
99         };
100 #endif
101
102         opj_image_cmptparm_t * l_current_param_ptr;
103         OPJ_UINT32 i;
104         OPJ_BYTE *l_data;
105
106   OPJ_UINT32 num_comps;
107   int image_width;
108   int image_height;
109   int tile_width;
110   int tile_height;
111   int comp_prec;
112   int irreversible;
113   char output_file[64];
114
115   /* should be test_tile_encoder 3 2000 2000 1000 1000 8 tte1.j2k */
116   if( argc == 9 )
117     {
118     num_comps = atoi( argv[1] );
119     image_width = atoi( argv[2] );
120     image_height = atoi( argv[3] );
121     tile_width = atoi( argv[4] );
122     tile_height = atoi( argv[5] );
123     comp_prec = atoi( argv[6] );
124     irreversible = atoi( argv[7] );
125     strcpy(output_file, argv[8] );
126     }
127   else
128     {
129     num_comps = 3;
130     image_width = 2000;
131     image_height = 2000;
132     tile_width = 1000;
133     tile_height = 1000;
134     comp_prec = 8;
135     irreversible = 1;
136     strcpy(output_file, "test.j2k" );
137     }
138   if( num_comps > NUM_COMPS_MAX )
139     {
140     return 1;
141     }
142         l_nb_tiles = (image_width/tile_width) * (image_height/tile_height);
143         l_data_size = tile_width * tile_height * num_comps * (comp_prec/8);
144
145         l_data = (OPJ_BYTE*) malloc(tile_width * tile_height * num_comps * (comp_prec/8) * sizeof(OPJ_BYTE));
146
147         fprintf(stdout, "Encoding random values -> keep in mind that this is very hard to compress\n");
148         for (i=0;i<l_data_size;++i)     {
149                 l_data[i] = i; /*rand();*/
150         }
151
152         opj_set_default_encoder_parameters(&l_param);
153         /** you may here add custom encoding parameters */
154         /* rate specifications */
155         /** number of quality layers in the stream */
156         l_param.tcp_numlayers = 1;
157         l_param.cp_fixed_quality = 1;
158         l_param.tcp_distoratio[0] = 20;
159         /* is using others way of calculation */
160         /* l_param.cp_disto_alloc = 1 or l_param.cp_fixed_alloc = 1 */
161         /* l_param.tcp_rates[0] = ... */
162
163
164         /* tile definitions parameters */
165         /* position of the tile grid aligned with the image */
166         l_param.cp_tx0 = 0;
167         l_param.cp_ty0 = 0;
168         /* tile size, we are using tile based encoding */
169         l_param.tile_size_on = OPJ_TRUE;
170         l_param.cp_tdx = tile_width;
171         l_param.cp_tdy = tile_height;
172
173         /* use irreversible encoding ?*/
174         l_param.irreversible = irreversible;
175
176         /* do not bother with mct, the rsiz is set when calling opj_set_MCT*/
177         /*l_param.cp_rsiz = OPJ_STD_RSIZ;*/
178
179         /* no cinema */
180         /*l_param.cp_cinema = 0;*/
181
182         /* no not bother using SOP or EPH markers, do not use custom size precinct */
183         /* number of precincts to specify */
184         /* l_param.csty = 0;*/
185         /* l_param.res_spec = ... */
186         /* l_param.prch_init[i] = .. */
187         /* l_param.prcw_init[i] = .. */
188
189
190         /* do not use progression order changes */
191         /*l_param.numpocs = 0;*/
192         /* l_param.POC[i].... */
193
194         /* do not restrain the size for a component.*/
195         /* l_param.max_comp_size = 0; */
196
197         /** block encoding style for each component, do not use at the moment */
198         /** J2K_CCP_CBLKSTY_TERMALL, J2K_CCP_CBLKSTY_LAZY, J2K_CCP_CBLKSTY_VSC, J2K_CCP_CBLKSTY_SEGSYM, J2K_CCP_CBLKSTY_RESET */
199         /* l_param.mode = 0;*/
200
201         /** number of resolutions */
202         l_param.numresolution = 6;
203
204         /** progression order to use*/
205         /** OPJ_LRCP, OPJ_RLCP, OPJ_RPCL, PCRL, CPRL */
206         l_param.prog_order = OPJ_LRCP;
207
208         /** no "region" of interest, more precisally component */
209         /* l_param.roi_compno = -1; */
210         /* l_param.roi_shift = 0; */
211
212         /* we are not using multiple tile parts for a tile. */
213         /* l_param.tp_on = 0; */
214         /* l_param.tp_flag = 0; */
215
216         /* if we are using mct */
217 #ifdef USING_MCT
218         opj_set_MCT(&l_param,l_mct,l_offsets,NUM_COMPS);
219 #endif
220
221
222         /* image definition */
223         l_current_param_ptr = l_params;
224         for (i=0;i<num_comps;++i) {
225                 /* do not bother bpp useless */
226                 /*l_current_param_ptr->bpp = COMP_PREC;*/
227                 l_current_param_ptr->dx = 1;
228                 l_current_param_ptr->dy = 1;
229
230                 l_current_param_ptr->h = image_height;
231                 l_current_param_ptr->w = image_width;
232
233                 l_current_param_ptr->sgnd = 0;
234                 l_current_param_ptr->prec = comp_prec;
235
236                 l_current_param_ptr->x0 = 0;
237                 l_current_param_ptr->y0 = 0;
238
239                 ++l_current_param_ptr;
240         }
241
242   /* should we do j2k or jp2 ?*/
243   len = strlen( output_file );
244   if( strcmp( output_file + len - 4, ".jp2" ) == 0 )
245     {
246     l_codec = opj_create_compress(OPJ_CODEC_JP2);
247     }
248   else
249     {
250     l_codec = opj_create_compress(OPJ_CODEC_J2K);
251     }
252         if (!l_codec) {
253                 return 1;
254         }
255
256         /* catch events using our callbacks and give a local context */
257         opj_set_info_handler(l_codec, info_callback,00);
258         opj_set_warning_handler(l_codec, warning_callback,00);
259         opj_set_error_handler(l_codec, error_callback,00);
260
261         l_image = opj_image_tile_create(num_comps,l_params,OPJ_CLRSPC_SRGB);
262         if (! l_image) {
263                 opj_destroy_codec(l_codec);
264                 return 1;
265         }
266
267         l_image->x0 = 0;
268         l_image->y0 = 0;
269         l_image->x1 = image_width;
270         l_image->y1 = image_height;
271         l_image->color_space = OPJ_CLRSPC_SRGB;
272
273         if (! opj_setup_encoder(l_codec,&l_param,l_image)) {
274                 fprintf(stderr, "ERROR -> test_tile_encoder: failed to setup the codec!\n");
275                 opj_destroy_codec(l_codec);
276                 opj_image_destroy(l_image);
277                 return 1;
278         }
279
280         l_stream = opj_stream_create_default_file_stream_v3(output_file, OPJ_FALSE);
281     if (! l_stream) {
282                 fprintf(stderr, "ERROR -> test_tile_encoder: failed to create the stream from the output file %s !\n",output_file );
283                 opj_destroy_codec(l_codec);
284                 opj_image_destroy(l_image);
285                 return 1;
286         }
287
288         if (! opj_start_compress(l_codec,l_image,l_stream)) {
289                 fprintf(stderr, "ERROR -> test_tile_encoder: failed to start compress!\n");
290                 opj_stream_destroy_v3(l_stream);
291                 opj_destroy_codec(l_codec);
292                 opj_image_destroy(l_image);
293                 return 1;
294         }
295
296         for (i=0;i<l_nb_tiles;++i) {
297                 if (! opj_write_tile(l_codec,i,l_data,l_data_size,l_stream)) {
298                         fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",i);
299                         opj_stream_destroy_v3(l_stream);
300                         opj_destroy_codec(l_codec);
301                         opj_image_destroy(l_image);
302                         return 1;
303                 }
304         }
305
306         if (! opj_end_compress(l_codec,l_stream)) {
307                 fprintf(stderr, "ERROR -> test_tile_encoder: failed to end compress!\n");
308                 opj_stream_destroy_v3(l_stream);
309                 opj_destroy_codec(l_codec);
310                 opj_image_destroy(l_image);
311                 return 1;
312         }
313
314         opj_stream_destroy_v3(l_stream);
315         opj_destroy_codec(l_codec);
316         opj_image_destroy(l_image);
317
318         free(l_data);
319
320         /* Print profiling*/
321         /*PROFPRINT();*/
322
323         return 0;
324 }
325
326
327
328
329
330