Update for version 0.8

This commit is contained in:
Yannick Verschueren 2004-02-13 09:47:40 +00:00
parent 4be829988a
commit bb349b2cee
21 changed files with 1895 additions and 2370 deletions

View File

@ -30,7 +30,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
#ifndef DONT_HAVE_GETOPT
#include <getopt.h>
@ -176,32 +175,37 @@ void help_display()
int give_progression(char progression[4])
{
if (progression[0] == 'L' && progression[1] == 'R'
&& progression[2] == 'C' && progression[3] == 'P') {
return 0;
} else {
if (progression[0] == 'R' && progression[1] == 'L'
&& progression[2] == 'C' && progression[3] == 'P') {
return 1;
} else {
if (progression[0] == 'R' && progression[1] == 'P'
&& progression[2] == 'C' && progression[3] == 'L') {
return 2;
} else {
if (progression[0] == 'P' && progression[1] == 'C'
&& progression[2] == 'R' && progression[3] == 'L') {
return 3;
} else {
if (progression[0] == 'C' && progression[1] == 'P'
&& progression[2] == 'R' && progression[3] == 'L') {
return 4;
} else {
return -1;
}
}
if (progression[0] == 'L' && progression[1] == 'R' && progression[2] == 'C' && progression[3] == 'P')
{
return 0;
} else
{
if (progression[0] == 'R' && progression[1] == 'L' && progression[2] == 'C' && progression[3] == 'P')
{
return 1;
} else
{
if (progression[0] == 'R' && progression[1] == 'P' && progression[2] == 'C' && progression[3] == 'L')
{
return 2;
} else
{
if (progression[0] == 'P' && progression[1] == 'C' && progression[2] == 'R' && progression[3] == 'L')
{
return 3;
} else
{
if (progression[0] == 'C' && progression[1] == 'P' && progression[2] == 'R' && progression[3] == 'L')
{
return 4;
} else
{
return -1;
}
}
}
}
}
}
}
}
double dwt_norms_97[4][10] = {
@ -240,42 +244,38 @@ void calc_explicit_stepsizes(j2k_tccp_t * tccp, int prec)
resno = bandno == 0 ? 0 : (bandno - 1) / 3 + 1;
orient = bandno == 0 ? 0 : (bandno - 1) % 3 + 1;
level = tccp->numresolutions - 1 - resno;
gain =
tccp->qmfbid == 0 ? 0 : (orient ==
0 ? 0 : (orient == 1
|| orient == 2 ? 1 : 2));
gain = tccp->qmfbid == 0 ? 0 : (orient == 0 ? 0 : (orient == 1 || orient == 2 ? 1 : 2));
if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
stepsize = 1.0;
} else {
double norm = dwt_norms_97[orient][level];
stepsize = (1 << (gain + 1)) / norm;
}
encode_stepsize((int) floor(stepsize * 8192.0), prec + gain,
&tccp->stepsizes[bandno].expn,
&tccp->stepsizes[bandno].mant);
encode_stepsize((int) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno].expn,
&tccp->stepsizes[bandno].mant);
}
}
int main(int argc, char **argv)
{
int len;
int NumResolution, numD_min; /* NumResolution : number of resolution */
int Tile_arg; /* Tile_arg = 0 (pas en argument ou = 1 (entre en argument) */
int CSty; /* CSty : coding style */
int Prog_order; /* progression order (default LRCP) */
int NumResolution, numD_min; /* NumResolution : number of resolution */
int Tile_arg; /* Tile_arg = 0 (not in argument) ou = 1 (in argument) */
int CSty; /* CSty : coding style */
int Prog_order; /* progression order (default LRCP) */
char progression[4];
int numpocs, numpocs_tile; /* Number of progression order change (POC) default 0 */
int prcw_init, prch_init; /* Initialisation precincts' size */
int cblockw_init, cblockh_init; /* Initialisation codeblocks' size */
int mode, value; /* mode switch (cblk_style) */
int subsampling_dx, subsampling_dy; /* subsampling value for dx and dy */
int ROI_compno, ROI_shift; /* region of interrest */
int Dim[2]; /* portion of the image coded */
int TX0, TY0; /* tile off-set */
int numpocs, numpocs_tile; /* Number of progression order change (POC) default 0 */
int prcw_init, prch_init; /* Initialisation precincts' size */
int cblockw_init, cblockh_init; /* Initialisation codeblocks' size */
int mode, value; /* Mode switch (cblk_style) */
int subsampling_dx, subsampling_dy; /* subsampling value for dx and dy */
int ROI_compno, ROI_shift; /* region of interrest */
int Dim[2]; /* portion of the image coded */
int TX0, TY0; /* tile off-set */
j2k_image_t img;
j2k_cp_t cp, cp_init; /* cp_init is used to initialise in multiple tiles */
j2k_tcp_t *tcp, *tcp_init; /* tcp_init is used to initialise in multiple tile */
j2k_poc_t POC[32]; /* POC : used in case of Progression order change */
j2k_cp_t cp, cp_init; /* cp_init is used to initialise in multiple tiles */
j2k_tcp_t *tcp, *tcp_init; /* tcp_init is used to initialise in multiple tile */
j2k_poc_t POC[32]; /* POC : used in case of Progression order change */
j2k_poc_t *tcp_poc;
j2k_tccp_t *tccp;
int i, tileno, j;
@ -299,7 +299,7 @@ int main(int argc, char **argv)
mode = 0;
subsampling_dx = 1;
subsampling_dy = 1;
ROI_compno = -1; /* no ROI */
ROI_compno = -1; /* no ROI */
ROI_shift = 0;
Dim[0] = 0;
Dim[1] = 0;
@ -321,52 +321,44 @@ int main(int argc, char **argv)
if (c == -1)
break;
switch (c) {
case 'i': /* IN fill */
case 'i': /* IN fill */
infile = optarg;
s = optarg;
while (*s && *s != '.') {
while (*s) {
s++;
}
s++;
S1 = *s;
s++;
S2 = *s;
s++;
s--;
S3 = *s;
s--;
S2 = *s;
s--;
S1 = *s;
if ((S1 == 'p' && S2 == 'g' && S3 == 'x')
|| (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
if ((S1 == 'p' && S2 == 'g' && S3 == 'x') || (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
cp.image_type = 0;
break;
}
if ((S1 == 'p' && S2 == 'n' && S3 == 'm')
|| (S1 == 'P' && S2 == 'N' && S3 == 'M') || (S1 == 'p'
&& S2 == 'g'
&& S3 == 'm')
|| (S1 == 'P' && S2 == 'G' && S3 == 'M') || (S1 == 'P'
&& S2 == 'P'
&& S3 == 'M')
|| (S1 == 'p' && S2 == 'p' && S3 == 'm')) {
if ((S1 == 'p' && S2 == 'n' && S3 == 'm')|| (S1 == 'P' && S2 == 'N' && S3 == 'M')
|| (S1 == 'p' && S2 == 'g' && S3 == 'm') || (S1 == 'P' && S2 == 'G' && S3 == 'M')
|| (S1 == 'P' && S2 == 'P' && S3 == 'M') || (S1 == 'p' && S2 == 'p' && S3 == 'm')) {
cp.image_type = 1;
break;
}
if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
|| (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
if ((S1 == 'b' && S2 == 'm' && S3 == 'p') || (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
cp.image_type = 2;
break;
}
fprintf(stderr,
"!! Unrecognized format for infile [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n");
fprintf(stderr, "!! Unrecognized format for infile : %c%c%c [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n",S1,S2,S3);
return 1;
break;
/* ----------------------------------------------------- */
case 'o': /* OUT fill */
case 'o': /* OUT fill */
outfile = optarg;
break;
/* ----------------------------------------------------- */
case 'r': /* rates */
case 'r': /* rates rates/distorsion*/
s = optarg;
while (sscanf(s, "%d", &tcp_init->rates[tcp_init->numlayers]) == 1) {
tcp_init->numlayers++;
@ -378,88 +370,109 @@ int main(int argc, char **argv)
s++;
}
cp.disto_alloc = 1;
cp.matrice = NULL;
break;
/* ----------------------------------------------------- */
case 'q': /* rates */
s = optarg;
sscanf(s, "%d", &tcp_init->numlayers);
for (i = 0; i < tcp_init->numlayers; i++) {
tcp_init->rates[i] = 1;
case 'q': /* rates fixed */
s=optarg;
sscanf(s, "%d",&tcp_init->numlayers);
s++;
if (tcp_init->numlayers>9) s++;
cp.matrice=(int*)malloc(tcp_init->numlayers*NumResolution*3*sizeof(int));
s=s+2;
for(i=0;i<tcp_init->numlayers;i++)
{
tcp_init->rates[i]=1;
sscanf(s, "%d,", &cp.matrice[i*NumResolution*3]);
s+=2;
if (cp.matrice[i*NumResolution*3]>9) s++;
cp.matrice[i*NumResolution*3+1]=0;
cp.matrice[i*NumResolution*3+2]=0;
for (j=1;j<NumResolution;j++)
{
sscanf(s, "%d,%d,%d", &cp.matrice[i*NumResolution*3+j*3+0],&cp.matrice[i*NumResolution*3+j*3+1],&cp.matrice[i*NumResolution*3+j*3+2]);
s+=6;
if (cp.matrice[i*NumResolution*3+j*3]>9) s++;
if (cp.matrice[i*NumResolution*3+j*3+1]>9) s++;
if (cp.matrice[i*NumResolution*3+j*3+2]>9) s++;
}
cp.fixed_alloc = 1;
break;
/* ----------------------------------------------------- */
case 't': /* tiles */
if (i<tcp_init->numlayers-1) s++;
}
cp.fixed_alloc=1;
break;
/* ----------------------------------------------------- */
case 't': /* tiles */
sscanf(optarg, "%d,%d", &cp.tdx, &cp.tdy);
Tile_arg = 1;
break;
/* ----------------------------------------------------- */
case 'n': /* resolution */
case 'n': /* resolution */
sscanf(optarg, "%d", &NumResolution);
break;
/* ----------------------------------------------------- */
case 'c': /* precinct dimension */
case 'c': /* precinct dimension */
sscanf(optarg, "%d,%d", &prcw_init, &prch_init);
CSty |= 0x01;
break;
/* ----------------------------------------------------- */
case 'b': /* code-block dimension */
case 'b': /* code-block dimension */
sscanf(optarg, "%d,%d", &cblockw_init, &cblockh_init);
if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
|| cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
fprintf(stderr,
"!! Size of code_block error (option -b) !!\n\nRestriction :\n * width*height<=4096\n * 4<=width,height<= 1024\n\n");
fprintf(stderr,"!! Size of code_block error (option -b) !!\n\nRestriction :\n * width*height<=4096\n * 4<=width,height<= 1024\n\n");
return 1;
}
break;
/* ----------------------------------------------------- */
case 'x': /* creation of index file */
index = optarg;
case 'x': /* creation of index file */
index = optarg;
img.index_on = 1;
break;
/* ----------------------------------------------------- */
case 'p': /* progression order */
sscanf(optarg, "%s", progression);
case 'p': /* progression order */
s = optarg;
for (i=0; i<4; i++)
{
progression[i] = *s;
s++;
}
Prog_order = give_progression(progression);
if (Prog_order == -1) {
fprintf(stderr,
"Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
return 1;
if (Prog_order == -1) {
fprintf(stderr,"Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
return 1;
}
break;
/* ----------------------------------------------------- */
case 's': /* subsampling factor */
case 's': /* subsampling factor */
if (sscanf(optarg, "%d,%d", &subsampling_dx, &subsampling_dy) != 2) {
fprintf(stderr,
"'-s' sub-sampling argument error ! [-s dx,dy]\n");
fprintf(stderr,"'-s' sub-sampling argument error ! [-s dx,dy]\n");
return 1;
}
break;
/* ----------------------------------------------------- */
case 'd': /* coordonnate of the reference grid */
case 'd': /* coordonnate of the reference grid */
if (sscanf(optarg, "%d,%d", &Dim[0], &Dim[1]) != 2) {
fprintf(stderr,
"-d 'coordonnate of the reference grid' argument error !! [-d x0,y0]\n");
fprintf(stderr,"-d 'coordonnate of the reference grid' argument error !! [-d x0,y0]\n");
return 1;
}
break;
/* ----------------------------------------------------- */
case 'h': /* Display an help description */
case 'h': /* Display an help description */
help_display();
return 0;
break;
/* ----------------------------------------------------- */
case 'P': /* POC */
case 'P': /* POC */
fprintf(stderr, "/----------------------------------\\\n");
fprintf(stderr, "| POC option not fully tested !! |\n");
fprintf(stderr, "\\----------------------------------/\n");
s = optarg;
while (sscanf
(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
&POC[numpocs].resno0, &POC[numpocs].compno0,
&POC[numpocs].layno1, &POC[numpocs].resno1,
&POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
&POC[numpocs].resno0, &POC[numpocs].compno0,
&POC[numpocs].layno1, &POC[numpocs].resno1,
&POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
POC[numpocs].prg = give_progression(POC[numpocs].progorder);
/* POC[numpocs].tile; */
numpocs++;
@ -472,15 +485,15 @@ int main(int argc, char **argv)
}
break;
/* ------------------------------------------------------ */
case 'S': /* SOP marker */
case 'S': /* SOP marker */
CSty |= 0x02;
break;
/* ------------------------------------------------------ */
case 'E': /* EPH marker */
case 'E': /* EPH marker */
CSty |= 0x04;
break;
/* ------------------------------------------------------ */
case 'M': /* Mode switch pas tous au point !! */
case 'M': /* Mode switch pas tous au point !! */
if (sscanf(optarg, "%d", &value) == 1) {
for (i = 0; i <= 5; i++) {
int cache = value & (1 << i);
@ -490,27 +503,25 @@ int main(int argc, char **argv)
}
break;
/* ------------------------------------------------------ */
case 'R': /* ROI */
case 'R': /* ROI */
if (sscanf(optarg, "OI:c=%d,U=%d", &ROI_compno, &ROI_shift) != 2) {
fprintf(stderr,
"ROI error !! [-ROI:c='compno',U='shift']\n");
fprintf(stderr,"ROI error !! [-ROI:c='compno',U='shift']\n");
return 1;
}
break;
/* ------------------------------------------------------ */
case 'T': /* Tile offset */
case 'T': /* Tile offset */
if (sscanf(optarg, "%d,%d", &TX0, &TY0) != 2) {
fprintf(stderr,
"-T 'tile offset' argument error !! [-T X0,Y0]");
fprintf(stderr,"-T 'tile offset' argument error !! [-T X0,Y0]");
return 1;
}
break;
/* ------------------------------------------------------ */
case 'C': /* Add a comment */
case 'C': /* Add a comment */
cp.comment = optarg;
break;
/* ------------------------------------------------------ */
case 'I': /* reversible or not */
case 'I': /* reversible or not */
ir = 1;
break;
/* ------------------------------------------------------ */
@ -525,14 +536,12 @@ int main(int argc, char **argv)
/* Error messages */
/* -------------- */
if (!infile || !outfile) {
fprintf(stderr,
"usage: pnmtoj2k -i pnm-file -o j2k-file\n");
fprintf(stderr, "usage: pnmtoj2k -i pnm-file -o j2k-file\n");
return 1;
}
if (cp.disto_alloc & cp.fixed_alloc) {
fprintf(stderr,
"Error: option -r and -q can not be used together !!\n");
fprintf(stderr, "Error: option -r and -q can not be used together !!\n");
return 1;
}
@ -544,32 +553,27 @@ int main(int argc, char **argv)
}
if (TX0 > Dim[0] || TY0 > Dim[1]) {
fprintf(stderr,
"Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
TX0, Dim[0], TY0, Dim[1]);
fprintf(stderr, "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
TX0, Dim[0], TY0, Dim[1]);
return 1;
}
for (i = 0; i < numpocs; i++) {
if (POC[i].prg == -1) {
fprintf(stderr,
"Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
i + 1);
fprintf(stderr, "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
i + 1);
}
}
switch (cp.image_type) {
case 0:
if (Tile_arg) {
if (!pgxtoimage
(infile, &img, cp.tdy, subsampling_dx, subsampling_dy, Dim,
cp)) {
if (!pgxtoimage(infile, &img, cp.tdy, subsampling_dx, subsampling_dy, Dim, cp)) {
fprintf(stderr, "not a pgx file\n");
return 1;
}
} else {
if (!pgxtoimage
(infile, &img, -1, subsampling_dx, subsampling_dy, Dim, cp)) {
if (!pgxtoimage(infile, &img, -1, subsampling_dx, subsampling_dy, Dim, cp)) {
fprintf(stderr, " not a pgx file\n");
return 1;
}
@ -610,6 +614,12 @@ int main(int argc, char **argv)
cp.tdy = img.y1 - cp.ty0;
}
/* Initialization for PPM marker */
cp.ppm=0;
cp.ppm_data=NULL;
cp.ppm_previous=0;
cp.ppm_store=0;
/* Init the mutiple tiles */
/* ---------------------- */
cp.tcps = (j2k_tcp_t *) malloc(cp.tw * cp.th * sizeof(j2k_tcp_t));
@ -623,9 +633,16 @@ int main(int argc, char **argv)
tcp->csty = CSty;
tcp->prg = Prog_order;
tcp->mct = img.numcomps == 3 ? 1 : 0;
tcp->ppt = 0;
tcp->ppt_data=NULL;
tcp->ppt_store=0;
numpocs_tile = 0;
if (numpocs) { /* intialisation of POC */
for (i = 0; i < numpocs; i++) {
tcp->POC=0;
if (numpocs) {
/* intialisation of POC */
tcp->POC=1;
for (i = 0; i < numpocs; i++) {
if (tileno == POC[i].tile - 1 || POC[i].tile == -1) {
tcp_poc = &tcp->pocs[numpocs_tile];
tcp_poc->resno0 = POC[numpocs_tile].resno0;
@ -691,27 +708,23 @@ int main(int argc, char **argv)
return 1;
}
if (cp.image_type) { /* PNM PGM PPM */
/* Remove the temporary file Compo */
/* ------------------------------- */
/* Remove the temporary files */
/* -------------------------- */
if (cp.image_type) { /* PNM PGM PPM */
for (i = 0; i < img.numcomps; i++) {
char tmp[256];
sprintf(tmp, "Compo%d", i);
if (unlink(tmp) == -1) {
fprintf(stderr, "failed to kill %s file !\n",
tmp);
char tmp;
sprintf(&tmp, "Compo%d", i);
if (remove(&tmp) == -1) {
fprintf(stderr, "failed to kill %s file !\n", &tmp);
}
}
} else { /* PGX */
/* Kill temporary bandtile file for cleaning the memory space on user's disk */
} else { /* PGX */
for (i = 0; i < cp.th; i++) {
char tmp;
sprintf(&tmp, "bandtile%d", i + 1);
if (unlink(&tmp) == -1) {
fprintf(stderr, "failed to kill %s file !\n",
&tmp);
if (remove(&tmp) == -1) {
fprintf(stderr, "failed to kill %s file !\n", &tmp);
}
}
}

View File

@ -1,6 +1,6 @@
/* Copyright (c) 2001 David Janssens
* Copyright (c) 2002 Yannick Verschueren
* Copyright (c) 2002 Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* 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
@ -30,7 +30,7 @@
#include <openjpeg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//#include <unistd.h>
int ceildiv(int a, int b)
{
@ -40,13 +40,14 @@ int ceildiv(int a, int b)
int main(int argc, char **argv)
{
FILE *f;
char *src;
char *src, *src_name;
char *dest, S1, S2, S3;
int len;
j2k_image_t *img;
j2k_cp_t *cp;
int w, h, max;
int i, image_type = -1;
int i, image_type = -1, compno;
int adjust;
if (argc < 3) {
fprintf(stderr, "usage: %s j2k-file pnm-file\n", argv[0]);
@ -61,37 +62,32 @@ int main(int argc, char **argv)
dest = argv[2];
while (*dest && *dest != '.') {
while (*dest) {
dest++;
}
dest++;
S1 = *dest;
dest++;
S2 = *dest;
dest++;
dest--;
S3 = *dest;
dest--;
S2 = *dest;
dest--;
S1 = *dest;
if ((S1 == 'p' && S2 == 'g' && S3 == 'x')
|| (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
if ((S1 == 'p' && S2 == 'g' && S3 == 'x') || (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
image_type = 0;
}
if ((S1 == 'p' && S2 == 'n' && S3 == 'm')
|| (S1 == 'P' && S2 == 'N' && S3 == 'M') || (S1 == 'p' && S2 == 'g'
&& S3 == 'm')
|| (S1 == 'P' && S2 == 'G' && S3 == 'M') || (S1 == 'P' && S2 == 'P'
&& S3 == 'M')
|| (S1 == 'p' && S2 == 'p' && S3 == 'm')) {
if ((S1 == 'p' && S2 == 'n' && S3 == 'm')|| (S1 == 'P' && S2 == 'N' && S3 == 'M') ||
(S1 == 'p' && S2 == 'g' && S3 == 'm')|| (S1 == 'P' && S2 == 'G' && S3 == 'M') ||
(S1 == 'P' && S2 == 'P' && S3 == 'M')|| (S1 == 'p' && S2 == 'p' && S3 == 'm')) {
image_type = 1;
}
if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
|| (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
if ((S1 == 'b' && S2 == 'm' && S3 == 'p') || (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
image_type = 2;
}
if (image_type == -1) {
fprintf(stderr,
"\033[0;33m!! Unrecognized format for infile [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\033[0;39m\n\n");
fprintf(stderr, "!! Unrecognized format for infile : %c%c%c [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n",S1,S2,S3);
return 1;
}
@ -102,249 +98,279 @@ int main(int argc, char **argv)
fread(src, 1, len, f);
fclose(f);
if (!j2k_decode(src, len, &img, &cp)) {
fprintf(stderr, "j2k_to_image: failed to decode image!\n");
return 1;
src_name=argv[1];
while (*src_name) {
src_name++;
}
free(src);
src_name--;
S3 = *src_name;
src_name--;
S2 = *src_name;
src_name--;
S1 = *src_name;
if (S1 == 'j' && S2 == '2' && S3 == 'k')
{
if (!j2k_decode(src, len, &img, &cp)) {
fprintf(stderr, "j2k_to_image: failed to decode image!\n");
return 1;
}
}
else
{
if (S1 == 'j' && S2 == 'p' && S3 == 't')
{
if (!j2k_decode_jpt_stream(src, len, &img, &cp)) {
fprintf(stderr, "j2k_to_image: failed to decode image!\n");
return 1;
}
}
else
{
fprintf(stderr,"j2k_to_image : Unknown format image *.%c%c%c [only *.j2k or *.jpt]!! \n",S1,S2,S3);
return 1;
}
}
free(src);
/* ------------------ CREATE OUT IMAGE WITH THE RIGHT FORMAT ----------------------- */
/* ------------------------ / */
/* / */
/* FORMAT : PNM, PGM or PPM / */
/* / */
/* ------------------------ / */
if (image_type == 1) { /* PNM PGM PPM */
if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
&& img->comps[1].dx == img->comps[2].dx
&& img->comps[0].dy == img->comps[1].dy
&& img->comps[1].dy == img->comps[2].dy
&& img->comps[0].prec == img->comps[1].prec
&& img->comps[1].prec == img->comps[2].prec) {
f = fopen(argv[2], "wb");
w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
max = (1 << img->comps[0].prec) - 1;
fprintf(f, "P6\n%d %d\n%d\n", w, h, max);
for (i = 0; i < w * h; i++) {
char r, g, b;
r = img->comps[0].data[i];
g = img->comps[1].data[i];
b = img->comps[2].data[i];
fprintf(f, "%c%c%c", r, g, b);
/* ---------------------------- / */
/* / / */
/* / FORMAT : PNM, PGM or PPM / */
/* / / */
/* ---------------------------- / */
switch (image_type)
{
case 1: /* PNM PGM PPM*/
if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
&& img->comps[1].dx == img->comps[2].dx
&& img->comps[0].dy == img->comps[1].dy
&& img->comps[1].dy == img->comps[2].dy
&& img->comps[0].prec == img->comps[1].prec
&& img->comps[1].prec == img->comps[2].prec)
{
f = fopen(argv[2], "wb");
w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
//max = (1 << img->comps[0].prec) - 1;
max =img->comps[0].prec>8? 255:(1 << img->comps[0].prec) - 1;
fprintf(f, "P6\n%d %d\n%d\n", w, h, max);
adjust=img->comps[0].prec>8?img->comps[0].prec-8:0;
for (i = 0; i < w * h; i++)
{
char r, g, b;
r = img->comps[0].data[i];
r+=(img->comps[0].sgnd? 1 << (img->comps[0].prec-1):0);
r=r>>adjust;
g = img->comps[1].data[i];
g+=(img->comps[1].sgnd? 1 << (img->comps[1].prec-1):0);
g=g>>adjust;
b = img->comps[2].data[i];
b+=(img->comps[2].sgnd? 1 << (img->comps[2].prec-1):0);
b=b>>adjust;
fprintf(f, "%c%c%c", r, g, b);
}
fclose(f);
} else
{
for (compno = 0; compno < img->numcomps; compno++)
{
char name[256];
if (img->numcomps > 1) {
sprintf(name, "%d.%s", compno, argv[2]);
} else
{
sprintf(name, "%s", argv[2]);
}
fclose(f);
} else {
int compno;
for (compno = 0; compno < img->numcomps; compno++) {
char name[256];
if (img->numcomps > 1) {
sprintf(name, "%d.%s", compno, argv[2]);
} else {
sprintf(name, "%s", argv[2]);
}
f = fopen(name, "wb");
w = ceildiv(img->x1 - img->x0, img->comps[compno].dx);
h = ceildiv(img->y1 - img->y0, img->comps[compno].dy);
max = (1 << img->comps[compno].prec) - 1;
fprintf(f, "P5\n%d %d\n%d\n", w, h, max);
for (i = 0; i < w * h; i++) {
char l;
l = img->comps[compno].data[i];
fprintf(f, "%c", l);
}
fclose(f);
f = fopen(name, "wb");
w = ceildiv(img->x1 - img->x0, img->comps[compno].dx);
h = ceildiv(img->y1 - img->y0, img->comps[compno].dy);
max =img->comps[compno].prec>8? 255:(1 << img->comps[compno].prec) - 1;
fprintf(f, "P5\n%d %d\n%d\n", w, h, max);
adjust=img->comps[compno].prec>8?img->comps[compno].prec-8:0;
for (i = 0; i < w * h; i++)
{
char l;
l = img->comps[compno].data[i];
l+=(img->comps[compno].sgnd? 1 << (img->comps[compno].prec-1):0);
l=l>>adjust;
fprintf(f, "%c", l);
}
fclose(f);
}
}
} else
/* ------------------------ / */
/* / */
/* FORMAT : PGX / */
/* / */
/* ------------------------ / */
if (image_type == 0) { /* PGX */
int compno;
for (compno = 0; compno < img->numcomps; compno++) {
j2k_comp_t *comp = &img->comps[compno];
char name[256];
/* sprintf(name, "%s-%d.pgx", argv[2], compno); */
sprintf(name, "%s", argv[2]);
f = fopen(name, "wb");
w = ceildiv(img->x1 - img->x0, comp->dx);
h = ceildiv(img->y1 - img->y0, comp->dy);
fprintf(f, "PG LM %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec,
w, h);
for (i = 0; i < w * h; i++) {
int v = img->comps[compno].data[i];
if (comp->prec <= 8) {
char c = (char) v;
fwrite(&c, 1, 1, f);
} else if (comp->prec <= 16) {
short s = (short) v;
fwrite(&s, 2, 1, f);
} else {
fwrite(&v, 4, 1, f);
}
}
fclose(f);
break ;
/* ------------------------ / */
/* / / */
/* / FORMAT : PGX / */
/* / / */
/* /----------------------- / */
case 0: /* PGX */
for (compno = 0; compno < img->numcomps; compno++)
{
j2k_comp_t *comp = &img->comps[compno];
char name[256];
if (img->numcomps>1)
sprintf(name, "%d_%s", compno, argv[2]);
else
sprintf(name, "%s", argv[2]);
f = fopen(name, "wb");
w = ceildiv(img->x1 - img->x0, comp->dx);
h = ceildiv(img->y1 - img->y0, comp->dy);
fprintf(f, "PG LM %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
for (i = 0; i < w * h; i++)
{
int v = img->comps[compno].data[i];
if (comp->prec <= 8)
{
char c = (char) v;
fwrite(&c, 1, 1, f);
} else if (comp->prec <= 16)
{
short s = (short) v;
fwrite(&s, 2, 1, f);
} else
{
fwrite(&v, 4, 1, f);
}
}
fclose(f);
}
break ;
/* ------------------------ / */
/* / / */
/* / FORMAT : BMP / */
/* / / */
/* /----------------------- / */
case 2: /* BMP */
if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
&& img->comps[1].dx == img->comps[2].dx
&& img->comps[0].dy == img->comps[1].dy
&& img->comps[1].dy == img->comps[2].dy
&& img->comps[0].prec == img->comps[1].prec
&& img->comps[1].prec == img->comps[2].prec)
{
/* -->> -->> -->> -->>
24 bits color
<<-- <<-- <<-- <<-- */
f = fopen(argv[2], "wb");
w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
fprintf(f, "BM");
/* FILE HEADER */
/* ------------- */
fprintf(f, "%c%c%c%c",
(unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff, ((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
/* INFO HEADER */
/* ------------- */
fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) ((w) & 0xff),(unsigned char) ((w) >> 8) & 0xff,
(unsigned char) ((w) >> 16) & 0xff, (unsigned char) ((w) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) ((h) & 0xff), (unsigned char) ((h) >> 8) & 0xff,
(unsigned char) ((h) >> 16) & 0xff, (unsigned char) ((h) >> 24) & 0xff);
fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
fprintf(f, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
for (i = 0; i < w * h; i++)
{
unsigned char R, G, B;
R = img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
G = img->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
B = img->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
fprintf(f, "%c%c%c", B, G, R);
if (((i + 1) % w == 0 && w % 2))
fprintf(f, "%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff);
}
fclose(f);
} else
{ /* Gray-scale */
/* -->> -->> -->> -->>
8 bits non code (Gray scale)
<<-- <<-- <<-- <<-- */
f = fopen(argv[2], "wb");
w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
fprintf(f, "BM");
/* FILE HEADER */
/* ------------- */
fprintf(f, "%c%c%c%c",
(unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
(unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
(unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
(unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
((54 + 1024) >> 16) & 0xff, ((54 + 1024) >> 24) & 0xff);
/* INFO HEADER */
/* ------------- */
fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) ((w) & 0xff), (unsigned char) ((w) >> 8) & 0xff,
(unsigned char) ((w) >> 16) & 0xff, (unsigned char) ((w) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) ((h) & 0xff), (unsigned char) ((h) >> 8) & 0xff,
(unsigned char) ((h) >> 16) & 0xff, (unsigned char) ((h) >> 24) & 0xff);
fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
fprintf(f, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
(unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
(unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
(unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
}
} else
/* ------------------------ / */
/* / */
/* FORMAT : BMP / */
/* / */
/* ------------------------ / */
if (image_type == 2) { /* BMP */
if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
&& img->comps[1].dx == img->comps[2].dx
&& img->comps[0].dy == img->comps[1].dy
&& img->comps[1].dy == img->comps[2].dy
&& img->comps[0].prec == img->comps[1].prec
&& img->comps[1].prec == img->comps[2].prec) {
/* -->> -->> -->> -->>
24 bits color
<<-- <<-- <<-- <<-- */
f = fopen(argv[2], "wb");
w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
fprintf(f, "BM");
/* FILE HEADER */
/* ------------- */
fprintf(f, "%c%c%c%c",
(unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) &
0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) &
0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) &
0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,
((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
/* INFO HEADER */
/* ------------- */
fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) ((w) & 0xff),
(unsigned char) ((w) >> 8) & 0xff,
(unsigned char) ((w) >> 16) & 0xff,
(unsigned char) ((w) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) ((h) & 0xff),
(unsigned char) ((h) >> 8) & 0xff,
(unsigned char) ((h) >> 16) & 0xff,
(unsigned char) ((h) >> 24) & 0xff);
fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
fprintf(f, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c",
(unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) &
0xff);
fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
for (i = 0; i < w * h; i++) {
unsigned char R, G, B;
R = img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
G = img->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
B = img->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
fprintf(f, "%c%c%c", B, G, R);
if (((i + 1) % w == 0 && w % 2))
fprintf(f, "%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff);
}
fclose(f);
} else { /* Gray-scale */
/* -->> -->> -->> -->>
8 bits non code (Gray scale)
<<-- <<-- <<-- <<-- */
f = fopen(argv[2], "wb");
w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
fprintf(f, "BM");
/* FILE HEADER */
/* ------------- */
fprintf(f, "%c%c%c%c",
(unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
(unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) &
0xff,
(unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) &
0xff,
(unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) &
0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
((54 + 1024) >> 16) & 0xff, ((54 + 1024) >> 24) & 0xff);
/* INFO HEADER */
/* ------------- */
fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) ((w) & 0xff),
(unsigned char) ((w) >> 8) & 0xff,
(unsigned char) ((w) >> 16) & 0xff,
(unsigned char) ((w) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) ((h) & 0xff),
(unsigned char) ((h) >> 8) & 0xff,
(unsigned char) ((h) >> 16) & 0xff,
(unsigned char) ((h) >> 24) & 0xff);
fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
fprintf(f, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
(unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
(unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
(unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
}
for (i = 0; i < 256; i++) {
fprintf(f, "%c%c%c%c", i, i, i, 0);
}
for (i = 0; i < w * h; i++) {
fprintf(f, "%c",
img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]);
if (((i + 1) % w == 0 && w % 2))
fprintf(f, "%c", 0);
}
}
for (i = 0; i < 256; i++)
{
fprintf(f, "%c%c%c%c", i, i, i, 0);
}
for (i = 0; i < w * h; i++)
{
fprintf(f, "%c", img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]);
if (((i + 1) % w == 0 && w % 2))
fprintf(f, "%c", 0);
}
break;
default :
break;
}
return 0;
}

View File

@ -29,12 +29,47 @@
#ifndef __BIO_H
#define __BIO_H
/*
* Number of bytes written.
*/
int bio_numbytes();
/*
* Init encoder.
*
* bp : Output buffer
* len : Output buffer length
*/
void bio_init_enc(unsigned char *bp, int len);
/*
* Init decoder.
*
* bp : Input buffer
* len : Input buffer length
*/
void bio_init_dec(unsigned char *bp, int len);
/*
* Write bits.
*
* v : Value of bits
* n : Number of bits to write
*/
void bio_write(int v, int n);
/*
* Read bits.
*
* n : Number of bits to read
*/
int bio_read(int n);
int bio_flush(); /* modified to eliminated longjmp !! */
int bio_inalign(); /* modified to eliminated longjmp !! */
/*
* Flush bits. Modified to eliminate longjmp !!
*/
int bio_flush();
int bio_inalign(); /* modified to eliminated longjmp !! */
#endif

View File

@ -27,53 +27,60 @@
#include "cio.h"
#include <setjmp.h>
static unsigned char *cio_start, *cio_end, *cio_bp;
static unsigned char *cio_start; /* pointer to the start of the stream */
static unsigned char *cio_end; /* pointer to the end of the stream */
static unsigned char *cio_bp; /* pointer to the present position */
extern jmp_buf j2k_error;
/* <summary> */
/* Number of bytes written. */
/* </summary> */
/*
* Number of bytes written.
*/
int cio_numbytes()
{
return cio_bp - cio_start;
}
/* <summary> */
/* Get position in byte stream. */
/* </summary> */
/*
* Get position in byte stream.
*/
int cio_tell()
{
return cio_bp - cio_start;
}
/* <summary> */
/* Set position in byte stream. */
/* </summary> */
/*
* Set position in byte stream.
*
* pos : position, in number of bytes, from the beginning of the stream
*/
void cio_seek(int pos)
{
cio_bp = cio_start + pos;
}
/* <summary> */
/* Number of bytes left before the end of the stream. */
/* </summary> */
/*
* Number of bytes left before the end of the stream.
*/
int cio_numbytesleft()
{
return cio_end - cio_bp;
}
/* <summary> */
/* Get pointer to the current position in the stream. */
/* </summary> */
/*
* Get pointer to the current position in the stream.
*/
unsigned char *cio_getbp()
{
return cio_bp;
}
/* <summary> */
/* Initialize byte IO. */
/* </summary> */
/*
* Initialize byte IO
*
* bp : destination/source stream
* len : length of the stream
*/
void cio_init(unsigned char *bp, int len)
{
cio_start = bp;
@ -81,9 +88,9 @@ void cio_init(unsigned char *bp, int len)
cio_bp = bp;
}
/* <summary> */
/* Write a byte. */
/* </summary> */
/*
* Write a byte.
*/
void cio_byteout(unsigned char v)
{
if (cio_bp >= cio_end)
@ -92,9 +99,9 @@ void cio_byteout(unsigned char v)
}
/* <summary> */
/* Read a byte. */
/* </summary> */
/*
* Read a byte.
*/
unsigned char cio_bytein()
{
if (cio_bp >= cio_end)
@ -102,9 +109,12 @@ unsigned char cio_bytein()
return *cio_bp++;
}
/* <summary> */
/* Write a byte. */
/* </summary> */
/*
* Write some bytes.
*
* v : value to write
* n : number of bytes to write
*/
void cio_write(unsigned int v, int n)
{
int i;
@ -113,9 +123,13 @@ void cio_write(unsigned int v, int n)
}
}
/* <summary> */
/* Read some bytes. */
/* </summary> */
/*
* Read some bytes.
*
* n : number of bytes to read
*
* return : value of the n bytes read
*/
unsigned int cio_read(int n)
{
int i;
@ -127,9 +141,11 @@ unsigned int cio_read(int n)
return v;
}
/* <summary> */
/* Write some bytes. */
/* </summary> */
/*
* Skip some bytes.
*
* n : number of bytes to skip
*/
void cio_skip(int n)
{
cio_bp += n;

View File

@ -27,14 +27,71 @@
#ifndef __CIO_H
#define __CIO_H
int cio_tell();
void cio_seek(int pos);
/*
* Number of bytes written.
*
* returns number of bytes written
*/
int cio_numbytes();
/*
* Get position in byte stream.
*
* return position in bytes
*/
int cio_tell();
/*
* Set position in byte stream.
*
* pos : position, in number of bytes, from the beginning of the stream
*/
void cio_seek(int pos);
/*
* Number of bytes left before the end of the stream.
*
* Returns the number of bytes before the end of the stream
*/
int cio_numbytesleft();
/*
* Get pointer to the current position in the stream.
*
* return : pointer to the position
*/
unsigned char *cio_getbp();
/*
* Initialize byte IO
*
* bp : destination/source stream
* len : length of the stream
*/
void cio_init(unsigned char *bp, int len);
/*
* Write some bytes.
*
* v : value to write
* n : number of bytes to write
*/
void cio_write(unsigned int v, int n);
/*
* Read some bytes.
*
* n : number of bytes to read
*
* return : value of the n bytes read
*/
unsigned int cio_read(int n);
/*
* Skip some bytes.
*
* n : number of bytes to skip
*/
void cio_skip(int n);
#endif

View File

@ -1,7 +1,7 @@
/*
* 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
* Copyright (c) 2002-2004, Yannick Verschueren
* Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -32,6 +32,7 @@
#include "tcd.h"
#include <stdlib.h>
#include <stdio.h>
//#include <math.h>
#define S(i) a[x*(i)*2]
#define D(i) a[x*(1+(i)*2)]
@ -41,9 +42,9 @@
#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> */
/* <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},
@ -51,9 +52,9 @@ double dwt_norms[4][10] = {
{.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> */
/* <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},
@ -65,6 +66,9 @@ double dwt_norms_real[4][10] = {
static int *b = NULL;
static int lastSizeOfB = 0;
/* <summary> */
/* Claning memory. */
/* </summary> */
void dwt_clean()
{
@ -77,9 +81,9 @@ void dwt_clean()
/* \ Add Patrick */
/* <summary> */
/* <summary> */
/* Forward lazy transform. */
/* </summary> */
/* </summary> */
void dwt_deinterleave(int *a, int n, int x, int res, int cas)
{
int dn, sn, i;
@ -107,41 +111,9 @@ void dwt_deinterleave(int *a, int n, int x, int res, int cas)
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> */
/* <summary> */
/* Inverse lazy transform. */
/* </summary> */
/* </summary> */
void dwt_interleave(int *a, int n, int x, int res, int cas)
{
int dn, sn, i;
@ -170,41 +142,9 @@ void dwt_interleave(int *a, int n, int x, int res, int cas)
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> */
/* <summary> */
/* Forward 5-3 wavelet tranform in 1-D. */
/* </summary> */
/* </summary> */
void dwt_encode_1(int *a, int n, int x, int res, int cas)
{
int dn, sn, i = 0;
@ -212,7 +152,7 @@ void dwt_encode_1(int *a, int n, int x, int res, int cas)
dn = n - res;
if (cas) {
if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
S(i) *= 2;
else {
for (i = 0; i < dn; i++)
@ -231,20 +171,18 @@ void dwt_encode_1(int *a, int n, int x, int res, int cas)
dwt_deinterleave(a, n, x, res, cas);
}
/* <summary> */
/* <summary> */
/* Inverse 5-3 wavelet tranform in 1-D. */
/* </summary> */
/* </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 */
if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
S(i) /= 2;
else {
for (i = 0; i < sn; i++)
@ -262,74 +200,27 @@ void dwt_decode_1(int *a, int n, int x, int res, int cas)
}
}
/* <summary> */
/* <summary> */
/* Forward 5-3 wavelet tranform in 2-D. */
/* </summary> */
/* </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;
int i, j;
int rw; /* width of the resolution level computed */
int rh; /* heigth of the resolution level computed */
int rw1; /* width of the resolution level once lower than computed one */
int rh1; /* height of the resolution level once lower than computed one */
for (i = 0; i < l; i++) {
int cas_col = 0;
int cas_row = 0;
int cas_col = 0; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
int cas_row = 0; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
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;
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;
cas_row = tilec->resolutions[l - i].x0 % 2;
cas_col = tilec->resolutions[l - i].y0 % 2;
for (j = 0; j < rw; j++)
dwt_encode_1(a + j, rh, w, rh1, cas_col);
@ -340,70 +231,28 @@ void dwt_encode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)
dwt_clean();
}
/* <summary> */
/* <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)
/* </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;
int i, j;
int rw; /* width of the resolution level computed */
int rh; /* heigth of the resolution level computed */
int rw1; /* width of the resolution level once lower than computed one */
int rh1; /* height of the resolution level once lower than computed one */
for (i = l - 1; i >= 0; i--) {
int cas_col = 0;
int cas_row = 0;
int cas_col = 0; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
int cas_row = 0; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
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;
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;
cas_row = tilec->resolutions[l - i].x0 % 2;
cas_col = tilec->resolutions[l - i].y0 % 2;
for (j = 0; j < rh; j++)
dwt_decode_1(a + j * w, rw, 1, rw1, cas_row);
@ -413,9 +262,9 @@ void dwt_decode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
dwt_clean();
}
/* <summary> */
/* <summary> */
/* Get gain of 5-3 wavelet transform. */
/* </summary> */
/* </summary> */
int dwt_getgain(int orient)
{
if (orient == 0)
@ -425,17 +274,17 @@ int dwt_getgain(int orient)
return 2;
}
/* <summary> */
/* <summary> */
/* Get norm of 5-3 wavelet. */
/* </summary> */
/* </summary> */
double dwt_getnorm(int level, int orient)
{
return dwt_norms[orient][level];
}
/* <summary> */
/* <summary> */
/* Forward 9-7 wavelet transform in 1-D. */
/* </summary> */
/* </summary> */
void dwt_encode_1_real(int *a, int n, int x, int res, int cas)
{
int dn, sn, i = 0;
@ -453,9 +302,9 @@ void dwt_encode_1_real(int *a, int n, int x, int res, int cas)
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);
S(i) = fix_mul(S(i), 5038); /*5038*/
for (i = 0; i < sn; i++)
D(i) = fix_mul(D(i), 6660);
D(i) = fix_mul(D(i), 6659); /*6660*/
}
} else {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
@ -468,29 +317,29 @@ void dwt_encode_1_real(int *a, int n, int x, int res, int cas)
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);
D(i) = fix_mul(D(i), 5038); /*5038*/
for (i = 0; i < sn; i++)
S(i) = fix_mul(S(i), 6660);
S(i) = fix_mul(S(i), 6659); /*6660*/
}
}
dwt_deinterleave_real(a, n, x, res, cas);
dwt_deinterleave(a, n, x, res, cas);
}
/* <summary> */
/* <summary> */
/* Inverse 9-7 wavelet transform in 1-D. */
/* </summary> */
/* </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);
dwt_interleave(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);
D(i) = fix_mul(D(i), 10078); /* 10076 */
for (i = 0; i < dn; i++)
S(i) = fix_mul(S(i), 13320);
S(i) = fix_mul(S(i), 13318); /* 13320*/
for (i = 0; i < sn; i++)
D(i) -= fix_mul(SS_(i) + SS_(i + 1), 3633);
for (i = 0; i < dn; i++)
@ -503,9 +352,9 @@ void dwt_decode_1_real(int *a, int n, int x, int res, int cas)
} else {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < sn; i++)
S(i) = fix_mul(S(i), 10076);
S(i) = fix_mul(S(i), 10078); /* 10076 */
for (i = 0; i < dn; i++)
D(i) = fix_mul(D(i), 13320);
D(i) = fix_mul(D(i), 13318); /* 13320*/
for (i = 0; i < sn; i++)
S(i) -= fix_mul(D_(i - 1) + D_(i), 3633);
for (i = 0; i < dn; i++)
@ -518,70 +367,28 @@ void dwt_decode_1_real(int *a, int n, int x, int res, int cas)
}
}
/* <summary> */
/* <summary> */
/* Forward 9-7 wavelet transform in 2-D. */
/* </summary> */
/* </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;
int i, j;
int rw; /* width of the resolution level computed */
int rh; /* heigth of the resolution level computed */
int rw1; /* width of the resolution level once lower than computed one */
int rh1; /* height of the resolution level once lower than computed one */
for (i = 0; i < l; i++) {
int cas_col = 0;
int cas_row = 0;
int cas_col = 0; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
int cas_row = 0; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
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;
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;
cas_row = tilec->resolutions[l - i].x0 % 2;
cas_col = tilec->resolutions[l - i].y0 % 2;
for (j = 0; j < rw; j++)
dwt_encode_1_real(a + j, rh, w, rh1, cas_col);
@ -590,72 +397,28 @@ void dwt_encode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)
}
}
/* <summary> */
/* <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)
/* </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;
int i, j;
int rw; /* width of the resolution level computed */
int rh; /* heigth of the resolution level computed */
int rw1; /* width of the resolution level once lower than computed one */
int rh1; /* height of the resolution level once lower than computed one */
for (i = l - 1; i >= 0; i--) {
int cas_col = 0;
int cas_row = 0;
int cas_col = 0; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
int cas_row = 0; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
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;
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;
cas_row = tilec->resolutions[l - i].x0 % 2;
cas_col = tilec->resolutions[l - i].y0 % 2;
for (j = 0; j < rh; j++)
dwt_decode_1_real(a + j * w, rw, 1, rw1, cas_row);
@ -664,17 +427,17 @@ void dwt_decode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
}
}
/* <summary> */
/* <summary> */
/* Get gain of 9-7 wavelet transform. */
/* </summary> */
/* </summary> */
int dwt_getgain_real(int orient)
{
return 0;
}
/* <summary> */
/* <summary> */
/* Get norm of 9-7 wavelet. */
/* </summary> */
/* </summary> */
double dwt_getnorm_real(int level, int orient)
{
return dwt_norms_real[orient][level];

View File

@ -36,6 +36,7 @@
* a: samples of the component
* w: width of the component
* h: height of the component
* tilec : tile component information (present tile)
* l: number of decomposition levels in the DWT
*/
/* void dwt_encode(int* a, int w, int h, int l); */
@ -45,11 +46,13 @@ void dwt_encode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l);
* a: samples of the component
* w: width of the component
* h: height of the component
* tilec : tile component information (present tile)
* l: number of decomposition levels in the DWT
* row_tilec : tile component information (previous tile on the same row)
* col_tilec : tile component information (previous tile on the same column)
*/
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); */
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);
/*
* Get the gain of a subband for the reversible DWT
* orient: number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH)
@ -79,9 +82,7 @@ void dwt_encode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l);
* 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);
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)

View File

@ -24,25 +24,33 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
/* <summary> */
/* Get the minimum of two integers. */
/* </summary> */
/*
* Get the minimum of two integers.
*
* returns a if a < b else b
*/
int int_min(int a, int b)
{
return a < b ? a : b;
}
/* <summary> */
/* Get the maximum of two integers. */
/* </summary> */
/*
* Get the maximum of two integers.
*
* returns a if a > b else b
*/
int int_max(int a, int b)
{
return a > b ? a : b;
}
/* <summary> */
/* Clamp an integer inside an interval. */
/* </summary> */
/*
* Clamp an integer inside an interval.
*
* return a if (min < a < max)
* return max if (a > max)
* return min if (a < min)
*/
int int_clamp(int a, int min, int max)
{
if (a < min)
@ -52,41 +60,49 @@ int int_clamp(int a, int min, int max)
return a;
}
/* <summary> */
/* Get absolute value of integer. */
/* </summary> */
/*
* Get absolute value of integer.
*/
int int_abs(int a)
{
return a < 0 ? -a : a;
}
/* <summary> */
/* Divide an integer and round upwards. */
/* </summary> */
/*
* Divide an integer and round upwards.
*
* a divided by b
*/
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> */
/*
* Divide an integer by a power of 2 and round upwards.
*
* a divided by 2^b
*/
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> */
/*
* Divide an integer by a power of 2 and round downwards.
*
* a divided by 2^b
*/
int int_floordivpow2(int a, int b)
{
return a >> b;
}
/* <summary> */
/* Get logarithm of an integer and round downwards. */
/* </summary> */
/*
* Get logarithm of an integer and round downwards.
*
* log2(a)
*/
int int_floorlog2(int a)
{
int l;

View File

@ -41,8 +41,8 @@
#ifndef __J2K_H
#define __J2K_H
#define J2K_MAXRLVLS 33
#define J2K_MAXBANDS (3*J2K_MAXRLVLS+1)
#define J2K_MAXRLVLS 33 /* Number of maximum resolution level authorized */
#define J2K_MAXBANDS (3*J2K_MAXRLVLS+1) /* Number of maximum sub-band linked to number of resolution level */
#define J2K_CP_CSTY_PRT 0x01
#define J2K_CP_CSTY_SOP 0x02
@ -59,40 +59,39 @@
#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 */
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 */
int x0, y0; /* XOsiz, YOsiz */
int x1, y1; /* Xsiz, Ysiz */
int numcomps; /* number of components */
int index_on; /* 0 = no index || 1 = index */
j2k_comp_t *comps; /* image-components */
} j2k_image_t;
typedef struct {
int expn; /* exponent */
int mant; /* mantissa */
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 */
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 {
@ -104,61 +103,70 @@ typedef struct {
} 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 */
int first; /* 1 : first part-tile of a tile */
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 */
int POC; /* Precise if a POC marker has been used O:NO, 1:YES */
j2k_poc_t pocs[32]; /* progression order changes */
unsigned char *ppt_data; /* packet header store there for futur use in t2_decode_packet */
int ppt; /* If ppt == 1 --> there was a PPT marker for the present tile */
int ppt_store; /* Use in case of multiple marker PPT (number of info already store) */
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 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 */
unsigned char *ppm_data; /* packet header store there for futur use in t2_decode_packet */
int ppm; /* If ppm == 1 --> there was a PPM marker for the present tile */
int ppm_store; /* Use in case of multiple marker PPM (number of info already store) */
int ppm_previous; /* Use in case of multiple marker PPM (case on non-finished previous info) */
j2k_tcp_t *tcps; /* tile coding parameters */
int *matrice; /* Fixed layer */
} j2k_cp_t;
typedef struct {
int start_pos, end_pos; /* start and end position */
double disto;
} info_packet; /* Index struct */
int start_pos, end_pos; /* start and end position */
double disto; /* ADD for Marcela */
} 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 */
double *thresh; /* value of thresh for each layer by tile cfr. Marcela */
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 */
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 */
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
@ -168,8 +176,7 @@ typedef struct {
* 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, char *outfile, int len, char *index);
/* LIBJ2K_API int j2k_encode(j2k_image_t *i, j2k_cp_t *cp,unsigned char *dest, int len); */
/*
@ -179,7 +186,17 @@ LIBJ2K_API int j2k_encode(j2k_image_t * i, j2k_cp_t * cp, char *outfile,
* 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);
LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t ** img, j2k_cp_t ** cp);
/*
* Decode an image form a JPT-stream (JPEG 2000, JPIP)
* src: source buffer
* len: length of source buffer
* i: decode image
* cp: coding parameters that were used to encode the image
*
*/
int j2k_decode_jpt_stream(unsigned char *src, int len, j2k_image_t ** img, j2k_cp_t ** cp);
#endif

View File

@ -33,10 +33,10 @@
/* 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 */
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> */
@ -318,9 +318,9 @@ void mqc_bypass_init_enc()
{
mqc_c = 0;
mqc_ct = 8;
if (*mqc_bp == 0xff) {
mqc_ct = 7;
}
/*if (*mqc_bp == 0xff) {
mqc_ct = 7;
}*/
}
/* <summary> */
@ -332,15 +332,16 @@ 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;
}
if (mqc_ct == 0)
{
mqc_bp++;
*mqc_bp = mqc_c;
mqc_ct = 8;
if (*mqc_bp == 0xff) {
mqc_ct = 7;
}
mqc_c = 0;
}
}
/* <summary> */
@ -405,6 +406,7 @@ int mqc_restart_enc()
void mqc_restart_init_enc()
{
/* <Re-init part> */
mqc_setcurctx(0);
mqc_a = 0x8000;
mqc_c = 0;
mqc_ct = 12;
@ -412,7 +414,6 @@ void mqc_restart_init_enc()
if (*mqc_bp == 0xff) {
mqc_ct = 13;
}
}

View File

@ -79,7 +79,7 @@ void mqc_bypass_init_enc();
/*
* BYPASS mode switch
*/
void mqc_bypass_enc();
void mqc_bypass_enc(int d);
/*
* BYPASS mode switch

View File

@ -1,7 +1,7 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* Copyright (c) 2003-2004, Yannick Verschueren
* Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -31,53 +31,40 @@
#include <stdlib.h>
#include <stdio.h>
/* <summary> */
/* Create a packet iterator. */
/* </summary> */
/* <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 p, q, i;
int compno, resno, pino;
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));
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));
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));
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);
@ -110,30 +97,24 @@ pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno)
res->ph = (py1 - py0) >> res->pdy;
}
}
tccp = &tcp->tccps[0];
pi[pino].step_p=1;
pi[pino].step_c=100*pi[pino].step_p;
pi[pino].step_r=img->numcomps*pi[pino].step_c;
pi[pino].step_l=maxres*pi[pino].step_r;
/* 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; */
if (pino==0)
{
pi[pino].include = (short int*)malloc(img->numcomps*maxres*tcp->numlayers*100*sizeof(short int));
for (i=0 ; i<img->numcomps*maxres*tcp->numlayers*100; i++)
pi[pino].include[i]=0;
}
/* pi[pino].include=(short int*)calloc(img->numcomps*maxres*tcp->numlayers*1000,sizeof(short int));*/
else
pi[pino].include=pi[pino-1].include;
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) {
if (tcp->POC == 0) {
pi[pino].first = 1;
pi[pino].poc.resno0 = 0;
pi[pino].poc.compno0 = 0;
@ -154,9 +135,11 @@ pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno)
return pi;
}
/* <summary> */
/* Get next packet in layer=resolution-component-precinct order. */
/* </summary> */
/* <summary>
* Get next packet in layer-resolution-component-precinct order.
*
* pi: packet iterator to modify
* </summary> */
int pi_next_lrcp(pi_iterator_t * pi)
{
pi_comp_t *comp;
@ -170,32 +153,32 @@ int pi_next_lrcp(pi_iterator_t * pi)
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:;
}
}
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->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p]){
pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p] = 1;
return 1;
}
skip:;
}
}
}
}
return 0;
}
/* <summary> */
/* Get next packet in resolution-layer-component-precinct order. */
/* </summary> */
/* <summary>
* Get next packet in resolution-layer-component-precinct order.
*
* pi: packet iterator to modify
* </summary> */
int pi_next_rlcp(pi_iterator_t * pi)
{
pi_comp_t *comp;
@ -210,28 +193,30 @@ int pi_next_rlcp(pi_iterator_t * pi)
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:;
}
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->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p]){
pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p] = 1;
return 1;
}
skip:;
}
}
}
}
return 0;
}
/* <summary> */
/* Get next packet in resolution-precinct-component-layer order. */
/* </summary> */
/* <summary>
* Get next packet in resolution-precinct-component-layer order.
*
* pi: packet iterator to modify
* </summary> */
int pi_next_rpcl(pi_iterator_t * pi)
{
pi_comp_t *comp;
@ -248,22 +233,17 @@ int pi_next_rpcl(pi_iterator_t * pi)
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));
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++) {
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;
@ -278,28 +258,21 @@ int pi_next_rpcl(pi_iterator_t * pi)
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)))) {
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)))) {
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);
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;
if (!pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p]){
pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p] = 1;
return 1;
}
skip:;
}
@ -310,9 +283,11 @@ int pi_next_rpcl(pi_iterator_t * pi)
return 0;
}
/* <summary> */
/* Get next packet in precinct-component-resolution-layer order. */
/* </summary> */
/* <summary>
* Get next packet in precinct-component-resolution-layer order.
*
* pi: packet iterator to modify
* </summary> */
int pi_next_pcrl(pi_iterator_t * pi)
{
pi_comp_t *comp;
@ -330,25 +305,18 @@ int pi_next_pcrl(pi_iterator_t * pi)
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));
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++) {
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++) {
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;
@ -359,27 +327,18 @@ int pi_next_pcrl(pi_iterator_t * pi)
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)))) {
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)))) {
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);
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;
if (! pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p]){
pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p] = 1;
return 1;
}
skip:;
@ -391,9 +350,11 @@ int pi_next_pcrl(pi_iterator_t * pi)
return 0;
}
/* <summary> */
/* Get next packet in component-precinct-resolution-layer order. */
/* </summary> */
/* <summary>
* Get next packet in component-precinct-resolution-layer order.
*
* pi: packet iterator to modify
* </summary> */
int pi_next_cprl(pi_iterator_t * pi)
{
pi_comp_t *comp;
@ -435,28 +396,19 @@ int pi_next_cprl(pi_iterator_t * pi)
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)))) {
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)))) {
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);
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;
if (! pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p]){
pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p] = 1;
return 1;
}
skip:;
}
@ -467,9 +419,11 @@ int pi_next_cprl(pi_iterator_t * pi)
return 0;
}
/* <summary> */
/* Get next packet. */
/* </summary> */
/* <summary>
* Get next packet.
*
* pi: packet iterator to modify
* </summary> */
int pi_next(pi_iterator_t * pi)
{
switch (pi->poc.prg) {

View File

@ -42,15 +42,16 @@ typedef struct {
} pi_comp_t;
typedef struct {
int include[10][10][3][99];
short int *include; /* precise if the packet has been already used (usefull for progression order change) */
int step_l, step_r, step_c, step_p; /* different steps (layer, resolution, component, precinct) to localize the packet in the include vector */
int compno, resno, precno, layno; /* component, resolution, precinct and layer that indentify the packet */
int first;
int first; /* 0 if the first packet */
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 */
} pi_iterator_t; /* packet iterator */
/*
* Create a packet iterator

View File

@ -27,76 +27,28 @@
#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;
unsigned char raw_c; /* temporary buffer where bits are coded or decoded */
unsigned int raw_ct; /* number of bits already read or free to write */
unsigned int raw_lenmax; /* maximum length to decode */
unsigned int raw_len; /* length decoded */
unsigned char *raw_bp; /* pointer to the current position in the buffer */
unsigned char *raw_start; /* pointer to the start of the buffer */
unsigned char *raw_end; /* pointer to the end of the buffer */
/* <summary> */
/* Return the number of bytes already encoded. */
/* </summary> */
/*
* Return the number of bytes already encoded.
*/
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> */
/*
* Initialize raw-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)
{
raw_start = bp;
@ -106,9 +58,9 @@ void raw_init_dec(unsigned char *bp, int len)
raw_ct = 0;
}
/* <summary> */
/* Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN */
/* </summary> */
/*
* Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN
*/
int raw_decode()
{
int d;

View File

@ -23,50 +23,26 @@
* 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 /*
*/
#endif

View File

@ -107,8 +107,7 @@ int t1_getctxno_sc(int f)
int t1_getctxno_mag(int f)
{
return t1_lut_ctxno_mag[(f & T1_SIG_OTH) |
(((f & T1_REFINE) != 0) << 11)];
return t1_lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];
}
int t1_getspb(int f)
@ -119,8 +118,7 @@ int t1_getspb(int f)
int t1_getnmsedec_sig(int x, int bitpos)
{
if (bitpos > T1_NMSEDEC_FRACBITS)
return t1_lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) &
((1 << T1_NMSEDEC_BITS) - 1)];
return t1_lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
else
return t1_lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
}
@ -128,8 +126,7 @@ int t1_getnmsedec_sig(int x, int bitpos)
int t1_getnmsedec_ref(int x, int bitpos)
{
if (bitpos > T1_NMSEDEC_FRACBITS)
return t1_lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) &
((1 << T1_NMSEDEC_BITS) - 1)];
return t1_lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
else
return t1_lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
}
@ -154,17 +151,14 @@ void t1_updateflags(int *fp, int s)
}
}
void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one,
int *nmsedec, char type, int vsc)
void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc)
{
int v, flag;
flag =
vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
: (*fp);
flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
v = int_abs(*dp) & one ? 1 : 0;
if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
mqc_setcurctx(t1_getctxno_zc(flag, orient)); /* ESSAI */
mqc_setcurctx(t1_getctxno_zc(flag, orient)); /* ESSAI */
mqc_bypass_enc(v);
} else {
mqc_setcurctx(t1_getctxno_zc(flag, orient));
@ -172,8 +166,7 @@ void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one,
}
if (v) {
v = *dp < 0 ? 1 : 0;
*nmsedec +=
t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
*nmsedec += t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
mqc_setcurctx(t1_getctxno_sc(flag)); /* ESSAI */
mqc_bypass_enc(v);
@ -188,8 +181,7 @@ void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one,
}
}
void t1_dec_sigpass_step(int *fp, int *dp, int orient, int oneplushalf,
char type, int vsc)
void t1_dec_sigpass_step(int *fp, int *dp, int orient, int oneplushalf, char type, int vsc)
{
int v, flag;
flag =
@ -215,10 +207,9 @@ void t1_dec_sigpass_step(int *fp, int *dp, int orient, int oneplushalf,
}
*fp |= T1_VISIT;
}
} /* VSC and BYPASS by Antonin */
} /* VSC and BYPASS by Antonin */
void t1_enc_sigpass(int w, int h, int bpno, int orient, int *nmsedec,
char type, int cblksty)
void t1_enc_sigpass(int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty)
{
int i, j, k, one, vsc;
*nmsedec = 0;
@ -226,47 +217,39 @@ void t1_enc_sigpass(int w, int h, int bpno, int orient, int *nmsedec,
for (k = 0; k < h; k += 4) {
for (i = 0; i < w; i++) {
for (j = k; j < k + 4 && j < h; j++) {
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
&& (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_enc_sigpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i],
orient, bpno, one, nmsedec, type, vsc);
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_enc_sigpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], orient, bpno, one, nmsedec, type, vsc);
}
}
}
}
void t1_dec_sigpass(int w, int h, int bpno, int orient, char type,
int cblksty)
void t1_dec_sigpass(int w, int h, int bpno, int orient, char type, int cblksty)
{
int i, j, k, one, half, oneplushalf, vsc;
one = 1 << bpno;
half = one >> 1;
oneplushalf = one | half;
for (k = 0; k < h; k += 4) {
for (i = 0; i < w; i++) {
for (j = k; j < k + 4 && j < h; j++) {
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
&& (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_dec_sigpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i],
orient, oneplushalf, type, vsc);
}
}
for (i = 0; i < w; i++) {
for (j = k; j < k + 4 && j < h; j++) {
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
&& (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_dec_sigpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], orient, oneplushalf, type, vsc);
}
}
}
} /* VSC and BYPASS by Antonin */
} /* VSC and BYPASS by Antonin */
void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec,
char type, int vsc)
void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc)
{
int v, flag;
flag =
vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
: (*fp);
flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
*nmsedec +=
t1_getnmsedec_ref(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
*nmsedec += t1_getnmsedec_ref(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
v = int_abs(*dp) & one ? 1 : 0;
if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
mqc_setcurctx(t1_getctxno_mag(flag)); /* ESSAI */
mqc_setcurctx(t1_getctxno_mag(flag)); /* ESSAI */
mqc_bypass_enc(v);
} else {
mqc_setcurctx(t1_getctxno_mag(flag));
@ -276,13 +259,10 @@ void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec,
}
}
void t1_dec_refpass_step(int *fp, int *dp, int poshalf, int neghalf,
char type, int vsc)
void t1_dec_refpass_step(int *fp, int *dp, int poshalf, int neghalf, char type, int vsc)
{
int v, t, flag;
flag =
vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
: (*fp);
flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
if (type == T1_TYPE_RAW) {
mqc_setcurctx(t1_getctxno_mag(flag)); /* ESSAI */
@ -295,10 +275,9 @@ void t1_dec_refpass_step(int *fp, int *dp, int poshalf, int neghalf,
*dp += *dp < 0 ? -t : t;
*fp |= T1_REFINE;
}
} /* VSC and BYPASS by Antonin */
} /* VSC and BYPASS by Antonin */
void t1_enc_refpass(int w, int h, int bpno, int *nmsedec, char type,
int cblksty)
void t1_enc_refpass(int w, int h, int bpno, int *nmsedec, char type, int cblksty)
{
int i, j, k, one, vsc;
*nmsedec = 0;
@ -306,10 +285,8 @@ void t1_enc_refpass(int w, int h, int bpno, int *nmsedec, char type,
for (k = 0; k < h; k += 4) {
for (i = 0; i < w; i++) {
for (j = k; j < k + 4 && j < h; j++) {
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
&& (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_enc_refpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], bpno,
one, nmsedec, type, vsc);
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_enc_refpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], bpno, one, nmsedec, type, vsc);
}
}
}
@ -325,22 +302,17 @@ void t1_dec_refpass(int w, int h, int bpno, char type, int cblksty)
for (k = 0; k < h; k += 4) {
for (i = 0; i < w; i++) {
for (j = k; j < k + 4 && j < h; j++) {
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
&& (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_dec_refpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i],
poshalf, neghalf, type, vsc);
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_dec_refpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], poshalf, neghalf, type, vsc);
}
}
}
} /* VSC and BYPASS by Antonin */
} /* VSC and BYPASS by Antonin */
void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one,
int *nmsedec, int partial, int vsc)
void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc)
{
int v, flag;
flag =
vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
: (*fp);
flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
if (partial)
goto label_partial;
if (!(*fp & (T1_SIG | T1_VISIT))) {
@ -349,8 +321,7 @@ void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one,
mqc_encode(v);
if (v) {
label_partial:
*nmsedec +=
t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
*nmsedec += t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
mqc_setcurctx(t1_getctxno_sc(flag));
v = *dp < 0 ? 1 : 0;
mqc_encode(v ^ t1_getspb(flag));
@ -361,13 +332,10 @@ void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one,
*fp &= ~T1_VISIT;
}
void t1_dec_clnpass_step(int *fp, int *dp, int orient, int oneplushalf,
int partial, int vsc)
void t1_dec_clnpass_step(int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc)
{
int v, flag;
flag =
vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
: (*fp);
flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
if (partial)
goto label_partial;
if (!(flag & (T1_SIG | T1_VISIT))) {
@ -382,10 +350,9 @@ void t1_dec_clnpass_step(int *fp, int *dp, int orient, int oneplushalf,
}
}
*fp &= ~T1_VISIT;
} /* VSC and BYPASS by Antonin */
} /* VSC and BYPASS by Antonin */
void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec,
int cblksty)
void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec, int cblksty)
{
int i, j, k, one, agg, runlen, vsc;
*nmsedec = 0;
@ -395,26 +362,15 @@ void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec,
if (k + 3 < h) {
if (cblksty & J2K_CCP_CBLKSTY_VSC) {
agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
|| t1_flags[1 + k + 1][1 +
i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH)
|| t1_flags[1 + k + 2][1 +
i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH)
|| (t1_flags[1 + k + 3][1 + i] &
(~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
& (T1_SIG | T1_VISIT | T1_SIG_OTH));
|| t1_flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH) || t1_flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH) || (t1_flags[1 + k + 3][1 + i] & (~(T1_SIG_S | T1_SIG_SE
| T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
} else {
agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
|| t1_flags[1 + k + 1][1 +
i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH)
|| t1_flags[1 + k + 2][1 +
i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH)
|| t1_flags[1 + k + 3][1 +
i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH));
|| t1_flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
|| t1_flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
|| t1_flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
}
} else {
agg = 0;
@ -436,11 +392,9 @@ void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec,
runlen = 0;
}
for (j = k + runlen; j < k + 4 && j < h; j++) {
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
&& (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_enc_clnpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i],
orient, bpno, one, nmsedec, agg
&& (j == k + runlen), vsc);
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_enc_clnpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], orient, bpno, one, nmsedec, agg
&& (j == k + runlen), vsc);
}
}
}
@ -458,26 +412,16 @@ void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
if (k + 3 < h) {
if (cblksty & J2K_CCP_CBLKSTY_VSC) {
agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
|| t1_flags[1 + k + 1][1 +
i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH)
|| t1_flags[1 + k + 2][1 +
i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH)
|| (t1_flags[1 + k + 3][1 + i] &
(~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
& (T1_SIG | T1_VISIT | T1_SIG_OTH));
|| t1_flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH) || t1_flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH) || (t1_flags[1 + k + 3][1 + i] & (~(T1_SIG_S | T1_SIG_SE |
T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
} else {
agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
|| t1_flags[1 + k + 1][1 +
i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH)
|| t1_flags[1 + k + 2][1 +
i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH)
|| t1_flags[1 + k + 3][1 +
i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH));
|| t1_flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH) || t1_flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH) || t1_flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH));
}
} else {
agg = 0;
@ -494,11 +438,9 @@ void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
runlen = 0;
}
for (j = k + runlen; j < k + 4 && j < h; j++) {
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
&& (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_dec_clnpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i],
orient, oneplushalf, agg
&& (j == k + runlen), vsc);
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_dec_clnpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], orient, oneplushalf,
agg && (j == k + runlen), vsc);
}
}
}
@ -514,10 +456,9 @@ void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
fprintf(stderr, "warning: bad segmentation symbol %x\n",v);
} */
}
} /* VSC and BYPASS by Antonin */
} /* VSC and BYPASS by Antonin */
double t1_getwmsedec(int nmsedec, int compno, int level, int orient,
int bpno, int qmfbid, double stepsize)
double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize)
{
double w1, w2, wmsedec;
if (qmfbid == 1) {
@ -532,8 +473,7 @@ double t1_getwmsedec(int nmsedec, int compno, int level, int orient,
return wmsedec;
}
void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
int qmfbid, double stepsize, int cblksty)
void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, double stepsize, int cblksty)
{
int i, j;
int w, h;
@ -549,9 +489,9 @@ void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
max = 0;
for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) {
max = int_max(max, int_abs(t1_data[j][i]));
}
for (i = 0; i < w; i++) {
max = int_max(max, int_abs(t1_data[j][i]));
}
}
cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
@ -571,8 +511,7 @@ void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
tcd_pass_t *pass = &cblk->passes[passno];
int correction = 3;
type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
&& (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
T1_TYPE_MQ;
&& (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
switch (passtype) {
case 0:
@ -589,43 +528,55 @@ void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
break;
}
cumwmsedec +=
t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
stepsize);
cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize);
/* Code switch "RESTART" (i.e. TERMALL) */
if ((cblksty & J2K_CCP_CBLKSTY_TERMALL)
&& !((passtype == 2) && (bpno - 1 < 0))) {
if (type == T1_TYPE_RAW)
correction = mqc_bypass_flush_enc();
else
correction = mqc_restart_enc();
pass->term = 1;
} else {
if (((bpno < (cblk->numbps - 4) && (passtype > 0))
|| ((bpno == (cblk->numbps - 4)) && (passtype == 2)))
&& (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
if (type == T1_TYPE_RAW)
correction = mqc_bypass_flush_enc();
else
correction = mqc_restart_enc();
pass->term = 1;
} else {
pass->term = 0;
}
}
if (++passtype == 3) {
passtype = 0;
bpno--;
}
if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0)))
{
if (type == T1_TYPE_RAW)
{
mqc_flush();
correction=1;
/* correction = mqc_bypass_flush_enc();*/
}
else
{ /* correction = mqc_restart_enc(); */
mqc_flush();
correction = 1;
}
pass->term = 1;
} else
{
if (((bpno < (cblk->numbps - 4) && (passtype > 0)) || ((bpno == (cblk->numbps - 4)) && (passtype == 2)))
&& (cblksty & J2K_CCP_CBLKSTY_LAZY))
{
if (type == T1_TYPE_RAW)
{
mqc_flush();
correction=1;
/* correction = mqc_bypass_flush_enc();*/
}
else
{ /* correction = mqc_restart_enc();*/
mqc_flush();
correction=1;
}
pass->term = 1;
} else
{
pass->term = 0;
}
}
if (++passtype == 3)
{
passtype = 0;
bpno--;
}
if (pass->term && bpno > 0) {
type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
&& (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
T1_TYPE_MQ;
&& (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
if (type == T1_TYPE_RAW)
mqc_bypass_init_enc();
else
@ -634,8 +585,7 @@ void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
pass->distortiondec = cumwmsedec;
pass->rate = mqc_numbytes() + correction; /* FIXME */
pass->len =
pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
/* Code-switch "RESET" */
if (cblksty & J2K_CCP_CBLKSTY_RESET)
@ -651,8 +601,7 @@ void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
cblk->totalpasses = passno;
}
void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,
int cblksty)
void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift, int cblksty)
{
int i;
int w, h;
@ -682,8 +631,7 @@ void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,
/* add TONY */
type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2)
&& (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
T1_TYPE_MQ;
&& (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
if (type == T1_TYPE_RAW)
raw_init_dec(seg->data, seg->len);
else
@ -716,166 +664,156 @@ void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,
void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
{
int compno, resno, bandno, precno, cblkno;
int x, y, i, j, orient;
tcd_tilecomp_t *tilec;
tcd_resolution_t *res;
tcd_band_t *band;
tcd_precinct_t *prc;
tcd_cblk_t *cblk;
for (compno = 0; compno < tile->numcomps; compno++) {
tilec = &tile->comps[compno];
for (resno = 0; resno < tilec->numresolutions; resno++) {
res = &tilec->resolutions[resno];
for (bandno = 0; bandno < res->numbands; bandno++) {
band = &res->bands[bandno];
for (precno = 0; precno < res->pw * res->ph; precno++) {
prc = &band->precincts[precno];
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
cblk = &prc->cblks[cblkno];
if (band->bandno == 0) {
x = cblk->x0 - band->x0;
y = cblk->y0 - band->y0;
} else if (band->bandno == 1) {
tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
y = cblk->y0 - band->y0;
} else if (band->bandno == 2) {
tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
x = cblk->x0 - band->x0;
y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
} else { /* if (band->bandno == 3) */
tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
}
if (tcp->tccps[compno].qmfbid == 1) {
for (j = 0; j < cblk->y1 - cblk->y0; j++) {
for (i = 0; i < cblk->x1 - cblk->x0; i++) {
t1_data[j][i] =
tilec->data[(x + i) +
(y + j) * (tilec->x1 -
tilec->
x0)] << T1_NMSEDEC_FRACBITS;
}
}
} else if (tcp->tccps[compno].qmfbid == 0) {
for (j = 0; j < cblk->y1 - cblk->y0; j++) {
for (i = 0; i < cblk->x1 - cblk->x0; i++) {
t1_data[j][i] =
fix_mul(tilec->
data[x + i +
(y + j) * (tilec->x1 - tilec->x0)],
8192 * 8192 / band->stepsize) >> (13 -
T1_NMSEDEC_FRACBITS);
}
}
}
orient = band->bandno; /* FIXME */
if (orient == 2) {
orient = 1;
} else if (orient == 1) {
orient = 2;
}
t1_encode_cblk(cblk, orient, compno,
tilec->numresolutions - 1 - resno,
tcp->tccps[compno].qmfbid, band->stepsize,
tcp->tccps[compno].cblksty);
} /* cblkno */
} /* precno */
} /* bandno */
} /* resno */
} /* compo */
int compno, resno, bandno, precno, cblkno;
int x, y, i, j, orient;
tcd_tilecomp_t *tilec;
tcd_resolution_t *res;
tcd_band_t *band;
tcd_precinct_t *prc;
tcd_cblk_t *cblk;
for (compno = 0; compno < tile->numcomps; compno++) {
tilec = &tile->comps[compno];
for (resno = 0; resno < tilec->numresolutions; resno++) {
res = &tilec->resolutions[resno];
for (bandno = 0; bandno < res->numbands; bandno++) {
band = &res->bands[bandno];
for (precno = 0; precno < res->pw * res->ph; precno++) {
prc = &band->precincts[precno];
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
cblk = &prc->cblks[cblkno];
if (band->bandno == 0) {
x = cblk->x0 - band->x0;
y = cblk->y0 - band->y0;
} else if (band->bandno == 1) {
tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
y = cblk->y0 - band->y0;
} else if (band->bandno == 2) {
tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
x = cblk->x0 - band->x0;
y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
} else { /* if (band->bandno == 3) */
tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
}
if (tcp->tccps[compno].qmfbid == 1) {
for (j = 0; j < cblk->y1 - cblk->y0; j++) {
for (i = 0; i < cblk->x1 - cblk->x0; i++) {
t1_data[j][i] = tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0)] << T1_NMSEDEC_FRACBITS;
}
}
} else if (tcp->tccps[compno].qmfbid == 0) {
for (j = 0; j < cblk->y1 - cblk->y0; j++) {
for (i = 0; i < cblk->x1 - cblk->x0; i++) {
t1_data[j][i] = fix_mul(tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)],
8192 * 8192 / band->stepsize) >> (13 - T1_NMSEDEC_FRACBITS);
}
}
}
orient = band->bandno; /* FIXME */
if (orient == 2) {
orient = 1;
} else if (orient == 1) {
orient = 2;
}
t1_encode_cblk(cblk, orient, compno,
tilec->numresolutions - 1 - resno,
tcp->tccps[compno].qmfbid, band->stepsize,
tcp->tccps[compno].cblksty);
} /* cblkno */
} /* precno */
} /* bandno */
} /* resno */
} /* compo */
}
void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
{
int compno, resno, bandno, precno, cblkno;
for (compno = 0; compno < tile->numcomps; compno++) {
tcd_tilecomp_t *tilec = &tile->comps[compno];
for (resno = 0; resno < tilec->numresolutions; resno++) {
tcd_resolution_t *res = &tilec->resolutions[resno];
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
for (precno = 0; precno < res->pw * res->ph; precno++) {
tcd_precinct_t *prc = &band->precincts[precno];
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
int x, y, i, j, orient;
tcd_cblk_t *cblk = &prc->cblks[cblkno];
orient = band->bandno; /* FIXME */
if (orient == 2)
orient = 1;
else if (orient == 1)
orient = 2;
t1_decode_cblk(cblk, orient, tcp->tccps[compno].roishift,
tcp->tccps[compno].cblksty);
if (band->bandno == 0) {
x = cblk->x0 - band->x0;
y = cblk->y0 - band->y0;
} else if (band->bandno == 1) {
tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
y = cblk->y0 - band->y0;
} else if (band->bandno == 2) {
tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
x = cblk->x0 - band->x0;
y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
} else { /* if (band->bandno == 3) */
tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
}
if (tcp->tccps[compno].roishift) {
int thresh, val, mag;
thresh = 1 << tcp->tccps[compno].roishift;
for (j = 0; j < cblk->y1 - cblk->y0; j++) {
for (i = 0; i < cblk->x1 - cblk->x0; i++) {
val = t1_data[j][i];
mag = int_abs(val);
if (mag >= thresh) {
mag >>= tcp->tccps[compno].roishift;
t1_data[j][i] = val < 0 ? -mag : mag;
}
}
}
}
if (tcp->tccps[compno].qmfbid == 1) {
for (j = 0; j < cblk->y1 - cblk->y0; j++) {
for (i = 0; i < cblk->x1 - cblk->x0; i++) {
tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] =
t1_data[j][i];
}
}
} else if (tcp->tccps[compno].qmfbid == 0) {
for (j = 0; j < cblk->y1 - cblk->y0; j++) {
for (i = 0; i < cblk->x1 - cblk->x0; i++) {
if (t1_data[j][i] == 0) {
tilec->data[x + i +
(y + j) * (tilec->x1 - tilec->x0)] = 0;
} else {
tilec->data[x + i +
(y + j) * (tilec->x1 - tilec->x0)] =
fix_mul(t1_data[j][i] << 13, band->stepsize);
}
}
}
}
}
}
int compno, resno, bandno, precno, cblkno;
for (compno = 0; compno < tile->numcomps; compno++)
{
tcd_tilecomp_t *tilec = &tile->comps[compno];
for (resno = 0; resno < tilec->numresolutions; resno++)
{
tcd_resolution_t *res = &tilec->resolutions[resno];
for (bandno = 0; bandno < res->numbands; bandno++)
{
tcd_band_t *band = &res->bands[bandno];
for (precno = 0; precno < res->pw * res->ph; precno++)
{
tcd_precinct_t *prc = &band->precincts[precno];
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++)
{
int x, y, i, j, orient;
tcd_cblk_t *cblk = &prc->cblks[cblkno];
orient = band->bandno; /* FIXME */
if (orient == 2)
orient = 1;
else if (orient == 1)
orient = 2;
t1_decode_cblk(cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
if (band->bandno == 0) {
x = cblk->x0 - band->x0;
y = cblk->y0 - band->y0;
} else if (band->bandno == 1) {
tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
y = cblk->y0 - band->y0;
} else if (band->bandno == 2) {
tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
x = cblk->x0 - band->x0;
y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
} else { /* if (band->bandno == 3) */
tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
}
if (tcp->tccps[compno].roishift)
{
int thresh, val, mag;
thresh = 1 << tcp->tccps[compno].roishift;
for (j = 0; j < cblk->y1 - cblk->y0; j++) {
for (i = 0; i < cblk->x1 - cblk->x0; i++) {
val = t1_data[j][i];
mag = int_abs(val);
if (mag >= thresh) {
mag >>= tcp->tccps[compno].roishift;
t1_data[j][i] = val < 0 ? -mag : mag;
}
}
}
}
if (tcp->tccps[compno].qmfbid == 1) {
for (j = 0; j < cblk->y1 - cblk->y0; j++) {
for (i = 0; i < cblk->x1 - cblk->x0; i++) {
tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = t1_data[j][i];
}
}
} else /* if (tcp->tccps[compno].qmfbid == 0)*/ {
for (j = 0; j < cblk->y1 - cblk->y0; j++) {
for (i = 0; i < cblk->x1 - cblk->x0; i++) {
if (t1_data[j][i] == 0) {
tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
} else {
tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = fix_mul(t1_data[j][i] << 13, band->stepsize);
}
}
}
}
}
}
}
}
}
}
int t1_init_ctxno_zc(int f, int orient)
@ -884,10 +822,7 @@ int t1_init_ctxno_zc(int f, int orient)
n = 0;
h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
d =
((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) !=
0) +
((f & T1_SIG_SW) != 0);
d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
switch (orient) {
case 2:
t = h;
@ -950,20 +885,12 @@ int t1_init_ctxno_sc(int f)
{
int hc, vc, n;
n = 0;
hc =
int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
(T1_SIG_E | T1_SGN_E)) +
((f & (T1_SIG_W | T1_SGN_W)) ==
(T1_SIG_W | T1_SGN_W)), 1);
vc =
int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
(T1_SIG_N | T1_SGN_N)) +
((f & (T1_SIG_S | T1_SGN_S)) ==
(T1_SIG_S | T1_SGN_S)), 1);
hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) == T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) == (T1_SIG_E | T1_SGN_E)) + ((f & (T1_SIG_W | T1_SGN_W)) ==
(T1_SIG_W | T1_SGN_W)), 1);
vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) == T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) == (T1_SIG_N | T1_SGN_N)) + ((f & (T1_SIG_S | T1_SGN_S)) ==
(T1_SIG_S | T1_SGN_S)), 1);
if (hc < 0) {
hc = -hc;
vc = -vc;
@ -999,20 +926,12 @@ int t1_init_ctxno_mag(int f)
int t1_init_spb(int f)
{
int hc, vc, n;
hc =
int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
(T1_SIG_E | T1_SGN_E)) +
((f & (T1_SIG_W | T1_SGN_W)) ==
(T1_SIG_W | T1_SGN_W)), 1);
vc =
int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
(T1_SIG_N | T1_SGN_N)) +
((f & (T1_SIG_S | T1_SGN_S)) ==
(T1_SIG_S | T1_SGN_S)), 1);
hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) == T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) == (T1_SIG_E | T1_SGN_E)) + ((f & (T1_SIG_W | T1_SGN_W)) ==
(T1_SIG_W | T1_SGN_W)), 1);
vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) == T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) == (T1_SIG_N | T1_SGN_N)) + ((f & (T1_SIG_S | T1_SGN_S)) ==
(T1_SIG_S | T1_SGN_S)), 1);
if (!hc && !vc)
n = 0;
else
@ -1047,31 +966,19 @@ void t1_init_luts()
t = i / pow(2, T1_NMSEDEC_FRACBITS);
u = t;
v = t - 1.5;
t1_lut_nmsedec_sig[i] =
int_max(0,
(int) (floor
((u * u - v * v) * pow(2,
T1_NMSEDEC_FRACBITS) +
0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
t1_lut_nmsedec_sig0[i] =
int_max(0,
(int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) /
pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
t1_lut_nmsedec_sig[i] = int_max(0, (int) (floor((u * u - v * v) * pow(2,T1_NMSEDEC_FRACBITS) +
0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
t1_lut_nmsedec_sig0[i] = int_max(0, (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) /
pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
u = t - 1.0;
if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
v = t - 1.5;
} else {
v = t - 0.5;
}
t1_lut_nmsedec_ref[i] =
int_max(0,
(int) (floor
((u * u - v * v) * pow(2,
T1_NMSEDEC_FRACBITS) +
0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
t1_lut_nmsedec_ref0[i] =
int_max(0,
(int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) /
pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
t1_lut_nmsedec_ref[i] = int_max(0, (int) (floor((u * u - v * v) * pow(2,T1_NMSEDEC_FRACBITS) +
0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
t1_lut_nmsedec_ref0[i] = int_max(0, (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) /
pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
}
}

View File

@ -1,7 +1,7 @@
/*
* 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
* Copyright (c) 2002-2004, Yannick Verschueren
* Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -92,9 +92,22 @@ int t2_getnumpasses()
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)
/*
* Encode a packet of a tile to a destination buffer
*
* Tile : the tile for which to write the packets
* tcp : the tile coding parameters
* compno : Identity of the packet --> component value
* resno : Identity of the packet --> resolution level value
* precno : Identity of the packet --> precinct value
* layno : Identity of the packet --> quality layer value
* dest : the destination buffer
* len : the length of the destination buffer
* info_IM : structure to create an index file
* tileno : number of the tile encoded
*/
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;
@ -102,9 +115,6 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int 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));
@ -135,7 +145,7 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
}
bio_init_enc(c, len);
bio_write(1, 1); /* Empty header bit */
bio_write(1, 1); /* Empty header bit */
/* Writing Packet header */
for (bandno = 0; bandno < res->numbands; bandno++) {
@ -178,14 +188,10 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
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;
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);
@ -198,18 +204,17 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
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 (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 !! */
return -999; /* modified to eliminate longjmp !! */
c += bio_numbytes();
@ -223,7 +228,7 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
c += 2;
}
/* </EPH> */
/* } */
/* Writing the packet body */
for (bandno = 0; bandno < res->numbands; bandno++) {
@ -239,7 +244,7 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
info_PK->disto = layer->disto;
if (info_IM->D_max < info_PK->disto)
info_IM->D_max = info_PK->disto;
} /* </ADD> */
} /* </ADD> */
continue;
}
if (c + layer->len > dest + len) {
@ -256,7 +261,7 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
info_PK->disto = layer->disto;
if (info_IM->D_max < info_PK->disto)
info_IM->D_max = info_PK->disto;
} /* </ADD> */
} /* </ADD> */
}
}
return c - dest;
@ -272,15 +277,25 @@ void t2_init_seg(tcd_seg_t * seg, int cblksty, int first)
if (first)
seg->maxpasses = 10;
else
seg->maxpasses = (((seg - 1)->maxpasses == 1)
|| ((seg - 1)->maxpasses == 10)) ? 2 : 1;
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)
/*
* Decode a packet of a tile from a source buffer
*
* src : the source buffer
* len : the length of the source buffer
* tile : the tile for which to write the packets
* cp : the image coding parameters
* tcp : the tile coding parameters
* compno : Identity of the packet --> component value
* resno : Identity of the packet --> resolution level value
* precno : Identity of the packet --> precinct value
* layno : Identity of the packet --> quality layer value
*/
int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile, j2k_cp_t * cp, j2k_tcp_t * tcp, int compno, int resno, int precno, int layno)
{
int bandno, cblkno;
tcd_tilecomp_t *tilec = &tile->comps[compno];
@ -301,77 +316,151 @@ int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile,
}
}
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;
/* When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
This part deal with this caracteristic
step 1: Read packet header in the saved structure
step 2: (futher) return to codestream for decoding */
if (cp->ppm == 1) /* PPM */
{
c=cp->ppm_data;
bio_init_dec(c,1000);
} else
{
if (tcp->ppt==1) /* PPT */
{
c=tcp->ppt_data;
bio_init_dec(c,1000);
} else /* Normal Case */
{
if (tcp->csty & J2K_CP_CSTY_SOP)
{
if ((*c)!=255 || (*(c+1)!=145)) {printf("Error : expected SOP marker [1]!!!\n");}
/*printf(" %d %d %d %d %d %d %d\n",*c,*(c+1),*(c+6),*(c+7),*(c+8),*(c+9),*(c+10));*/
c += 6;
}
bio_init_dec(c, src + len - c);
}
}
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);
}
present = bio_read(1);
if (!present)
{
bio_inalign();
/* Normal case */
c += bio_numbytes();
if (tcp->csty & J2K_CP_CSTY_EPH)
{
if ((*c)!=255 || (*(c+1)!=146)) {printf("Error : expected EPH marker [1]!!!\n");}
c += 2;
}
/* PPT and PPM dealing */
if (cp->ppm == 1) /* PPM */
{
cp->ppm_data=c;
return 0;
}
if (tcp->ppt==1) /* PPT */
{
tcp->ppt_data=c;
return 0;
}
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;
return -999;
c += bio_numbytes();
if (tcp->csty & J2K_CP_CSTY_EPH) {
c += 2;
if (tcp->csty & J2K_CP_CSTY_EPH) { /* EPH marker */
if ((*c)!=255 || (*(c+1)!=146)) { printf("Error : expected EPH marker [2]!!!\n");
printf(" %d %d %d %d %d %d %d\n",*c,*(c+1),*(c+2),*(c+3),*(c+4),*(c+5),*(c+6));}
c += 2;
}
/* PPT Step 2 : see above for details */
if (cp->ppm==1)
{
cp->ppm_data=c; /* Update pointer */
c=src;
if (tcp->csty & J2K_CP_CSTY_SOP)
{
if ((*c)!=255 || (*(c+1)!=145)) {printf("Error : expected SOP marker [2] !!!\n"); }
c += 6;
}
bio_init_dec(c,src + len - c);
} else
{
if (tcp->ppt==1)
{
tcp->ppt_data=c; /* Update pointer */
c=src;
if (tcp->csty & J2K_CP_CSTY_SOP) /* SOP marker */
{
if ((*c)!=255 || (*(c+1)!=145)) {printf("Error : expected SOP marker [2] !!!\n"); }
c += 6;
}
bio_init_dec(c,src + len - c);
}
}
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
@ -391,21 +480,22 @@ int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile,
}
}
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++;
}
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);
}
}
@ -413,9 +503,21 @@ int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile,
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)
/*
* Encode the packets of a tile to a destination buffer
*
* img : the source image
* cp : the image coding parameters
* tileno : number of the tile encoded
* tile : the tile for which to write the packets
* maxlayers : maximum number of layers
* dest : the destination buffer
* len : the length of the destination buffer
* info_IM : structure to create an index file
*/
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;
@ -423,37 +525,36 @@ int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
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;
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;
}
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++;
}
if ((info_IM->index_write
&& cp->tcps[tileno].csty & J2K_CP_CSTY_SOP)
|| (info_IM->index_write && info_IM->index_on)) {
info_IM->num++;
}
}
}
@ -464,6 +565,7 @@ int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
}
free(pi[pino].comps);
}
free(pi[0].include);
free(pi);
if (e == -999)
return e;
@ -471,35 +573,53 @@ int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
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)
/*
* 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)
{
unsigned char *c = src;
pi_iterator_t *pi;
int pino, compno, e = 0;
int n=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);
while (pi_next(&pi[pino]))
{
// fprintf(stderr,"codeblock %d [%d %d %d %d] pino %d/%d\n",n,pi[pino].layno,pi[pino].resno,pi[pino].compno,pi[pino].precno,pino,cp->tcps[tileno].numpocs);
e = t2_decode_packet(c, src + len - c, tile, cp, &cp->tcps[tileno], pi[pino].compno,
pi[pino].resno, pi[pino].precno, pi[pino].layno);
n++;
if (e == -999) { /* ADD */
break;
} else
c += e;
/* printf("next\n"); */
}
/* 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[0].include);
free(pi);
if (e == -999)
return e;
return e;
else
return c - src;
return c - src;
}

View File

@ -34,18 +34,21 @@
/*
* 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
*
* img : the source image
* cp : the image coding parameters
* tileno : number of the tile encoded
* tile : the tile for which to write the packets
* maxlayers : maximum number of layers
* dest : the destination buffer
* len : the length of the destination buffer
* info_IM : structure to create an index file
*/
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);
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
@ -53,7 +56,6 @@ int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
* 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);
int t2_decode_packets(unsigned char *src, int len, j2k_image_t * img, j2k_cp_t * cp, int tileno, tcd_tile_t * tile);
#endif

File diff suppressed because it is too large Load Diff

View File

@ -33,101 +33,116 @@
#include "tgt.h"
typedef struct {
int numpasses;
int len;
unsigned char *data;
int maxpasses;
int numnewpasses;
int newlen;
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;
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;
int numpasses; /* Number of passes in the layer */
int len; /* len of information */
double disto; /* add for index (Cfr. Marcela) */
unsigned char *data; /* 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];
int x0, y0, x1, y1; /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
int numbps;
int numlenbits;
int len; /* length */
int numpasses; /* number of pass already done for the code-blocks */
int numnewpasses; /* number of pass added to the code-blocks */
int numsegs; /* number of segments */
tcd_seg_t segs[100]; /* segments informations */
unsigned char data[8192]; /* Data */
int numpassesinlayers; /* number of passes in the layer */
tcd_layer_t layers[100]; /* layer information */
int totalpasses; /* total number of passes */
tcd_pass_t passes[100]; /* information about the passes */
} 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;
int x0, y0, x1, y1; /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */
int cw, ch; /* number of precinct in width and heigth */
tcd_cblk_t *cblks; /* code-blocks informations */
tgt_tree_t *incltree; /* inclusion tree */
tgt_tree_t *imsbtree; /* IMSB tree */
} tcd_precinct_t;
typedef struct {
int x0, y0, x1, y1;
int bandno;
tcd_precinct_t *precincts;
int numbps;
int stepsize;
int x0, y0, x1, y1; /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */
int bandno;
tcd_precinct_t *precincts; /* precinct information */
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];
int x0, y0, x1, y1; /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */
int pw, ph;
int numbands; /* number sub-band for the resolution level */
tcd_band_t bands[3]; /* subband information */
} 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;
int x0, y0, x1, y1; /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */
int numresolutions; /* number of resolutions level */
tcd_resolution_t *resolutions; /* resolutions information */
int *data; /* data of the component */
} tcd_tilecomp_t;
typedef struct {
int x0, y0, x1, y1;
int numcomps;
/* int PPT; */
/* int len_ppt; */
tcd_tilecomp_t *comps;
int x0, y0, x1, y1; /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */
int numcomps; /* number of components in tile */
tcd_tilecomp_t *comps; /* Components information */
} tcd_tile_t;
typedef struct {
int tw, th;
tcd_tile_t *tiles;
int tw, th; /* number of tiles in width and heigth */
tcd_tile_t *tiles; /* Tiles information */
} tcd_image_t;
/*
* Initialize the tile coder/decoder
* Initialize the tile coder (reuses the memory allocated by tcd_malloc_encode)
* img: raw image
* cp: coding parameters
* info_IM: creation of index file
* curtileno : number that identifies the tile that will be encoded
*/
void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
/*
* Initialize the tile coder (allocate the memory)
* img: raw image
* cp: coding parameters
* curtileno : number that identifies the tile that will be encoded
*/
void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
/*
* Initialize the tile decoder
* img: raw image
* cp: coding parameters
*/
void tcd_init(j2k_image_t * img, j2k_cp_t * cp);
/*
* Free the memory allocated for encoding
* img: raw image
* cp: coding parameters
* curtileno : number that identifies the tile that will be encoded
*/
void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
/*
@ -135,18 +150,19 @@ void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
* tileno: number that identifies one of the tiles to be encoded
* dest: destination buffer
* len: length of destination buffer
* info_IM: creation of index file
*/
int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
info_image * info_IM);
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
* info_IM: creation of index file
*/
int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
info_image * info_IM);
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

View File

@ -41,6 +41,14 @@ typedef struct {
tgt_node_t *nodes;
} tgt_tree_t;
/*
* Reset a tag-tree (set all leaves to 0)
* tree: tag-tree to reset
*/
void tgt_reset(tgt_tree_t * tree);
/*
* Create a tag-tree
* numleafsh: width of the array of leafs of the tree
@ -48,12 +56,6 @@ typedef struct {
*/
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