| 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 |
{ |
| 42 |
(void)msg; |
| 43 |
(void)v; |
| 44 |
puts(msg); |
| 45 |
} |
| 46 |
void warning_callback(const char *msg, void *v) |
| 47 |
{ |
| 48 |
(void)msg; |
| 49 |
(void)v; |
| 50 |
puts(msg); |
| 51 |
} |
| 52 |
void info_callback(const char *msg, void *v) |
| 53 |
{ |
| 54 |
(void)msg; |
| 55 |
(void)v; |
| 56 |
puts(msg); |
| 57 |
} |
| 58 |
|
| 59 |
int main(int argc, char *argv[]) |
| 60 |
{ |
| 61 |
const char * v = opj_version(); |
| 62 |
|
| 63 |
const OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_GRAY; |
| 64 |
unsigned int numcomps = 1; |
| 65 |
unsigned int i; |
| 66 |
unsigned int image_width = 256; |
| 67 |
unsigned int image_height = 256; |
| 68 |
|
| 69 |
opj_cparameters_t parameters; |
| 70 |
|
| 71 |
unsigned int subsampling_dx; |
| 72 |
unsigned int subsampling_dy; |
| 73 |
const char outputfile[] = "testempty2.j2k"; |
| 74 |
|
| 75 |
opj_image_cmptparm_t cmptparm; |
| 76 |
opj_image_t *image; |
| 77 |
opj_codec_t* l_codec = 00; |
| 78 |
OPJ_BOOL bSuccess; |
| 79 |
opj_stream_t *l_stream = 00; |
| 80 |
(void)argc; |
| 81 |
(void)argv; |
| 82 |
|
| 83 |
opj_set_default_encoder_parameters(¶meters); |
| 84 |
parameters.cod_format = J2K_CFMT; |
| 85 |
puts(v); |
| 86 |
subsampling_dx = (unsigned int)parameters.subsampling_dx; |
| 87 |
subsampling_dy = (unsigned int)parameters.subsampling_dy; |
| 88 |
cmptparm.prec = 8; |
| 89 |
cmptparm.sgnd = 0; |
| 90 |
cmptparm.dx = subsampling_dx; |
| 91 |
cmptparm.dy = subsampling_dy; |
| 92 |
cmptparm.w = image_width; |
| 93 |
cmptparm.h = image_height; |
| 94 |
strncpy(parameters.outfile, outputfile, sizeof(parameters.outfile) - 1); |
| 95 |
|
| 96 |
image = opj_image_create(numcomps, &cmptparm, color_space); |
| 97 |
assert(image); |
| 98 |
|
| 99 |
for (i = 0; i < image_width * image_height; i++) { |
| 100 |
unsigned int compno; |
| 101 |
for (compno = 0; compno < numcomps; compno++) { |
| 102 |
image->comps[compno].data[i] = 0; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/* catch events using our callbacks and give a local context */ |
| 107 |
opj_set_info_handler(l_codec, info_callback, 00); |
| 108 |
opj_set_warning_handler(l_codec, warning_callback, 00); |
| 109 |
opj_set_error_handler(l_codec, error_callback, 00); |
| 110 |
|
| 111 |
l_codec = opj_create_compress(OPJ_CODEC_J2K); |
| 112 |
opj_set_info_handler(l_codec, info_callback, 00); |
| 113 |
opj_set_warning_handler(l_codec, warning_callback, 00); |
| 114 |
opj_set_error_handler(l_codec, error_callback, 00); |
| 115 |
|
| 116 |
opj_setup_encoder(l_codec, ¶meters, image); |
| 117 |
|
| 118 |
l_stream = opj_stream_create_default_file_stream(parameters.outfile, OPJ_FALSE); |
| 119 |
if (!l_stream) { |
| 120 |
fprintf(stderr, "Something went wrong during creation of stream\n"); |
| 121 |
opj_destroy_codec(l_codec); |
| 122 |
opj_image_destroy(image); |
| 123 |
opj_stream_destroy(l_stream); |
| 124 |
return 1; |
| 125 |
} |
| 126 |
assert(l_stream); |
| 127 |
bSuccess = opj_start_compress(l_codec, image, l_stream); |
| 128 |
if (!bSuccess) { |
| 129 |
opj_stream_destroy(l_stream); |
| 130 |
opj_destroy_codec(l_codec); |
| 131 |
opj_image_destroy(image); |
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
assert(bSuccess); |
| 136 |
bSuccess = opj_encode(l_codec, l_stream); |
| 137 |
assert(bSuccess); |
| 138 |
bSuccess = opj_end_compress(l_codec, l_stream); |
| 139 |
assert(bSuccess); |
| 140 |
|
| 141 |
opj_stream_destroy(l_stream); |
| 142 |
|
| 143 |
opj_destroy_codec(l_codec); |
| 144 |
opj_image_destroy(image); |
| 145 |
|
| 146 |
|
| 147 |
/* read back the generated file */ |
| 148 |
{ |
| 149 |
opj_codec_t* d_codec = 00; |
| 150 |
opj_dparameters_t dparameters; |
| 151 |
|
| 152 |
d_codec = opj_create_decompress(OPJ_CODEC_J2K); |
| 153 |
opj_set_info_handler(d_codec, info_callback, 00); |
| 154 |
opj_set_warning_handler(d_codec, warning_callback, 00); |
| 155 |
opj_set_error_handler(d_codec, error_callback, 00); |
| 156 |
|
| 157 |
bSuccess = opj_setup_decoder(d_codec, &dparameters); |
| 158 |
assert(bSuccess); |
| 159 |
|
| 160 |
l_stream = opj_stream_create_default_file_stream(outputfile, 1); |
| 161 |
assert(l_stream); |
| 162 |
|
| 163 |
bSuccess = opj_read_header(l_stream, d_codec, &image); |
| 164 |
assert(bSuccess); |
| 165 |
|
| 166 |
bSuccess = opj_decode(l_codec, l_stream, image); |
| 167 |
assert(bSuccess); |
| 168 |
|
| 169 |
bSuccess = opj_end_decompress(l_codec, l_stream); |
| 170 |
assert(bSuccess); |
| 171 |
|
| 172 |
opj_stream_destroy(l_stream); |
| 173 |
|
| 174 |
opj_destroy_codec(d_codec); |
| 175 |
|
| 176 |
opj_image_destroy(image); |
| 177 |
} |
| 178 |
|
| 179 |
puts("end"); |
| 180 |
return 0; |
| 181 |
} |