Initial revision

This commit is contained in:
Sebastien Lugan 2003-11-27 10:10:17 +00:00
parent a8de4a648a
commit 2ad6a9770a
31 changed files with 8328 additions and 0 deletions

6
libopenjpeg/.cvsignore Normal file
View File

@ -0,0 +1,6 @@
obj
obj.w32
bin
bin.w32
lib
lib.w32

67
libopenjpeg/Makefile Normal file
View File

@ -0,0 +1,67 @@
# $Id$
#
# makefile for OpenJPEG library
CFLAGS = -Wall -O3 -fno-strength-reduce -fomit-frame-pointer
ifndef DEBUG
LDFLAGS = -s -lm
else
LDFLAGS = -lm
endif
OBJ_DIR_W32 = obj.w32
LIB_DIR_W32 = lib.w32
ifdef MINGW32
CC = i386-mingw32-gcc
AR = i386-mingw32-ar
OBJ_DIR = $(OBJ_DIR_W32)
LIB_DIR = $(LIB_DIR_W32)
all: $(LIB_DIR)/libopenjpeg.a $(LIB_DIR)/libopenjpeg.dll
else
CC = gcc
AR = ar
OBJ_DIR = obj
LIB_DIR = lib
all: $(LIB_DIR)/libopenjpeg.a $(LIB_DIR)/libopenjpeg.so
endif
$(OBJ_DIR)/%.o:
$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
$(OBJ_DIR)/bio.o: bio.c bio.h
$(OBJ_DIR)/cio.o: cio.c cio.h
$(OBJ_DIR)/dwt.o: dwt.c dwt.h int.h fix.h tcd.h
$(OBJ_DIR)/fix.o: fix.c fix.h
$(OBJ_DIR)/int.o: int.c
$(OBJ_DIR)/j2k.o: j2k.c j2k.h cio.h tcd.h dwt.h int.h
$(OBJ_DIR)/mct.o: mct.c mct.h fix.h
$(OBJ_DIR)/mqc.o: mqc.c mqc.h
$(OBJ_DIR)/pi.o: pi.c pi.h int.h
$(OBJ_DIR)/raw.o: raw.c raw.h
$(OBJ_DIR)/t1.o: t1.c t1.h j2k.h mqc.h raw.h int.h mct.h dwt.h fix.h
$(OBJ_DIR)/t2.o: t2.c t2.h tcd.h bio.h j2k.h pi.h tgt.h int.h cio.h
$(OBJ_DIR)/tcd.o: tcd.c tcd.h int.h t1.h t2.h dwt.h mct.h
$(OBJ_DIR)/tgt.o: tgt.c tgt.h bio.h
COM_OBJS = $(addprefix $(OBJ_DIR)/, j2k.o bio.o cio.o dwt.o fix.o int.o mct.o \
mqc.o pi.o t1.o t2.o tgt.o tcd.o raw.o)
$(LIB_DIR)/libopenjpeg.a: ${COM_OBJS}
$(AR) -sr $@ $^
$(LIB_DIR)/libopenjpeg.dll: ${COM_OBJS}
${CC} -s -shared -Wl,-soname,libopenjpeg.dll -o $@ $^
$(LIB_DIR)/libopenjpeg.so.1.0: ${COM_OBJS}
${CC} -s -shared -Wl,-soname,libopenjpeg.so.1 -o $@ $^
$(LIB_DIR)/libopenjpeg.so.1: $(LIB_DIR)/libopenjpeg.so.1.0
ln -s libopenjpeg.so.1.0 $(LIB_DIR)/libopenjpeg.so.1
$(LIB_DIR)/libopenjpeg.so: $(LIB_DIR)/libopenjpeg.so.1
ln -s libopenjpeg.so.1 $(LIB_DIR)/libopenjpeg.so
clean:
rm -f $(OBJ_DIR_W32)/* $(OBJ_DIR)/* $(LIB_DIR_W32)/* $(LIB_DIR)/*

182
libopenjpeg/bio.c Normal file
View File

@ -0,0 +1,182 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
*
* 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 "bio.h"
#include <stdio.h>
#include <setjmp.h>
static unsigned char *bio_start, *bio_end, *bio_bp;
static unsigned int bio_buf;
static int bio_ct;
extern jmp_buf j2k_error;
/* <summary> */
/* Number of bytes written. */
/* </summary> */
int bio_numbytes()
{
return bio_bp - bio_start;
}
/* <summary> */
/* Init encoder. */
/* </summary> */
/* <param name="bp">Output buffer</param> */
/* <param name="len">Output buffer length</param> */
void bio_init_enc(unsigned char *bp, int len)
{
bio_start = bp;
bio_end = bp + len;
bio_bp = bp;
bio_buf = 0;
bio_ct = 8;
}
/* <summary> */
/* Init decoder. */
/* </summary> */
/* <param name="bp">Input buffer</param> */
/* <param name="len">Input buffer length</param> */
void bio_init_dec(unsigned char *bp, int len)
{
bio_start = bp;
bio_end = bp + len;
bio_bp = bp;
bio_buf = 0;
bio_ct = 0;
}
/* <summary> */
/* Write byte. --> function modified to eliminate longjmp !!! */
/* </summary> */
int bio_byteout()
{
bio_buf = (bio_buf << 8) & 0xffff;
bio_ct = bio_buf == 0xff00 ? 7 : 8;
if (bio_bp >= bio_end)
return 1;
*bio_bp++ = bio_buf >> 8;
return 0;
}
/* <summary> */
/* Read byte. --> function modified to eliminate longjmp !! */
/* </summary> */
int bio_bytein()
{
bio_buf = (bio_buf << 8) & 0xffff;
bio_ct = bio_buf == 0xff00 ? 7 : 8;
if (bio_bp >= bio_end)
return 1;
bio_buf |= *bio_bp++;
return 0;
}
/* <summary> */
/* Write bit. */
/* </summary> */
/* <param name="b">Bit to write (0 or 1)</param> */
void bio_putbit(int b)
{
if (bio_ct == 0) {
bio_byteout();
}
bio_ct--;
bio_buf |= b << bio_ct;
}
/* <summary> */
/* Read bit. */
/* </summary> */
int bio_getbit()
{
if (bio_ct == 0) {
bio_bytein();
}
bio_ct--;
return (bio_buf >> bio_ct) & 1;
}
/* <summary> */
/* Write bits. */
/* </summary> */
/* <param name="v">Value of bits</param> */
/* <param name="n">Number of bits to write</param> */
void bio_write(int v, int n)
{
int i;
for (i = n - 1; i >= 0; i--) {
bio_putbit((v >> i) & 1);
}
}
/* <summary> */
/* Read bits. */
/* </summary> */
/* <param name="n">Number of bits to read</param> */
int bio_read(int n)
{
int i, v;
v = 0;
for (i = n - 1; i >= 0; i--) {
v += bio_getbit() << i;
}
return v;
}
/* <summary> */
/* Flush bits. MOdified to eliminate longjmp !! */
/* </summary> */
int bio_flush()
{
bio_ct = 0;
if (bio_byteout())
return 1;
if (bio_ct == 7) {
bio_ct = 0;
if (bio_byteout())
return 1;
}
return 0;
}
/* <summary> */
/* </summary> */
int bio_inalign()
{
bio_ct = 0;
if ((bio_buf & 0xff) == 0xff) {
if (bio_bytein())
return 1;
bio_ct = 0;
}
return 0;
}

40
libopenjpeg/bio.h Normal file
View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* 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.
*/
#ifndef __BIO_H
#define __BIO_H
int bio_numbytes();
void bio_init_enc(unsigned char *bp, int len);
void bio_init_dec(unsigned char *bp, int len);
void bio_write(int v, int n);
int bio_read(int n);
int bio_flush(); /* modified to eliminated longjmp !! */
int bio_inalign(); /* modified to eliminated longjmp !! */
#endif

136
libopenjpeg/cio.c Normal file
View File

@ -0,0 +1,136 @@
/*
* Copyright (c) 2001-2002, David Janssens
* 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 "cio.h"
#include <setjmp.h>
static unsigned char *cio_start, *cio_end, *cio_bp;
extern jmp_buf j2k_error;
/* <summary> */
/* Number of bytes written. */
/* </summary> */
int cio_numbytes()
{
return cio_bp - cio_start;
}
/* <summary> */
/* Get position in byte stream. */
/* </summary> */
int cio_tell()
{
return cio_bp - cio_start;
}
/* <summary> */
/* Set position in byte stream. */
/* </summary> */
void cio_seek(int pos)
{
cio_bp = cio_start + pos;
}
/* <summary> */
/* Number of bytes left before the end of the stream. */
/* </summary> */
int cio_numbytesleft()
{
return cio_end - cio_bp;
}
/* <summary> */
/* Get pointer to the current position in the stream. */
/* </summary> */
unsigned char *cio_getbp()
{
return cio_bp;
}
/* <summary> */
/* Initialize byte IO. */
/* </summary> */
void cio_init(unsigned char *bp, int len)
{
cio_start = bp;
cio_end = bp + len;
cio_bp = bp;
}
/* <summary> */
/* Write a byte. */
/* </summary> */
void cio_byteout(unsigned char v)
{
if (cio_bp >= cio_end)
longjmp(j2k_error, 1);
*cio_bp++ = v;
}
/* <summary> */
/* Read a byte. */
/* </summary> */
unsigned char cio_bytein()
{
if (cio_bp >= cio_end)
longjmp(j2k_error, 1);
return *cio_bp++;
}
/* <summary> */
/* Write a byte. */
/* </summary> */
void cio_write(unsigned int v, int n)
{
int i;
for (i = n - 1; i >= 0; i--) {
cio_byteout((unsigned char) ((v >> (i << 3)) & 0xff));
}
}
/* <summary> */
/* Read some bytes. */
/* </summary> */
unsigned int cio_read(int n)
{
int i;
unsigned int v;
v = 0;
for (i = n - 1; i >= 0; i--) {
v += cio_bytein() << (i << 3);
}
return v;
}
/* <summary> */
/* Write some bytes. */
/* </summary> */
void cio_skip(int n)
{
cio_bp += n;
}

40
libopenjpeg/cio.h Normal file
View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2001-2002, David Janssens
* 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.
*/
#ifndef __CIO_H
#define __CIO_H
int cio_tell();
void cio_seek(int pos);
int cio_numbytes();
int cio_numbytesleft();
unsigned char *cio_getbp();
void cio_init(unsigned char *bp, int len);
void cio_write(unsigned int v, int n);
unsigned int cio_read(int n);
void cio_skip(int n);
#endif

681
libopenjpeg/dwt.c Normal file
View File

@ -0,0 +1,681 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* 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 "dwt.h"
#include "int.h"
#include "fix.h"
#include "tcd.h"
#include <stdlib.h>
#include <stdio.h>
#define S(i) a[x*(i)*2]
#define D(i) a[x*(1+(i)*2)]
#define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
#define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
/* new */
#define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
#define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))
/* <summary> */
/* This table contains the norms of the 5-3 wavelets for different bands. */
/* </summary> */
double dwt_norms[4][10] = {
{1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
{1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
{1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
{.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
};
/* <summary> */
/* This table contains the norms of the 9-7 wavelets for different bands. */
/* </summary> */
double dwt_norms_real[4][10] = {
{1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
{2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
{2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
{2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
};
/* Add Patrick */
static int *b = NULL;
static int lastSizeOfB = 0;
void dwt_clean()
{
if (b != NULL) {
free(b);
}
b = NULL;
lastSizeOfB = 0;
}
/* \ Add Patrick */
/* <summary> */
/* Forward lazy transform. */
/* </summary> */
void dwt_deinterleave(int *a, int n, int x, int res, int cas)
{
int dn, sn, i;
sn = res;
dn = n - res;
if (lastSizeOfB != n) {
if (b != NULL)
free(b);
b = (int *) malloc(n * sizeof(int));
lastSizeOfB = n;
}
if (cas) {
for (i = 0; i < sn; i++)
b[i] = a[(2 * i + 1) * x];
for (i = 0; i < dn; i++)
b[sn + i] = a[2 * i * x];
} else {
for (i = 0; i < sn; i++)
b[i] = a[2 * i * x];
for (i = 0; i < dn; i++)
b[sn + i] = a[(2 * i + 1) * x];
}
for (i = 0; i < n; i++)
a[i * x] = b[i];
}
/* <summary> */
/* Forward lazy transform. */
/* </summary> */
void dwt_deinterleave_real(int *a, int n, int x, int res, int cas)
{
int dn, sn, i;
sn = res;
dn = n - res;
if (lastSizeOfB != n) {
if (b != NULL)
free(b);
b = (int *) malloc(n * sizeof(int));
lastSizeOfB = n;
}
if (cas) {
for (i = 0; i < sn; i++)
b[i] = a[(2 * i + 1) * x];
for (i = 0; i < dn; i++)
b[sn + i] = a[2 * i * x];
} else {
for (i = 0; i < sn; i++)
b[i] = a[2 * i * x];
for (i = 0; i < dn; i++)
b[sn + i] = a[(2 * i + 1) * x];
}
for (i = 0; i < n; i++)
a[i * x] = b[i];
}
/* <summary> */
/* Inverse lazy transform. */
/* </summary> */
void dwt_interleave(int *a, int n, int x, int res, int cas)
{
int dn, sn, i;
sn = res;
dn = n - res;
if (lastSizeOfB != n) {
if (b != NULL)
free(b);
b = (int *) malloc(n * sizeof(int));
lastSizeOfB = n;
}
if (cas) {
for (i = 0; i < sn; i++)
b[2 * i + 1] = a[i * x];
for (i = 0; i < dn; i++)
b[2 * i] = a[(sn + i) * x];
} else {
for (i = 0; i < sn; i++)
b[2 * i] = a[i * x];
for (i = 0; i < dn; i++)
b[2 * i + 1] = a[(sn + i) * x];
}
for (i = 0; i < n; i++)
a[i * x] = b[i];
}
/* <summary> */
/* Inverse lazy transform. */
/* </summary> */
void dwt_interleave_real(int *a, int n, int x, int res, int cas)
{
int dn, sn, i;
sn = res;
dn = n - res;
if (lastSizeOfB != n) {
if (b != NULL)
free(b);
b = (int *) malloc(n * sizeof(int));
lastSizeOfB = n;
}
if (cas) {
for (i = 0; i < sn; i++)
b[2 * i + 1] = a[i * x];
for (i = 0; i < dn; i++)
b[2 * i] = a[(sn + i) * x];
} else {
for (i = 0; i < sn; i++)
b[2 * i] = a[i * x];
for (i = 0; i < dn; i++)
b[2 * i + 1] = a[(sn + i) * x];
}
for (i = 0; i < n; i++)
a[i * x] = b[i];
}
/* <summary> */
/* Forward 5-3 wavelet tranform in 1-D. */
/* </summary> */
void dwt_encode_1(int *a, int n, int x, int res, int cas)
{
int dn, sn, i = 0;
sn = res;
dn = n - res;
if (cas) {
if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
S(i) *= 2;
else {
for (i = 0; i < dn; i++)
S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
for (i = 0; i < sn; i++)
D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
}
} else {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < dn; i++)
D(i) -= (S_(i) + S_(i + 1)) >> 1;
for (i = 0; i < sn; i++)
S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
}
}
dwt_deinterleave(a, n, x, res, cas);
}
/* <summary> */
/* Inverse 5-3 wavelet tranform in 1-D. */
/* </summary> */
void dwt_decode_1(int *a, int n, int x, int res, int cas)
{
int dn, sn, i = 0;
/* dn=n/2; */
/* sn=(n+1)/2; */
sn = res;
dn = n - res;
dwt_interleave(a, n, x, res, cas);
if (cas) {
if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
S(i) /= 2;
else {
for (i = 0; i < sn; i++)
D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
for (i = 0; i < dn; i++)
S(i) += (DD_(i) + DD_(i - 1)) >> 1;
}
} else {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < sn; i++)
S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
for (i = 0; i < dn; i++)
D(i) += (S_(i) + S_(i + 1)) >> 1;
}
}
}
/* <summary> */
/* Forward 5-3 wavelet tranform in 2-D. */
/* </summary> */
void dwt_encode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)
{
int i, j, rw, rh, rw1, rh1;
int previous_rw, previous_rh, previous_rw1, previous_rh1;
for (i = 0; i < l; i++) {
int cas_col = 0;
int cas_row = 0;
rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
rw1 =
tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1 =
tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
/* Check the different cases for that it's necessary to invert high pass and low pass filter */
if (tilec->previous_row) {
previous_rw =
tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
i].
previous_x0;
previous_rw1 =
tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
i -
1].
previous_x0;
/* inversion on the previous and propagation of the inversion on the other tile */
if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
|| (tilec->resolutions[l - i - 1].cas_row
&& previous_rw1 == previous_rw - previous_rw1))
cas_row = 1;
}
if (tilec->previous_col) {
previous_rh =
tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
i].
previous_y0;
previous_rh1 =
tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
i -
1].
previous_y0;
/* inversion on the previous and propagation of the inversion on the other tile */
if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
|| (tilec->resolutions[l - i - 1].cas_col
&& previous_rh1 == previous_rh - previous_rh1))
cas_col = 1;
}
/* subband LL shorter than LH or HL */
if (rw1 < rw - rw1)
cas_row = 1;
if (rh1 < rh - rh1)
cas_col = 1;
/* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
cas_row = 1;
if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
cas_col = 1;
tilec->resolutions[l - i - 1].cas_row = cas_row;
tilec->resolutions[l - i - 1].cas_col = cas_col;
for (j = 0; j < rw; j++)
dwt_encode_1(a + j, rh, w, rh1, cas_col);
for (j = 0; j < rh; j++)
dwt_encode_1(a + j * w, rw, 1, rw1, cas_row);
}
dwt_clean();
}
/* <summary> */
/* Inverse 5-3 wavelet tranform in 2-D. */
/* </summary> */
void dwt_decode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
tcd_tilecomp_t * row_tilec, tcd_tilecomp_t * col_tilec)
{
int i, j, rw, rh, rw1, rh1;
int previous_rw, previous_rh, previous_rw1, previous_rh1;
for (i = l - 1; i >= 0; i--) {
int cas_col = 0;
int cas_row = 0;
rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
rw1 =
tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1 =
tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
if (tilec->previous_row) {
previous_rw =
tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
i].
previous_x0;
previous_rw1 =
tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
i -
1].
previous_x0;
if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
|| (row_tilec->resolutions[l - i - 1].cas_row
&& previous_rw1 == previous_rw - previous_rw1))
cas_row = 1;
}
if (tilec->previous_col) {
previous_rh =
tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
i].
previous_y0;
previous_rh1 =
tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
i -
1].
previous_y0;
if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
|| (col_tilec->resolutions[l - i - 1].cas_col
&& previous_rh1 == previous_rh - previous_rh1))
cas_col = 1;
}
if (rw1 < rw - rw1)
cas_row = 1;
if (rh1 < rh - rh1)
cas_col = 1;
/* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
cas_row = 1;
if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
cas_col = 1;
tilec->resolutions[l - i - 1].cas_row = cas_row;
tilec->resolutions[l - i - 1].cas_col = cas_col;
for (j = 0; j < rh; j++)
dwt_decode_1(a + j * w, rw, 1, rw1, cas_row);
for (j = 0; j < rw; j++)
dwt_decode_1(a + j, rh, w, rh1, cas_col);
}
dwt_clean();
}
/* <summary> */
/* Get gain of 5-3 wavelet transform. */
/* </summary> */
int dwt_getgain(int orient)
{
if (orient == 0)
return 0;
if (orient == 1 || orient == 2)
return 1;
return 2;
}
/* <summary> */
/* Get norm of 5-3 wavelet. */
/* </summary> */
double dwt_getnorm(int level, int orient)
{
return dwt_norms[orient][level];
}
/* <summary> */
/* Forward 9-7 wavelet transform in 1-D. */
/* </summary> */
void dwt_encode_1_real(int *a, int n, int x, int res, int cas)
{
int dn, sn, i = 0;
dn = n - res;
sn = res;
if (cas) {
if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < dn; i++)
S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);
for (i = 0; i < sn; i++)
D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);
for (i = 0; i < dn; i++)
S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);
for (i = 0; i < sn; i++)
D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
for (i = 0; i < dn; i++)
S(i) = fix_mul(S(i), 5038);
for (i = 0; i < sn; i++)
D(i) = fix_mul(D(i), 6660);
}
} else {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < dn; i++)
D(i) -= fix_mul(S_(i) + S_(i + 1), 12993);
for (i = 0; i < sn; i++)
S(i) -= fix_mul(D_(i - 1) + D_(i), 434);
for (i = 0; i < dn; i++)
D(i) += fix_mul(S_(i) + S_(i + 1), 7233);
for (i = 0; i < sn; i++)
S(i) += fix_mul(D_(i - 1) + D_(i), 3633);
for (i = 0; i < dn; i++)
D(i) = fix_mul(D(i), 5038);
for (i = 0; i < sn; i++)
S(i) = fix_mul(S(i), 6660);
}
}
dwt_deinterleave_real(a, n, x, res, cas);
}
/* <summary> */
/* Inverse 9-7 wavelet transform in 1-D. */
/* </summary> */
void dwt_decode_1_real(int *a, int n, int x, int res, int cas)
{
int dn, sn, i = 0;
dn = n - res;
sn = res;
dwt_interleave_real(a, n, x, res, cas);
if (cas) {
if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < sn; i++)
D(i) = fix_mul(D(i), 10076);
for (i = 0; i < dn; i++)
S(i) = fix_mul(S(i), 13320);
for (i = 0; i < sn; i++)
D(i) -= fix_mul(SS_(i) + SS_(i + 1), 3633);
for (i = 0; i < dn; i++)
S(i) -= fix_mul(DD_(i) + DD_(i - 1), 7233);
for (i = 0; i < sn; i++)
D(i) += fix_mul(SS_(i) + SS_(i + 1), 434);
for (i = 0; i < dn; i++)
S(i) += fix_mul(DD_(i) + DD_(i - 1), 12993);
}
} else {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < sn; i++)
S(i) = fix_mul(S(i), 10076);
for (i = 0; i < dn; i++)
D(i) = fix_mul(D(i), 13320);
for (i = 0; i < sn; i++)
S(i) -= fix_mul(D_(i - 1) + D_(i), 3633);
for (i = 0; i < dn; i++)
D(i) -= fix_mul(S_(i) + S_(i + 1), 7233);
for (i = 0; i < sn; i++)
S(i) += fix_mul(D_(i - 1) + D_(i), 434);
for (i = 0; i < dn; i++)
D(i) += fix_mul(S_(i) + S_(i + 1), 12993);
}
}
}
/* <summary> */
/* Forward 9-7 wavelet transform in 2-D. */
/* </summary> */
void dwt_encode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)
{
int i, j, rw, rh, rw1, rh1;
int previous_rw, previous_rh, previous_rw1, previous_rh1;
for (i = 0; i < l; i++) {
int cas_col = 0;
int cas_row = 0;
rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
rw1 =
tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1 =
tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
if (tilec->previous_row) {
previous_rw =
tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
i].
previous_x0;
previous_rw1 =
tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
i -
1].
previous_x0;
if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
|| (tilec->resolutions[l - i - 1].cas_row
&& previous_rw1 == previous_rw - previous_rw1))
cas_row = 1;
}
if (tilec->previous_col) {
previous_rh =
tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
i].
previous_y0;
previous_rh1 =
tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
i -
1].
previous_y0;
if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
|| (tilec->resolutions[l - i - 1].cas_col
&& previous_rh1 == previous_rh - previous_rh1))
cas_col = 1;
}
if (rw1 < rw - rw1)
cas_row = 1;
if (rh1 < rh - rh1)
cas_col = 1;
/* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
cas_row = 1;
if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
cas_col = 1;
tilec->resolutions[l - i - 1].cas_row = cas_row;
tilec->resolutions[l - i - 1].cas_col = cas_col;
for (j = 0; j < rw; j++)
dwt_encode_1_real(a + j, rh, w, rh1, cas_col);
for (j = 0; j < rh; j++)
dwt_encode_1_real(a + j * w, rw, 1, rw1, cas_row);
}
}
/* <summary> */
/* Inverse 9-7 wavelet transform in 2-D. */
/* </summary> */
void dwt_decode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
tcd_tilecomp_t * row_tilec,
tcd_tilecomp_t * col_tilec)
{
int i, j, rw, rh, rw1, rh1;
int previous_rw, previous_rh, previous_rw1, previous_rh1;
for (i = l - 1; i >= 0; i--) {
int cas_col = 0;
int cas_row = 0;
rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
rw1 =
tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1 =
tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
if (tilec->previous_row) {
previous_rw =
tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
i].
previous_x0;
previous_rw1 =
tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
i -
1].
previous_x0;
if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
|| (row_tilec->resolutions[l - i - 1].cas_row
&& previous_rw1 == previous_rw - previous_rw1))
cas_row = 1;
}
if (tilec->previous_col) {
previous_rh =
tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
i].
previous_y0;
previous_rh1 =
tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
i -
1].
previous_y0;
if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
|| (col_tilec->resolutions[l - i - 1].cas_col
&& previous_rh1 == previous_rh - previous_rh1))
cas_col = 1;
}
if (rw1 < rw - rw1)
cas_row = 1;
if (rh1 < rh - rh1)
cas_col = 1;
/* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
cas_row = 1;
if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
cas_col = 1;
tilec->resolutions[l - i - 1].cas_row = cas_row;
tilec->resolutions[l - i - 1].cas_col = cas_col;
for (j = 0; j < rh; j++)
dwt_decode_1_real(a + j * w, rw, 1, rw1, cas_row);
for (j = 0; j < rw; j++)
dwt_decode_1_real(a + j, rh, w, rh1, cas_col);
}
}
/* <summary> */
/* Get gain of 9-7 wavelet transform. */
/* </summary> */
int dwt_getgain_real(int orient)
{
return 0;
}
/* <summary> */
/* Get norm of 9-7 wavelet. */
/* </summary> */
double dwt_getnorm_real(int level, int orient)
{
return dwt_norms_real[orient][level];
}

98
libopenjpeg/dwt.h Normal file
View File

@ -0,0 +1,98 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* 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 "tcd.h"
#ifndef __DWT_H
#define __DWT_H
/*
* Apply a reversible DWT transform to a component of an image
* a: samples of the component
* w: width of the component
* h: height of the component
* l: number of decomposition levels in the DWT
*/
/* void dwt_encode(int* a, int w, int h, int l); */
void dwt_encode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l);
/*
* Apply a reversible inverse DWT transform to a component of an image
* a: samples of the component
* w: width of the component
* h: height of the component
* l: number of decomposition levels in the DWT
*/
void dwt_decode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
tcd_tilecomp_t * row_tilec, tcd_tilecomp_t * col_tilec);
/* void dwt_decode(int* a, int w, int h,tcd_tilecomp_t *tilec, int l); */
/*
* Get the gain of a subband for the reversible DWT
* orient: number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH)
*/
int dwt_getgain(int orient);
/*
* Get the norm of a wavelet function of a subband at a specified level for the reversible DWT
* level: level of the wavelet function
* orient: band of the wavelet function
*/
double dwt_getnorm(int level, int orient);
/*
* Apply an irreversible DWT transform to a component of an image
* a: samples of the component
* w: width of the component
* h: height of the component
* l: number of decomposition levels in the DWT
*/
void dwt_encode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l);
/*
* Apply an irreversible inverse DWT transform to a component of an image
* a: samples of the component
* w: width of the component
* h: height of the component
* l: number of decomposition levels in the DWT
*/
void dwt_decode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
tcd_tilecomp_t * row_tilec,
tcd_tilecomp_t * col_tilec);
/*
* Get the gain of a subband for the irreversible DWT
* orient: number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH)
*/
int dwt_getgain_real(int orient);
/*
* Get the norm of a wavelet function of a subband at a specified level for the irreversible DWT
* level: level of the wavelet function
* orient: band of the wavelet function
*/
double dwt_getnorm_real(int level, int orient);
#endif

41
libopenjpeg/fix.c Normal file
View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2001-2002, David Janssens
* 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 "fix.h"
#ifdef DAVID_WIN32
#define int64 __int64
#else
#define int64 long long
#endif
/* <summary> */
/* Multiply two fixed-precision rational numbers. */
/* </summary> */
int fix_mul(int a, int b)
{
return (int) ((int64) a * (int64) b >> 13);
}

33
libopenjpeg/fix.h Normal file
View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2001-2002, David Janssens
* 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.
*/
#ifndef __FIX_H
#define __FIX_H
int fix_mul(int a, int b);
#endif

97
libopenjpeg/int.c Normal file
View File

@ -0,0 +1,97 @@
/*
* Copyright (c) 2001-2002, David Janssens
* 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.
*/
/* <summary> */
/* Get the minimum of two integers. */
/* </summary> */
int int_min(int a, int b)
{
return a < b ? a : b;
}
/* <summary> */
/* Get the maximum of two integers. */
/* </summary> */
int int_max(int a, int b)
{
return a > b ? a : b;
}
/* <summary> */
/* Clamp an integer inside an interval. */
/* </summary> */
int int_clamp(int a, int min, int max)
{
if (a < min)
return min;
if (a > max)
return max;
return a;
}
/* <summary> */
/* Get absolute value of integer. */
/* </summary> */
int int_abs(int a)
{
return a < 0 ? -a : a;
}
/* <summary> */
/* Divide an integer and round upwards. */
/* </summary> */
int int_ceildiv(int a, int b)
{
return (a + b - 1) / b;
}
/* <summary> */
/* Divide an integer by a power of 2 and round upwards. */
/* </summary> */
int int_ceildivpow2(int a, int b)
{
return (a + (1 << b) - 1) >> b;
}
/* <summary> */
/* Divide an integer by a power of 2 and round downwards. */
/* </summary> */
int int_floordivpow2(int a, int b)
{
return a >> b;
}
/* <summary> */
/* Get logarithm of an integer and round downwards. */
/* </summary> */
int int_floorlog2(int a)
{
int l;
for (l = 0; a > 1; l++) {
a >>= 1;
}
return l;
}

39
libopenjpeg/int.h Normal file
View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2001-2002, David Janssens
* 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.
*/
#ifndef __INT_H
#define __INT_H
int int_min(int a, int b);
int int_max(int a, int b);
int int_clamp(int a, int min, int max);
int int_abs(int a);
int int_ceildiv(int a, int b);
int int_ceildivpow2(int a, int b);
int int_floordivpow2(int a, int b);
int int_floorlog2(int a);
#endif

1153
libopenjpeg/j2k.c Normal file

File diff suppressed because it is too large Load Diff

185
libopenjpeg/j2k.h Normal file
View File

@ -0,0 +1,185 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* 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.
*/
#define VERSION "0.0.8"
#ifdef DAVID_WIN32
#ifdef LIBJ2K_EXPORTS
#define LIBJ2K_API __declspec(dllexport)
#else
#define LIBJ2K_API __declspec(dllimport)
#endif
#else
#define LIBJ2K_API
#endif
#ifndef __J2K_H
#define __J2K_H
#define J2K_MAXRLVLS 33
#define J2K_MAXBANDS (3*J2K_MAXRLVLS+1)
#define J2K_CP_CSTY_PRT 0x01
#define J2K_CP_CSTY_SOP 0x02
#define J2K_CP_CSTY_EPH 0x04
#define J2K_CCP_CSTY_PRT 0x01
#define J2K_CCP_CBLKSTY_LAZY 0x01
#define J2K_CCP_CBLKSTY_RESET 0x02
#define J2K_CCP_CBLKSTY_TERMALL 0x04
#define J2K_CCP_CBLKSTY_VSC 0x08
#define J2K_CCP_CBLKSTY_PTERM 0x10
#define J2K_CCP_CBLKSTY_SEGSYM 0x20
#define J2K_CCP_QNTSTY_NOQNT 0
#define J2K_CCP_QNTSTY_SIQNT 1
#define J2K_CCP_QNTSTY_SEQNT 2
typedef struct {
int dx, dy; /* XRsiz, YRsiz */
int prec; /* precision */
int bpp; /* deapth of image in bits */
int sgnd; /* signed */
int *data; /* image-component data */
} j2k_comp_t;
typedef struct {
int x0, y0; /* XOsiz, YOsiz */
int x1, y1; /* Xsiz, Ysiz */
int numcomps; /* number of components */
int index_on; /* 0 = no index || 1 = index */
/* int PPT; */
j2k_comp_t *comps; /* image-components */
} j2k_image_t;
typedef struct {
int expn; /* exponent */
int mant; /* mantissa */
} j2k_stepsize_t;
typedef struct {
int csty; /* coding style */
int numresolutions; /* number of resolutions */
int cblkw; /* width of code-blocks */
int cblkh; /* height of code-blocks */
int cblksty; /* code-block coding style */
int qmfbid; /* discrete wavelet transform identifier */
int qntsty; /* quantisation style */
j2k_stepsize_t stepsizes[J2K_MAXBANDS]; /* stepsizes used for quantisation */
int numgbits; /* number of guard bits */
int roishift; /* Region Of Interest shift */
int prcw[J2K_MAXRLVLS]; /* Precinct width */
int prch[J2K_MAXRLVLS]; /* Precinct height */
} j2k_tccp_t;
typedef struct {
int resno0, compno0;
int layno1, resno1, compno1;
int prg;
int tile;
char progorder[4];
} j2k_poc_t;
typedef struct {
int csty; /* coding style */
int prg; /* progression order */
int numlayers; /* number of layers */
int mct; /* multi-component transform identifier */
int rates[100]; /* rates of layers */
int numpocs; /* number of progression order changes */
j2k_poc_t pocs[32]; /* progression order changes */
j2k_tccp_t *tccps; /* tile-component coding parameters */
} j2k_tcp_t;
typedef struct {
int image_type; /* 0: PNM, PGM, PPM 1: PGX */
int disto_alloc; /* Allocation by rate/distortion */
int fixed_alloc; /* Allocation by fixed layer */
int tx0, ty0; /* XTOsiz, YTOsiz */
int tdx, tdy; /* XTsiz, YTsiz */
char *comment; /* comment for coding */
int tw, th;
j2k_tcp_t *tcps; /* tile coding parameters */
} j2k_cp_t;
typedef struct {
int start_pos, end_pos; /* start and end position */
double disto;
} info_packet; /* Index struct */
typedef struct {
int num_tile; /* Number of Tile */
int start_pos; /* Start position */
int end_header; /* End position of the header */
int end_pos; /* End position */
int pw, ph; /* number of precinct by tile */
info_packet *packet; /* information concerning packets inside tile */
} info_tile; /* index struct */
typedef struct {
int index_on;
double D_max; /* ADD for Marcela */
int num; /* numero of packet */
int index_write; /* writing the packet inthe index with t2_encode_packets */
int Im_w, Im_h; /* Image width and Height */
int Prog; /* progression order */
int Tile_x, Tile_y; /* Number of Tile in X and Y */
int tw, th;
int Comp; /* Component numbers */
int Layer; /* number of layer */
int Decomposition; /* number of decomposition */
int pw, ph; /* nombre precinct in X and Y */
int pdx, pdy; /* size of precinct in X and Y */
int Main_head_end; /* Main header position */
int codestream_size; /* codestream's size */
info_tile *tile; /* information concerning tiles inside image */
} info_image; /* index struct */
/*
* Encode an image into a JPEG-2000 codestream
* i: image to encode
* cp: coding parameters
* dest: destination buffer
* len: length of destination buffer
* index : index file name
*/
LIBJ2K_API int j2k_encode(j2k_image_t * i, j2k_cp_t * cp, char *outfile,
int len, char *index);
/* LIBJ2K_API int j2k_encode(j2k_image_t *i, j2k_cp_t *cp,unsigned char *dest, int len); */
/*
* Decode an image from a JPEG-2000 codestream
* src: source buffer
* len: length of source buffer
* i: decode image
* cp: coding parameters that were used to encode the image
*/
LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t ** i,
j2k_cp_t ** cp);
#endif

134
libopenjpeg/mct.c Normal file
View File

@ -0,0 +1,134 @@
/*
* Copyright (c) 2001-2002, David Janssens
* 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 "mct.h"
#include "fix.h"
/* <summary> */
/* This table contains the norms of the basis function of the reversible MCT. */
/* </summary> */
double mct_norms[3] = { 1.732, .8292, .8292 };
/* <summary> */
/* This table contains the norms of the basis function of the irreversible MCT. */
/* </summary> */
double mct_norms_real[3] = { 1.732, 1.805, 1.573 };
/* <summary> */
/* Foward reversible MCT. */
/* </summary> */
void mct_encode(int *c0, int *c1, int *c2, int n)
{
int i;
for (i = 0; i < n; i++) {
int r, g, b, y, u, v;
r = c0[i];
g = c1[i];
b = c2[i];
y = (r + (g << 1) + b) >> 2;
u = b - g;
v = r - g;
c0[i] = y;
c1[i] = u;
c2[i] = v;
}
}
/* <summary> */
/* Inverse reversible MCT. */
/* </summary> */
void mct_decode(int *c0, int *c1, int *c2, int n)
{
int i;
for (i = 0; i < n; i++) {
int y, u, v, r, g, b;
y = c0[i];
u = c1[i];
v = c2[i];
g = y - ((u + v) >> 2);
r = v + g;
b = u + g;
c0[i] = r;
c1[i] = g;
c2[i] = b;
}
}
/* <summary> */
/* Get norm of basis function of reversible MCT. */
/* </summary> */
double mct_getnorm(int compno)
{
return mct_norms[compno];
}
/* <summary> */
/* Foward irreversible MCT. */
/* </summary> */
void mct_encode_real(int *c0, int *c1, int *c2, int n)
{
int i;
for (i = 0; i < n; i++) {
int r, g, b, y, u, v;
r = c0[i];
g = c1[i];
b = c2[i];
y = fix_mul(r, 2449) + fix_mul(g, 4809) + fix_mul(b, 934);
u = -fix_mul(r, 1382) - fix_mul(g, 2714) + fix_mul(b, 4096);
v = fix_mul(r, 4096) - fix_mul(g, 3430) - fix_mul(b, 666);
c0[i] = y;
c1[i] = u;
c2[i] = v;
}
}
/* <summary> */
/* Inverse irreversible MCT. */
/* </summary> */
void mct_decode_real(int *c0, int *c1, int *c2, int n)
{
int i;
for (i = 0; i < n; i++) {
int y, u, v, r, g, b;
y = c0[i];
u = c1[i];
v = c2[i];
r = y + fix_mul(v, 11485);
g = y - fix_mul(u, 2819) - fix_mul(v, 5850);
b = y + fix_mul(u, 14516);
c0[i] = r;
c1[i] = g;
c2[i] = b;
}
}
/* <summary> */
/* Get norm of basis function of irreversible MCT. */
/* </summary> */
double mct_getnorm_real(int compno)
{
return mct_norms_real[compno];
}

74
libopenjpeg/mct.h Normal file
View File

@ -0,0 +1,74 @@
/*
* Copyright (c) 2001-2002, David Janssens
* 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.
*/
#ifndef __MCT_H
#define __MCT_H
/*
* Apply a reversible multi-component transform to an image
* R: samples for red component
* G: samples for green component
* B: samples blue component
* n: number of samples for each component
*/
void mct_encode(int *R, int *G, int *B, int n);
/*
* Apply a reversible multi-component inverse transform to an image
* Y: samples for luminance component
* U: samples for red chrominance component
* V: samples for blue chrominance component
* n: number of samples for each component
*/
void mct_decode(int *V, int *U, int *Y, int n);
/*
* Get norm of the basis function used for the reversible multi-component transform
* compno: number of the component (0->Y, 1->U, 2->V)
*/
double mct_getnorm(int compno);
/*
* Apply an irreversible multi-component transform to an image
* R: samples for red component
* G: samples for green component
* B: samples blue component
* n: number of samples for each component
*/
void mct_encode_real(int *c0, int *c1, int *c2, int n);
/*
* Apply an irreversible multi-component inverse transform to an image
* Y: samples for luminance component
* U: samples for red chrominance component
* V: samples for blue chrominance component
* n: number of samples for each component
*/
void mct_decode_real(int *c0, int *c1, int *c2, int n);
/*
* Get norm of the basis function used for the irreversible multi-component transform
* compno: number of the component (0->Y, 1->U, 2->V)
*/
double mct_getnorm_real(int compno);
#endif

588
libopenjpeg/mqc.c Normal file
View File

@ -0,0 +1,588 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* 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 "mqc.h"
#include <stdio.h>
/* <summary> */
/* This struct defines the state of a context. */
/* </summary> */
typedef struct mqc_state_s {
unsigned int qeval; /* the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */
int mps; /* the Most Probable Symbol (0 or 1) */
struct mqc_state_s *nmps; /* next state if the next encoded symbol is the MPS */
struct mqc_state_s *nlps; /* next state if the next encoded symbol is the LPS */
} mqc_state_t;
/* <summary> */
/* This array defines all the possible states for a context. */
/* </summary> */
mqc_state_t mqc_states[47 * 2] = {
{0x5601, 0, &mqc_states[2], &mqc_states[3]},
{0x5601, 1, &mqc_states[3], &mqc_states[2]},
{0x3401, 0, &mqc_states[4], &mqc_states[12]},
{0x3401, 1, &mqc_states[5], &mqc_states[13]},
{0x1801, 0, &mqc_states[6], &mqc_states[18]},
{0x1801, 1, &mqc_states[7], &mqc_states[19]},
{0x0ac1, 0, &mqc_states[8], &mqc_states[24]},
{0x0ac1, 1, &mqc_states[9], &mqc_states[25]},
{0x0521, 0, &mqc_states[10], &mqc_states[58]},
{0x0521, 1, &mqc_states[11], &mqc_states[59]},
{0x0221, 0, &mqc_states[76], &mqc_states[66]},
{0x0221, 1, &mqc_states[77], &mqc_states[67]},
{0x5601, 0, &mqc_states[14], &mqc_states[13]},
{0x5601, 1, &mqc_states[15], &mqc_states[12]},
{0x5401, 0, &mqc_states[16], &mqc_states[28]},
{0x5401, 1, &mqc_states[17], &mqc_states[29]},
{0x4801, 0, &mqc_states[18], &mqc_states[28]},
{0x4801, 1, &mqc_states[19], &mqc_states[29]},
{0x3801, 0, &mqc_states[20], &mqc_states[28]},
{0x3801, 1, &mqc_states[21], &mqc_states[29]},
{0x3001, 0, &mqc_states[22], &mqc_states[34]},
{0x3001, 1, &mqc_states[23], &mqc_states[35]},
{0x2401, 0, &mqc_states[24], &mqc_states[36]},
{0x2401, 1, &mqc_states[25], &mqc_states[37]},
{0x1c01, 0, &mqc_states[26], &mqc_states[40]},
{0x1c01, 1, &mqc_states[27], &mqc_states[41]},
{0x1601, 0, &mqc_states[58], &mqc_states[42]},
{0x1601, 1, &mqc_states[59], &mqc_states[43]},
{0x5601, 0, &mqc_states[30], &mqc_states[29]},
{0x5601, 1, &mqc_states[31], &mqc_states[28]},
{0x5401, 0, &mqc_states[32], &mqc_states[28]},
{0x5401, 1, &mqc_states[33], &mqc_states[29]},
{0x5101, 0, &mqc_states[34], &mqc_states[30]},
{0x5101, 1, &mqc_states[35], &mqc_states[31]},
{0x4801, 0, &mqc_states[36], &mqc_states[32]},
{0x4801, 1, &mqc_states[37], &mqc_states[33]},
{0x3801, 0, &mqc_states[38], &mqc_states[34]},
{0x3801, 1, &mqc_states[39], &mqc_states[35]},
{0x3401, 0, &mqc_states[40], &mqc_states[36]},
{0x3401, 1, &mqc_states[41], &mqc_states[37]},
{0x3001, 0, &mqc_states[42], &mqc_states[38]},
{0x3001, 1, &mqc_states[43], &mqc_states[39]},
{0x2801, 0, &mqc_states[44], &mqc_states[38]},
{0x2801, 1, &mqc_states[45], &mqc_states[39]},
{0x2401, 0, &mqc_states[46], &mqc_states[40]},
{0x2401, 1, &mqc_states[47], &mqc_states[41]},
{0x2201, 0, &mqc_states[48], &mqc_states[42]},
{0x2201, 1, &mqc_states[49], &mqc_states[43]},
{0x1c01, 0, &mqc_states[50], &mqc_states[44]},
{0x1c01, 1, &mqc_states[51], &mqc_states[45]},
{0x1801, 0, &mqc_states[52], &mqc_states[46]},
{0x1801, 1, &mqc_states[53], &mqc_states[47]},
{0x1601, 0, &mqc_states[54], &mqc_states[48]},
{0x1601, 1, &mqc_states[55], &mqc_states[49]},
{0x1401, 0, &mqc_states[56], &mqc_states[50]},
{0x1401, 1, &mqc_states[57], &mqc_states[51]},
{0x1201, 0, &mqc_states[58], &mqc_states[52]},
{0x1201, 1, &mqc_states[59], &mqc_states[53]},
{0x1101, 0, &mqc_states[60], &mqc_states[54]},
{0x1101, 1, &mqc_states[61], &mqc_states[55]},
{0x0ac1, 0, &mqc_states[62], &mqc_states[56]},
{0x0ac1, 1, &mqc_states[63], &mqc_states[57]},
{0x09c1, 0, &mqc_states[64], &mqc_states[58]},
{0x09c1, 1, &mqc_states[65], &mqc_states[59]},
{0x08a1, 0, &mqc_states[66], &mqc_states[60]},
{0x08a1, 1, &mqc_states[67], &mqc_states[61]},
{0x0521, 0, &mqc_states[68], &mqc_states[62]},
{0x0521, 1, &mqc_states[69], &mqc_states[63]},
{0x0441, 0, &mqc_states[70], &mqc_states[64]},
{0x0441, 1, &mqc_states[71], &mqc_states[65]},
{0x02a1, 0, &mqc_states[72], &mqc_states[66]},
{0x02a1, 1, &mqc_states[73], &mqc_states[67]},
{0x0221, 0, &mqc_states[74], &mqc_states[68]},
{0x0221, 1, &mqc_states[75], &mqc_states[69]},
{0x0141, 0, &mqc_states[76], &mqc_states[70]},
{0x0141, 1, &mqc_states[77], &mqc_states[71]},
{0x0111, 0, &mqc_states[78], &mqc_states[72]},
{0x0111, 1, &mqc_states[79], &mqc_states[73]},
{0x0085, 0, &mqc_states[80], &mqc_states[74]},
{0x0085, 1, &mqc_states[81], &mqc_states[75]},
{0x0049, 0, &mqc_states[82], &mqc_states[76]},
{0x0049, 1, &mqc_states[83], &mqc_states[77]},
{0x0025, 0, &mqc_states[84], &mqc_states[78]},
{0x0025, 1, &mqc_states[85], &mqc_states[79]},
{0x0015, 0, &mqc_states[86], &mqc_states[80]},
{0x0015, 1, &mqc_states[87], &mqc_states[81]},
{0x0009, 0, &mqc_states[88], &mqc_states[82]},
{0x0009, 1, &mqc_states[89], &mqc_states[83]},
{0x0005, 0, &mqc_states[90], &mqc_states[84]},
{0x0005, 1, &mqc_states[91], &mqc_states[85]},
{0x0001, 0, &mqc_states[90], &mqc_states[86]},
{0x0001, 1, &mqc_states[91], &mqc_states[87]},
{0x5601, 0, &mqc_states[92], &mqc_states[92]},
{0x5601, 1, &mqc_states[93], &mqc_states[93]},
};
#define MQC_NUMCTXS 32
unsigned int mqc_c;
unsigned int mqc_a;
unsigned int mqc_ct;
unsigned char *mqc_bp;
unsigned char *mqc_start;
unsigned char *mqc_end;
mqc_state_t *mqc_ctxs[MQC_NUMCTXS];
mqc_state_t **mqc_curctx;
/* <summary> */
/* Return the number of bytes already encoded. */
/* </summary> */
int mqc_numbytes()
{
return mqc_bp - mqc_start;
}
/* <summary> */
/* Output a byte, doing bit-stuffing if necessary. */
/* After a 0xff byte, the next byte must be smaller than 0x90 */
/* </summary> */
void mqc_byteout()
{
if (*mqc_bp == 0xff) {
mqc_bp++;
*mqc_bp = mqc_c >> 20;
mqc_c &= 0xfffff;
mqc_ct = 7;
} else {
if ((mqc_c & 0x8000000) == 0) { /* ((mqc_c&0x8000000)==0) CHANGE */
mqc_bp++;
*mqc_bp = mqc_c >> 19;
mqc_c &= 0x7ffff;
mqc_ct = 8;
} else {
(*mqc_bp)++;
if (*mqc_bp == 0xff) {
mqc_c &= 0x7ffffff;
mqc_bp++;
*mqc_bp = mqc_c >> 20;
mqc_c &= 0xfffff;
mqc_ct = 7;
} else {
mqc_bp++;
*mqc_bp = mqc_c >> 19;
mqc_c &= 0x7ffff;
mqc_ct = 8;
}
}
}
}
/* <summary> */
/* Renormalize mqc_a and mqc_c while encoding, so that mqc_a stays between 0x8000 and 0x10000 */
/* </summary> */
void mqc_renorme()
{
do {
mqc_a <<= 1;
mqc_c <<= 1;
mqc_ct--;
if (mqc_ct == 0) {
mqc_byteout();
}
} while ((mqc_a & 0x8000) == 0);
}
/* <summary> */
/* Encode the most probable symbol. */
/* </summary> */
void mqc_codemps()
{
mqc_a -= (*mqc_curctx)->qeval;
if ((mqc_a & 0x8000) == 0) {
if (mqc_a < (*mqc_curctx)->qeval) {
mqc_a = (*mqc_curctx)->qeval;
} else {
mqc_c += (*mqc_curctx)->qeval;
}
*mqc_curctx = (*mqc_curctx)->nmps;
mqc_renorme();
} else {
mqc_c += (*mqc_curctx)->qeval;
}
}
/* <summary> */
/* Encode the most least symbol. */
/* </summary> */
void mqc_codelps()
{
mqc_a -= (*mqc_curctx)->qeval;
if (mqc_a < (*mqc_curctx)->qeval) {
mqc_c += (*mqc_curctx)->qeval;
} else {
mqc_a = (*mqc_curctx)->qeval;
}
*mqc_curctx = (*mqc_curctx)->nlps;
mqc_renorme();
}
/* <summary> */
/* Initialize encoder. */
/* </summary> */
/* <param name="bp">Output buffer.</param> */
void mqc_init_enc(unsigned char *bp)
{
mqc_setcurctx(0);
mqc_a = 0x8000;
mqc_c = 0;
mqc_bp = bp - 1;
mqc_ct = 12;
if (*mqc_bp == 0xff) {
mqc_ct = 13;
}
mqc_start = bp;
}
/* <summary> */
/* Set current context. */
/* </summary> */
/* <param name="ctxno">Context number.</param> */
void mqc_setcurctx(int ctxno)
{
mqc_curctx = &mqc_ctxs[ctxno];
}
/* <summary> */
/* Encode a symbol using the MQ-coder. */
/* </summary> */
/* <param name="d"> The symbol to be encoded (0 or 1).</param> */
void mqc_encode(int d)
{
if ((*mqc_curctx)->mps == d) {
mqc_codemps();
} else {
mqc_codelps();
}
}
/* <summary> */
/* Fill mqc_c with 1's for flushing */
/* </summary> */
void mqc_setbits()
{
unsigned int tempc = mqc_c + mqc_a;
mqc_c |= 0xffff;
if (mqc_c >= tempc) {
mqc_c -= 0x8000;
}
}
/* <summary> */
/* Flush encoded data. */
/* </summary> */
void mqc_flush()
{
mqc_setbits();
mqc_c <<= mqc_ct;
mqc_byteout();
mqc_c <<= mqc_ct;
mqc_byteout();
if (*mqc_bp != 0xff) {
mqc_bp++;
}
}
/* <summary> */
/* not fully implemented and tested !! */
/* BYPASS mode switch, initialization operation */
/* JPEG 2000 p 505 */
/* </summary> */
void mqc_bypass_init_enc()
{
mqc_c = 0;
mqc_ct = 8;
if (*mqc_bp == 0xff) {
mqc_ct = 7;
}
}
/* <summary> */
/* not fully implemented and tested !! */
/* BYPASS mode switch, coding operation */
/* JPEG 2000 p 505 */
/* </summary> */
void mqc_bypass_enc(int d)
{
mqc_ct--;
mqc_c = mqc_c + (d << mqc_ct);
if (mqc_ct == 0) {
mqc_bp++;
*mqc_bp = mqc_c;
mqc_ct = 8;
if (*mqc_bp == 0xff) {
mqc_ct = 7;
}
mqc_c = 0;
}
}
/* <summary> */
/* not fully implemented and tested !! */
/* BYPASS mode switch, flush operation */
/* </summary> */
int mqc_bypass_flush_enc()
{
unsigned char bit_padding;
bit_padding = 0;
if (mqc_ct != 0) {
while (mqc_ct > 0) {
mqc_ct--;
mqc_c += bit_padding << mqc_ct;
bit_padding = (bit_padding + 1) & 0x01;
}
mqc_bp++;
*mqc_bp = mqc_c;
mqc_ct = 8;
mqc_c = 0;
}
return 1;
}
/* <summary> */
/* RESET mode switch */
/* </summary> */
void mqc_reset_enc()
{
mqc_resetstates();
mqc_setstate(18, 0, 46);
mqc_setstate(0, 0, 3);
mqc_setstate(1, 0, 4);
}
/* <summary> */
/* mode switch RESTART (TERMALL) */
/* </summary> */
int mqc_restart_enc()
{
int correction = 1;
/* <flush part> */
int n = 27 - 15 - mqc_ct;
mqc_c <<= mqc_ct;
while (n > 0) {
mqc_byteout();
n -= mqc_ct;
mqc_c <<= mqc_ct;
}
mqc_byteout();
return correction;
}
/* <summary> */
/* mode switch RESTART (TERMALL) reinitialisation */
/* </summary> */
void mqc_restart_init_enc()
{
/* <Re-init part> */
mqc_a = 0x8000;
mqc_c = 0;
mqc_ct = 12;
mqc_bp--;
if (*mqc_bp == 0xff) {
mqc_ct = 13;
}
}
/* <summary> */
/* ERTERM mode switch */
/* </summary> */
void mqc_erterm_enc()
{
int k = 11 - mqc_ct + 1;
while (k > 0) {
mqc_c <<= mqc_ct;
mqc_ct = 0;
mqc_byteout();
k -= mqc_ct;
}
if (*mqc_bp != 0xff) {
mqc_byteout();
}
}
/* <summary> */
/* SEGMARK mode switch (SEGSYM) */
/* </summary> */
void mqc_segmark_enc()
{
int i;
mqc_setcurctx(18);
for (i = 1; i < 5; i++) {
mqc_encode(i % 2);
}
}
/* <summary> */
/* </summary> */
int mqc_mpsexchange()
{
int d;
if (mqc_a < (*mqc_curctx)->qeval) {
d = 1 - (*mqc_curctx)->mps;
*mqc_curctx = (*mqc_curctx)->nlps;
} else {
d = (*mqc_curctx)->mps;
*mqc_curctx = (*mqc_curctx)->nmps;
}
return d;
}
/* <summary> */
/* </summary> */
int mqc_lpsexchange()
{
int d;
if (mqc_a < (*mqc_curctx)->qeval) {
mqc_a = (*mqc_curctx)->qeval;
d = (*mqc_curctx)->mps;
*mqc_curctx = (*mqc_curctx)->nmps;
} else {
mqc_a = (*mqc_curctx)->qeval;
d = 1 - (*mqc_curctx)->mps;
*mqc_curctx = (*mqc_curctx)->nlps;
}
return d;
}
/* <summary> */
/* Input a byte. */
/* </summary> */
void mqc_bytein()
{
if (mqc_bp != mqc_end) {
unsigned int c;
if (mqc_bp + 1 != mqc_end) {
c = *(mqc_bp + 1);
} else {
c = 0xff;
}
if (*mqc_bp == 0xff) {
if (c > 0x8f) {
mqc_c += 0xff00;
mqc_ct = 8;
} else {
mqc_bp++;
mqc_c += c << 9;
mqc_ct = 7;
}
} else {
mqc_bp++;
mqc_c += c << 8;
mqc_ct = 8;
}
} else {
mqc_c += 0xff00;
mqc_ct = 8;
}
}
/* <summary> */
/* Renormalize mqc_a and mqc_c while decoding. */
/* </summary> */
void mqc_renormd()
{
do {
if (mqc_ct == 0) {
mqc_bytein();
}
mqc_a <<= 1;
mqc_c <<= 1;
mqc_ct--;
} while (mqc_a < 0x8000);
}
/* <summary> */
/* Initialize decoder. */
/* </summary> */
void mqc_init_dec(unsigned char *bp, int len)
{
mqc_setcurctx(0);
mqc_start = bp;
mqc_end = bp + len;
mqc_bp = bp;
mqc_c = *mqc_bp << 16;
mqc_bytein();
mqc_c <<= 7;
mqc_ct -= 7;
mqc_a = 0x8000;
}
/* <summary> */
/* Decode a symbol. */
/* </summary> */
int mqc_decode()
{
int d;
mqc_a -= (*mqc_curctx)->qeval;
if ((mqc_c >> 16) < (*mqc_curctx)->qeval) {
d = mqc_lpsexchange();
mqc_renormd();
} else {
mqc_c -= (*mqc_curctx)->qeval << 16;
if ((mqc_a & 0x8000) == 0) {
d = mqc_mpsexchange();
mqc_renormd();
} else {
d = (*mqc_curctx)->mps;
}
}
return d;
}
/* <summary> */
/* Reset states of all contexts. */
/* </summary> */
void mqc_resetstates()
{
int i;
for (i = 0; i < MQC_NUMCTXS; i++) {
mqc_ctxs[i] = mqc_states;
}
}
/* <summary> */
/* Set the state for a context. */
/* </summary> */
/* <param name="ctxno">Context number</param> */
/* <param name="msb">Most significant bit</param> */
/* <param name="prob">Index to the probability of symbols</param> */
void mqc_setstate(int ctxno, int msb, int prob)
{
mqc_ctxs[ctxno] = &mqc_states[msb + (prob << 1)];
}

127
libopenjpeg/mqc.h Normal file
View File

@ -0,0 +1,127 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* 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.
*/
#ifndef __MQC_H
#define __MQC_H
/*
* Return the number of bytes written/read since initialisation
*/
int mqc_numbytes();
/*
* Reset the states of all the context of the coder/decoder
* (each context is set to a state where 0 and 1 are more or less equiprobable)
*/
void mqc_resetstates();
/*
* Set the state of a particular context
* ctxno: number that identifies the context
* msb: the MSB of the new state of the context
* prob: number that identifies the probability of the symbols for the new state of the context
*/
void mqc_setstate(int ctxno, int msb, int prob);
/*
* Initialize the encoder
* bp: pointer to the start of the buffer where the bytes will be written
*/
void mqc_init_enc(unsigned char *bp);
/*
* Set the current context used for coding/decoding
* ctxno: number that identifies the context
*/
void mqc_setcurctx(int ctxno);
/*
* Encode a bit
* d: bit to encode (0 or 1)
*/
void mqc_encode(int d);
/*
* Flush the encoder, so that all remaining data is written
*/
void mqc_flush();
/*
* BYPASS mode switch
*/
void mqc_bypass_init_enc();
/*
* BYPASS mode switch
*/
void mqc_bypass_enc();
/*
* BYPASS mode switch
*/
int mqc_bypass_flush_enc();
/*
* RESET mode switch
*/
void mqc_reset_enc();
/*
* RESTART mode switch (TERMALL)
*/
int mqc_restart_enc();
/*
* RESTART mode switch (TERMALL)
*/
void mqc_restart_init_enc();
/*
* ERTERM mode switch (PTERM)
*/
void mqc_erterm_enc();
/*
* SEGMARK mode switch (SEGSYM)
*/
void mqc_segmark_enc();
/*
* Initialize the decoder
* bp: pointer to the start of the buffer from which the bytes will be read
* len: length of the input buffer
*/
void mqc_init_dec(unsigned char *bp, int len);
/*
* Decode a bit (returns 0 or 1)
*/
int mqc_decode();
#endif

48
libopenjpeg/openjpeg.h Normal file
View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* 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.
*/
#ifndef __OPENJPEG_H
#define __OPENJPEG_H
#include <j2k.h>
#include <tcd.h>
#include <bio.h>
#include <cio.h>
#include <dwt.h>
#include <fix.h>
#include <int.h>
#include <mct.h>
#include <mqc.h>
#include <pi.h>
#include <raw.h>
#include <t1.h>
#include <t2.h>
#include <tgt.h>
#endif

488
libopenjpeg/pi.c Normal file
View File

@ -0,0 +1,488 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* 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 "pi.h"
#include "int.h"
#include <stdlib.h>
#include <stdio.h>
/* <summary> */
/* Create a packet iterator. */
/* </summary> */
pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno)
{
int p, q;
int compno, resno, pino, layno, precno;
int maxres = 0;
pi_iterator_t *pi;
j2k_tcp_t *tcp;
j2k_tccp_t *tccp;
tcp = &cp->tcps[tileno];
pi =
(pi_iterator_t *) malloc((tcp->numpocs + 1) * sizeof(pi_iterator_t));
for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */
p = tileno % cp->tw;
q = tileno / cp->tw;
/* pi->tx0=int_max(cp->tx0+p*cp->tdx, img->x0);
pi->ty0=int_max(cp->ty0+q*cp->tdy, img->y0);
pi->tx1=int_min(cp->tx0+(p+1)*cp->tdx, img->x1);
pi->ty1=int_min(cp->ty0+(q+1)*cp->tdy, img->y1);
pi->numcomps=img->numcomps;
pi->comps=(pi_comp_t*)malloc(img->numcomps*sizeof(pi_comp_t)); */
pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
pi[pino].numcomps = img->numcomps;
pi[pino].comps =
(pi_comp_t *) malloc(img->numcomps * sizeof(pi_comp_t));
for (compno = 0; compno < pi->numcomps; compno++) {
int tcx0, tcy0, tcx1, tcy1;
/* pi_comp_t *comp=&pi->comps[compno]; */
pi_comp_t *comp = &pi[pino].comps[compno];
tccp = &tcp->tccps[compno];
comp->dx = img->comps[compno].dx;
comp->dy = img->comps[compno].dy;
comp->numresolutions = tccp->numresolutions;
comp->resolutions =
(pi_resolution_t *) malloc(comp->numresolutions *
sizeof(pi_resolution_t));
tcx0 = int_ceildiv(pi->tx0, comp->dx);
tcy0 = int_ceildiv(pi->ty0, comp->dy);
tcx1 = int_ceildiv(pi->tx1, comp->dx);
tcy1 = int_ceildiv(pi->ty1, comp->dy);
if (comp->numresolutions > maxres) {
maxres = comp->numresolutions;
}
for (resno = 0; resno < comp->numresolutions; resno++) {
int levelno;
int rx0, ry0, rx1, ry1;
int px0, py0, px1, py1;
pi_resolution_t *res = &comp->resolutions[resno];
if (tccp->csty & J2K_CCP_CSTY_PRT) {
res->pdx = tccp->prcw[resno];
res->pdy = tccp->prch[resno];
} else {
res->pdx = 15;
res->pdy = 15;
}
levelno = comp->numresolutions - 1 - resno;
rx0 = int_ceildivpow2(tcx0, levelno);
ry0 = int_ceildivpow2(tcy0, levelno);
rx1 = int_ceildivpow2(tcx1, levelno);
ry1 = int_ceildivpow2(tcy1, levelno);
px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
res->pw = (px1 - px0) >> res->pdx;
res->ph = (py1 - py0) >> res->pdy;
}
}
/* pi->first=1;
pi->poc.resno0=0;
pi->poc.compno0=0;
pi->poc.layno1=tcp->numlayers;
pi->poc.resno1=maxres;
pi->poc.compno1=img->numcomps;
pi->poc.prg=tcp->prg; */
for (layno = 0; layno < 10; layno++) {
/* pi_comp_t *compo; */
/* pi_resolution_t *res; */
for (resno = 0; resno < 10; resno++) {
for (compno = 0; compno < 3; compno++) {
/* compo=&pi[pino].comps[compno]; */
/* res=&compo->resolutions[pi->resno]; */
for (precno = 0; precno < 99; precno++) {
pi[pino].include[layno][resno][compno][precno] = 0;
}
}
}
}
if (pino == tcp->numpocs) {
pi[pino].first = 1;
pi[pino].poc.resno0 = 0;
pi[pino].poc.compno0 = 0;
pi[pino].poc.layno1 = tcp->numlayers;
pi[pino].poc.resno1 = maxres;
pi[pino].poc.compno1 = img->numcomps;
pi[pino].poc.prg = tcp->prg;
} else {
pi[pino].first = 1;
pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
pi[pino].poc.prg = tcp->pocs[pino].prg;
}
}
return pi;
}
/* <summary> */
/* Get next packet in layer=resolution-component-precinct order. */
/* </summary> */
int pi_next_lrcp(pi_iterator_t * pi)
{
pi_comp_t *comp;
pi_resolution_t *res;
if (!pi->first) {
comp = &pi->comps[pi->compno];
res = &comp->resolutions[pi->resno];
goto skip;
} else {
pi->first = 0;
}
for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
pi->resno++) {
for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;
pi->compno++) {
comp = &pi->comps[pi->compno];
if (pi->resno >= comp->numresolutions) {
continue;
}
res = &comp->resolutions[pi->resno];
for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) {
if (!pi->include[pi->layno][pi->resno][pi->compno][pi->precno]) {
pi->include[pi->layno][pi->resno][pi->compno][pi->precno] = 1;
return 1;
}
skip:;
}
}
}
}
return 0;
}
/* <summary> */
/* Get next packet in resolution-layer-component-precinct order. */
/* </summary> */
int pi_next_rlcp(pi_iterator_t * pi)
{
pi_comp_t *comp;
pi_resolution_t *res;
if (!pi->first) {
comp = &pi->comps[pi->compno];
res = &comp->resolutions[pi->resno];
goto skip;
} else {
pi->first = 0;
}
for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;
pi->compno++) {
comp = &pi->comps[pi->compno];
if (pi->resno >= comp->numresolutions) {
continue;
}
res = &comp->resolutions[pi->resno];
for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) {
if (!pi->include[pi->layno][pi->resno][pi->compno][pi->precno]) {
pi->include[pi->layno][pi->resno][pi->compno][pi->precno] = 1;
return 1;
}
skip:;
}
}
}
}
return 0;
}
/* <summary> */
/* Get next packet in resolution-precinct-component-layer order. */
/* </summary> */
int pi_next_rpcl(pi_iterator_t * pi)
{
pi_comp_t *comp;
pi_resolution_t *res;
if (!pi->first) {
goto skip;
} else {
int compno, resno;
pi->first = 0;
pi->dx = 0;
pi->dy = 0;
for (compno = 0; compno < pi->numcomps; compno++) {
comp = &pi->comps[compno];
for (resno = 0; resno < comp->numresolutions; resno++) {
int dx, dy;
res = &comp->resolutions[resno];
dx =
comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
dy =
comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
}
}
}
for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
for (pi->y = pi->ty0; pi->y < pi->ty1;
pi->y += pi->dy - (pi->y % pi->dy)) {
for (pi->x = pi->tx0; pi->x < pi->tx1;
pi->x += pi->dx - (pi->x % pi->dx)) {
for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;
pi->compno++) {
int levelno;
int trx0, try0;
int rpx, rpy;
int prci, prcj;
comp = &pi->comps[pi->compno];
if (pi->resno >= comp->numresolutions) {
continue;
}
res = &comp->resolutions[pi->resno];
levelno = comp->numresolutions - 1 - pi->resno;
trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
rpx = res->pdx + levelno;
rpy = res->pdy + levelno;
if (!
(pi->x % (comp->dx << rpx) == 0
|| (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {
continue;
}
if (!
(pi->y % (comp->dy << rpy) == 0
|| (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
continue;
}
prci =
int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno),
res->pdx) - int_floordivpow2(trx0, res->pdx);
prcj =
int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno),
res->pdy) - int_floordivpow2(try0, res->pdy);
pi->precno = prci + prcj * res->pw;
for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
if (!pi->include[pi->layno][pi->resno][pi->compno][pi->precno]) {
pi->include[pi->layno][pi->resno][pi->compno][pi->precno] =
1;
return 1;
}
skip:;
}
}
}
}
}
return 0;
}
/* <summary> */
/* Get next packet in precinct-component-resolution-layer order. */
/* </summary> */
int pi_next_pcrl(pi_iterator_t * pi)
{
pi_comp_t *comp;
pi_resolution_t *res;
if (!pi->first) {
comp = &pi->comps[pi->compno];
goto skip;
} else {
int compno, resno;
pi->first = 0;
pi->dx = 0;
pi->dy = 0;
for (compno = 0; compno < pi->numcomps; compno++) {
comp = &pi->comps[compno];
for (resno = 0; resno < comp->numresolutions; resno++) {
int dx, dy;
res = &comp->resolutions[resno];
dx =
comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
dy =
comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
}
}
}
for (pi->y = pi->ty0; pi->y < pi->ty1;
pi->y += pi->dy - (pi->y % pi->dy)) {
for (pi->x = pi->tx0; pi->x < pi->tx1;
pi->x += pi->dx - (pi->x % pi->dx)) {
for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;
pi->compno++) {
comp = &pi->comps[pi->compno];
for (pi->resno = pi->poc.resno0;
pi->resno < int_min(pi->poc.resno1, comp->numresolutions);
pi->resno++) {
int levelno;
int trx0, try0;
int rpx, rpy;
int prci, prcj;
res = &comp->resolutions[pi->resno];
levelno = comp->numresolutions - 1 - pi->resno;
trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
rpx = res->pdx + levelno;
rpy = res->pdy + levelno;
if (!
(pi->x % (comp->dx << rpx) == 0
|| (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {
continue;
}
if (!
(pi->y % (comp->dy << rpy) == 0
|| (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
continue;
}
prci =
int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno),
res->pdx) - int_floordivpow2(trx0, res->pdx);
prcj =
int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno),
res->pdy) - int_floordivpow2(try0, res->pdy);
pi->precno = prci + prcj * res->pw;
for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
if (!pi->include[pi->layno][pi->resno][pi->compno][pi->precno]) {
pi->include[pi->layno][pi->resno][pi->compno][pi->precno] =
1;
return 1;
}
skip:;
}
}
}
}
}
return 0;
}
/* <summary> */
/* Get next packet in component-precinct-resolution-layer order. */
/* </summary> */
int pi_next_cprl(pi_iterator_t * pi)
{
pi_comp_t *comp;
pi_resolution_t *res;
if (!pi->first) {
comp = &pi->comps[pi->compno];
goto skip;
} else {
pi->first = 0;
}
for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;
pi->compno++) {
int resno;
comp = &pi->comps[pi->compno];
pi->dx = 0;
pi->dy = 0;
for (resno = 0; resno < comp->numresolutions; resno++) {
int dx, dy;
res = &comp->resolutions[resno];
dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
}
for (pi->y = pi->ty0; pi->y < pi->ty1;
pi->y += pi->dy - (pi->y % pi->dy)) {
for (pi->x = pi->tx0; pi->x < pi->tx1;
pi->x += pi->dx - (pi->x % pi->dx)) {
for (pi->resno = pi->poc.resno0;
pi->resno < int_min(pi->poc.resno1, comp->numresolutions);
pi->resno++) {
int levelno;
int trx0, try0;
int rpx, rpy;
int prci, prcj;
res = &comp->resolutions[pi->resno];
levelno = comp->numresolutions - 1 - pi->resno;
trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
rpx = res->pdx + levelno;
rpy = res->pdy + levelno;
if (!
(pi->x % (comp->dx << rpx) == 0
|| (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {
continue;
}
if (!
(pi->y % (comp->dy << rpy) == 0
|| (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
continue;
}
prci =
int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno),
res->pdx) - int_floordivpow2(trx0, res->pdx);
prcj =
int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno),
res->pdy) - int_floordivpow2(try0, res->pdy);
pi->precno = prci + prcj * res->pw;
for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
if (!pi->include[pi->layno][pi->resno][pi->compno][pi->precno]) {
pi->include[pi->layno][pi->resno][pi->compno][pi->precno] =
1;
return 1;
}
skip:;
}
}
}
}
}
return 0;
}
/* <summary> */
/* Get next packet. */
/* </summary> */
int pi_next(pi_iterator_t * pi)
{
switch (pi->poc.prg) {
case 0:
return pi_next_lrcp(pi);
case 1:
return pi_next_rlcp(pi);
case 2:
return pi_next_rpcl(pi);
case 3:
return pi_next_pcrl(pi);
case 4:
return pi_next_cprl(pi);
}
return 0;
}

71
libopenjpeg/pi.h Normal file
View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2001-2002, David Janssens
* 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.
*/
#ifndef __PI_H
#define __PI_H
#include "j2k.h"
#include "tcd.h"
typedef struct {
int pdx, pdy;
int pw, ph;
} pi_resolution_t;
typedef struct {
int dx, dy;
int numresolutions;
pi_resolution_t *resolutions;
} pi_comp_t;
typedef struct {
int include[10][10][3][99];
int compno, resno, precno, layno; /* component, resolution, precinct and layer that indentify the packet */
int first;
j2k_poc_t poc;
int numcomps;
pi_comp_t *comps;
int tx0, ty0, tx1, ty1;
int x, y, dx, dy;
} pi_iterator_t; /* packet iterator */
/*
* Create a packet iterator
* img: raw image for which the packets will be listed
* cp: coding paremeters
* tileno: number that identifies the tile for which to list the packets
* return value: returns a packet iterator that points to the first packet of the tile
*/
pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno);
/*
* Modify the packet iterator to point to the next packet
* pi: packet iterator to modify
* return value: returns 0 if pi pointed to the last packet or else returns 1
*/
int pi_next(pi_iterator_t * pi);
#endif

129
libopenjpeg/raw.c Normal file
View File

@ -0,0 +1,129 @@
/*
* Copyright (c) 2002-2003, Antonin Descampe
* 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 "raw.h"
unsigned char raw_c;
unsigned int raw_ct, raw_lenmax, raw_len;
unsigned char *raw_bp;
unsigned char *raw_start;
unsigned char *raw_end;
/* <summary> */
/* Return the number of bytes already encoded. */
/* </summary> */
int raw_numbytes()
{
return raw_bp - raw_start;
}
/* <summary> */
/* Initialize raw-encoder. */
/* </summary> */
/* <param name="bp">Output buffer.</param> */
void raw_init_enc(unsigned char *bp)
{
raw_bp = bp - 1;
raw_c = 0;
raw_ct = 7;
raw_start = bp;
}
/* <summary> */
/* Encode a symbol using the RAW-coder. */
/* </summary> */
/* <param name="d"> The symbol to be encoded (0 or 1).</param> */
void raw_encode(int d)
{
/* raw_c+=d; */
raw_ct--;
raw_c += (d << raw_ct);
if (raw_ct == 0) {
raw_bp++;
*raw_bp = raw_c;
raw_ct = 7;
if (raw_c == 0xff) {
raw_ct = 6;
}
raw_c = 0;
}
/*else
{
raw_ct--;
raw_c<<=1;
} */
}
/* <summary> */
/* Flush encoded data. */
/* </summary> */
void raw_flush()
{
char first = 1;
int prev = 1;
while (raw_ct != 7) {
raw_encode(first ? 0 : !(prev));
prev = first ? 0 : !(prev);
first = 0;
}
}
/* <summary> */
/* Initialize raw-decoder. */
/* </summary> */
void raw_init_dec(unsigned char *bp, int len)
{
raw_start = bp;
raw_lenmax = len;
raw_len = 0;
raw_c = 0;
raw_ct = 0;
}
/* <summary> */
/* Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN */
/* </summary> */
int raw_decode()
{
int d;
if (raw_ct == 0) {
raw_ct = 8;
if (raw_len == raw_lenmax)
raw_c = 0xff;
else {
if (raw_c == 0xff)
raw_ct = 7;
raw_c = *(raw_start + raw_len);
raw_len++;
}
}
raw_ct--;
d = (raw_c >> raw_ct) & 0x01;
return d;
}

72
libopenjpeg/raw.h Normal file
View File

@ -0,0 +1,72 @@
/*
* Copyright (c) 2002-2003, Antonin Descampe
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* 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.
*/
#ifndef __RAW_H
#define __RAW_H
/*
* Return the number of bytes written/read since initialisation
*/
int raw_numbytes();
/*
* Initialize the encoder
* bp: pointer to the start of the buffer where the bytes will be written
*/
void raw_init_enc(unsigned char *bp);
/*
* Encode a bit
* d: bit to encode (0 or 1)
*/
void raw_encode(int d);
/*
* Flush the encoder, so that all remaining data is written
*/
void raw_flush();
/*
* Initialize the decoder
* bp: pointer to the start of the buffer from which the bytes will be read
* len: length of the input buffer
*/
void raw_init_dec(unsigned char *bp, int len);
/*
* Decode a bit (returns 0 or 1)
*/
int raw_decode();
#endif /*
*/

1077
libopenjpeg/t1.c Normal file

File diff suppressed because it is too large Load Diff

52
libopenjpeg/t1.h Normal file
View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2001-2002, David Janssens
* 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.
*/
#ifndef __T1_H
#define __T1_H
#include "tcd.h"
#include "j2k.h"
/*
* Initialize the look-up tables of the Tier-1 coder/decoder
*/
void t1_init_luts();
/*
* Encode the code-blocks of a tile
* tile: the tile to encode
* tcp: tile coding parameters
*/
void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp);
/*
* Decode the code-blocks of a tile
* tile: the tile to encode
* tcp: tile coding parameters
*/
void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp);
#endif

505
libopenjpeg/t2.c Normal file
View File

@ -0,0 +1,505 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* 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 "t2.h"
#include "tcd.h"
#include "bio.h"
#include "j2k.h"
#include "pi.h"
#include "tgt.h"
#include "int.h"
#include "cio.h"
#include <stdio.h>
#include <setjmp.h>
#include <string.h>
#include <stdlib.h>
#define RESTART 0x04
extern jmp_buf j2k_error;
void t2_putcommacode(int n)
{
while (--n >= 0) {
bio_write(1, 1);
}
bio_write(0, 1);
}
int t2_getcommacode()
{
int n;
for (n = 0; bio_read(1); n++) {
}
return n;
}
/* <summary> */
/* Variable length code for signalling delta Zil (truncation point) */
/* <val> n : delta Zil */
/* <\summary> */
void t2_putnumpasses(int n)
{
if (n == 1) {
bio_write(0, 1);
} else if (n == 2) {
bio_write(2, 2);
} else if (n <= 5) {
bio_write(0xc | (n - 3), 4);
} else if (n <= 36) {
bio_write(0x1e0 | (n - 6), 9);
} else if (n <= 164) {
bio_write(0xff80 | (n - 37), 16);
}
}
int t2_getnumpasses()
{
int n;
if (!bio_read(1))
return 1;
if (!bio_read(1))
return 2;
if ((n = bio_read(2)) != 3)
return 3 + n;
if ((n = bio_read(5)) != 31)
return 6 + n;
return 37 + bio_read(7);
}
int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
int resno, int precno, int layno, unsigned char *dest,
int len, info_image * info_IM, int tileno)
{
int bandno, cblkno;
unsigned char *sop = 0, *eph = 0;
tcd_tilecomp_t *tilec = &tile->comps[compno];
tcd_resolution_t *res = &tilec->resolutions[resno];
unsigned char *c = dest;
/* int PPT=tile->PPT, ppt_len=0; */
/* FILE *PPT_file; */
/* <SOP 0xff91> */
if (tcp->csty & J2K_CP_CSTY_SOP) {
sop = (unsigned char *) malloc(6 * sizeof(unsigned char));
sop[0] = 255;
sop[1] = 145;
sop[2] = 0;
sop[3] = 4;
sop[4] = (info_IM->num % 65536) / 256;
sop[5] = (info_IM->num % 65536) % 256;
memcpy(c, sop, 6);
free(sop);
c += 6;
}
/* </SOP> */
if (!layno) {
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
tgt_reset(prc->incltree);
tgt_reset(prc->imsbtree);
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
tcd_cblk_t *cblk = &prc->cblks[cblkno];
cblk->numpasses = 0;
tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
}
}
}
bio_init_enc(c, len);
bio_write(1, 1); /* Empty header bit */
/* Writing Packet header */
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
tcd_cblk_t *cblk = &prc->cblks[cblkno];
tcd_layer_t *layer = &cblk->layers[layno];
if (!cblk->numpasses && layer->numpasses) {
tgt_setvalue(prc->incltree, cblkno, layno);
}
}
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
tcd_cblk_t *cblk = &prc->cblks[cblkno];
tcd_layer_t *layer = &cblk->layers[layno];
int increment = 0;
int nump = 0;
int len = 0, passno;
/* cblk inclusion bits */
if (!cblk->numpasses) {
tgt_encode(prc->incltree, cblkno, layno + 1);
} else {
bio_write(layer->numpasses != 0, 1);
}
/* if cblk not included, go to the next cblk */
if (!layer->numpasses) {
continue;
}
/* if first instance of cblk --> zero bit-planes information */
if (!cblk->numpasses) {
cblk->numlenbits = 3;
tgt_encode(prc->imsbtree, cblkno, 999);
}
/* number of coding passes included */
t2_putnumpasses(layer->numpasses);
/* computation of the increase of the length indicator and insertion in the header */
for (passno = cblk->numpasses;
passno < cblk->numpasses + layer->numpasses; passno++) {
tcd_pass_t *pass = &cblk->passes[passno];
nump++;
len += pass->len;
if (pass->term
|| passno == (cblk->numpasses + layer->numpasses) - 1) {
increment =
int_max(increment,
int_floorlog2(len) + 1 - (cblk->numlenbits +
int_floorlog2(nump)));
len = 0;
nump = 0;
}
}
t2_putcommacode(increment);
/* computation of the new Length indicator */
cblk->numlenbits += increment;
/* insertion of the codeword segment length */
for (passno = cblk->numpasses;
passno < cblk->numpasses + layer->numpasses; passno++) {
tcd_pass_t *pass = &cblk->passes[passno];
nump++;
len += pass->len;
if (pass->term
|| passno == (cblk->numpasses + layer->numpasses) - 1) {
bio_write(len, cblk->numlenbits + int_floorlog2(nump));
len = 0;
nump = 0;
}
}
}
}
if (bio_flush())
return -999; /* modified to eliminate longjmp !! */
c += bio_numbytes();
/* <EPH 0xff92> */
if (tcp->csty & J2K_CP_CSTY_EPH) {
eph = (unsigned char *) malloc(2 * sizeof(unsigned char));
eph[0] = 255;
eph[1] = 146;
memcpy(c, eph, 2);
free(eph);
c += 2;
}
/* </EPH> */
/* } */
/* Writing the packet body */
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
tcd_cblk_t *cblk = &prc->cblks[cblkno];
tcd_layer_t *layer = &cblk->layers[layno];
if (!layer->numpasses) { /* ADD for index Cfr. Marcela --> delta disto by packet */
if (info_IM->index_write && info_IM->index_on) {
info_tile *info_TL = &info_IM->tile[tileno];
info_packet *info_PK = &info_TL->packet[info_IM->num];
info_PK->disto = layer->disto;
if (info_IM->D_max < info_PK->disto)
info_IM->D_max = info_PK->disto;
} /* </ADD> */
continue;
}
if (c + layer->len > dest + len) {
return -999;
}
memcpy(c, layer->data, layer->len);
cblk->numpasses += layer->numpasses;
c += layer->len;
/* ADD for index Cfr. Marcela --> delta disto by packet */
if (info_IM->index_write && info_IM->index_on) {
info_tile *info_TL = &info_IM->tile[tileno];
info_packet *info_PK = &info_TL->packet[info_IM->num];
info_PK->disto = layer->disto;
if (info_IM->D_max < info_PK->disto)
info_IM->D_max = info_PK->disto;
} /* </ADD> */
}
}
return c - dest;
}
void t2_init_seg(tcd_seg_t * seg, int cblksty, int first)
{
seg->numpasses = 0;
seg->len = 0;
if (cblksty & J2K_CCP_CBLKSTY_TERMALL)
seg->maxpasses = 1;
else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
if (first)
seg->maxpasses = 10;
else
seg->maxpasses = (((seg - 1)->maxpasses == 1)
|| ((seg - 1)->maxpasses == 10)) ? 2 : 1;
} else
seg->maxpasses = 109;
}
int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile,
j2k_tcp_t * tcp, int compno, int resno, int precno,
int layno)
{
int bandno, cblkno;
tcd_tilecomp_t *tilec = &tile->comps[compno];
tcd_resolution_t *res = &tilec->resolutions[resno];
unsigned char *c = src;
int present;
if (layno == 0) {
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
tgt_reset(prc->incltree);
tgt_reset(prc->imsbtree);
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
tcd_cblk_t *cblk = &prc->cblks[cblkno];
cblk->numsegs = 0;
}
}
}
if (tcp->csty & J2K_CP_CSTY_SOP) {
c += 6;
}
bio_init_dec(c, src + len - c);
present = bio_read(1);
if (!present) {
bio_inalign();
c += bio_numbytes();
return c - src;
}
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
int included, increment, n;
tcd_cblk_t *cblk = &prc->cblks[cblkno];
tcd_seg_t *seg;
/* if cblk not yet included before --> inclusion tagtree */
if (!cblk->numsegs) {
included = tgt_decode(prc->incltree, cblkno, layno + 1);
/* else one bit */
} else {
included = bio_read(1);
}
/* if cblk not included */
if (!included) {
cblk->numnewpasses = 0;
continue;
}
/* if cblk not yet included --> zero-bitplane tagtree */
if (!cblk->numsegs) {
int i, numimsbs;
for (i = 0; !tgt_decode(prc->imsbtree, cblkno, i); i++) {
}
numimsbs = i - 1;
cblk->numbps = band->numbps - numimsbs;
cblk->numlenbits = 3;
}
/* number of coding passes */
cblk->numnewpasses = t2_getnumpasses();
increment = t2_getcommacode();
/* length indicator increment */
cblk->numlenbits += increment;
if (!cblk->numsegs) {
seg = &cblk->segs[0];
t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
} else {
seg = &cblk->segs[cblk->numsegs - 1];
if (seg->numpasses == seg->maxpasses) {
t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
}
}
n = cblk->numnewpasses;
do {
seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
seg->newlen =
bio_read(cblk->numlenbits + int_floorlog2(seg->numnewpasses));
n -= seg->numnewpasses;
if (n > 0) {
t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
}
} while (n > 0);
}
}
if (bio_inalign())
return -999;
c += bio_numbytes();
if (tcp->csty & J2K_CP_CSTY_EPH) {
c += 2;
}
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
tcd_cblk_t *cblk = &prc->cblks[cblkno];
tcd_seg_t *seg;
if (!cblk->numnewpasses)
continue;
if (!cblk->numsegs) {
seg = &cblk->segs[cblk->numsegs++];
cblk->len = 0;
} else {
seg = &cblk->segs[cblk->numsegs - 1];
if (seg->numpasses == seg->maxpasses) {
seg++;
cblk->numsegs++;
}
}
do {
if (c + seg->newlen > src + len)
return -999;
memcpy(cblk->data + cblk->len, c, seg->newlen);
if (seg->numpasses == 0) {
seg->data = cblk->data + cblk->len;
}
c += seg->newlen;
cblk->len += seg->newlen;
seg->len += seg->newlen;
seg->numpasses += seg->numnewpasses;
cblk->numnewpasses -= seg->numnewpasses;
if (cblk->numnewpasses > 0) {
seg++;
cblk->numsegs++;
}
} while (cblk->numnewpasses > 0);
}
}
return c - src;
}
int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
tcd_tile_t * tile, int maxlayers,
unsigned char *dest, int len, info_image * info_IM)
{
unsigned char *c = dest;
int e = 0;
pi_iterator_t *pi;
int pino, compno;
pi = pi_create(img, cp, tileno);
for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
while (pi_next(&pi[pino])) {
if (pi[pino].layno < maxlayers) {
e =
t2_encode_packet(tile, &cp->tcps[tileno], pi[pino].compno,
pi[pino].resno, pi[pino].precno, pi[pino].layno,
c, dest + len - c, info_IM, tileno);
if (e == -999) {
break;
} else
c += e;
/* INDEX >> */
if (info_IM->index_write && info_IM->index_on) {
info_tile *info_TL = &info_IM->tile[tileno];
info_packet *info_PK = &info_TL->packet[info_IM->num];
if (!info_IM->num) {
info_PK->start_pos = info_TL->end_header + 1;
} else {
info_PK->start_pos =
info_TL->packet[info_IM->num - 1].end_pos + 1;
}
info_PK->end_pos = info_PK->start_pos + e - 1;
}
/* << INDEX */
if ((info_IM->index_write
&& cp->tcps[tileno].csty & J2K_CP_CSTY_SOP)
|| (info_IM->index_write && info_IM->index_on)) {
info_IM->num++;
}
}
}
/* FREE space memory taken by pi */
for (compno = 0; compno < pi[pino].numcomps; compno++) {
free(pi[pino].comps[compno].resolutions);
}
free(pi[pino].comps);
}
free(pi);
if (e == -999)
return e;
else
return c - dest;
}
int t2_decode_packets(unsigned char *src, int len, j2k_image_t * img,
j2k_cp_t * cp, int tileno, tcd_tile_t * tile)
{
unsigned char *c = src;
pi_iterator_t *pi;
int pino, compno, e = 0;
pi = pi_create(img, cp, tileno);
for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
while (pi_next(&pi[pino])) {
e =
t2_decode_packet(c, src + len - c, tile, &cp->tcps[tileno],
pi[pino].compno, pi[pino].resno, pi[pino].precno,
pi[pino].layno);
if (e == -999) { /* ADD */
break;
} else
c += e;
}
/* FREE space memory taken by pi */
for (compno = 0; compno < pi[pino].numcomps; compno++) {
free(pi[pino].comps[compno].resolutions);
}
free(pi[pino].comps);
}
free(pi);
if (e == -999)
return e;
else
return c - src;
}

59
libopenjpeg/t2.h Normal file
View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* 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.
*/
#ifndef __T2_H
#define __T2_H
#include "tcd.h"
#include "j2k.h"
/*
* Encode the packets of a tile to a destination buffer
* img: the source image
* cp: the image coding parameters
* tile: the tile for which to write the packets
* dest: the destination buffer
* len: the length of the destination buffer
*/
int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
tcd_tile_t * tile, int maxlayers,
unsigned char *dest, int len, info_image * info_IM);
/*
* Decode the packets of a tile from a source buffer
* src: the source buffer
* len: length of the source buffer
* img: destination image
* cp: image coding parameters
* tileno: number that identifies the tile for which to decode the packets
* tile: tile for which to decode the packets
*/
int t2_decode_packets(unsigned char *src, int len, j2k_image_t * img,
j2k_cp_t * cp, int tileno, tcd_tile_t * tile);
#endif

1637
libopenjpeg/tcd.c Normal file

File diff suppressed because it is too large Load Diff

159
libopenjpeg/tcd.h Normal file
View File

@ -0,0 +1,159 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* 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.
*/
#ifndef __TCD_H
#define __TCD_H
#include "j2k.h"
#include "tgt.h"
typedef struct {
int numpasses;
int len;
unsigned char *data;
int maxpasses;
int numnewpasses;
int newlen;
} tcd_seg_t;
typedef struct {
int rate;
double distortiondec;
int term, len;
} tcd_pass_t;
typedef struct {
int numpasses;
int len;
double disto; /* add for index (Cfr. Marcela) */
unsigned char *data;
} tcd_layer_t;
typedef struct {
int x0, y0, x1, y1;
int numbps;
int numlenbits;
int len;
int numpasses;
int numnewpasses;
int numsegs;
tcd_seg_t segs[100];
unsigned char data[8192];
int numpassesinlayers;
tcd_layer_t layers[100];
int totalpasses;
tcd_pass_t passes[100];
} tcd_cblk_t;
typedef struct {
int x0, y0, x1, y1;
int cw, ch;
tcd_cblk_t *cblks;
tgt_tree_t *incltree;
tgt_tree_t *imsbtree;
} tcd_precinct_t;
typedef struct {
int x0, y0, x1, y1;
int bandno;
tcd_precinct_t *precincts;
int numbps;
int stepsize;
} tcd_band_t;
typedef struct {
int x0, y0, x1, y1;
int previous_x0, previous_y0, previous_x1, previous_y1; /* usefull for the DWT */
int cas_col, cas_row; /* usefull for the DWT */
int pw, ph; /* , old_pw,old_ph, old_pw_max,old_ph_max; */
int numbands;
tcd_band_t bands[3];
} tcd_resolution_t;
typedef struct {
int x0, y0, x1, y1;
int previous_row, previous_col; /* usefull for the DWT */
int numresolutions;
tcd_resolution_t *resolutions;
int *data;
} tcd_tilecomp_t;
typedef struct {
int x0, y0, x1, y1;
int numcomps;
/* int PPT; */
/* int len_ppt; */
tcd_tilecomp_t *comps;
} tcd_tile_t;
typedef struct {
int tw, th;
tcd_tile_t *tiles;
} tcd_image_t;
/*
* Initialize the tile coder/decoder
* img: raw image
* cp: coding parameters
* info_IM: creation of index file
*/
void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
void tcd_init(j2k_image_t * img, j2k_cp_t * cp);
void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
/*
* Encode a tile from the raw image into a buffer, format pnm, pgm or ppm
* tileno: number that identifies one of the tiles to be encoded
* dest: destination buffer
* len: length of destination buffer
*/
int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
info_image * info_IM);
/*
* Encode a tile from the raw image into a buffer, format pgx
* tileno: number that identifies one of the tiles to be encoded
* dest: destination buffer
* len: length of destination buffer
*/
int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
info_image * info_IM);
/*
* Decode a tile from a buffer into a raw image
* src: source buffer
* len: length of the source buffer
* tileno: number that identifies the tile that will be decoded
*/
int tcd_decode_tile(unsigned char *src, int len, int tileno);
#endif

225
libopenjpeg/tgt.c Normal file
View File

@ -0,0 +1,225 @@
/*
* Copyright (c) 2001-2002, David Janssens
* 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 "tgt.h"
#include "bio.h"
#include <stdlib.h>
#include <stdio.h>
/* <summary> */
/* Reset tag-tree. */
/* </summary> */
void tgt_reset(tgt_tree_t * tree)
{
int i;
/* new */
if (!tree || tree == NULL)
return;
for (i = 0; i < tree->numnodes; i++) {
tree->nodes[i].value = 999;
tree->nodes[i].low = 0;
tree->nodes[i].known = 0;
}
}
/* <summary> */
/* Create tag-tree. */
/* </summary> */
tgt_tree_t *tgt_create(int numleafsh, int numleafsv)
{
int nplh[32];
int nplv[32];
tgt_node_t *node;
tgt_node_t *parentnode;
tgt_node_t *parentnode0;
tgt_tree_t *tree;
int i, j, k;
int numlvls;
int n;
tree = (tgt_tree_t *) malloc(sizeof(tgt_tree_t));
tree->numleafsh = numleafsh;
tree->numleafsv = numleafsv;
numlvls = 0;
nplh[0] = numleafsh;
nplv[0] = numleafsv;
tree->numnodes = 0;
do {
n = nplh[numlvls] * nplv[numlvls];
nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;
nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;
tree->numnodes += n;
++numlvls;
} while (n > 1);
/* ADD */
if (tree->numnodes == 0) {
free(tree);
return NULL;
}
tree->nodes = (tgt_node_t *) malloc(tree->numnodes * sizeof(tgt_node_t));
node = tree->nodes;
parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv];
parentnode0 = parentnode;
for (i = 0; i < numlvls - 1; ++i) {
for (j = 0; j < nplv[i]; ++j) {
k = nplh[i];
while (--k >= 0) {
node->parent = parentnode;
++node;
if (--k >= 0) {
node->parent = parentnode;
++node;
}
++parentnode;
}
if ((j & 1) || j == nplv[i] - 1) {
parentnode0 = parentnode;
} else {
parentnode = parentnode0;
parentnode0 += nplh[i];
}
}
}
node->parent = 0;
tgt_reset(tree);
return tree;
}
/* <summary> */
/* Destroy tag-tree. */
/* </summary> */
void tgt_destroy(tgt_tree_t * t)
{
free(t->nodes);
free(t);
}
/* <summary> */
/* Set the value of a leaf of the tag-tree. */
/* </summary> */
void tgt_setvalue(tgt_tree_t * tree, int leafno, int value)
{
tgt_node_t *node;
node = &tree->nodes[leafno];
while (node && node->value > value) {
node->value = value;
node = node->parent;
}
}
/* <summary> */
/* Encode the value of a leaf of the tag-tree. */
/* </summary> */
void tgt_encode(tgt_tree_t * tree, int leafno, int threshold)
{
tgt_node_t *stk[31];
tgt_node_t **stkptr;
tgt_node_t *node;
int low;
stkptr = stk;
node = &tree->nodes[leafno];
while (node->parent) {
*stkptr++ = node;
node = node->parent;
}
low = 0;
for (;;) {
if (low > node->low) {
node->low = low;
} else {
low = node->low;
}
while (low < threshold) {
if (low >= node->value) {
if (!node->known) {
bio_write(1, 1);
node->known = 1;
}
break;
}
bio_write(0, 1);
++low;
}
node->low = low;
if (stkptr == stk)
break;
node = *--stkptr;
}
}
/* <summary> */
/* Decode the value of a leaf of the tag-tree. */
/* </summary> */
int tgt_decode(tgt_tree_t * tree, int leafno, int threshold)
{
tgt_node_t *stk[31];
tgt_node_t **stkptr;
tgt_node_t *node;
int low;
stkptr = stk;
node = &tree->nodes[leafno];
while (node->parent) {
*stkptr++ = node;
node = node->parent;
}
low = 0;
for (;;) {
if (low > node->low) {
node->low = low;
} else {
low = node->low;
}
while (low < threshold && low < node->value) {
if (bio_read(1)) {
node->value = low;
} else {
++low;
}
}
node->low = low;
if (stkptr == stk) {
break;
}
node = *--stkptr;
}
return (node->value < threshold) ? 1 : 0;
}

85
libopenjpeg/tgt.h Normal file
View File

@ -0,0 +1,85 @@
/*
* Copyright (c) 2001-2002, David Janssens
* 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.
*/
#ifndef __TGT_H
#define __TGT_H
typedef struct tgt_node {
struct tgt_node *parent;
int value;
int low;
int known;
} tgt_node_t;
typedef struct {
int numleafsh;
int numleafsv;
int numnodes;
tgt_node_t *nodes;
} tgt_tree_t;
/*
* Create a tag-tree
* numleafsh: width of the array of leafs of the tree
* numleafsv: height of the array of leafs of the tree
*/
tgt_tree_t *tgt_create(int numleafsh, int numleafsv);
/*
* Reset a tag-tree (set all leafs to 0)
* tree: tag-tree to reset
*/
void tgt_reset(tgt_tree_t * tree);
/*
* Destroy a tag-tree, liberating memory
* tree: tag-tree to destroy
*/
void tgt_destroy(tgt_tree_t * tree);
/*
* Set the value of a leaf of a tag-tree
* tree: tag-tree to modify
* leafno: number that identifies the leaf to modify
* value: new value of the leaf
*/
void tgt_setvalue(tgt_tree_t * tree, int leafno, int value);
/*
* Encode the value of a leaf of the tag-tree up to a given threshold
* leafno: number that identifies the leaf to encode
* threshold: threshold to use when encoding value of the leaf
*/
void tgt_encode(tgt_tree_t * tree, int leafno, int threshold);
/*
* Decode the value of a leaf of the tag-tree up to a given threshold
* leafno: number that identifies the leaf to decode
* threshold: threshold to use when decoding value of the leaf
*/
int tgt_decode(tgt_tree_t * tree, int leafno, int threshold);
#endif