[trunk] import unit testing from 1.5.x branch
[openjpeg.git] / tests / unit / testempty1.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
30 #include "opj_config.h"
31 #include "openjpeg.h"
32
33 #define J2K_CFMT 0
34
35 void error_callback(const char *msg, void *v);
36 void warning_callback(const char *msg, void *v);
37 void info_callback(const char *msg, void *v);
38
39 void error_callback(const char *msg, void *v) {
40 (void)msg;
41 (void)v;
42 assert(0);
43 }
44 void warning_callback(const char *msg, void *v) {
45 (void)msg;
46 (void)v;
47 puts(msg);
48 }
49 void info_callback(const char *msg, void *v) {
50 (void)msg;
51 (void)v;
52 puts(msg);
53 }
54
55 int main(int argc, char *argv[])
56 {
57   const char * v = opj_version();
58
59   const OPJ_COLOR_SPACE color_space = CLRSPC_GRAY;
60   int numcomps = 1;
61   int i;
62   int image_width = 256;
63   int image_height = 256;
64
65   opj_cparameters_t parameters;
66
67   int subsampling_dx = 0;
68   int subsampling_dy = 0;
69
70   opj_image_cmptparm_t cmptparm;
71   opj_image_t *image;
72   opj_event_mgr_t event_mgr;
73   opj_codec_t* l_codec = 00;
74   opj_cio_t *cio;
75   opj_bool bSuccess;
76   size_t codestream_length;
77   FILE *f;
78         opj_stream_t *l_stream = 00;
79   (void)argc;
80   (void)argv;
81
82   opj_set_default_encoder_parameters(&parameters);
83   parameters.cod_format = J2K_CFMT;
84   puts(v);
85   cmptparm.prec = 8;
86   cmptparm.bpp = 8;
87   cmptparm.sgnd = 0;
88   cmptparm.dx = subsampling_dx;
89   cmptparm.dy = subsampling_dy;
90   cmptparm.w = image_width;
91   cmptparm.h = image_height;
92
93   image = opj_image_create(numcomps, &cmptparm, color_space);
94   assert( image );
95
96   for (i = 0; i < image_width * image_height; i++)
97     {
98     int compno;
99     for(compno = 0; compno < numcomps; compno++)
100       {
101       image->comps[compno].data[i] = 0;
102       }
103     }
104
105   event_mgr.error_handler = error_callback;
106   event_mgr.warning_handler = warning_callback;
107   event_mgr.info_handler = info_callback;
108
109   l_codec = opj_create_compress(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, "testempty1.j2k");
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   puts( "end" );
137   return 0;
138 }