2012-10-01 14:26:27 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012, Mathieu Malaterre
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "opj_config.h"
|
|
|
|
#include "openjpeg.h"
|
|
|
|
|
|
|
|
#define J2K_CFMT 0
|
|
|
|
|
|
|
|
void error_callback(const char *msg, void *v);
|
|
|
|
void warning_callback(const char *msg, void *v);
|
|
|
|
void info_callback(const char *msg, void *v);
|
|
|
|
|
|
|
|
void error_callback(const char *msg, void *v) {
|
|
|
|
(void)msg;
|
|
|
|
(void)v;
|
2014-02-24 15:22:25 +01:00
|
|
|
puts(msg);
|
2012-10-01 14:26:27 +02:00
|
|
|
}
|
|
|
|
void warning_callback(const char *msg, void *v) {
|
|
|
|
(void)msg;
|
|
|
|
(void)v;
|
|
|
|
puts(msg);
|
|
|
|
}
|
|
|
|
void info_callback(const char *msg, void *v) {
|
|
|
|
(void)msg;
|
|
|
|
(void)v;
|
|
|
|
puts(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
const char * v = opj_version();
|
|
|
|
|
2012-11-15 14:38:35 +01:00
|
|
|
const OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_GRAY;
|
2014-02-24 15:27:02 +01:00
|
|
|
unsigned int numcomps = 1;
|
2014-02-24 15:50:40 +01:00
|
|
|
unsigned int i;
|
2014-02-24 15:27:02 +01:00
|
|
|
unsigned int image_width = 256;
|
|
|
|
unsigned int image_height = 256;
|
2012-10-01 14:26:27 +02:00
|
|
|
|
|
|
|
opj_cparameters_t parameters;
|
|
|
|
|
2014-02-24 15:27:02 +01:00
|
|
|
unsigned int subsampling_dx;
|
|
|
|
unsigned int subsampling_dy;
|
2012-10-01 14:26:27 +02:00
|
|
|
const char outputfile[] = "testempty2.j2k";
|
|
|
|
|
|
|
|
opj_image_cmptparm_t cmptparm;
|
|
|
|
opj_image_t *image;
|
|
|
|
opj_codec_t* l_codec = 00;
|
2012-11-15 14:38:35 +01:00
|
|
|
OPJ_BOOL bSuccess;
|
2014-02-24 15:22:25 +01:00
|
|
|
opj_stream_t *l_stream = 00;
|
2012-10-01 14:26:27 +02:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
|
|
|
|
|
|
|
opj_set_default_encoder_parameters(¶meters);
|
|
|
|
parameters.cod_format = J2K_CFMT;
|
|
|
|
puts(v);
|
2014-02-24 15:27:02 +01:00
|
|
|
subsampling_dx = (unsigned int)parameters.subsampling_dx;
|
|
|
|
subsampling_dy = (unsigned int)parameters.subsampling_dy;
|
2012-10-01 14:26:27 +02:00
|
|
|
cmptparm.prec = 8;
|
|
|
|
cmptparm.bpp = 8;
|
|
|
|
cmptparm.sgnd = 0;
|
|
|
|
cmptparm.dx = subsampling_dx;
|
|
|
|
cmptparm.dy = subsampling_dy;
|
|
|
|
cmptparm.w = image_width;
|
|
|
|
cmptparm.h = image_height;
|
2014-02-24 15:22:25 +01:00
|
|
|
strncpy(parameters.outfile, outputfile, sizeof(parameters.outfile)-1);
|
2012-10-01 14:26:27 +02:00
|
|
|
|
|
|
|
image = opj_image_create(numcomps, &cmptparm, color_space);
|
|
|
|
assert( image );
|
|
|
|
|
|
|
|
for (i = 0; i < image_width * image_height; i++)
|
|
|
|
{
|
|
|
|
int compno;
|
|
|
|
for(compno = 0; compno < numcomps; compno++)
|
|
|
|
{
|
|
|
|
image->comps[compno].data[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-24 15:22:25 +01:00
|
|
|
/* catch events using our callbacks and give a local context */
|
|
|
|
opj_set_info_handler(l_codec, info_callback,00);
|
|
|
|
opj_set_warning_handler(l_codec, warning_callback,00);
|
|
|
|
opj_set_error_handler(l_codec, error_callback,00);
|
2012-10-01 14:26:27 +02:00
|
|
|
|
2012-11-15 14:38:35 +01:00
|
|
|
l_codec = opj_create_compress(OPJ_CODEC_J2K);
|
2012-10-01 14:26:27 +02:00
|
|
|
opj_set_info_handler(l_codec, info_callback,00);
|
|
|
|
opj_set_warning_handler(l_codec, warning_callback,00);
|
|
|
|
opj_set_error_handler(l_codec, error_callback,00);
|
|
|
|
|
|
|
|
opj_setup_encoder(l_codec, ¶meters, image);
|
|
|
|
|
2013-02-17 11:34:31 +01:00
|
|
|
l_stream = opj_stream_create_default_file_stream_v3(parameters.outfile,OPJ_FALSE);
|
2014-02-24 15:22:25 +01:00
|
|
|
if( !l_stream )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Something went wrong during creation of stream\n" );
|
|
|
|
opj_destroy_codec(l_codec);
|
|
|
|
opj_image_destroy(image);
|
|
|
|
opj_stream_destroy_v3(l_stream);
|
|
|
|
return 1;
|
|
|
|
}
|
2012-10-01 14:26:27 +02:00
|
|
|
assert(l_stream);
|
|
|
|
bSuccess = opj_start_compress(l_codec,image,l_stream);
|
2014-02-24 15:22:25 +01:00
|
|
|
if( !bSuccess )
|
|
|
|
{
|
|
|
|
opj_stream_destroy_v3(l_stream);
|
|
|
|
opj_destroy_codec(l_codec);
|
|
|
|
opj_image_destroy(image);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-01 14:26:27 +02:00
|
|
|
|
|
|
|
assert( bSuccess );
|
|
|
|
bSuccess = opj_encode(l_codec, l_stream);
|
|
|
|
assert( bSuccess );
|
|
|
|
bSuccess = opj_end_compress(l_codec, l_stream);
|
|
|
|
assert( bSuccess );
|
|
|
|
|
2013-02-16 18:39:23 +01:00
|
|
|
opj_stream_destroy_v3(l_stream);
|
2012-10-01 14:26:27 +02:00
|
|
|
|
|
|
|
opj_destroy_codec(l_codec);
|
|
|
|
opj_image_destroy(image);
|
|
|
|
|
|
|
|
|
|
|
|
/* read back the generated file */
|
|
|
|
{
|
|
|
|
opj_codec_t* d_codec = 00;
|
2014-02-24 15:22:25 +01:00
|
|
|
opj_dparameters_t dparameters;
|
2012-10-01 14:26:27 +02:00
|
|
|
|
2012-11-15 14:38:35 +01:00
|
|
|
d_codec = opj_create_decompress(OPJ_CODEC_J2K);
|
2012-10-01 14:26:27 +02:00
|
|
|
opj_set_info_handler(d_codec, info_callback,00);
|
|
|
|
opj_set_warning_handler(d_codec, warning_callback,00);
|
|
|
|
opj_set_error_handler(d_codec, error_callback,00);
|
|
|
|
|
|
|
|
bSuccess = opj_setup_decoder(d_codec, &dparameters);
|
|
|
|
assert( bSuccess );
|
|
|
|
|
2013-02-17 11:34:31 +01:00
|
|
|
l_stream = opj_stream_create_default_file_stream_v3(outputfile,1);
|
2012-10-01 14:26:27 +02:00
|
|
|
assert( l_stream );
|
|
|
|
|
|
|
|
bSuccess = opj_read_header(l_stream, d_codec, &image);
|
|
|
|
assert( bSuccess );
|
|
|
|
|
|
|
|
bSuccess = opj_decode(l_codec, l_stream, image);
|
|
|
|
assert( bSuccess );
|
|
|
|
|
2014-02-24 15:22:25 +01:00
|
|
|
bSuccess = opj_end_decompress(l_codec, l_stream);
|
2012-10-01 14:26:27 +02:00
|
|
|
assert( bSuccess );
|
|
|
|
|
2013-02-16 18:39:23 +01:00
|
|
|
opj_stream_destroy_v3(l_stream);
|
2013-02-17 11:34:31 +01:00
|
|
|
|
2012-10-01 14:26:27 +02:00
|
|
|
opj_destroy_codec(d_codec);
|
|
|
|
|
|
|
|
opj_image_destroy(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
puts( "end" );
|
|
|
|
return 0;
|
|
|
|
}
|