#include <openjpeg.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
//#include <unistd.h>
int ceildiv(int a, int b)
int len;
j2k_image_t *img;
j2k_cp_t *cp;
- int w, h, max;
+ j2k_option_t option;
+ int w, wr, wrr, h, hr, hrr, max;
int i, image_type = -1, compno, pad;
int adjust;
if (argc < 3) {
- fprintf(stderr, "usage: %s j2k-file pnm-file\n", argv[0]);
+ fprintf(stderr, "usage: %s j2k-file image-file -reduce n (<- optional)\n", argv[0]);
return 1;
}
dest = argv[2];
+ option.reduce_on = 0;
+ option.reduce_value = 0;
+
+ /* OPTION REDUCE IS ACTIVE */
+ if (argc == 5)
+ {
+ if (strcmp(argv[3],"-reduce"))
+ {
+ fprintf(stderr, "usage: options ""-reduce n"" where n is the factor of reduction [%s]\n",argv[3]);
+ return 1;
+ }
+ option.reduce_on = 1;
+ sscanf(argv[4],"%d",&option.reduce_value);
+ }
+
while (*dest) {
dest++;
}
if (S1 == 'j' && S2 == '2' && S3 == 'k')
{
- if (!j2k_decode(src, len, &img, &cp)) {
+ if (!j2k_decode(src, len, &img, &cp, option)) {
fprintf(stderr, "j2k_to_image: failed to decode image!\n");
return 1;
}
/* / FORMAT : PNM, PGM or PPM / */
/* / / */
/* ---------------------------- / */
-
+
switch (image_type)
{
case 1: /* PNM PGM PPM*/
{
f = fopen(argv[2], "wb");
w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
+ // wr = ceildiv(int_ceildivpow2(img->x1 - img->x0,img->factor),img->comps[0].dx);
+ wr = img->comps[0].w;
+ wrr = int_ceildivpow2(img->comps[0].w ,img->comps[0].factor);
+
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++)
+ // hr = ceildiv(int_ceildivpow2(img->y1 - img->y0,img->factor), img->comps[0].dy);
+ hr = img->comps[0].h;
+ hrr = int_ceildivpow2(img->comps[0].h ,img->comps[0].factor);
+
+ max = img->comps[0].prec > 8 ? 255 : (1 << img->comps[0].prec) - 1;
+
+ img->comps[0].x0 = int_ceildivpow2(img->comps[0].x0 - int_ceildiv(img->x0, img->comps[0].dx),img->comps[0].factor);
+ img->comps[0].y0 = int_ceildivpow2(img->comps[0].y0 - int_ceildiv(img->y0, img->comps[0].dy),img->comps[0].factor);
+
+
+ fprintf(f, "P6\n# %d %d %d %d %d\n%d %d\n%d\n",cp->tcps[cp->tileno[0]].tccps[0].numresolutions, w, h,img->comps[0].x0, img->comps[0].y0, wrr, hrr, max);
+ adjust = img->comps[0].prec > 8 ? img->comps[0].prec - 8 : 0;
+ for (i = 0; i < wrr * hrr; 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;
+ r = img->comps[0].data[i / wrr * wr + i % wrr];
+ 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;
+ g = img->comps[1].data[i / wrr * wr + i % wrr];
+ 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;
+ b = img->comps[2].data[i / wrr * wr + i % wrr];
+ b += (img->comps[2].sgnd ? 1 << (img->comps[2].prec-1):0);
+ b = b >> adjust;
fprintf(f, "%c%c%c", r, g, b);
}
}
f = fopen(name, "wb");
w = ceildiv(img->x1 - img->x0, img->comps[compno].dx);
- // w = ceildiv(int_ceildivpow2(img->x1 - img->x0,img->factor),img->comps[compno].dx);
+ // wr = ceildiv(int_ceildivpow2(img->x1 - img->x0,img->factor),img->comps[compno].dx);
+ wr = img->comps[compno].w;
+ wrr = int_ceildivpow2(img->comps[compno].w ,img->comps[compno].factor);
+
h = ceildiv(img->y1 - img->y0, img->comps[compno].dy);
- // h = ceildiv(int_ceildivpow2(img->y1 - img->y0,img->factor), 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++)
+ // hr = ceildiv(int_ceildivpow2(img->y1 - img->y0,img->factor), img->comps[compno].dy);
+ hr = img->comps[compno].h;
+ hrr = int_ceildivpow2(img->comps[compno].h ,img->comps[compno].factor);
+
+ max = img->comps[compno].prec > 8 ? 255 : (1 << img->comps[compno].prec) - 1;
+
+ img->comps[compno].x0 = int_ceildivpow2(img->comps[compno].x0 - int_ceildiv(img->x0, img->comps[compno].dx),
+ img->comps[compno].factor);
+ img->comps[compno].y0 = int_ceildivpow2(img->comps[compno].y0 - int_ceildiv(img->y0, img->comps[compno].dy),
+ img->comps[compno].factor);
+
+ fprintf(f, "P5\n# %d %d %d %d %d\n%d %d\n%d\n", cp->tcps[cp->tileno[0]].tccps[compno].numresolutions, w,
+ h, img->comps[compno].x0, img->comps[compno].y0,wrr, hrr, max);
+ adjust = img->comps[compno].prec > 8 ? img->comps[compno].prec - 8 : 0;
+ for (i = 0; i < wrr * hrr; i++)
{
char l;
- l = img->comps[compno].data[i];
- l+=(img->comps[compno].sgnd? 1 << (img->comps[compno].prec-1):0);
- l=l>>adjust;
+ l = img->comps[compno].data[i / wrr * wr + i % wrr];
+ l += (img->comps[compno].sgnd ? 1 << (img->comps[compno].prec - 1) : 0);
+ l = l >> adjust;
fprintf(f, "%c", l);
}
fclose(f);
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++)
+ // w = ceildiv(img->x1 - img->x0, comp->dx);
+ // wr = ceildiv(int_ceildivpow2(img->x1 - img->x0,img->factor), comp->dx);
+ w = img->comps[compno].w;
+ wr = int_ceildivpow2(img->comps[compno].w ,img->comps[compno].factor);
+
+ // h = ceildiv(img->y1 - img->y0, comp->dy);
+ // hr = ceildiv(int_ceildivpow2(img->y1 - img->y0,img->factor), comp->dy);
+ h = img->comps[compno].h;
+ hr = int_ceildivpow2(img->comps[compno].h ,img->comps[compno].factor);
+
+ fprintf(f, "PG LM %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, wr, hr);
+ for (i = 0; i < wr * hr; i++)
{
- int v = img->comps[compno].data[i];
+ int v = img->comps[compno].data[i / wr * w + i % wr];
if (comp->prec <= 8)
{
char c = (char) v;
<<-- <<-- <<-- <<-- */
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);
+ // w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
+ // wr = ceildiv(int_ceildivpow2(img->x1 - img->x0,img->factor), img->comps[0].dx);
+ w = img->comps[0].w;
+ wr = int_ceildivpow2(img->comps[0].w ,img->comps[0].factor);
+ // h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
+ // hr = ceildiv(int_ceildivpow2(img->y1 - img->y0,img->factor), img->comps[0].dy);
+ h = img->comps[0].h;
+ hr = int_ceildivpow2(img->comps[0].h ,img->comps[0].factor);
+
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);
+ (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff,
+ (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 8) & 0xff,
+ (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 16) & 0xff,
+ (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 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%c%c", (unsigned char) ((wr) & 0xff),(unsigned char) ((wr) >> 8) & 0xff,
+ (unsigned char) ((wr) >> 16) & 0xff, (unsigned char) ((wr) >> 24) & 0xff);
+ fprintf(f, "%c%c%c%c", (unsigned char) ((hr) & 0xff), (unsigned char) ((hr) >> 8) & 0xff,
+ (unsigned char) ((hr) >> 16) & 0xff, (unsigned char) ((hr) >> 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", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff,
+ (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff,
+ (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff,
+ (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 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++)
+ for (i = 0; i < wr * hr; 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)];
+ /* a modifier */
+ // R = img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
+ R = img->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
+ // G = img->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
+ G = img->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
+ // B = img->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
+ B = img->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
fprintf(f, "%c%c%c", B, G, R);
- if ((i + 1) % w == 0)
+ if ((i + 1) % wr == 0)
{
- for (pad = (3*w)%4?4-(3*w)%4:0 ; pad > 0 ; pad--) /* ADD */
+ for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0 ; pad > 0 ; pad--) /* ADD */
fprintf(f, "%c", 0);
}
}
<<-- <<-- <<-- <<-- */
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);
+ // w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
+ // wr = ceildiv(int_ceildivpow2(img->x1 - img->x0,img->factor), img->comps[0].dx);
+ w = img->comps[0].w;
+ wr = int_ceildivpow2(img->comps[0].w ,img->comps[0].factor);
+
+ // h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
+ // hr = ceildiv(int_ceildivpow2(img->y1 - img->y0,img->factor), img->comps[0].dy);
+ h = img->comps[0].h;
+ hr = int_ceildivpow2(img->comps[0].h ,img->comps[0].factor);
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);
+ (unsigned char) (hr * wr + 54 + 1024 + hr * (wr % 2)) & 0xff,
+ (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 8) & 0xff,
+ (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 16) & 0xff,
+ (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 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%c%c", (unsigned char) ((wr) & 0xff), (unsigned char) ((wr) >> 8) & 0xff,
+ (unsigned char) ((wr) >> 16) & 0xff, (unsigned char) ((wr) >> 24) & 0xff);
+ fprintf(f, "%c%c%c%c", (unsigned char) ((hr) & 0xff), (unsigned char) ((hr) >> 8) & 0xff,
+ (unsigned char) ((hr) >> 16) & 0xff, (unsigned char) ((hr) >> 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", (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
+ (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) & 0xff,
+ (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) & 0xff,
+ (unsigned char) ((hr * wr + hr * (wr % 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", i, i, i, 0);
}
- for (i = 0; i < w * h; i++)
+ for (i = 0; i < wr * hr; 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);
+ /* a modifier !! */
+ // fprintf(f, "%c", img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]);
+ fprintf(f, "%c", img->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]);
+ /*if (((i + 1) % w == 0 && w % 2))
+ fprintf(f, "%c", 0);*/
+ if ((i + 1) % wr == 0)
+ {
+ for (pad = wr % 4 ? 4 - wr % 4 : 0 ; pad > 0 ; pad--) /* ADD */
+ fprintf(f, "%c", 0);
+ }
}
break;
default :
/* <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)
+void dwt_decode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l, int stop)
{
int i, j;
int rw; /* width 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--) {
+ for (i = l - 1; i >= stop; i--) {
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 */
/* <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)
+void dwt_decode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l, int stop)
{
int i, j;
int rw; /* width 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--) {
+ for (i = l - 1; i >= stop; i--) {
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 */
* 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, int stop);
/*
* Get the gain of a subband for the reversible DWT
* 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, int stop);
/*
* Get the gain of a subband for the irreversible DWT
* orient: number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH)
void j2k_write_soc()
{
- /* fprintf(stderr, "%.8x: SOC\n", cio_tell()); */
cio_write(J2K_MS_SOC, 2);
}
void j2k_read_soc()
{
- fprintf(stderr, "%.8x: SOC\n", cio_tell()-2);
- j2k_state = J2K_STATE_MHSIZ;
+ j2k_state = J2K_STATE_MHSIZ;
}
void j2k_write_siz()
{
int i;
int lenp, len;
- /* fprintf(stderr, "%.8x: SIZ\n", cio_tell()); */
+
cio_write(J2K_MS_SIZ, 2); /* SIZ */
lenp = cio_tell();
cio_skip(2);
void j2k_read_siz()
{
int len, i;
- fprintf(stderr, "%.8x: SIZ\n", cio_tell());
+
len = cio_read(2); /* Lsiz */
cio_read(2); /* Rsiz (capabilities) */
j2k_img->x1 = cio_read(4); /* Xsiz */
j2k_img->comps[i].dy = cio_read(1); /* YRsiz_i */
w = int_ceildiv(j2k_img->x1 - j2k_img->x0, j2k_img->comps[i].dx);
h = int_ceildiv(j2k_img->y1 - j2k_img->y0, j2k_img->comps[i].dy);
- j2k_img->comps[i].data = (int *) malloc(sizeof(int) * w * h);
+ j2k_img->comps[i].resno_decoded = 0; /* number of resolution decoded */
+ j2k_img->comps[i].factor = 0; /* reducing factor by component */
}
j2k_cp->tw = int_ceildiv(j2k_img->x1 - j2k_cp->tx0, j2k_cp->tdx);
j2k_cp->th = int_ceildiv(j2k_img->y1 - j2k_cp->ty0, j2k_cp->tdy);
j2k_cp->tcps = (j2k_tcp_t *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(j2k_tcp_t));
+ j2k_cp->tileno = (int*)calloc(j2k_cp->tw * j2k_cp->th, sizeof(int));
+ j2k_cp->tileno_size = 0;
+
for (i=0; i<j2k_cp->tw * j2k_cp->th; i++)
{
j2k_cp->tcps[i].POC=0;
int lenp, len;
char str[256];
sprintf(str, "%s", j2k_cp->comment);
- /* fprintf(stderr, "%.8x: COM\n", cio_tell()); */
+
cio_write(J2K_MS_COM, 2);
lenp = cio_tell();
cio_skip(2);
void j2k_read_com()
{
int len;
- fprintf(stderr, "%.8x: COM\n", cio_tell()-2);
+
len = cio_read(2);
cio_skip(len - 2);
{
j2k_tcp_t *tcp;
int lenp, len;
- /* fprintf(stderr, "%.8x: COD\n", cio_tell()+pos_correction); */
+
cio_write(J2K_MS_COD, 2); /* COD */
lenp = cio_tell();
{
int len, i, pos;
j2k_tcp_t *tcp;
- fprintf(stderr, "%.8x: COD\n", cio_tell()-2);
+
tcp = j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
len = cio_read(2); /* Lcod */
tcp->csty = cio_read(1); /* Scod */
tcp->prg = cio_read(1); /* SGcod (A) */
tcp->numlayers = cio_read(2); /* SGcod (B) */
tcp->mct = cio_read(1); /* SGcod (C) */
- /*tcp->numpocs=0;
- tcp->POC=0;*/
+
pos = cio_tell();
for (i = 0; i < j2k_img->numcomps; i++) {
tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
{
j2k_tcp_t *tcp;
int lenp, len;
- /* fprintf(stderr, "%.8x: COC\n", cio_tell()+pos_correction); */
+
cio_write(J2K_MS_COC, 2); /* COC */
lenp = cio_tell();
cio_skip(2);
{
int len, compno;
j2k_tcp_t *tcp;
- fprintf(stderr, "%.8x: COC\n", cio_tell());
+
tcp = j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
len = cio_read(2); /* Lcoc */
compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* Ccoc */
j2k_tccp_t *tccp;
int bandno, numbands;
int expn, mant;
+
tcp = &j2k_cp->tcps[j2k_curtileno];
tccp = &tcp->tccps[compno];
j2k_tcp_t *tcp;
j2k_tccp_t *tccp;
int bandno, numbands;
+
tcp = j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
tccp = &tcp->tccps[compno];
tmp = cio_read(1); /* Sqcx */
void j2k_write_qcd()
{
int lenp, len;
- /* fprintf(stderr, "%.8x: QCD\n", cio_tell()+pos_correction); */
+
cio_write(J2K_MS_QCD, 2); /* QCD */
lenp = cio_tell();
cio_skip(2);
void j2k_read_qcd()
{
int len, i, pos;
- fprintf(stderr, "%.8x: QCD\n", cio_tell()-2);
+
len = cio_read(2); /* Lqcd */
pos = cio_tell();
for (i = 0; i < j2k_img->numcomps; i++) {
void j2k_write_qcc(int compno)
{
int lenp, len;
- /* fprintf(stderr, "%.8x: QCC\n", cio_tell()+pos_correction); */
+
cio_write(J2K_MS_QCC, 2); /* QCC */
lenp = cio_tell();
cio_skip(2);
void j2k_read_qcc()
{
int len, compno;
- fprintf(stderr, "%.8x: QCC\n", cio_tell()-2);
+
len = cio_read(2); /* Lqcc */
compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* Cqcc */
j2k_read_qcx(compno, len - 2 - (j2k_img->numcomps <= 256 ? 1 : 2));
int len, numpchgs, i;
j2k_tcp_t *tcp;
j2k_tccp_t *tccp;
- fprintf(stderr, "%.8x: POC\n", cio_tell() + pos_correction);
+
tcp = &j2k_cp->tcps[j2k_curtileno];
tccp = &tcp->tccps[0];
numpchgs = tcp->numpocs;
int len, numpchgs, i, old_poc;
j2k_tcp_t *tcp;
j2k_tccp_t *tccp;
- fprintf(stderr, "%.8x: POC\n", cio_tell()-2);
+
tcp = j2k_state==J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
old_poc = tcp->POC ? tcp->numpocs+1 : 0;
- printf("old_poc %d\n",old_poc);
tcp->POC = 1;
tccp = &tcp->tccps[0];
len = cio_read(2); /* Lpoc */
poc->resno1 = int_min(cio_read(1), tccp->numresolutions); /* REpoc_i */
poc->compno1 = int_min(cio_read(j2k_img->numcomps <= 256 ? 1 : 2), j2k_img->numcomps); /* CEpoc_i */
poc->prg = cio_read(1); /* Ppoc_i */
- printf("res0 %d comp0 %d lay1 %d res1 %d comp1 %d prg %d\n",poc->resno0,poc->compno0,poc->layno1,poc->resno1,poc->compno1,poc->prg);
}
tcp->numpocs = numpchgs+old_poc-1;
void j2k_read_crg()
{
int len, i, Xcrg_i, Ycrg_i;
- fprintf(stderr, "%.8x: CRG\n", cio_tell() - 2);
+
len = cio_read(2); /* Lcrg */
for (i=0;i<j2k_img->numcomps;i++)
{
{
int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
long int Ttlm_i, Ptlm_i;
- fprintf(stderr, "%.8x: TLM\n", cio_tell() - 2);
+
len = cio_read(2); /* Ltlm */
Ztlm = cio_read(1); /* Ztlm */
Stlm = cio_read(1); /* Stlm */
void j2k_read_plm()
{
- int len, i, Zplm, Nplm, add, packet_len=0;
- fprintf(stderr, "%.8x: PLM\n", cio_tell() - 2);
+ int len, i, Zplm, Nplm, add, packet_len = 0;
+
len = cio_read(2); /* Lplm */
Zplm = cio_read(1); /* Zplm */
len-=3;
void j2k_read_plt()
{
int len, i, Zplt, packet_len=0, add;
- fprintf(stderr, "%.8x: PLT\n", cio_tell() - 2);
+
len = cio_read(2); /* Lplt */
Zplt=cio_read(1); /* Zplt */
for (i=len-3;i>0;i--)
{
int len, Z_ppm, i, j;
int N_ppm;
- fprintf(stderr, "%.8x: PPM\n", cio_tell() - 2);
+
len = cio_read(2);
j2k_cp->ppm=1;
{
int len, Z_ppt, i, j=0;
j2k_tcp_t *tcp;
- fprintf(stderr, "%.8x: PPT\n", cio_tell() - 2);
+
len = cio_read(2);
Z_ppt = cio_read(1);
tcp=&j2k_cp->tcps[j2k_curtileno];
void j2k_write_sot()
{
int lenp, len;
- /* fprintf(stderr, "%.8x: SOT\n", cio_tell()+pos_correction); */
+
j2k_sot_start = cio_tell();
cio_write(J2K_MS_SOT, 2); /* SOT */
lenp = cio_tell();
int len, tileno, totlen, partno, numparts, i;
j2k_tcp_t *tcp;
j2k_tccp_t *tmp;
+ char status = 0;
- fprintf(stderr, "%.8x: SOT\n", cio_tell()-2);
len = cio_read(2);
tileno = cio_read(2);
+
+ if (j2k_cp->tileno_size == 0)
+ {
+ j2k_cp->tileno[j2k_cp->tileno_size] = tileno;
+ j2k_cp->tileno_size++;
+ }
+ else
+ {
+ i = 0;
+ while (i < j2k_cp->tileno_size && status == 0)
+ {
+ status = j2k_cp->tileno[i] == tileno ? 1 : 0;
+ i++;
+ }
+ if (status == 0)
+ {
+ j2k_cp->tileno[j2k_cp->tileno_size] = tileno;
+ j2k_cp->tileno_size++;
+ }
+ }
+
totlen = cio_read(4);
if (!totlen)
totlen = cio_numbytesleft() + 8;
j2k_tcp_t *tcp;
static int j2k_sod_start;
- /* fprintf(stderr, "%.8x: SOD\n", cio_tell()+pos_correction); */
cio_write(J2K_MS_SOD, 2);
if (j2k_curtileno == 0) {
j2k_sod_start = cio_tell() + pos_correction;
/* INDEX >> */
if (info_IM.index_on) {
info_IM.tile[j2k_curtileno].end_header = cio_tell() + pos_correction - 1;
- info_IM.tile[j2k_curtileno].packet = (info_packet *) calloc(info_IM.Comp * info_IM.Layer * (info_IM.Decomposition + 1) *
- 10,sizeof(info_packet));
+ info_IM.tile[j2k_curtileno].packet = (info_packet *) calloc(info_IM.Comp * info_IM.Layer *
+ (info_IM.Decomposition + 1) * 100,sizeof(info_packet));
}
/* << INDEX */
tcp = &j2k_cp->tcps[j2k_curtileno];
for (layno = 0; layno < tcp->numlayers; layno++) {
tcp->rates[layno] -= (j2k_sod_start / (j2k_cp->th * j2k_cp->tw));
- /* fprintf(stderr, "tcp->rates[%d]=%d\n", layno, tcp->rates[layno]); */
}
info_IM.num = 0;
void j2k_read_sod()
{
- int len, truncate = 0;
+ int len, truncate = 0, i;
unsigned char *data;
- fprintf(stderr, "%.8x: SOD\n", cio_tell()-2);
len = int_min(j2k_eot - cio_getbp(), cio_numbytesleft() + 1);
if (len == cio_numbytesleft() + 1)
truncate = 1; /* Case of a truncate codestream */
-
+
+ data = (unsigned char*)malloc((j2k_tile_len[j2k_curtileno] + len) * sizeof(unsigned char));
+ for (i=0; i<j2k_tile_len[j2k_curtileno]; i++)
+ data[i] = j2k_tile_data[j2k_curtileno][i];
+ for (i=0 ; i<len ; i++)
+ data[i+j2k_tile_len[j2k_curtileno]] = cio_read(1);
+
j2k_tile_len[j2k_curtileno] += len;
- data = (unsigned char *) realloc(j2k_tile_data[j2k_curtileno], j2k_tile_len[j2k_curtileno]);
- memcpy(data, cio_getbp(), len);
- j2k_tile_data[j2k_curtileno] = data;
-
- cio_skip(len);
+ free(j2k_tile_data[j2k_curtileno]);
+ j2k_tile_data[j2k_curtileno] = data;
+ data=NULL;
if (!truncate)
j2k_state = J2K_STATE_TPHSOT;
void j2k_write_rgn(int compno, int tileno)
{
j2k_tcp_t *tcp = &j2k_cp->tcps[tileno];
- /* fprintf(stderr, "%.8x: RGN\n",cio_tell()+pos_correction); */
+
cio_write(J2K_MS_RGN, 2); /* RGN */
cio_write(j2k_img->numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Crgn */
{
int len, compno, roisty;
j2k_tcp_t *tcp;
- fprintf(stderr, "%.8x: RGN\n", cio_tell() - 2);
+
tcp = j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
len = cio_read(2); /* Lrgn */
compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* Crgn */
void j2k_read_eoc()
{
- int tileno;
- fprintf(stderr, "%.8x: EOC\n", cio_tell()-2);
- /* j2k_dump_image(j2k_img); */
- /* j2k_dump_cp(j2k_img, j2k_cp); */
+ int i, tileno;
tcd_init(j2k_img, j2k_cp);
- for (tileno = 0; tileno < j2k_cp->tw * j2k_cp->th; tileno++) {
- tcd_decode_tile(j2k_tile_data[tileno], j2k_tile_len[tileno], tileno);
+
+ for (i = 0; i < j2k_cp->tileno_size; i++) {
+ tileno = j2k_cp->tileno[i];
+ tcd_decode_tile(j2k_tile_data[tileno], j2k_tile_len[tileno], tileno);
}
j2k_state = J2K_STATE_MT;
fprintf(INDEX, "%d %d\n", info_IM.pdx, info_IM.pdy);
fprintf(INDEX, "%d\n", info_IM.Main_head_end);
fprintf(INDEX, "%d\n", info_IM.codestream_size);
-
+ fprintf(INDEX, "%f\n",info_IM.D_max);
for (tileno = 0; tileno < j2k_cp->tw * j2k_cp->th; tileno++) {
fprintf(INDEX, "%d %d %d %d", info_IM.tile[tileno].num_tile,
info_IM.tile[tileno].start_pos,
precno <
info_IM.tile[tileno].pw * info_IM.tile[tileno].ph;
precno++) {
- fprintf(INDEX,"%d %d %d %d %d %d %d %d %.04f\n",pack_nb,tileno,layno,resno,compno,precno,info_IM.tile[tileno].packet[pack_nb].start_pos,info_IM.tile[tileno].packet[pack_nb].end_pos,info_IM.tile[tileno].packet[pack_nb].disto);
+ fprintf(INDEX,"%d %d %d %d %d %d %d %d %.08f\n",pack_nb,tileno,layno,resno,compno,precno,info_IM.tile[tileno].packet[pack_nb].start_pos,info_IM.tile[tileno].packet[pack_nb].end_pos,info_IM.tile[tileno].packet[pack_nb].disto/info_IM.D_max);
/*fprintf(INDEX, "%d %d %d %d %d %d %d %d\n", pack_nb, tileno, layno, resno, compno, precno, info_IM.tile[tileno].packet[pack_nb].start_pos, info_IM.tile[tileno].packet[pack_nb].end_pos);*/
pack_nb++;
}
return e;
}
-LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t ** img, j2k_cp_t ** cp)
+LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t ** img, j2k_cp_t ** cp, j2k_option_t option)
{
if (setjmp(j2k_error)) {
j2k_cp = (j2k_cp_t *) malloc(sizeof(j2k_cp_t));
*img = j2k_img;
*cp = j2k_cp;
+ /* Option */
+ j2k_cp->reduce_on = option.reduce_on;
+ j2k_cp->reduce_value = option.reduce_value;
+
j2k_state = J2K_STATE_MHSOC;
cio_init(src, len);
j2k_cp = (j2k_cp_t *) malloc(sizeof(j2k_cp_t));
*img = j2k_img;
*cp = j2k_cp;
+
j2k_state = J2K_STATE_MHSOC;
cio_init(src, len);
#define J2K_CCP_QNTSTY_SIQNT 1
#define J2K_CCP_QNTSTY_SEQNT 2
+typedef struct {
+ int reduce_on; /* option reduce is used if reduce = 1 */
+ int reduce_value; /* if option reduce is used -> original dimension divided by 2^value */
+} j2k_option_t;
+
typedef struct {
int dx, dy; /* XRsiz, YRsiz */
+ int w, h; /* width and height of data */
+ int x0, y0; /* offset of the component compare to the whole image */
int prec; /* precision */
int bpp; /* deapth of image in bits */
int sgnd; /* signed */
- int *data; /* image-component data */
+ int resno_decoded; /* number of decoded resolution */
+ int factor; /* number of division by 2 of the out image compare to the original size of image */
+ int *data; /* image-component data */
} j2k_comp_t;
typedef struct {
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_comp_t *comps; /* image-components */
} j2k_image_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 reduce_on; /* option reduce is used if reduce = 1 */
+ int reduce_value; /* if option reduce is used -> original dimension divided by 2^value */
int tx0, ty0; /* XTOsiz, YTOsiz */
int tdx, tdy; /* XTsiz, YTsiz */
char *comment; /* comment for coding */
- int tw, th;
+ int tw, th; /* number of tiles in width and heigth */
+ int *tileno; /* ID number of the tiles present in the codestream */
+ int tileno_size; /* size of the vector tileno */
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) */
* 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 ** img, j2k_cp_t ** cp);
+LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t ** img, j2k_cp_t ** cp, j2k_option_t option);
/*
if (info_IM->index_write && info_IM->index_on) {
info_tile *info_TL = &info_IM->tile[tileno];
info_packet *info_PK = &info_TL->packet[info_IM->num];
- info_PK->disto = layer->disto;
+ info_PK->disto += layer->disto;
if (info_IM->D_max < info_PK->disto)
info_IM->D_max = info_PK->disto;
} /* </ADD> */
if (info_IM->index_write && info_IM->index_on) {
info_tile *info_TL = &info_IM->tile[tileno];
info_packet *info_PK = &info_TL->packet[info_IM->num];
- info_PK->disto = layer->disto;
+ info_PK->disto += layer->disto;
if (info_IM->D_max < info_PK->disto)
info_IM->D_max = info_PK->disto;
} /* </ADD> */
*/
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];
- tcd_resolution_t *res = &tilec->resolutions[resno];
- unsigned char *c = src;
- int present;
-
- if (layno == 0) {
- for (bandno = 0; bandno < res->numbands; bandno++) {
- tcd_band_t *band = &res->bands[bandno];
- tcd_precinct_t *prc = &band->precincts[precno];
- tgt_reset(prc->incltree);
- tgt_reset(prc->imsbtree);
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
- tcd_cblk_t *cblk = &prc->cblks[cblkno];
- cblk->numsegs = 0;
- }
- }
+ int bandno, cblkno;
+ tcd_tilecomp_t *tilec = &tile->comps[compno];
+ tcd_resolution_t *res = &tilec->resolutions[resno];
+ unsigned char *c = src;
+ int present;
+
+ if (layno == 0)
+ {
+ for (bandno = 0; bandno < res->numbands; bandno++)
+ {
+ tcd_band_t *band = &res->bands[bandno];
+ tcd_precinct_t *prc = &band->precincts[precno];
+ tgt_reset(prc->incltree);
+ tgt_reset(prc->imsbtree);
+ for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++)
+ {
+ tcd_cblk_t *cblk = &prc->cblks[cblkno];
+ cblk->numsegs = 0;
+ }
}
-
- /* 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;
+ }
+
+ /* 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
+ } else /* Normal Case */
{
- if (tcp->ppt==1) /* PPT */
+ if (tcp->csty & J2K_CP_CSTY_SOP)
{
- 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);
- }
+ if ((*c)!=255 || (*(c+1)!=145)) {printf("Error : expected SOP marker [1]!!!\n");}
+ c += 6;
+ }
+ bio_init_dec(c, src + len - c);
}
-
- 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) {
+ }
+
+ 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) {
+ /* if cblk not included */
+ if (!included)
+ {
cblk->numnewpasses = 0;
continue;
}
- /* if cblk not yet included --> zero-bitplane tagtree */
- if (!cblk->numsegs) {
+ /* if cblk not yet included --> zero-bitplane tagtree */
+ if (!cblk->numsegs)
+ {
int i, numimsbs;
for (i = 0; !tgt_decode(prc->imsbtree, cblkno, i); i++) {
}
c += bio_numbytes();
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;
+ if ((*c)!=255 || (*(c+1)!=146)) {
+ printf("Error : expected EPH marker [2]!!!\n"); }
+ c += 2;
}
/* PPT Step 2 : see above for details */
*/
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);
+ 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, &cp->tcps[tileno], pi[pino].compno,
+ pi[pino].resno, pi[pino].precno, pi[pino].layno);
- for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
- 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);
+ /* progression in resolution */
+ img->comps[pi[pino].compno].resno_decoded = e > 0 ? int_max(pi[pino].resno, img->comps[pi[pino].compno].resno_decoded) : img->comps[pi[pino].compno].resno_decoded;
+ n++;
- if (e == -999)
- return e;
- else
- return c - src;
+ if (e == -999) { /* ADD */
+ break;
+ } else
+ c += e;
+ }
+
+ /* FREE space memory taken by pi */
+ for (compno = 0; compno < pi[pino].numcomps; compno++) {
+ free(pi[pino].comps[compno].resolutions);
+ }
+ free(pi[pino].comps);
+ }
+ free(pi[0].include);
+ free(pi);
+
+ if (e == -999)
+ return e;
+ else
+ return c - src;
}
tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
- tilec->data = (int *) malloc(sizeof(int) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
+ tilec->data = (int *) malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
tilec->numresolutions = tccp->numresolutions;
tilec->resolutions = (tcd_resolution_t *) malloc(tilec->numresolutions * sizeof(tcd_resolution_t));
tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
- tilec->data = (int *) malloc(sizeof(int) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
+ tilec->data = (int *) malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
tilec->numresolutions = tccp->numresolutions;
/* tilec->resolutions=(tcd_resolution_t*)realloc(tilec->resolutions,tilec->numresolutions*sizeof(tcd_resolution_t)); */
for (resno = 0; resno < tilec->numresolutions; resno++) {
void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
{
- int tileno, compno, resno, bandno, precno, cblkno;
+ int tileno, compno, resno, bandno, precno, cblkno, i;
+ unsigned int x0=0 , y0=0, x1 = 0, y1 = 0, w, h, j;
tcd_img = img;
tcd_cp = cp;
tcd_image.tw = cp->tw;
tcd_image.th = cp->th;
tcd_image.tiles = (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));
- for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
+
+ /*for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
j2k_tcp_t *tcp = &cp->tcps[tileno];
- tcd_tile_t *tile = &tcd_image.tiles[tileno];
+ tcd_tile_t *tile = &tcd_image.tiles[tileno];*/
+
+ for (i = 0 ; i < cp->tileno_size ; i++){
+ j2k_tcp_t *tcp = &cp->tcps[cp->tileno[i]];
+ tcd_tile_t *tile = &tcd_image.tiles[cp->tileno[i]];
+ tileno = cp->tileno[i];
+
+
// int previous_x0, previous_x1, previous_y0, previous_y1;
/* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
int p = tileno % cp->tw; /* si numerotation matricielle .. */
tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
- tilec->data = (int *) malloc(sizeof(int) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
+ tilec->data = (int *) malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
tilec->numresolutions = tccp->numresolutions;
tilec->resolutions = (tcd_resolution_t *) malloc(tilec->numresolutions * sizeof(tcd_resolution_t));
for (resno = 0; resno < tilec->numresolutions; resno++) {
}
}
/* tcd_dump(&tcd_image,0); */
+
+
+ /* Allocate place to store the date decoded = fianl image */
+ /* Place limited by the tile really present in the codestream */
+ for (i = 0; i < img->numcomps; i++) {
+ for (j = 0; j < cp->tileno_size; j++) {
+ tileno = cp->tileno[j];
+ x0 = j == 0 ? tcd_image.tiles[tileno].x0 : int_min(x0 , tcd_image.tiles[tileno].x0);
+ y0 = j == 0 ? tcd_image.tiles[tileno].y0 : int_min(y0 , tcd_image.tiles[tileno].y0);
+ x1 = j == 0 ? tcd_image.tiles[tileno].x1 : int_max(x1 , tcd_image.tiles[tileno].x1);
+ y1 = j == 0 ? tcd_image.tiles[tileno].y1 : int_max(y1 , tcd_image.tiles[tileno].y1);
+ }
+ w = int_ceildiv(x1 - x0, img->comps[i].dx);
+ h = int_ceildiv(y1 - y0, img->comps[i].dy);
+ img->comps[i].data = (int *) calloc(w * h,sizeof(int));
+ img->comps[i].w = w;
+ img->comps[i].h = h;
+ img->comps[i].x0 = x0;
+ img->comps[i].y0 = y0;
+ }
+
}
void tcd_makelayer_fixed(int layno, int final) {
{
int compno;
int l;
- clock_t time7;
+ clock_t time;
tcd_tile_t *tile;
j2k_tcp_t *tcp = &tcd_cp->tcps[0];
j2k_tccp_t *tccp = &tcp->tccps[0];
}
/* << INDEX */
/*---------------TILE-------------------*/
- time7 = clock();
+ time = clock();
for (compno = 0; compno < tile->numcomps; compno++) {
FILE *src;
l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile, tcd_tcp->numlayers, dest, len, info_IM);
/*---------------CLEAN-------------------*/
- time7 = clock() - time7;
- printf("total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC, (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
+ time = clock() - time;
+ printf("total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC, (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
for (compno = 0; compno < tile->numcomps; compno++) {
tilec = &tile->comps[compno];
int l;
int compno;
int eof = 0;
- clock_t time1, time2, time3, time4, time5, time6;
+ clock_t time;
tcd_tile_t *tile;
tcd_tileno = tileno;
tcd_tcp = &tcd_cp->tcps[tileno];
tile = tcd_tile;
- time6 = clock();
+ time = clock();
+
+ fprintf(stderr,"tile decoding time %d/%d: ", tileno + 1, tcd_cp->tw * tcd_cp->th);
- time1 = clock();
- printf("tile decoding time %d/%d:\n", tileno + 1, tcd_cp->tw * tcd_cp->th);
+ /*--------------TIER2------------------*/
l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);
eof = 1;
fprintf(stderr, "tcd_decode: incomplete bistream\n");
}
- time1 = clock() - time1;
-
- /* printf("tier 2: %ld.%.3ld s\n", time1/CLOCKS_PER_SEC, (time1%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
- time2 = clock();
+ /*------------------TIER1-----------------*/
t1_init_luts();
t1_decode_cblks(tile, tcd_tcp);
- time2 = clock() - time2;
- /* printf("tier 1: %ld.%.3ld s\n", time2/CLOCKS_PER_SEC, (time2%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
- time3 = clock();
+ /*----------------DWT---------------------*/
+
for (compno = 0; compno < tile->numcomps; compno++)
{
tcd_tilecomp_t *tilec = &tile->comps[compno];
+ if (tcd_cp->reduce_on == 1)
+ {
+ tcd_img->comps[compno].resno_decoded = tile->comps[compno].numresolutions - tcd_cp->reduce_value - 1;
+ }
+
+
if (tcd_tcp->tccps[compno].qmfbid == 1)
{
- dwt_decode(tilec->data, tilec->x1 - tilec->x0,tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
- } else
- { /*if (tcd_tcp->tccps[compno].qmfbid == 0) {*/
- dwt_decode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
+ dwt_decode(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1,
+ tilec->numresolutions - 1 - tcd_img->comps[compno].resno_decoded);
+ } else {
+ dwt_decode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1,
+ tilec->numresolutions - 1 - tcd_img->comps[compno].resno_decoded);
}
- }
- time3 = clock() - time3;
- /* printf("dwt: %ld.%.3ld s\n", time3/CLOCKS_PER_SEC, (time3%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
+ if (tile->comps[compno].numresolutions > 0)
+ tcd_img->comps[compno].factor = tile->comps[compno].numresolutions - (tcd_img->comps[compno].resno_decoded + 1);
+ }
+
+ /*----------------MCT-------------------*/
- time4 = clock();
if (tcd_tcp->mct) {
- if (tcd_tcp->tccps[0].qmfbid == 0) {
- mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
- (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
+ if (tcd_tcp->tccps[0].qmfbid == 1) {
+ mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
+ (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
} else {
- mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
- (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
+ mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
+ (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
}
}
- time4 = clock() - time4;
- /* printf("mct: %ld.%.3ld s\n", time4/CLOCKS_PER_SEC, (time4%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
- time5 = clock();
- for (compno = 0; compno < tile->numcomps; compno++) {
- tcd_tilecomp_t *tilec = &tile->comps[compno];
- int adjust = tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec - 1);
- int min = tcd_img->comps[compno].sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;
- int max = tcd_img->comps[compno].sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) - 1 : (1 << tcd_img->comps[compno].prec) - 1;
- int tw = tilec->x1 - tilec->x0;
- int w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
- int i, j;
- int offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
- int offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
+ /*---------------TILE-------------------*/
- for (j = tilec->y0; j < tilec->y1; j++) {
- for (i = tilec->x0; i < tilec->x1; i++) {
- int v;
- if (tcd_tcp->tccps[compno].qmfbid == 1) {
- v = tilec->data[i - tilec->x0 + (j - tilec->y0) * tw];
- } else { /* if (tcd_tcp->tccps[compno].qmfbid == 0) */
- v = tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] >> 13;
- }
- v += adjust;
-
- /* tcd_img->comps[compno].data[i+j*w]=int_clamp(v, min, max); */
- tcd_img->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max); /* change ! */
- }
- }
+ for (compno = 0; compno < tile->numcomps; compno++) {
+ tcd_tilecomp_t *tilec = &tile->comps[compno];
+ tcd_resolution_t *res = &tilec->resolutions[tcd_img->comps[compno].resno_decoded];
+ int adjust = tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec - 1);
+ int min = tcd_img->comps[compno].sgnd ? - (1 << (tcd_img->comps[compno].prec - 1)) : 0;
+ int max = tcd_img->comps[compno].sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) - 1 : (1 << tcd_img->comps[compno].prec) - 1;
+
+ int tw = tilec->x1 - tilec->x0;
+ int w = tcd_img->comps[compno].w;
+
+ int i, j;
+ int offset_x = int_ceildivpow2(tcd_img->comps[compno].x0, tcd_img->comps[compno].factor);
+ int offset_y = int_ceildivpow2(tcd_img->comps[compno].y0, tcd_img->comps[compno].factor);
+
+ for (j = res->y0; j < res->y1; j++) {
+ for (i = res->x0; i < res->x1; i++) {
+
+ int v;
+ if (tcd_tcp->tccps[compno].qmfbid == 1) {
+ v = tilec->data[i - res->x0 + (j - res->y0) * tw];
+ } else {
+ v = tilec->data[i - res->x0 + (j - res->y0) * tw] >> 13;
+ }
+ v += adjust;
+
+ tcd_img->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
+ }
+ }
}
- time5 = clock() - time5;
- /* printf("tile->img: %ld.%.3ld s\n", time5/CLOCKS_PER_SEC, (time5%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
- time6 = clock() - time6;
- printf("total: %ld.%.3ld s\n\n", time6 / CLOCKS_PER_SEC, (time6 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
+ time = clock() - time;
+ fprintf(stderr,"total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC, (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
if (eof) {
longjmp(j2k_error, 1);