opj_jp2_read_header(): move setting icc_profile here instead in opj_jp2_decode()...
[openjpeg.git] / tests / unit / testjp2.c
1 /*
2  * Copyright (c) 2023, Even Rouault
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 <stdlib.h>
28
29 #include "openjpeg.h"
30
31 static void test_colorspace(const char* pszDirectory)
32 {
33     char szFile[2048];
34     opj_image_t* image = NULL;
35     opj_stream_t *l_stream = NULL;              /* Stream */
36     opj_codec_t* l_codec = NULL;                /* Handle to a decompressor */
37     opj_dparameters_t parameters;               /* decompression parameters */
38
39     snprintf(szFile, sizeof(szFile), "%s/input/conformance/file1.jp2",
40              pszDirectory);
41     l_stream = opj_stream_create_default_file_stream(szFile, 1);
42     if (!l_stream) {
43         fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n",
44                 szFile);
45         exit(1);
46     }
47     l_codec = opj_create_decompress(OPJ_CODEC_JP2);
48
49     /* Setup the decoder */
50     opj_set_default_decoder_parameters(&parameters);
51     if (!opj_setup_decoder(l_codec, &parameters)) {
52         fprintf(stderr, "ERROR -> opj_decompress: failed to setup the decoder\n");
53         opj_stream_destroy(l_stream);
54         opj_destroy_codec(l_codec);
55         exit(1);
56     }
57
58     /* Read the main header of the codestream and if necessary the JP2 boxes*/
59     if (! opj_read_header(l_stream, l_codec, &image)) {
60         fprintf(stderr, "ERROR -> opj_decompress: failed to read the header\n");
61         opj_stream_destroy(l_stream);
62         opj_destroy_codec(l_codec);
63         opj_image_destroy(image);
64         exit(1);
65     }
66
67     /* Check that color_space is set after opj_read_header() */
68     if (image->color_space != OPJ_CLRSPC_SRGB) {
69         fprintf(stderr, "ERROR -> image->color_space (=%d) != OPJ_CLRSPC_SRGB\n",
70                 image->color_space);
71         opj_stream_destroy(l_stream);
72         opj_destroy_codec(l_codec);
73         opj_image_destroy(image);
74         exit(1);
75     }
76
77     opj_destroy_codec(l_codec);
78     opj_stream_destroy(l_stream);
79     opj_image_destroy(image);
80 }
81
82 static void test_iccprofile(const char* pszDirectory)
83 {
84     char szFile[2048];
85     opj_image_t* image = NULL;
86     opj_stream_t *l_stream = NULL;              /* Stream */
87     opj_codec_t* l_codec = NULL;                /* Handle to a decompressor */
88     opj_dparameters_t parameters;               /* decompression parameters */
89
90     snprintf(szFile, sizeof(szFile), "%s/input/nonregression/relax.jp2",
91              pszDirectory);
92     l_stream = opj_stream_create_default_file_stream(szFile, 1);
93     if (!l_stream) {
94         fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n",
95                 szFile);
96         exit(1);
97     }
98     l_codec = opj_create_decompress(OPJ_CODEC_JP2);
99
100     /* Setup the decoder */
101     opj_set_default_decoder_parameters(&parameters);
102     if (!opj_setup_decoder(l_codec, &parameters)) {
103         fprintf(stderr, "ERROR -> opj_decompress: failed to setup the decoder\n");
104         opj_stream_destroy(l_stream);
105         opj_destroy_codec(l_codec);
106         exit(1);
107     }
108
109     /* Read the main header of the codestream and if necessary the JP2 boxes*/
110     if (! opj_read_header(l_stream, l_codec, &image)) {
111         fprintf(stderr, "ERROR -> opj_decompress: failed to read the header\n");
112         opj_stream_destroy(l_stream);
113         opj_destroy_codec(l_codec);
114         opj_image_destroy(image);
115         exit(1);
116     }
117
118     /* Check that icc_profile_len is set after opj_read_header() */
119     if (image->icc_profile_len != 278) {
120         fprintf(stderr, "ERROR -> image->icc_profile_len (=%d) != 278\n",
121                 image->icc_profile_len);
122         opj_stream_destroy(l_stream);
123         opj_destroy_codec(l_codec);
124         opj_image_destroy(image);
125         exit(1);
126     }
127
128     opj_destroy_codec(l_codec);
129     opj_stream_destroy(l_stream);
130     opj_image_destroy(image);
131 }
132
133 int main(int argc, char* argv[])
134 {
135     if (argc != 2) {
136         fprintf(stderr, "usage: testjp2 /path/to/opj_data_root\n");
137         exit(1);
138     }
139
140     test_colorspace(argv[1]);
141     test_iccprofile(argv[1]);
142
143     return 0;
144 }