To build the programming library, simply compile all of the
.c files in the
OpenJPEG/libopenjpeg directory.
Note: If compiling for C++, change the filename extensions
from .c to
whatever your C++ programming environment requires;
no other changes are needed to compile in C++.
The source code files in that directory are
version 1.1.1, as defined
in that directory's
openjpeg.h
file:
|
|
#define OPENJPEG_VERSION "1.1.1"
|
If you will be incorporating the code directly into your
code (not in a DLL),
then define OPJ_STATIC right after that version define in
openjpeg.h:
|
#define OPENJPEG_VERSION "1.1.1"
#define OPJ_STATIC
|
|
We illustrate how to use Open Jpeg 1.1.1
with source code examples from these
files in the
OpenJPEG/codec
directory:
|
convert.c
image_to_j2k.c
j2k_to_image.c
|
|
This intermediate image is then used to create a JPEG 200 image in
image_to_j2k.c.
Go to the beginning of the main() function in that file
(near the end of the file).
We call a function to set the
encoding parameters to default values:
|
|
opj_set_default_encoder_parameters(¶meters);
|
After that are code for parsing arguments and setting a comment.
Skip all that, and use this source code instead:
|
parameters.tcp_rates[0] = 15.0; // compression rate
parameters.tcp_numlayers = 1; // only one resolution
parameters.cp_disto_alloc = 1;
|
|
You can set tcp_rates[0] to 10 for better quality,
or higher than 15 for more compression.
After that is a switch statement that calls bmptoimage()
to create the intermediate image. Skip that.
We've already created the intermediate image.
Then there is an
if
block for creating a J2K image,
followed by an
else
block for creating a JP2 image.
We want to create a JP2 image.
Skip the
if block
and go to the
else block
to create a JP2 image.
This source code creates the JP2 image
(see comments in image_to_j2k.c for
explanations):
|
opj_cinfo_t* cinfo = opj_create_compress(CODEC_JP2);
opj_setup_encoder(cinfo, ¶meters, image);
cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
bSuccess = opj_encode(cinfo, cio, image, parameters.index);
codestream_length = cio_tell(cio);
|
|
You now have a buffer (memory block) of bytes
containing a JP2 image.
The variable cio->buffer
points to the beginning of that buffer.
For testing purposes, write that buffer
to disk as shown in image_to_j2k.c
(not shown here) and open the JP2 file
in a paint program that can open JP2 files
(such as Corel PhotoPaint)
to make sure the JP2 image was created
correctly.
After writing the file,
free the JP2 buffer,
and free the intermediate image:
|
/* close and free the byte stream */
opj_cio_close(cio);
/* free remaining compression structures */
opj_destroy_compress(cinfo);
/* free image data */
opj_image_destroy(image);
|
|
For the reverse process,
use source code from
j2k_to_image.c.
|