[trunk] Fix compilation issue where size_t would be undefined
[openjpeg.git] / tests / unit / testempty2.c
1 /*
2  * Copyright (c) 2012, Mathieu Malaterre
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 #include <assert.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "opj_config.h"
32 #include "openjpeg.h"
33
34 #define J2K_CFMT 0
35
36 void error_callback(const char *msg, void *v);
37 void warning_callback(const char *msg, void *v);
38 void info_callback(const char *msg, void *v);
39
40 void error_callback(const char *msg, void *v) {
41 (void)msg;
42 (void)v;
43 assert(0);
44 }
45 void warning_callback(const char *msg, void *v) {
46 (void)msg;
47 (void)v;
48 puts(msg);
49 }
50 void info_callback(const char *msg, void *v) {
51 (void)msg;
52 (void)v;
53 puts(msg);
54 }
55
56 int main(int argc, char *argv[])
57 {
58   const char * v = opj_version();
59
60   const OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_GRAY;
61   int numcomps = 1;
62   int i;
63   int image_width = 256;
64   int image_height = 256;
65
66   opj_cparameters_t parameters;
67
68   int subsampling_dx = parameters.subsampling_dx;
69   int subsampling_dy = parameters.subsampling_dy;
70   const char outputfile[] = "testempty2.j2k";
71
72   opj_image_cmptparm_t cmptparm;
73   opj_image_t *image;
74   opj_codec_t* l_codec = 00;
75   OPJ_BOOL bSuccess;
76   FILE *f;
77         opj_stream_t *l_stream = 00;
78   (void)argc;
79   (void)argv;
80
81   opj_set_default_encoder_parameters(&parameters);
82   parameters.cod_format = J2K_CFMT;
83   puts(v);
84   cmptparm.prec = 8;
85   cmptparm.bpp = 8;
86   cmptparm.sgnd = 0;
87   cmptparm.dx = subsampling_dx;
88   cmptparm.dy = subsampling_dy;
89   cmptparm.w = image_width;
90   cmptparm.h = image_height;
91
92   image = opj_image_create(numcomps, &cmptparm, color_space);
93   assert( image );
94
95   for (i = 0; i < image_width * image_height; i++)
96     {
97     int compno;
98     for(compno = 0; compno < numcomps; compno++)
99       {
100       image->comps[compno].data[i] = 0;
101       }
102     }
103
104     /* catch events using our callbacks and give a local context */             \r
105     opj_set_info_handler(l_codec, info_callback,00);\r
106     opj_set_warning_handler(l_codec, warning_callback,00);\r
107     opj_set_error_handler(l_codec, error_callback,00);
108
109   l_codec = opj_create_compress(OPJ_CODEC_J2K);
110   opj_set_info_handler(l_codec, info_callback,00);
111   opj_set_warning_handler(l_codec, warning_callback,00);
112   opj_set_error_handler(l_codec, error_callback,00);
113
114   opj_setup_encoder(l_codec, &parameters, image);
115
116   strcpy(parameters.outfile, outputfile);
117   f = fopen(parameters.outfile, "wb");
118   assert( f );
119
120   l_stream = opj_stream_create_default_file_stream(f,OPJ_FALSE);
121   assert(l_stream);
122   bSuccess = opj_start_compress(l_codec,image,l_stream);
123
124   assert( bSuccess );
125   bSuccess = opj_encode(l_codec, l_stream);
126   assert( bSuccess );
127   bSuccess = opj_end_compress(l_codec, l_stream);
128   assert( bSuccess );
129
130   opj_stream_destroy(l_stream);
131   fclose(f);
132
133   opj_destroy_codec(l_codec);
134   opj_image_destroy(image);
135
136
137   /* read back the generated file */
138 {
139   FILE *fsrc = fopen(outputfile, "rb");
140   opj_codec_t* d_codec = 00;
141         opj_dparameters_t dparameters;
142   assert( fsrc );
143
144   d_codec = opj_create_decompress(OPJ_CODEC_J2K);
145   opj_set_info_handler(d_codec, info_callback,00);
146   opj_set_warning_handler(d_codec, warning_callback,00);
147   opj_set_error_handler(d_codec, error_callback,00);
148
149   bSuccess = opj_setup_decoder(d_codec, &dparameters);
150   assert( bSuccess );
151
152   l_stream = opj_stream_create_default_file_stream(fsrc,1);
153   assert( l_stream );
154
155   bSuccess = opj_read_header(l_stream, d_codec, &image);
156   assert( bSuccess );
157
158   bSuccess = opj_decode(l_codec, l_stream, image);
159   assert( bSuccess );
160
161   bSuccess = opj_end_decompress(l_codec,        l_stream);
162   assert( bSuccess );
163
164   opj_stream_destroy(l_stream);
165   fclose(fsrc);
166   opj_destroy_codec(d_codec);
167
168   opj_image_destroy(image);
169 }
170
171   puts( "end" );
172   return 0;
173 }