From ec9bd9f6a455a83da8c2c77f5b2628b416cca18f Mon Sep 17 00:00:00 2001 From: Sebastien Lugan Date: Thu, 29 Apr 2004 13:10:05 +0000 Subject: [PATCH] Reformatage : indent -kr $(find . -name '*.c') $(find . -name '*.h') --- codec/compat/getopt.c | 111 +- codec/compat/getopt.h | 2 +- codec/convert.c | 1554 +++++++++++++------------ codec/convert.h | 8 +- codec/image_to_j2k.c | 1320 ++++++++++----------- codec/j2k_to_image.c | 871 +++++++------- libopenjpeg/bio.c | 124 +- libopenjpeg/bio.h | 4 +- libopenjpeg/cio.c | 58 +- libopenjpeg/dwt.c | 566 ++++----- libopenjpeg/dwt.h | 6 +- libopenjpeg/fix.c | 2 +- libopenjpeg/int.c | 32 +- libopenjpeg/j2k.c | 2040 +++++++++++++++++---------------- libopenjpeg/j2k.h | 199 ++-- libopenjpeg/jpt.c | 182 ++- libopenjpeg/jpt.h | 20 +- libopenjpeg/mct.c | 108 +- libopenjpeg/mqc.c | 639 ++++++----- libopenjpeg/pi.c | 750 ++++++------ libopenjpeg/pi.h | 30 +- libopenjpeg/raw.c | 54 +- libopenjpeg/t1.c | 1618 ++++++++++++++------------ libopenjpeg/t2.c | 894 ++++++++------- libopenjpeg/t2.h | 7 +- libopenjpeg/tcd.c | 2523 +++++++++++++++++++++++------------------ libopenjpeg/tcd.h | 104 +- libopenjpeg/tgt.c | 283 ++--- libopenjpeg/tgt.h | 16 +- 29 files changed, 7469 insertions(+), 6656 deletions(-) diff --git a/codec/compat/getopt.c b/codec/compat/getopt.c index baf35a40..f2854ff3 100644 --- a/codec/compat/getopt.c +++ b/codec/compat/getopt.c @@ -35,17 +35,17 @@ #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95"; -#endif /* LIBC_SCCS and not lint */ +#endif /* LIBC_SCCS and not lint */ #include #include #include -int opterr = 1, /* if error message should be printed */ - optind = 1, /* index into parent argv vector */ - optopt, /* character checked for validity */ - optreset; /* reset getopt */ -char *optarg; /* argument associated with option */ +int opterr = 1, /* if error message should be printed */ + optind = 1, /* index into parent argv vector */ + optopt, /* character checked for validity */ + optreset; /* reset getopt */ +char *optarg; /* argument associated with option */ #define BADCH (int)'?' #define BADARG (int)':' @@ -61,56 +61,57 @@ char *const *nargv; const char *ostr; { # define __progname nargv[0] - static char *place = EMSG; /* option letter processing */ - char *oli; /* option letter list index */ + static char *place = EMSG; /* option letter processing */ + char *oli; /* option letter list index */ - if (optreset || !*place) { /* update scanning pointer */ - optreset = 0; - if (optind >= nargc || *(place = nargv[optind]) != '-') { - place = EMSG; - return (-1); - } - if (place[1] && *++place == '-') { /* found "--" */ - ++optind; - place = EMSG; - return (-1); - } - } /* option letter okay? */ - if ((optopt = (int) *place++) == (int) ':' || - !(oli = strchr(ostr, optopt))) { - /* - * if the user didn't specify '-' as an option, - * assume it means -1. - */ - if (optopt == (int) '-') - return (-1); - if (!*place) - ++optind; - if (opterr && *ostr != ':') - (void) fprintf(stderr, - "%s: illegal option -- %c\n", __progname, optopt); - return (BADCH); + if (optreset || !*place) { /* update scanning pointer */ + optreset = 0; + if (optind >= nargc || *(place = nargv[optind]) != '-') { + place = EMSG; + return (-1); } - if (*++oli != ':') { /* don't need argument */ - optarg = NULL; - if (!*place) - ++optind; - } else { /* need an argument */ - if (*place) /* no white space */ - optarg = place; - else if (nargc <= ++optind) { /* no arg */ - place = EMSG; - if (*ostr == ':') - return (BADARG); - if (opterr) - (void) fprintf(stderr, - "%s: option requires an argument -- %c\n", - __progname, optopt); - return (BADCH); - } else /* white space */ - optarg = nargv[optind]; - place = EMSG; - ++optind; + if (place[1] && *++place == '-') { /* found "--" */ + ++optind; + place = EMSG; + return (-1); } - return (optopt); /* dump back option letter */ + } /* option letter okay? */ + if ((optopt = (int) *place++) == (int) ':' || + !(oli = strchr(ostr, optopt))) { + /* + * if the user didn't specify '-' as an option, + * assume it means -1. + */ + if (optopt == (int) '-') + return (-1); + if (!*place) + ++optind; + if (opterr && *ostr != ':') + (void) fprintf(stderr, + "%s: illegal option -- %c\n", __progname, + optopt); + return (BADCH); + } + if (*++oli != ':') { /* don't need argument */ + optarg = NULL; + if (!*place) + ++optind; + } else { /* need an argument */ + if (*place) /* no white space */ + optarg = place; + else if (nargc <= ++optind) { /* no arg */ + place = EMSG; + if (*ostr == ':') + return (BADARG); + if (opterr) + (void) fprintf(stderr, + "%s: option requires an argument -- %c\n", + __progname, optopt); + return (BADCH); + } else /* white space */ + optarg = nargv[optind]; + place = EMSG; + ++optind; + } + return (optopt); /* dump back option letter */ } diff --git a/codec/compat/getopt.h b/codec/compat/getopt.h index f2cf1fad..ab9c1a7b 100644 --- a/codec/compat/getopt.h +++ b/codec/compat/getopt.h @@ -11,4 +11,4 @@ extern char *optarg; extern int getopt(int nargc, char *const *nargv, const char *ostr); -#endif /* _GETOPT_H_ */ +#endif /* _GETOPT_H_ */ diff --git a/codec/convert.c b/codec/convert.c index 1784fbe8..575eec11 100644 --- a/codec/convert.c +++ b/codec/convert.c @@ -45,407 +45,482 @@ typedef unsigned short int UINT2; typedef unsigned long int UINT4; typedef struct { - UINT2 bfType; /* 'BM' for Bitmap (19776) */ - UINT4 bfSize; /* Size of the file */ - UINT2 bfReserved1; /* Reserved : 0 */ - UINT2 bfReserved2; /* Reserved : 0 */ - UINT4 bfOffBits; /* Offset */ + UINT2 bfType; /* 'BM' for Bitmap (19776) */ + UINT4 bfSize; /* Size of the file */ + UINT2 bfReserved1; /* Reserved : 0 */ + UINT2 bfReserved2; /* Reserved : 0 */ + UINT4 bfOffBits; /* Offset */ } BITMAPFILEHEADER_t; typedef struct { - UINT4 biSize; /* Size of the structure in bytes */ - UINT4 biWidth; /* Width of the image in pixels */ - UINT4 biHeight; /* Heigth of the image in pixels */ - UINT2 biPlanes; /* 1 */ - UINT2 biBitCount; /* Number of color bits by pixels */ - UINT4 biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */ - UINT4 biSizeImage; /* Size of the image in bytes */ - UINT4 biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */ - UINT4 biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */ - UINT4 biClrUsed; /* Number of color used in the image (0: ALL) */ - UINT4 biClrImportant; /* Number of important color (0: ALL) */ + UINT4 biSize; /* Size of the structure in bytes */ + UINT4 biWidth; /* Width of the image in pixels */ + UINT4 biHeight; /* Heigth of the image in pixels */ + UINT2 biPlanes; /* 1 */ + UINT2 biBitCount; /* Number of color bits by pixels */ + UINT4 biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */ + UINT4 biSizeImage; /* Size of the image in bytes */ + UINT4 biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */ + UINT4 biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */ + UINT4 biClrUsed; /* Number of color used in the image (0: ALL) */ + UINT4 biClrImportant; /* Number of important color (0: ALL) */ } BITMAPINFOHEADER_t; -int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx, int subsampling_dy, int Dim[2]) +int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx, + int subsampling_dy, int Dim[2]) { - FILE *IN; - FILE *Compo0 = NULL, *Compo1 = NULL, *Compo2 = NULL; - BITMAPFILEHEADER_t File_h; - BITMAPINFOHEADER_t Info_h; - unsigned char *RGB; - unsigned char *table_R, *table_G, *table_B; - int i, w, h, PAD, type = 0; - int gray_scale = 1, not_end_file = 1, line = 0, col = 0; - unsigned char v, v2; - UINT4 W, H; - - IN = fopen(filename, "rb"); - if (!IN) { - fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename); - return 0; - } - - File_h.bfType = getc(IN); - File_h.bfType = (getc(IN) << 8) + File_h.bfType; - - if (File_h.bfType != 19778) { - printf("Error, not a BMP file!\n"); - return 0; - } else { - /* FILE HEADER */ - /* ------------- */ - File_h.bfSize = getc(IN); - File_h.bfSize = (getc(IN) << 8) + File_h.bfSize; - File_h.bfSize = (getc(IN) << 16) + File_h.bfSize; - File_h.bfSize = (getc(IN) << 24) + File_h.bfSize; - - File_h.bfReserved1 = getc(IN); - File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1; - - File_h.bfReserved2 = getc(IN); - File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2; - - File_h.bfOffBits = getc(IN); - File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits; - File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits; - File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits; - - /* INFO HEADER */ - /* ------------- */ - - Info_h.biSize = getc(IN); - Info_h.biSize = (getc(IN) << 8) + Info_h.biSize; - Info_h.biSize = (getc(IN) << 16) + Info_h.biSize; - Info_h.biSize = (getc(IN) << 24) + Info_h.biSize; - - Info_h.biWidth = getc(IN); - Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth; - Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth; - Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth; - w = Info_h.biWidth; - - Info_h.biHeight = getc(IN); - Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight; - Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight; - Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight; - h = Info_h.biHeight; - - Info_h.biPlanes = getc(IN); - Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes; - - Info_h.biBitCount = getc(IN); - Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount; - - Info_h.biCompression = getc(IN); - Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression; - Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression; - Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression; - - Info_h.biSizeImage = getc(IN); - Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage; - Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage; - Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage; - - Info_h.biXpelsPerMeter = getc(IN); - Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter; - Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter; - Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter; - - Info_h.biYpelsPerMeter = getc(IN); - Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter; - Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter; - Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter; - - Info_h.biClrUsed = getc(IN); - Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed; - Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed; - Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed; - - Info_h.biClrImportant = getc(IN); - Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant; - Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant; - Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant; - - /* Read the data and store them in the OUT file */ - - if (Info_h.biBitCount == 24) { - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * subsampling_dx + 1; - img->y1 = !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * subsampling_dy + 1; - img->numcomps = 3; - img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); - for (i = 0; i < img->numcomps; i++) { - img->comps[i].prec = 8; - img->comps[i].bpp = 8; - img->comps[i].sgnd = 0; - img->comps[i].dx = subsampling_dx; - img->comps[i].dy = subsampling_dy; - } - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } - Compo1 = fopen("Compo1", "wb"); - if (!Compo1) { - fprintf(stderr, "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n"); - } - Compo2 = fopen("Compo2", "wb"); - if (!Compo2) { - fprintf(stderr, "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n"); - } - - /* Place the cursor at the beginning of the image information */ - fseek(IN, 0, SEEK_SET); - fseek(IN, File_h.bfOffBits, SEEK_SET); - - W = Info_h.biWidth; - H = Info_h.biHeight; - - // PAD = 4 - (3 * W) % 4; - // PAD = (PAD == 4) ? 0 : PAD; - PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0; - - - RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char)); - - fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN); - - for (i = 0; i < (3 * W + PAD) * H; i++) - { - unsigned char elmt; - int Wp = 3 * W + PAD; - - elmt = RGB[(H - (i/Wp + 1)) * Wp + i % Wp]; - if ((i % Wp) < (3 * W)) - { - switch (type) - { - case 0: - fprintf(Compo2, "%c", elmt); - type = 1; - break; - case 1: - fprintf(Compo1, "%c", elmt); - type = 2; - break; - case 2: - fprintf(Compo0, "%c", elmt); - type = 0; - break; - } - } - } - - fclose(Compo0); - fclose(Compo1); - fclose(Compo2); - free(RGB); - } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) { - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * subsampling_dx + 1; - img->y1 = !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * subsampling_dy + 1; - - table_R = (unsigned char *) malloc(256 * sizeof(unsigned char)); - table_G = (unsigned char *) malloc(256 * sizeof(unsigned char)); - table_B = (unsigned char *) malloc(256 * sizeof(unsigned char)); - - for (i = 0; i < Info_h.biClrUsed; i++) { - table_B[i] = getc(IN); - table_G[i] = getc(IN); - table_R[i] = getc(IN); - getc(IN); - if (table_R[i] != table_G[i] && table_R[i] != table_B[i] && table_G[i] != table_B[i]) - gray_scale = 0; - } + FILE *IN; + FILE *Compo0 = NULL, *Compo1 = NULL, *Compo2 = NULL; + BITMAPFILEHEADER_t File_h; + BITMAPINFOHEADER_t Info_h; + unsigned char *RGB; + unsigned char *table_R, *table_G, *table_B; + int i, w, h, PAD, type = 0; + int gray_scale = 1, not_end_file = 1, line = 0, col = 0; + unsigned char v, v2; + UINT4 W, H; + + IN = fopen(filename, "rb"); + if (!IN) { + fprintf(stderr, + "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", + filename); + return 0; + } + + File_h.bfType = getc(IN); + File_h.bfType = (getc(IN) << 8) + File_h.bfType; + + if (File_h.bfType != 19778) { + printf("Error, not a BMP file!\n"); + return 0; + } else { + /* FILE HEADER */ + /* ------------- */ + File_h.bfSize = getc(IN); + File_h.bfSize = (getc(IN) << 8) + File_h.bfSize; + File_h.bfSize = (getc(IN) << 16) + File_h.bfSize; + File_h.bfSize = (getc(IN) << 24) + File_h.bfSize; + + File_h.bfReserved1 = getc(IN); + File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1; + + File_h.bfReserved2 = getc(IN); + File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2; + + File_h.bfOffBits = getc(IN); + File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits; + File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits; + File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits; + + /* INFO HEADER */ + /* ------------- */ + + Info_h.biSize = getc(IN); + Info_h.biSize = (getc(IN) << 8) + Info_h.biSize; + Info_h.biSize = (getc(IN) << 16) + Info_h.biSize; + Info_h.biSize = (getc(IN) << 24) + Info_h.biSize; + + Info_h.biWidth = getc(IN); + Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth; + Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth; + Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth; + w = Info_h.biWidth; + + Info_h.biHeight = getc(IN); + Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight; + Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight; + Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight; + h = Info_h.biHeight; + + Info_h.biPlanes = getc(IN); + Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes; + + Info_h.biBitCount = getc(IN); + Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount; + + Info_h.biCompression = getc(IN); + Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression; + Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression; + Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression; + + Info_h.biSizeImage = getc(IN); + Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage; + Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage; + Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage; + + Info_h.biXpelsPerMeter = getc(IN); + Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter; + Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter; + Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter; + + Info_h.biYpelsPerMeter = getc(IN); + Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter; + Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter; + Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter; + + Info_h.biClrUsed = getc(IN); + Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed; + Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed; + Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed; + + Info_h.biClrImportant = getc(IN); + Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant; + Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant; + Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant; + + /* Read the data and store them in the OUT file */ + + if (Info_h.biBitCount == 24) { + img->x0 = Dim[0]; + img->y0 = Dim[1]; + img->x1 = + !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - + 1) * + subsampling_dx + 1; + img->y1 = + !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - + 1) * + subsampling_dy + 1; + img->numcomps = 3; + img->comps = + (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); + for (i = 0; i < img->numcomps; i++) { + img->comps[i].prec = 8; + img->comps[i].bpp = 8; + img->comps[i].sgnd = 0; + img->comps[i].dx = subsampling_dx; + img->comps[i].dy = subsampling_dy; + } + Compo0 = fopen("Compo0", "wb"); + if (!Compo0) { + fprintf(stderr, + "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); + } + Compo1 = fopen("Compo1", "wb"); + if (!Compo1) { + fprintf(stderr, + "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n"); + } + Compo2 = fopen("Compo2", "wb"); + if (!Compo2) { + fprintf(stderr, + "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n"); + } + + /* Place the cursor at the beginning of the image information */ + fseek(IN, 0, SEEK_SET); + fseek(IN, File_h.bfOffBits, SEEK_SET); + + W = Info_h.biWidth; + H = Info_h.biHeight; + + // PAD = 4 - (3 * W) % 4; + // PAD = (PAD == 4) ? 0 : PAD; + PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0; + + + RGB = + (unsigned char *) malloc((3 * W + PAD) * H * + sizeof(unsigned char)); + + fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN); + + for (i = 0; i < (3 * W + PAD) * H; i++) { + unsigned char elmt; + int Wp = 3 * W + PAD; + + elmt = RGB[(H - (i / Wp + 1)) * Wp + i % Wp]; + if ((i % Wp) < (3 * W)) { + switch (type) { + case 0: + fprintf(Compo2, "%c", elmt); + type = 1; + break; + case 1: + fprintf(Compo1, "%c", elmt); + type = 2; + break; + case 2: + fprintf(Compo0, "%c", elmt); + type = 0; + break; + } + } + } + + fclose(Compo0); + fclose(Compo1); + fclose(Compo2); + free(RGB); + } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) { + img->x0 = Dim[0]; + img->y0 = Dim[1]; + img->x1 = + !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - + 1) * + subsampling_dx + 1; + img->y1 = + !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - + 1) * + subsampling_dy + 1; + + table_R = + (unsigned char *) malloc(256 * sizeof(unsigned char)); + table_G = + (unsigned char *) malloc(256 * sizeof(unsigned char)); + table_B = + (unsigned char *) malloc(256 * sizeof(unsigned char)); + + for (i = 0; i < Info_h.biClrUsed; i++) { + table_B[i] = getc(IN); + table_G[i] = getc(IN); + table_R[i] = getc(IN); + getc(IN); + if (table_R[i] != table_G[i] && table_R[i] != table_B[i] + && table_G[i] != table_B[i]) + gray_scale = 0; + } + + /* Place the cursor at the beginning of the image information */ + fseek(IN, 0, SEEK_SET); + fseek(IN, File_h.bfOffBits, SEEK_SET); + + W = Info_h.biWidth; + H = Info_h.biHeight; + if (Info_h.biWidth % 2) + W++; + + RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char)); + + fread(RGB, sizeof(unsigned char), W * H, IN); + if (gray_scale) { + img->numcomps = 1; + img->comps = + (j2k_comp_t *) malloc(img->numcomps * + sizeof(j2k_comp_t)); + img->comps[0].prec = 8; + img->comps[0].bpp = 8; + img->comps[0].sgnd = 0; + img->comps[0].dx = subsampling_dx; + img->comps[0].dy = subsampling_dy; + Compo0 = fopen("Compo0", "wb"); + if (!Compo0) { + fprintf(stderr, + "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); + } + for (i = 0; i < W * H; i++) { + if ((i % W < W - 1 && Info_h.biWidth % 2) + || !(Info_h.biWidth % 2)) + fprintf(Compo0, "%c", + table_R[RGB + [W * H - ((i) / (W) + 1) * W + + (i) % (W)]]); + } + fclose(Compo0); + } else { + img->numcomps = 3; + img->comps = + (j2k_comp_t *) malloc(img->numcomps * + sizeof(j2k_comp_t)); + for (i = 0; i < img->numcomps; i++) { + img->comps[i].prec = 8; + img->comps[i].bpp = 8; + img->comps[i].sgnd = 0; + img->comps[i].dx = subsampling_dx; + img->comps[i].dy = subsampling_dy; + } - /* Place the cursor at the beginning of the image information */ - fseek(IN, 0, SEEK_SET); - fseek(IN, File_h.bfOffBits, SEEK_SET); - - W = Info_h.biWidth; - H = Info_h.biHeight; - if (Info_h.biWidth % 2) - W++; - - RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char)); - - fread(RGB, sizeof(unsigned char), W * H, IN); - if (gray_scale) { - img->numcomps = 1; - img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); - img->comps[0].prec = 8; - img->comps[0].bpp = 8; - img->comps[0].sgnd = 0; - img->comps[0].dx = subsampling_dx; - img->comps[0].dy = subsampling_dy; - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } - for (i = 0; i < W * H; i++) { - if ((i % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) - fprintf(Compo0, "%c", table_R[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]); - } - fclose(Compo0); - } else { - img->numcomps = 3; - img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); - for (i = 0; i < img->numcomps; i++) { - img->comps[i].prec = 8; - img->comps[i].bpp = 8; - img->comps[i].sgnd = 0; - img->comps[i].dx = subsampling_dx; - img->comps[i].dy = subsampling_dy; - } - - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } - Compo1 = fopen("Compo1", "wb"); - if (!Compo1) { - fprintf(stderr, "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n"); - } - Compo2 = fopen("Compo2", "wb"); - if (!Compo2) { - fprintf(stderr, "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n"); - } - - for (i = 0; i < W * H; i++) { - if ((i % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) { - fprintf(Compo0, "%c", table_R[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]); - fprintf(Compo1, "%c", table_G[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]); - fprintf(Compo2, "%c", table_B[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]); - } - - } - fclose(Compo0); - fclose(Compo1); - fclose(Compo2); - } - free(RGB); - - } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) { - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * subsampling_dx + 1; - img->y1 = !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * subsampling_dy + 1; - - table_R = (unsigned char *) malloc(256 * sizeof(unsigned char)); - table_G = (unsigned char *) malloc(256 * sizeof(unsigned char)); - table_B = (unsigned char *) malloc(256 * sizeof(unsigned char)); - - for (i = 0; i < Info_h.biClrUsed; i++) { - table_B[i] = getc(IN); - table_G[i] = getc(IN); - table_R[i] = getc(IN); - getc(IN); - if (table_R[i] != table_G[i] && table_R[i] != table_B[i] && table_G[i] != table_B[i]) - gray_scale = 0; - } + Compo0 = fopen("Compo0", "wb"); + if (!Compo0) { + fprintf(stderr, + "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); + } + Compo1 = fopen("Compo1", "wb"); + if (!Compo1) { + fprintf(stderr, + "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n"); + } + Compo2 = fopen("Compo2", "wb"); + if (!Compo2) { + fprintf(stderr, + "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n"); + } - /* Place the cursor at the beginning of the image information */ - fseek(IN, 0, SEEK_SET); - fseek(IN, File_h.bfOffBits, SEEK_SET); - - if (gray_scale) { - img->numcomps = 1; - img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t)); - img->comps[0].prec = 8; - img->comps[0].bpp = 8; - img->comps[0].sgnd = 0; - img->comps[0].dx = subsampling_dx; - img->comps[0].dy = subsampling_dy; - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } - } else { - img->numcomps = 3; - img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); - for (i = 0; i < img->numcomps; i++) { - img->comps[i].prec = 8; - img->comps[i].bpp = 8; - img->comps[i].sgnd = 0; - img->comps[i].dx = subsampling_dx; - img->comps[i].dy = subsampling_dy; - } - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } - Compo1 = fopen("Compo1", "wb"); - if (!Compo1) { - fprintf(stderr, "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n"); - } - Compo2 = fopen("Compo2", "wb"); - if (!Compo2) { - fprintf(stderr, "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n"); - } - } + for (i = 0; i < W * H; i++) { + if ((i % W < W - 1 && Info_h.biWidth % 2) + || !(Info_h.biWidth % 2)) { + fprintf(Compo0, "%c", + table_R[RGB + [W * H - ((i) / (W) + 1) * W + + (i) % (W)]]); + fprintf(Compo1, "%c", + table_G[RGB + [W * H - ((i) / (W) + 1) * W + + (i) % (W)]]); + fprintf(Compo2, "%c", + table_B[RGB + [W * H - ((i) / (W) + 1) * W + + (i) % (W)]]); + } - RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char)); - - while (not_end_file) { - v = getc(IN); - if (v) { - v2 = getc(IN); - for (i = 0; i < (int) v; i++) { - RGB[line * Info_h.biWidth + col] = v2; - col++; - } - } else { - v = getc(IN); - switch (v) { - case 0: - col = 0; - line++; - break; - case 1: - line++; - not_end_file = 0; - break; - case 2: - printf("No Delta supported\n"); - return 1; - break; - default: - for (i = 0; i < v; i++) { - v2 = getc(IN); - RGB[line * Info_h.biWidth + col] = v2; - col++; - } - if (v % 2) - v2 = getc(IN); - } - } - } - if (gray_scale) { - for (line = 0; line < Info_h.biHeight; line++) - for (col = 0; col < Info_h.biWidth; col++) - fprintf(Compo0, "%c", table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]]); - fclose(Compo0); - } else { - for (line = 0; line < Info_h.biHeight; line++) - for (col = 0; col < Info_h.biWidth; col++) { - fprintf(Compo0, "%c", table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]]); - fprintf(Compo1, "%c", table_G[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]]); - fprintf(Compo2, "%c", table_B[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]]); - } - fclose(Compo0); - fclose(Compo1); - fclose(Compo2); + } + fclose(Compo0); + fclose(Compo1); + fclose(Compo2); + } + free(RGB); + + } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) { + img->x0 = Dim[0]; + img->y0 = Dim[1]; + img->x1 = + !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - + 1) * + subsampling_dx + 1; + img->y1 = + !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - + 1) * + subsampling_dy + 1; + + table_R = + (unsigned char *) malloc(256 * sizeof(unsigned char)); + table_G = + (unsigned char *) malloc(256 * sizeof(unsigned char)); + table_B = + (unsigned char *) malloc(256 * sizeof(unsigned char)); + + for (i = 0; i < Info_h.biClrUsed; i++) { + table_B[i] = getc(IN); + table_G[i] = getc(IN); + table_R[i] = getc(IN); + getc(IN); + if (table_R[i] != table_G[i] && table_R[i] != table_B[i] + && table_G[i] != table_B[i]) + gray_scale = 0; + } + + /* Place the cursor at the beginning of the image information */ + fseek(IN, 0, SEEK_SET); + fseek(IN, File_h.bfOffBits, SEEK_SET); + + if (gray_scale) { + img->numcomps = 1; + img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t)); + img->comps[0].prec = 8; + img->comps[0].bpp = 8; + img->comps[0].sgnd = 0; + img->comps[0].dx = subsampling_dx; + img->comps[0].dy = subsampling_dy; + Compo0 = fopen("Compo0", "wb"); + if (!Compo0) { + fprintf(stderr, + "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); + } + } else { + img->numcomps = 3; + img->comps = + (j2k_comp_t *) malloc(img->numcomps * + sizeof(j2k_comp_t)); + for (i = 0; i < img->numcomps; i++) { + img->comps[i].prec = 8; + img->comps[i].bpp = 8; + img->comps[i].sgnd = 0; + img->comps[i].dx = subsampling_dx; + img->comps[i].dy = subsampling_dy; + } + Compo0 = fopen("Compo0", "wb"); + if (!Compo0) { + fprintf(stderr, + "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); + } + Compo1 = fopen("Compo1", "wb"); + if (!Compo1) { + fprintf(stderr, + "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n"); + } + Compo2 = fopen("Compo2", "wb"); + if (!Compo2) { + fprintf(stderr, + "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n"); + } + } + + RGB = + (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * + sizeof(unsigned char)); + + while (not_end_file) { + v = getc(IN); + if (v) { + v2 = getc(IN); + for (i = 0; i < (int) v; i++) { + RGB[line * Info_h.biWidth + col] = v2; + col++; + } + } else { + v = getc(IN); + switch (v) { + case 0: + col = 0; + line++; + break; + case 1: + line++; + not_end_file = 0; + break; + case 2: + printf("No Delta supported\n"); + return 1; + break; + default: + for (i = 0; i < v; i++) { + v2 = getc(IN); + RGB[line * Info_h.biWidth + col] = v2; + col++; } - free(RGB); - } else - fprintf(stderr,"Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", - Info_h.biBitCount); - - fclose(IN); - return 1; - } + if (v % 2) + v2 = getc(IN); + } + } + } + if (gray_scale) { + for (line = 0; line < Info_h.biHeight; line++) + for (col = 0; col < Info_h.biWidth; col++) + fprintf(Compo0, "%c", + table_R[(int) + RGB[(Info_h.biHeight - line - + 1) * Info_h.biWidth + col]]); + fclose(Compo0); + } else { + for (line = 0; line < Info_h.biHeight; line++) + for (col = 0; col < Info_h.biWidth; col++) { + fprintf(Compo0, "%c", + table_R[(int) + RGB[(Info_h.biHeight - line - + 1) * Info_h.biWidth + col]]); + fprintf(Compo1, "%c", + table_G[(int) + RGB[(Info_h.biHeight - line - + 1) * Info_h.biWidth + col]]); + fprintf(Compo2, "%c", + table_B[(int) + RGB[(Info_h.biHeight - line - + 1) * Info_h.biWidth + col]]); + } + fclose(Compo0); + fclose(Compo1); + fclose(Compo2); + } + free(RGB); + } else + fprintf(stderr, + "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", + Info_h.biBitCount); + + fclose(IN); + return 1; + } } /* -->> -->> -->> -->> @@ -457,150 +532,149 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx, int subsam unsigned char readuchar(FILE * f) { - unsigned char c1; - fread(&c1, 1, 1, f); - return c1; + unsigned char c1; + fread(&c1, 1, 1, f); + return c1; } unsigned short readushort(FILE * f, int bigendian) { - unsigned char c1, c2; - fread(&c1, 1, 1, f); - fread(&c2, 1, 1, f); - if (bigendian) - return (c1 << 8) + c2; - else - return (c2 << 8) + c1; + unsigned char c1, c2; + fread(&c1, 1, 1, f); + fread(&c2, 1, 1, f); + if (bigendian) + return (c1 << 8) + c2; + else + return (c2 << 8) + c1; } unsigned int readuint(FILE * f, int bigendian) { - unsigned char c1, c2, c3, c4; - fread(&c1, 1, 1, f); - fread(&c2, 1, 1, f); - fread(&c3, 1, 1, f); - fread(&c4, 1, 1, f); - if (bigendian) - return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4; - else - return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1; + unsigned char c1, c2, c3, c4; + fread(&c1, 1, 1, f); + fread(&c2, 1, 1, f); + fread(&c3, 1, 1, f); + fread(&c4, 1, 1, f); + if (bigendian) + return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4; + else + return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1; } int pgxtoimage(char *filename, j2k_image_t * img, int tdy, - int subsampling_dx, int subsampling_dy, int Dim[2], - j2k_cp_t cp) + int subsampling_dx, int subsampling_dy, int Dim[2], + j2k_cp_t cp) { - FILE *f; - int w, h, prec; - int i, compno, bandno; - char str[256], endian[16]; - char sign; - int bigendian; - j2k_comp_t *comp; - - img->numcomps = 1; - img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); - for (compno = 0; compno < img->numcomps; compno++) { - FILE *src; - char tmp[16]; - int max = 0; - int Y1; - comp = &img->comps[compno]; - sprintf(str, "%s", filename); - f = fopen(str, "rb"); - if (!f) { - fprintf(stderr, "Failed to open %s for reading !\n", str); - return 0; + FILE *f; + int w, h, prec; + int i, compno, bandno; + char str[256], endian[16]; + char sign; + int bigendian; + j2k_comp_t *comp; + + img->numcomps = 1; + img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); + for (compno = 0; compno < img->numcomps; compno++) { + FILE *src; + char tmp[16]; + int max = 0; + int Y1; + comp = &img->comps[compno]; + sprintf(str, "%s", filename); + f = fopen(str, "rb"); + if (!f) { + fprintf(stderr, "Failed to open %s for reading !\n", str); + return 0; + } + if (fscanf(f, "PG %s %c %d %d %d", endian, &sign, &prec, &w, &h) == + 5) { + fgetc(f); + if (!strcmp(endian, "ML")) + bigendian = 1; + else + bigendian = 0; + if (compno == 0) { + img->x0 = Dim[0]; + img->y0 = Dim[1]; + img->x1 = + !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - + 1) * + subsampling_dx + 1; + img->y1 = + !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - + 1) * + subsampling_dy + 1; + } else { + if (w != img->x1 || h != img->y1) + return 0; + } + + if (sign == '-') { + comp->sgnd = 1; + } else { + comp->sgnd = 0; + } + comp->prec = prec; + comp->dx = subsampling_dx; + comp->dy = subsampling_dy; + bandno = 1; + + Y1 = cp.ty0 + bandno * cp.tdy < + img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1; + Y1 -= img->y0; + + sprintf(tmp, "bandtile%d", bandno); /* bandtile file */ + src = fopen(tmp, "wb"); + if (!src) { + fprintf(stderr, "failed to open %s for writing !\n", tmp); + } + for (i = 0; i < w * h; i++) { + int v; + if (i == Y1 * w / subsampling_dy && tdy != -1) { /* bandtile is full */ + fclose(src); + bandno++; + sprintf(tmp, "bandtile%d", bandno); + src = fopen(tmp, "wb"); + if (!src) { + fprintf(stderr, + "failed to open %s for writing !\n", tmp); + } + Y1 = cp.ty0 + bandno * cp.tdy < + img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1; + Y1 -= img->y0; } - if (fscanf(f, "PG %s %c %d %d %d", endian, &sign, &prec, &w, &h) == 5) - { - fgetc(f); - if (!strcmp(endian, "ML")) - bigendian = 1; - else - bigendian = 0; - if (compno == 0) { - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = - !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - - 1) * - subsampling_dx + 1; - img->y1 = - !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - - 1) * - subsampling_dy + 1; - } else { - if (w != img->x1 || h != img->y1) - return 0; - } - - if (sign == '-') { - comp->sgnd = 1; - } else { - comp->sgnd = 0; - } - comp->prec = prec; - comp->dx = subsampling_dx; - comp->dy = subsampling_dy; - bandno = 1; - - Y1 = - cp.ty0 + bandno * cp.tdy < - img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1; - Y1 -= img->y0; - - sprintf(tmp, "bandtile%d", bandno); /* bandtile file */ - src = fopen(tmp, "wb"); - if (!src) { - fprintf(stderr, "failed to open %s for writing !\n", tmp); - } - for (i = 0; i < w * h; i++) { - int v; - if (i == Y1 * w / subsampling_dy && tdy != -1) { /* bandtile is full */ - fclose(src); - bandno++; - sprintf(tmp, "bandtile%d", bandno); - src = fopen(tmp, "wb"); - if (!src) { - fprintf(stderr, "failed to open %s for writing !\n", tmp); - } - Y1 = - cp.ty0 + bandno * cp.tdy < - img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1; - Y1 -= img->y0; - } - if (comp->prec <= 8) { - if (!comp->sgnd) { - v = readuchar(f); - } else { - v = (char) readuchar(f); - } - } else if (comp->prec <= 16) { - if (!comp->sgnd) { - v = readushort(f, bigendian); - } else { - v = (short) readushort(f, bigendian); - } - } else { - if (!comp->sgnd) { - v = readuint(f, bigendian); - } else { - v = (int) readuint(f, bigendian); - } - } - if (v > max) - max = v; - fprintf(src, "%d ", v); - } + if (comp->prec <= 8) { + if (!comp->sgnd) { + v = readuchar(f); + } else { + v = (char) readuchar(f); + } + } else if (comp->prec <= 16) { + if (!comp->sgnd) { + v = readushort(f, bigendian); + } else { + v = (short) readushort(f, bigendian); + } } else { - return 0; + if (!comp->sgnd) { + v = readuint(f, bigendian); + } else { + v = (int) readuint(f, bigendian); + } } - fclose(f); - fclose(src); - comp->bpp = int_floorlog2(max) + 1; + if (v > max) + max = v; + fprintf(src, "%d ", v); + } + } else { + return 0; } - return 1; + fclose(f); + fclose(src); + comp->bpp = int_floorlog2(max) + 1; + } + return 1; } /* -->> -->> -->> -->> @@ -610,239 +684,241 @@ int pgxtoimage(char *filename, j2k_image_t * img, int tdy, <<-- <<-- <<-- <<-- */ int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx, - int subsampling_dy, int Dim[2]) + int subsampling_dy, int Dim[2]) { - FILE *f; - FILE *Compo0, *Compo1, *Compo2; - int w, h; - int i; - char value; - char comment[256]; - - f = fopen(filename, "rb"); - if (!f) { - fprintf(stderr, - "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", - filename); - return 0; + FILE *f; + FILE *Compo0, *Compo1, *Compo2; + int w, h; + int i; + char value; + char comment[256]; + + f = fopen(filename, "rb"); + if (!f) { + fprintf(stderr, + "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", + filename); + return 0; + } + + if (fgetc(f) != 'P') + return 0; + value = fgetc(f); + + if (value == '2') { + fgetc(f); + if (fgetc(f) == '#') { + fseek(f, 0, SEEK_SET); + fscanf(f, "P2\n"); + fgets(comment, 256, f); + fscanf(f, "%d %d\n255", &w, &h); + } else { + fseek(f, 0, SEEK_SET); + fscanf(f, "P2\n%d %d\n255", &w, &h); } - if (fgetc(f) != 'P') - return 0; - value = fgetc(f); - - if (value == '2') { - fgetc(f); - if (fgetc(f) == '#') { - fseek(f, 0, SEEK_SET); - fscanf(f, "P2\n"); - fgets(comment, 256, f); - fscanf(f, "%d %d\n255", &w, &h); - } else { - fseek(f, 0, SEEK_SET); - fscanf(f, "P2\n%d %d\n255", &w, &h); - } - - fgetc(f); - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = - !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - - 1) * - subsampling_dx + 1; - img->y1 = - !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - - 1) * - subsampling_dy + 1; - - img->numcomps = 1; - img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t)); - img->comps[0].prec = 8; - img->comps[0].bpp = 8; - img->comps[0].sgnd = 0; - img->comps[0].dx = subsampling_dx; - img->comps[0].dy = subsampling_dy; + fgetc(f); + img->x0 = Dim[0]; + img->y0 = Dim[1]; + img->x1 = + !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - + 1) * + subsampling_dx + 1; + img->y1 = + !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - + 1) * + subsampling_dy + 1; - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, - "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } - for (i = 0; i < w * h; i++) { - unsigned int l; - fscanf(f, "%d", &l); - fprintf(Compo0, "%c", l); - } - fclose(Compo0); - } else if (value == '5') { - fgetc(f); - if (fgetc(f) == '#') { - fseek(f, 0, SEEK_SET); - fscanf(f, "P5\n"); - fgets(comment, 256, f); - fscanf(f, "%d %d\n255", &w, &h); - } else { - fseek(f, 0, SEEK_SET); - fscanf(f, "P5\n%d %d\n255", &w, &h); - } + img->numcomps = 1; + img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t)); + img->comps[0].prec = 8; + img->comps[0].bpp = 8; + img->comps[0].sgnd = 0; + img->comps[0].dx = subsampling_dx; + img->comps[0].dy = subsampling_dy; + + Compo0 = fopen("Compo0", "wb"); + if (!Compo0) { + fprintf(stderr, + "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); + } + for (i = 0; i < w * h; i++) { + unsigned int l; + fscanf(f, "%d", &l); + fprintf(Compo0, "%c", l); + } + fclose(Compo0); + } else if (value == '5') { + fgetc(f); + if (fgetc(f) == '#') { + fseek(f, 0, SEEK_SET); + fscanf(f, "P5\n"); + fgets(comment, 256, f); + fscanf(f, "%d %d\n255", &w, &h); + } else { + fseek(f, 0, SEEK_SET); + fscanf(f, "P5\n%d %d\n255", &w, &h); + } - fgetc(f); - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = - !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - - 1) * - subsampling_dx + 1; - img->y1 = - !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - - 1) * - subsampling_dy + 1; + fgetc(f); + img->x0 = Dim[0]; + img->y0 = Dim[1]; + img->x1 = + !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - + 1) * + subsampling_dx + 1; + img->y1 = + !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - + 1) * + subsampling_dy + 1; - img->numcomps = 1; - img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t)); - img->comps[0].prec = 8; - img->comps[0].bpp = 8; - img->comps[0].sgnd = 0; - img->comps[0].dx = subsampling_dx; - img->comps[0].dy = subsampling_dy; - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, - "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } - for (i = 0; i < w * h; i++) { - unsigned char l; - fread(&l, 1, 1, f); - fwrite(&l, 1, 1, Compo0); - } - fclose(Compo0); - } else if (value == '3') { - fgetc(f); - if (fgetc(f) == '#') { - fseek(f, 0, SEEK_SET); - fscanf(f, "P3\n"); - fgets(comment, 256, f); - fscanf(f, "%d %d\n255", &w, &h); - } else { - fseek(f, 0, SEEK_SET); - fscanf(f, "P3\n%d %d\n255", &w, &h); - } + img->numcomps = 1; + img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t)); + img->comps[0].prec = 8; + img->comps[0].bpp = 8; + img->comps[0].sgnd = 0; + img->comps[0].dx = subsampling_dx; + img->comps[0].dy = subsampling_dy; + Compo0 = fopen("Compo0", "wb"); + if (!Compo0) { + fprintf(stderr, + "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); + } + for (i = 0; i < w * h; i++) { + unsigned char l; + fread(&l, 1, 1, f); + fwrite(&l, 1, 1, Compo0); + } + fclose(Compo0); + } else if (value == '3') { + fgetc(f); + if (fgetc(f) == '#') { + fseek(f, 0, SEEK_SET); + fscanf(f, "P3\n"); + fgets(comment, 256, f); + fscanf(f, "%d %d\n255", &w, &h); + } else { + fseek(f, 0, SEEK_SET); + fscanf(f, "P3\n%d %d\n255", &w, &h); + } - fgetc(f); - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = - !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - - 1) * - subsampling_dx + 1; - img->y1 = - !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - - 1) * - subsampling_dy + 1; - img->numcomps = 3; - img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); - for (i = 0; i < img->numcomps; i++) { - img->comps[i].prec = 8; - img->comps[i].bpp = 8; - img->comps[i].sgnd = 0; - img->comps[i].dx = subsampling_dx; - img->comps[i].dy = subsampling_dy; - } - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, - "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } + fgetc(f); + img->x0 = Dim[0]; + img->y0 = Dim[1]; + img->x1 = + !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - + 1) * + subsampling_dx + 1; + img->y1 = + !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - + 1) * + subsampling_dy + 1; + img->numcomps = 3; + img->comps = + (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); + for (i = 0; i < img->numcomps; i++) { + img->comps[i].prec = 8; + img->comps[i].bpp = 8; + img->comps[i].sgnd = 0; + img->comps[i].dx = subsampling_dx; + img->comps[i].dy = subsampling_dy; + } + Compo0 = fopen("Compo0", "wb"); + if (!Compo0) { + fprintf(stderr, + "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); + } - Compo1 = fopen("Compo1", "wb"); - if (!Compo1) { - fprintf(stderr, - "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n"); - } + Compo1 = fopen("Compo1", "wb"); + if (!Compo1) { + fprintf(stderr, + "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n"); + } - Compo2 = fopen("Compo2", "wb"); - if (!Compo2) { - fprintf(stderr, - "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n"); - } + Compo2 = fopen("Compo2", "wb"); + if (!Compo2) { + fprintf(stderr, + "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n"); + } - for (i = 0; i < w * h; i++) { - unsigned int r, g, b; - fscanf(f, "%d", &r); - fscanf(f, "%d", &g); - fscanf(f, "%d", &b); - fprintf(Compo0, "%c", r); - fprintf(Compo1, "%c", g); - fprintf(Compo2, "%c", b); - } - fclose(Compo0); - fclose(Compo1); - fclose(Compo2); - } else if (value == '6') { - fgetc(f); - if (fgetc(f) == '#') { - fseek(f, 0, SEEK_SET); - fscanf(f, "P6\n"); - fgets(comment, 256, f); - fscanf(f, "%d %d\n255", &w, &h); - } else { - fseek(f, 0, SEEK_SET); - fscanf(f, "P6\n%d %d\n255", &w, &h); - } + for (i = 0; i < w * h; i++) { + unsigned int r, g, b; + fscanf(f, "%d", &r); + fscanf(f, "%d", &g); + fscanf(f, "%d", &b); + fprintf(Compo0, "%c", r); + fprintf(Compo1, "%c", g); + fprintf(Compo2, "%c", b); + } + fclose(Compo0); + fclose(Compo1); + fclose(Compo2); + } else if (value == '6') { + fgetc(f); + if (fgetc(f) == '#') { + fseek(f, 0, SEEK_SET); + fscanf(f, "P6\n"); + fgets(comment, 256, f); + fscanf(f, "%d %d\n255", &w, &h); + } else { + fseek(f, 0, SEEK_SET); + fscanf(f, "P6\n%d %d\n255", &w, &h); + } - fgetc(f); - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = - !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - - 1) * - subsampling_dx + 1; - img->y1 = - !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - - 1) * - subsampling_dy + 1; - img->numcomps = 3; - img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); - for (i = 0; i < img->numcomps; i++) { - img->comps[i].prec = 8; - img->comps[i].bpp = 8; - img->comps[i].sgnd = 0; - img->comps[i].dx = subsampling_dx; - img->comps[i].dy = subsampling_dy; - } - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, - "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } + fgetc(f); + img->x0 = Dim[0]; + img->y0 = Dim[1]; + img->x1 = + !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - + 1) * + subsampling_dx + 1; + img->y1 = + !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - + 1) * + subsampling_dy + 1; + img->numcomps = 3; + img->comps = + (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); + for (i = 0; i < img->numcomps; i++) { + img->comps[i].prec = 8; + img->comps[i].bpp = 8; + img->comps[i].sgnd = 0; + img->comps[i].dx = subsampling_dx; + img->comps[i].dy = subsampling_dy; + } + Compo0 = fopen("Compo0", "wb"); + if (!Compo0) { + fprintf(stderr, + "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); + } - Compo1 = fopen("Compo1", "wb"); - if (!Compo1) { - fprintf(stderr, - "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n"); - } + Compo1 = fopen("Compo1", "wb"); + if (!Compo1) { + fprintf(stderr, + "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n"); + } - Compo2 = fopen("Compo2", "wb"); - if (!Compo2) { - fprintf(stderr, - "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n"); - } + Compo2 = fopen("Compo2", "wb"); + if (!Compo2) { + fprintf(stderr, + "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n"); + } - for (i = 0; i < w * h; i++) { - unsigned char r, g, b; - fread(&r, 1, 1, f); - fread(&g, 1, 1, f); - fread(&b, 1, 1, f); - fwrite(&r, 1, 1, Compo0); - fwrite(&g, 1, 1, Compo1); - fwrite(&b, 1, 1, Compo2); - } - fclose(Compo0); - fclose(Compo1); - fclose(Compo2); - } else { - return 0; + for (i = 0; i < w * h; i++) { + unsigned char r, g, b; + fread(&r, 1, 1, f); + fread(&g, 1, 1, f); + fread(&b, 1, 1, f); + fwrite(&r, 1, 1, Compo0); + fwrite(&g, 1, 1, Compo1); + fwrite(&b, 1, 1, Compo2); } - fclose(f); - return 1; + fclose(Compo0); + fclose(Compo1); + fclose(Compo2); + } else { + return 0; + } + fclose(f); + return 1; } diff --git a/codec/convert.h b/codec/convert.h index da667340..337dae76 100644 --- a/codec/convert.h +++ b/codec/convert.h @@ -29,11 +29,11 @@ #include "j2k.h" int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx, - int subsampling_dy, int Dim[2]); + int subsampling_dy, int Dim[2]); int pgxtoimage(char *filename, j2k_image_t * img, int tdy, - int subsampling_dx, int subsampling_dy, int Dim[2], - j2k_cp_t cp); + int subsampling_dx, int subsampling_dy, int Dim[2], + j2k_cp_t cp); int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx, - int subsampling_dy, int Dim[2]); + int subsampling_dy, int Dim[2]); diff --git a/codec/image_to_j2k.c b/codec/image_to_j2k.c index 6f23e681..0f595806 100644 --- a/codec/image_to_j2k.c +++ b/codec/image_to_j2k.c @@ -40,694 +40,730 @@ void help_display() { - printf("HELP\n----\n\n"); - printf - ("- the option -help displays the readme.txt file on screen\n\n"); - - - printf("List of parameters for the coder JPEG 2000 :\n"); - printf("\n"); - printf - ("- The markers COD and QCD are writed both of two in the main_header and never appear in the tile_header. The markers in the main header are : SOC SIZ COD QCD COM.\n"); - printf("\n"); - printf - ("- This coder can encode mega image, a test was made on a 24000x24000 pixels color image. You need enough disk space memory (twice the original) to encode the image. (i.e. for a 1.5 Gb image you need a minimum of 3Gb of disk memory)\n"); - printf("\n"); - printf("REMARKS :\n"); - printf("---------\n"); - printf("\n"); - printf - ("* the value of rate enter in the code line is the compression factor !\n"); - printf("exemple :\n"); - printf("\n"); - printf - ("-r 20,10,1 means quality 1 : compress 20x, quality 2 : compress 10x and quality 3 : compress 1x = lossless\n"); - printf("\n"); - printf("By default :\n"); - printf("------------\n"); - printf("\n"); - printf(" * lossless\n"); - printf(" * 1 tile\n"); - printf(" * size of precinct 2^15 x 2^15 (means 1 precinct)\n"); - printf(" * size of code-block 64 x 64\n"); - printf(" * Number of resolution : 6\n"); - printf(" * No SOP marker in the codestream\n"); - printf(" * No EPH marker in the codestream\n"); - printf(" * No sub-sampling in x and y direction\n"); - printf(" * No mode switch activated\n"); - printf(" * progression order : LRCP\n"); - printf(" * No index file\n"); - printf(" * No ROI upshifted\n"); - printf(" * No offset of the origin of the image\n"); - printf(" * No offset of the origin of the tiles\n"); - printf(" * Reversible DWT 5-3\n"); - printf("\n"); - printf("Parameters :\n"); - printf("------------\n"); - printf("\n"); - printf - ("-i : source file (-i source.pnm also *.pgm, *.ppm) required\n"); - printf("\n"); - printf - ("-o : destination file (-o dest.j2k) required\n"); - printf("\n"); - printf - ("-help : Display the help information optional\n "); - printf("\n"); - printf - ("-r : different rates (-r 20,10,5) optional\n "); - printf("\n"); - printf - ("-n : Number of resolution (-n 3) optional\n"); - printf("\n"); - printf - ("-b : size of code block (-b 32,32) optional\n"); - printf("\n"); - printf - ("-c : size of precinct (-c 128,128) optional\n"); - printf("\n"); - printf - ("-t : size of tile (-t 512,512) optional\n"); - printf("\n"); - printf - ("-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] optional\n"); - printf("\n"); - printf - ("-s : subsampling factor (-s 2,2) [-s X,Y] optional\n"); - printf("\n"); - printf - ("-SOP : write SOP marker before each packet optional\n"); - printf("\n"); - printf - ("-EPH : write EPH marker after each header packet optional\n"); - printf("\n"); - printf - ("-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL) 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] optional\n"); - printf - (" for several mode switch you have to add the value of each mode you want\n"); - printf - (" ex : RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n"); - printf("\n"); - printf - ("-x : Create an index file *.Idx (-x index_name.Idx) optional\n"); - printf("\n"); - printf - ("-ROI:c=%%d,U=%%d : quantization indices upshifted for component c=%%d [\%%d = 0,1,2]\n"); - printf - (" with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) optional\n"); - printf("\n"); - printf - ("-d : offset of the origin of the image (-d 150,300) optional\n"); - printf("\n"); - printf - ("-T : offset of the origin of the tiles (-T 100,75) optional\n"); - printf("\n"); - printf - ("-I : Use the irreversible DWT 9-7 (-I) optional\n"); - printf("\n"); - printf("IMPORTANT :\n"); - printf("-----------\n"); - printf("\n"); - printf("* subsampling bigger than 2 can produce error\n"); - printf("\n"); - printf - ("The index file respect the structure below :\n"); - printf - ("---------------------------------------------\n"); - printf("\n"); - printf("Image_height Image_width\n"); - printf("progression order\n"); - printf("Tiles_size_X Tiles_size_Y\n"); - printf("Components_nb\n"); - printf("Layers_nb\n"); - printf("decomposition_levels\n"); - printf("Precincts_size_X Precincts_size_Y\n"); - printf("Main_header_end_position\n"); - printf("Codestream_size\n"); - printf("Tile0 start_pos end_Theader end_pos\n"); - printf("Tile1 '' '' ''\n"); - printf("...\n"); - printf("TileN '' '' ''\n"); - printf("Tpacket_0 Tile layer res. comp. prec. start_pos end_pos\n"); - printf("...\n"); - printf("Tpacket_M '' '' '' '' '' '' ''\n"); + printf("HELP\n----\n\n"); + printf + ("- the option -help displays the readme.txt file on screen\n\n"); + + + printf("List of parameters for the coder JPEG 2000 :\n"); + printf("\n"); + printf + ("- The markers COD and QCD are writed both of two in the main_header and never appear in the tile_header. The markers in the main header are : SOC SIZ COD QCD COM.\n"); + printf("\n"); + printf + ("- This coder can encode mega image, a test was made on a 24000x24000 pixels color image. You need enough disk space memory (twice the original) to encode the image. (i.e. for a 1.5 Gb image you need a minimum of 3Gb of disk memory)\n"); + printf("\n"); + printf("REMARKS :\n"); + printf("---------\n"); + printf("\n"); + printf + ("* the value of rate enter in the code line is the compression factor !\n"); + printf("exemple :\n"); + printf("\n"); + printf + ("-r 20,10,1 means quality 1 : compress 20x, quality 2 : compress 10x and quality 3 : compress 1x = lossless\n"); + printf("\n"); + printf("By default :\n"); + printf("------------\n"); + printf("\n"); + printf(" * lossless\n"); + printf(" * 1 tile\n"); + printf(" * size of precinct 2^15 x 2^15 (means 1 precinct)\n"); + printf(" * size of code-block 64 x 64\n"); + printf(" * Number of resolution : 6\n"); + printf(" * No SOP marker in the codestream\n"); + printf(" * No EPH marker in the codestream\n"); + printf(" * No sub-sampling in x and y direction\n"); + printf(" * No mode switch activated\n"); + printf(" * progression order : LRCP\n"); + printf(" * No index file\n"); + printf(" * No ROI upshifted\n"); + printf(" * No offset of the origin of the image\n"); + printf(" * No offset of the origin of the tiles\n"); + printf(" * Reversible DWT 5-3\n"); + printf("\n"); + printf("Parameters :\n"); + printf("------------\n"); + printf("\n"); + printf + ("-i : source file (-i source.pnm also *.pgm, *.ppm) required\n"); + printf("\n"); + printf("-o : destination file (-o dest.j2k) required\n"); + printf("\n"); + printf("-help : Display the help information optional\n "); + printf("\n"); + printf("-r : different rates (-r 20,10,5) optional\n "); + printf("\n"); + printf("-n : Number of resolution (-n 3) optional\n"); + printf("\n"); + printf("-b : size of code block (-b 32,32) optional\n"); + printf("\n"); + printf("-c : size of precinct (-c 128,128) optional\n"); + printf("\n"); + printf("-t : size of tile (-t 512,512) optional\n"); + printf("\n"); + printf + ("-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] optional\n"); + printf("\n"); + printf + ("-s : subsampling factor (-s 2,2) [-s X,Y] optional\n"); + printf("\n"); + printf + ("-SOP : write SOP marker before each packet optional\n"); + printf("\n"); + printf + ("-EPH : write EPH marker after each header packet optional\n"); + printf("\n"); + printf + ("-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL) 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] optional\n"); + printf + (" for several mode switch you have to add the value of each mode you want\n"); + printf + (" ex : RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n"); + printf("\n"); + printf + ("-x : Create an index file *.Idx (-x index_name.Idx) optional\n"); + printf("\n"); + printf + ("-ROI:c=%%d,U=%%d : quantization indices upshifted for component c=%%d [\%%d = 0,1,2]\n"); + printf + (" with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) optional\n"); + printf("\n"); + printf + ("-d : offset of the origin of the image (-d 150,300) optional\n"); + printf("\n"); + printf + ("-T : offset of the origin of the tiles (-T 100,75) optional\n"); + printf("\n"); + printf + ("-I : Use the irreversible DWT 9-7 (-I) optional\n"); + printf("\n"); + printf("IMPORTANT :\n"); + printf("-----------\n"); + printf("\n"); + printf("* subsampling bigger than 2 can produce error\n"); + printf("\n"); + printf("The index file respect the structure below :\n"); + printf("---------------------------------------------\n"); + printf("\n"); + printf("Image_height Image_width\n"); + printf("progression order\n"); + printf("Tiles_size_X Tiles_size_Y\n"); + printf("Components_nb\n"); + printf("Layers_nb\n"); + printf("decomposition_levels\n"); + printf("Precincts_size_X Precincts_size_Y\n"); + printf("Main_header_end_position\n"); + printf("Codestream_size\n"); + printf("Tile0 start_pos end_Theader end_pos\n"); + printf("Tile1 '' '' ''\n"); + printf("...\n"); + printf("TileN '' '' ''\n"); + printf("Tpacket_0 Tile layer res. comp. prec. start_pos end_pos\n"); + printf("...\n"); + printf("Tpacket_M '' '' '' '' '' '' ''\n"); } 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') - { + 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; - } - } - } + } 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] = { - {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9}, - {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0}, - {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0}, - {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2} + {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9}, + {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0}, + {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0}, + {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2} }; int floorlog2(int a) { - int l; - for (l = 0; a > 1; l++) { - a >>= 1; - } - return l; + int l; + for (l = 0; a > 1; l++) { + a >>= 1; + } + return l; } void encode_stepsize(int stepsize, int numbps, int *expn, int *mant) { - int p, n; - p = floorlog2(stepsize) - 13; - n = 11 - floorlog2(stepsize); - *mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff; - *expn = numbps - p; + int p, n; + p = floorlog2(stepsize) - 13; + n = 11 - floorlog2(stepsize); + *mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff; + *expn = numbps - p; } void calc_explicit_stepsizes(j2k_tccp_t * tccp, int prec) { - int numbands, bandno; - numbands = 3 * tccp->numresolutions - 2; - for (bandno = 0; bandno < numbands; bandno++) { - double stepsize; - - int resno, level, orient, gain; - 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)); - 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); + int numbands, bandno; + numbands = 3 * tccp->numresolutions - 2; + for (bandno = 0; bandno < numbands; bandno++) { + double stepsize; + + int resno, level, orient, gain; + 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)); + 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); + } } int main(int argc, char **argv) { - int len; - 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 */ - 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_poc_t *tcp_poc; - j2k_tccp_t *tccp; - int i, tileno, j; - char *infile = 0; - char *outfile = 0; - char *index = 0; - char *s, S1, S2, S3; - int ir = 0; - - /* default value */ - /* ------------- */ - NumResolution = 6; - CSty = 0; - cblockw_init = 64; - cblockh_init = 64; - cp.tw = 1; - cp.th = 1; - img.index_on = 0; - Prog_order = 0; - numpocs = 0; - mode = 0; - subsampling_dx = 1; - subsampling_dy = 1; - ROI_compno = -1; /* no ROI */ - ROI_shift = 0; - Dim[0] = 0; - Dim[1] = 0; - TX0 = 0; - TY0 = 0; - cp.comment = NULL; - cp.disto_alloc = 0; - cp.fixed_alloc = 0; - /* img.PPT=0; */ - - Tile_arg = 0; - cp_init.tcps = (j2k_tcp_t *) malloc(sizeof(j2k_tcp_t)); /* initialisation if only one tile */ - tcp_init = &cp_init.tcps[0]; - tcp_init->numlayers = 0; - - while (1) { - int c = - getopt(argc, argv, "i:o:r:q:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I"); - if (c == -1) - break; - switch (c) { - case 'i': /* IN fill */ - infile = optarg; - s = optarg; - while (*s) { - s++; - } - s--; - S3 = *s; - s--; - S2 = *s; - s--; - S1 = *s; - - 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')) { - cp.image_type = 1; - break; - } - - if ((S1 == 'b' && S2 == 'm' && S3 == 'p') || (S1 == 'B' && S2 == 'M' && S3 == 'P')) { - cp.image_type = 2; - break; - } - 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 */ - outfile = optarg; - break; - /* ----------------------------------------------------- */ - case 'r': /* rates rates/distorsion*/ - s = optarg; - while (sscanf(s, "%d", &tcp_init->rates[tcp_init->numlayers]) == 1) { - tcp_init->numlayers++; - while (*s && *s != ',') { - s++; - } - if (!*s) - break; - s++; - } - cp.disto_alloc = 1; - cp.matrice = NULL; - break; - /* ----------------------------------------------------- */ - 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;inumlayers;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;j9) s++; - if (cp.matrice[i*NumResolution*3+j*3+1]>9) s++; - if (cp.matrice[i*NumResolution*3+j*3+2]>9) s++; - } - if (inumlayers-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 */ - sscanf(optarg, "%d", &NumResolution); - break; - /* ----------------------------------------------------- */ - case 'c': /* precinct dimension */ - sscanf(optarg, "%d,%d", &prcw_init, &prch_init); - CSty |= 0x01; - break; - /* ----------------------------------------------------- */ - 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"); - return 1; - } - break; - /* ----------------------------------------------------- */ - case 'x': /* creation of index file */ - index = optarg; - img.index_on = 1; - break; - /* ----------------------------------------------------- */ - 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; - } - break; - /* ----------------------------------------------------- */ - 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"); - return 1; - } - break; - /* ----------------------------------------------------- */ - 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"); - return 1; - } - break; - /* ----------------------------------------------------- */ - case 'h': /* Display an help description */ - help_display(); - return 0; - break; - /* ----------------------------------------------------- */ - 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) { - POC[numpocs].prg = give_progression(POC[numpocs].progorder); - /* POC[numpocs].tile; */ - numpocs++; - while (*s && *s != '/') { - s++; - } - if (!*s) - break; - s++; - } - break; - /* ------------------------------------------------------ */ - case 'S': /* SOP marker */ - CSty |= 0x02; - break; - /* ------------------------------------------------------ */ - case 'E': /* EPH marker */ - CSty |= 0x04; - break; - /* ------------------------------------------------------ */ - 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); - if (cache) - mode |= (1 << i); - } - } - break; - /* ------------------------------------------------------ */ - 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"); - return 1; - } - break; - /* ------------------------------------------------------ */ - case 'T': /* Tile offset */ - if (sscanf(optarg, "%d,%d", &TX0, &TY0) != 2) { - fprintf(stderr,"-T 'tile offset' argument error !! [-T X0,Y0]"); - return 1; - } - break; - /* ------------------------------------------------------ */ - case 'C': /* Add a comment */ - cp.comment = optarg; - break; - /* ------------------------------------------------------ */ - case 'I': /* reversible or not */ - ir = 1; - break; - /* ------------------------------------------------------ */ - default: - return 1; - } - } + int len; + 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 */ + 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_poc_t *tcp_poc; + j2k_tccp_t *tccp; + int i, tileno, j; + char *infile = 0; + char *outfile = 0; + char *index = 0; + char *s, S1, S2, S3; + int ir = 0; + + /* default value */ + /* ------------- */ + NumResolution = 6; + CSty = 0; + cblockw_init = 64; + cblockh_init = 64; + cp.tw = 1; + cp.th = 1; + img.index_on = 0; + Prog_order = 0; + numpocs = 0; + mode = 0; + subsampling_dx = 1; + subsampling_dy = 1; + ROI_compno = -1; /* no ROI */ + ROI_shift = 0; + Dim[0] = 0; + Dim[1] = 0; + TX0 = 0; + TY0 = 0; + cp.comment = NULL; + cp.disto_alloc = 0; + cp.fixed_alloc = 0; + /* img.PPT=0; */ + + Tile_arg = 0; + cp_init.tcps = (j2k_tcp_t *) malloc(sizeof(j2k_tcp_t)); /* initialisation if only one tile */ + tcp_init = &cp_init.tcps[0]; + tcp_init->numlayers = 0; + + while (1) { + int c = + getopt(argc, argv, + "i:o:r:q:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I"); + if (c == -1) + break; + switch (c) { + case 'i': /* IN fill */ + infile = optarg; + s = optarg; + while (*s) { + s++; + } + s--; + S3 = *s; + s--; + S2 = *s; + s--; + S1 = *s; + + if ((S1 == 'p' && S2 == 'g' && S3 == 'x') + || (S1 == 'P' && S2 == 'G' && S3 == 'X')) { + cp.image_type = 0; + break; + } - cp.tx0 = TX0; - cp.ty0 = TY0; + 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; + } - /* Error messages */ - /* -------------- */ - if (!infile || !outfile) { - fprintf(stderr, "usage: image_to_j2k -i image-file -o j2k-file (+ options)\n"); + if ((S1 == 'b' && S2 == 'm' && S3 == 'p') + || (S1 == 'B' && S2 == 'M' && S3 == 'P')) { + cp.image_type = 2; + break; + } + 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 */ + outfile = optarg; + break; + /* ----------------------------------------------------- */ + case 'r': /* rates rates/distorsion */ + s = optarg; + while (sscanf(s, "%d", &tcp_init->rates[tcp_init->numlayers]) + == 1) { + tcp_init->numlayers++; + while (*s && *s != ',') { + s++; + } + if (!*s) + break; + s++; + } + cp.disto_alloc = 1; + cp.matrice = NULL; + break; + /* ----------------------------------------------------- */ + 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++; + } + 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 */ + sscanf(optarg, "%d", &NumResolution); + break; + /* ----------------------------------------------------- */ + case 'c': /* precinct dimension */ + sscanf(optarg, "%d,%d", &prcw_init, &prch_init); + CSty |= 0x01; + break; + /* ----------------------------------------------------- */ + 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"); return 1; - } + } + break; + /* ----------------------------------------------------- */ + case 'x': /* creation of index file */ + index = optarg; + img.index_on = 1; + break; + /* ----------------------------------------------------- */ + case 'p': /* progression order */ + s = optarg; + for (i = 0; i < 4; i++) { + progression[i] = *s; + s++; + } + Prog_order = give_progression(progression); - if (cp.disto_alloc & cp.fixed_alloc) { - fprintf(stderr, "Error: option -r and -q can not be used together !!\n"); + if (Prog_order == -1) { + fprintf(stderr, + "Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n"); + return 1; + } + break; + /* ----------------------------------------------------- */ + 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"); + return 1; + } + break; + /* ----------------------------------------------------- */ + 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"); + return 1; + } + break; + /* ----------------------------------------------------- */ + case 'h': /* Display an help description */ + help_display(); + return 0; + break; + /* ----------------------------------------------------- */ + 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) { + POC[numpocs].prg = + give_progression(POC[numpocs].progorder); + /* POC[numpocs].tile; */ + numpocs++; + while (*s && *s != '/') { + s++; + } + if (!*s) + break; + s++; + } + break; + /* ------------------------------------------------------ */ + case 'S': /* SOP marker */ + CSty |= 0x02; + break; + /* ------------------------------------------------------ */ + case 'E': /* EPH marker */ + CSty |= 0x04; + break; + /* ------------------------------------------------------ */ + 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); + if (cache) + mode |= (1 << i); + } + } + break; + /* ------------------------------------------------------ */ + 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"); + return 1; + } + break; + /* ------------------------------------------------------ */ + case 'T': /* Tile offset */ + if (sscanf(optarg, "%d,%d", &TX0, &TY0) != 2) { + fprintf(stderr, + "-T 'tile offset' argument error !! [-T X0,Y0]"); return 1; + } + break; + /* ------------------------------------------------------ */ + case 'C': /* Add a comment */ + cp.comment = optarg; + break; + /* ------------------------------------------------------ */ + case 'I': /* reversible or not */ + ir = 1; + break; + /* ------------------------------------------------------ */ + default: + return 1; } - - /* if no rate entered, lossless by default */ - if (tcp_init->numlayers == 0) { - tcp_init->rates[tcp_init->numlayers] = 1; - tcp_init->numlayers++; - cp.disto_alloc = 1; + } + + cp.tx0 = TX0; + cp.ty0 = TY0; + + /* Error messages */ + /* -------------- */ + if (!infile || !outfile) { + fprintf(stderr, + "usage: image_to_j2k -i image-file -o j2k-file (+ options)\n"); + return 1; + } + + if (cp.disto_alloc & cp.fixed_alloc) { + fprintf(stderr, + "Error: option -r and -q can not be used together !!\n"); + return 1; + } + + /* if no rate entered, lossless by default */ + if (tcp_init->numlayers == 0) { + tcp_init->rates[tcp_init->numlayers] = 1; + tcp_init->numlayers++; + cp.disto_alloc = 1; + } + + 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]); + 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); } - - 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]); + } + + switch (cp.image_type) { + case 0: + if (Tile_arg) { + 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)) { + fprintf(stderr, " not a pgx file\n"); + return 1; + } } + break; - 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); - } + case 1: + if (!pnmtoimage(infile, &img, subsampling_dx, subsampling_dy, Dim)) { + fprintf(stderr, " not a pnm file\n"); + return 1; } + break; - switch (cp.image_type) { - case 0: - if (Tile_arg) { - 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)) { - fprintf(stderr, " not a pgx file\n"); - return 1; - } - } - break; - - case 1: - if (!pnmtoimage(infile, &img, subsampling_dx, subsampling_dy, Dim)) { - fprintf(stderr, " not a pnm file\n"); - return 1; - } - break; - - case 2: - if (!bmptoimage(infile, &img, subsampling_dx, subsampling_dy, Dim)) { - fprintf(stderr, " not a bmp file\n"); - return 1; - } - break; + case 2: + if (!bmptoimage(infile, &img, subsampling_dx, subsampling_dy, Dim)) { + fprintf(stderr, " not a bmp file\n"); + return 1; } - /* to respect profile - 0 */ - /* ---------------------- */ - numD_min = 0; - /* while (int_ceildiv(img.x1,(1<120 || int_ceildiv(img.y1,(1<160) numD_min++; - if ((numD_min+1)>NumResolution) - { - fprintf(stderr,"\n********************************************************************************\n\n"); - fprintf(stderr, "In view to respect Profile-0, the number of resolution used is %d in place of %d\n\n",numD_min+1,NumResolution); - fprintf(stderr, "********************************************************************************\n\n"); - NumResolution=numD_min+1; - } */ - - if (Tile_arg == 1) { - cp.tw = int_ceildiv(img.x1 - cp.tx0, cp.tdx); - cp.th = int_ceildiv(img.y1 - cp.ty0, cp.tdy); - } else { - cp.tdx = img.x1 - cp.tx0; - cp.tdy = img.y1 - cp.ty0; + break; + } + /* to respect profile - 0 */ + /* ---------------------- */ + numD_min = 0; + /* while (int_ceildiv(img.x1,(1<120 || int_ceildiv(img.y1,(1<160) numD_min++; + if ((numD_min+1)>NumResolution) + { + fprintf(stderr,"\n********************************************************************************\n\n"); + fprintf(stderr, "In view to respect Profile-0, the number of resolution used is %d in place of %d\n\n",numD_min+1,NumResolution); + fprintf(stderr, "********************************************************************************\n\n"); + NumResolution=numD_min+1; + } */ + + if (Tile_arg == 1) { + cp.tw = int_ceildiv(img.x1 - cp.tx0, cp.tdx); + cp.th = int_ceildiv(img.y1 - cp.ty0, cp.tdy); + } else { + cp.tdx = img.x1 - cp.tx0; + 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)); + + for (tileno = 0; tileno < cp.tw * cp.th; tileno++) { + tcp = &cp.tcps[tileno]; + tcp->numlayers = tcp_init->numlayers; + for (j = 0; j < tcp->numlayers; j++) { + tcp->rates[j] = tcp_init->rates[j]; } - - /* 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)); - - for (tileno = 0; tileno < cp.tw * cp.th; tileno++) { - tcp = &cp.tcps[tileno]; - tcp->numlayers = tcp_init->numlayers; - for (j = 0; j < tcp->numlayers; j++) { - tcp->rates[j] = tcp_init->rates[j]; + 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; + 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; + tcp_poc->compno0 = POC[numpocs_tile].compno0; + tcp_poc->layno1 = POC[numpocs_tile].layno1; + tcp_poc->resno1 = POC[numpocs_tile].resno1; + tcp_poc->compno1 = POC[numpocs_tile].compno1; + tcp_poc->prg = POC[numpocs_tile].prg; + tcp_poc->tile = POC[numpocs_tile].tile; + numpocs_tile++; } - 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; - 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; - tcp_poc->compno0 = POC[numpocs_tile].compno0; - tcp_poc->layno1 = POC[numpocs_tile].layno1; - tcp_poc->resno1 = POC[numpocs_tile].resno1; - tcp_poc->compno1 = POC[numpocs_tile].compno1; - tcp_poc->prg = POC[numpocs_tile].prg; - tcp_poc->tile = POC[numpocs_tile].tile; - numpocs_tile++; - } - } + } + } + tcp->numpocs = numpocs_tile; + tcp->tccps = + (j2k_tccp_t *) malloc(img.numcomps * sizeof(j2k_tccp_t)); + + for (i = 0; i < img.numcomps; i++) { + tccp = &tcp->tccps[i]; + tccp->csty = CSty & 0x01; /* 0 => one precinct || 1 => custom precinct */ + tccp->numresolutions = NumResolution; + tccp->cblkw = int_floorlog2(cblockw_init); + tccp->cblkh = int_floorlog2(cblockh_init); + tccp->cblksty = mode; + tccp->qmfbid = ir ? 0 : 1; + tccp->qntsty = + ir ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT; + tccp->numgbits = 2; + if (i == ROI_compno) + tccp->roishift = ROI_shift; + else + tccp->roishift = 0; + if (CSty & J2K_CCP_CSTY_PRT) { + + for (j = 0; j < tccp->numresolutions; j++) { + int size_prcw, size_prch; + size_prcw = + prcw_init >> (tccp->numresolutions - j - 1); + size_prch = + prch_init >> (tccp->numresolutions - j - 1); + if (size_prcw < 1) { + tccp->prcw[j] = 1; + } else { + tccp->prcw[j] = + int_floorlog2(prcw_init >> + (tccp->numresolutions - j - 1)); + } + if (size_prch < 1) { + tccp->prch[j] = 1; + } else { + tccp->prch[j] = + int_floorlog2(prch_init >> + (tccp->numresolutions - j - 1)); + } } - tcp->numpocs = numpocs_tile; - tcp->tccps = (j2k_tccp_t *) malloc(img.numcomps * sizeof(j2k_tccp_t)); - - for (i = 0; i < img.numcomps; i++) { - tccp = &tcp->tccps[i]; - tccp->csty = CSty & 0x01; /* 0 => one precinct || 1 => custom precinct */ - tccp->numresolutions = NumResolution; - tccp->cblkw = int_floorlog2(cblockw_init); - tccp->cblkh = int_floorlog2(cblockh_init); - tccp->cblksty = mode; - tccp->qmfbid = ir ? 0 : 1; - tccp->qntsty = ir ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT; - tccp->numgbits = 2; - if (i == ROI_compno) - tccp->roishift = ROI_shift; - else - tccp->roishift = 0; - if (CSty & J2K_CCP_CSTY_PRT) { - - for (j = 0; j < tccp->numresolutions; j++) { - int size_prcw, size_prch; - size_prcw = prcw_init >> (tccp->numresolutions - j - 1); - size_prch = prch_init >> (tccp->numresolutions - j - 1); - if (size_prcw < 1) { - tccp->prcw[j] = 1; - } else { - tccp->prcw[j] = - int_floorlog2(prcw_init >> (tccp->numresolutions - j - 1)); - } - if (size_prch < 1) { - tccp->prch[j] = 1; - } else { - tccp->prch[j] = - int_floorlog2(prch_init >> (tccp->numresolutions - j - 1)); - } - } - } else { - for (j = 0; j < tccp->numresolutions; j++) { - tccp->prcw[j] = 15; - tccp->prch[j] = 15; - } - } - calc_explicit_stepsizes(tccp, img.comps[i].prec); + } else { + for (j = 0; j < tccp->numresolutions; j++) { + tccp->prcw[j] = 15; + tccp->prch[j] = 15; } + } + calc_explicit_stepsizes(tccp, img.comps[i].prec); } - - len = j2k_encode(&img, &cp, outfile, cp.tdx * cp.tdy * 2, index); - if (len == 0) { - fprintf(stderr, "failed to encode image\n"); - return 1; + } + + len = j2k_encode(&img, &cp, outfile, cp.tdx * cp.tdy * 2, index); + if (len == 0) { + fprintf(stderr, "failed to encode image\n"); + return 1; + } + + /* Remove the temporary files */ + /* -------------------------- */ + if (cp.image_type) { /* PNM PGM PPM */ + for (i = 0; i < img.numcomps; i++) { + char tmp; + sprintf(&tmp, "Compo%d", i); + if (remove(&tmp) == -1) { + fprintf(stderr, "failed to kill %s file !\n", &tmp); + } } + } else { /* PGX */ + for (i = 0; i < cp.th; i++) { + char tmp; + sprintf(&tmp, "bandtile%d", i + 1); - /* Remove the temporary files */ - /* -------------------------- */ - if (cp.image_type) { /* PNM PGM PPM */ - for (i = 0; i < img.numcomps; i++) { - char tmp; - sprintf(&tmp, "Compo%d", i); - if (remove(&tmp) == -1) { - fprintf(stderr, "failed to kill %s file !\n", &tmp); - } - } - } else { /* PGX */ - for (i = 0; i < cp.th; i++) { - char tmp; - sprintf(&tmp, "bandtile%d", i + 1); - - if (remove(&tmp) == -1) { - fprintf(stderr, "failed to kill %s file !\n", &tmp); - } - } + if (remove(&tmp) == -1) { + fprintf(stderr, "failed to kill %s file !\n", &tmp); + } } + } - return 0; + return 0; } diff --git a/codec/j2k_to_image.c b/codec/j2k_to_image.c index f26b5672..d8bbc334 100644 --- a/codec/j2k_to_image.c +++ b/codec/j2k_to_image.c @@ -35,423 +35,488 @@ int ceildiv(int a, int b) { - return (a + b - 1) / b; + return (a + b - 1) / b; } int main(int argc, char **argv) { - FILE *f; - char *src, *src_name; - char *dest, S1, S2, S3; - int len; - j2k_image_t *img; - j2k_cp_t *cp; - 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 image-file -reduce n (<- optional)\n", argv[0]); - return 1; + FILE *f; + char *src, *src_name; + char *dest, S1, S2, S3; + int len; + j2k_image_t *img; + j2k_cp_t *cp; + 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 image-file -reduce n (<- optional)\n", + argv[0]); + return 1; + } + + f = fopen(argv[1], "rb"); + if (!f) { + fprintf(stderr, "failed to open %s for reading\n", argv[1]); + 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; } - - f = fopen(argv[1], "rb"); - if (!f) { - fprintf(stderr, "failed to open %s for reading\n", argv[1]); - return 1; + option.reduce_on = 1; + sscanf(argv[4], "%d", &option.reduce_value); + } + + while (*dest) { + dest++; + } + dest--; + S3 = *dest; + dest--; + S2 = *dest; + dest--; + S1 = *dest; + + 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')) { + image_type = 1; + } + + if ((S1 == 'b' && S2 == 'm' && S3 == 'p') + || (S1 == 'B' && S2 == 'M' && S3 == 'P')) { + image_type = 2; + } + + if (image_type == -1) { + fprintf(stderr, + "!! Unrecognized format for infile : %c%c%c [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n", + S1, S2, S3); + return 1; + } + + fseek(f, 0, SEEK_END); + len = ftell(f); + fseek(f, 0, SEEK_SET); + src = (char *) malloc(len); + fread(src, 1, len, f); + fclose(f); + + src_name = argv[1]; + while (*src_name) { + src_name++; + } + 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, option)) { + fprintf(stderr, "j2k_to_image: failed to decode image!\n"); + 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]); + } 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; - } - option.reduce_on = 1; - sscanf(argv[4],"%d",&option.reduce_value); - } - - while (*dest) { - dest++; + } + } else { + fprintf(stderr, + "j2k_to_image : Unknown format image *.%c%c%c [only *.j2k or *.jpt]!! \n", + S1, S2, S3); + return 1; } - dest--; - S3 = *dest; - dest--; - S2 = *dest; - dest--; - S1 = *dest; - - if ((S1 == 'p' && S2 == 'g' && S3 == 'x') || (S1 == 'P' && S2 == 'G' && S3 == 'X')) { - image_type = 0; + } + + free(src); + /* ------------------ CREATE OUT IMAGE WITH THE RIGHT FORMAT ----------------------- */ + + /* ---------------------------- / */ + /* / / */ + /* / 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); + // 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); + // 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 / 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 / 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 / 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); + } + 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]); + } + f = fopen(name, "wb"); + w = ceildiv(img->x1 - img->x0, 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); + // 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 / 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); + } } - - 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; + 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); + // 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 / wr * w + i % wr]; + 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); } - - if ((S1 == 'b' && S2 == 'm' && S3 == 'p') || (S1 == 'B' && S2 == 'M' && S3 == 'P')) { - image_type = 2; + 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); + // 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) (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) ((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 * 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 < wr * hr; i++) { + unsigned char R, G, B; + /* 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) % wr == 0) { + for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */ + fprintf(f, "%c", 0); + } + } + 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); + // 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) (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) ((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) (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", (256) & 0xff, ((256) >> 8) & 0xff, + ((256) >> 16) & 0xff, ((256) >> 24) & 0xff); } - if (image_type == -1) { - fprintf(stderr, "!! Unrecognized format for infile : %c%c%c [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n",S1,S2,S3); - return 1; + for (i = 0; i < 256; i++) { + fprintf(f, "%c%c%c%c", i, i, i, 0); } - fseek(f, 0, SEEK_END); - len = ftell(f); - fseek(f, 0, SEEK_SET); - src = (char *) malloc(len); - fread(src, 1, len, f); - fclose(f); - - src_name=argv[1]; - while (*src_name) { - src_name++; - } - 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, option)) { - fprintf(stderr, "j2k_to_image: failed to decode image!\n"); - return 1; + for (i = 0; i < wr * hr; i++) { + /* 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); } - } - 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 / */ - /* / / */ - /* ---------------------------- / */ - - 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); - // 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); - // 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 / 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 / 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 / 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); - } - 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]); - } - f = fopen(name, "wb"); - w = ceildiv(img->x1 - img->x0, 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); - // 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 / 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); - } - } - 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]); + } + break; + default: + break; + } - f = fopen(name, "wb"); - // 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 / wr * w + i % wr]; - 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); - // 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) (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) ((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 * 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 < wr * hr; i++) - { - unsigned char R, G, B; - /* 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) % wr == 0) - { - for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0 ; pad > 0 ; pad--) /* ADD */ - fprintf(f, "%c", 0); - } - } - 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); - // 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) (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) ((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) (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", (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 < wr * hr; i++) - { - /* 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 : - break; - } - - return 0; + return 0; } diff --git a/libopenjpeg/bio.c b/libopenjpeg/bio.c index 45e875f1..b9e32162 100644 --- a/libopenjpeg/bio.c +++ b/libopenjpeg/bio.c @@ -31,11 +31,11 @@ #include #include -static unsigned char *bio_start; /* pointer to the start of the buffer */ -static unsigned char *bio_end; /* pointer to the end of the buffer */ -static unsigned char *bio_bp; /* pointer to the present position in the buffer */ -static unsigned int bio_buf; /* temporary place where each byte is read or written */ -static int bio_ct; /* coder : number of bits free to write // decoder : number of bits read */ +static unsigned char *bio_start; /* pointer to the start of the buffer */ +static unsigned char *bio_end; /* pointer to the end of the buffer */ +static unsigned char *bio_bp; /* pointer to the present position in the buffer */ +static unsigned int bio_buf; /* temporary place where each byte is read or written */ +static int bio_ct; /* coder : number of bits free to write // decoder : number of bits read */ extern jmp_buf j2k_error; @@ -44,7 +44,7 @@ extern jmp_buf j2k_error; */ int bio_numbytes() { - return bio_bp - bio_start; + return bio_bp - bio_start; } /* @@ -55,11 +55,11 @@ int bio_numbytes() */ void bio_init_enc(unsigned char *bp, int len) { - bio_start = bp; - bio_end = bp + len; - bio_bp = bp; - bio_buf = 0; - bio_ct = 8; + bio_start = bp; + bio_end = bp + len; + bio_bp = bp; + bio_buf = 0; + bio_ct = 8; } /* @@ -70,11 +70,11 @@ void bio_init_enc(unsigned char *bp, int len) */ void bio_init_dec(unsigned char *bp, int len) { - bio_start = bp; - bio_end = bp + len; - bio_bp = bp; - bio_buf = 0; - bio_ct = 0; + bio_start = bp; + bio_end = bp + len; + bio_bp = bp; + bio_buf = 0; + bio_ct = 0; } /* @@ -83,12 +83,12 @@ void bio_init_dec(unsigned char *bp, int len) */ int bio_byteout() { - bio_buf = (bio_buf << 8) & 0xffff; - bio_ct = bio_buf == 0xff00 ? 7 : 8; - if (bio_bp >= bio_end) - return 1; - *bio_bp++ = bio_buf >> 8; - return 0; + bio_buf = (bio_buf << 8) & 0xffff; + bio_ct = bio_buf == 0xff00 ? 7 : 8; + if (bio_bp >= bio_end) + return 1; + *bio_bp++ = bio_buf >> 8; + return 0; } /* @@ -97,12 +97,12 @@ int bio_byteout() */ int bio_bytein() { - bio_buf = (bio_buf << 8) & 0xffff; - bio_ct = bio_buf == 0xff00 ? 7 : 8; - if (bio_bp >= bio_end) - return 1; - bio_buf |= *bio_bp++; - return 0; + bio_buf = (bio_buf << 8) & 0xffff; + bio_ct = bio_buf == 0xff00 ? 7 : 8; + if (bio_bp >= bio_end) + return 1; + bio_buf |= *bio_bp++; + return 0; } /* @@ -112,11 +112,11 @@ int bio_bytein() */ void bio_putbit(int b) { - if (bio_ct == 0) { - bio_byteout(); - } - bio_ct--; - bio_buf |= b << bio_ct; + if (bio_ct == 0) { + bio_byteout(); + } + bio_ct--; + bio_buf |= b << bio_ct; } /* @@ -125,11 +125,11 @@ void bio_putbit(int b) */ int bio_getbit() { - if (bio_ct == 0) { - bio_bytein(); - } - bio_ct--; - return (bio_buf >> bio_ct) & 1; + if (bio_ct == 0) { + bio_bytein(); + } + bio_ct--; + return (bio_buf >> bio_ct) & 1; } /* @@ -140,10 +140,10 @@ int bio_getbit() */ void bio_write(int v, int n) { - int i; - for (i = n - 1; i >= 0; i--) { - bio_putbit((v >> i) & 1); - } + int i; + for (i = n - 1; i >= 0; i--) { + bio_putbit((v >> i) & 1); + } } /* @@ -153,12 +153,12 @@ void bio_write(int v, int n) */ int bio_read(int n) { - int i, v; - v = 0; - for (i = n - 1; i >= 0; i--) { - v += bio_getbit() << i; - } - return v; + int i, v; + v = 0; + for (i = n - 1; i >= 0; i--) { + v += bio_getbit() << i; + } + return v; } /* @@ -167,16 +167,16 @@ int bio_read(int n) */ int bio_flush() { + bio_ct = 0; + if (bio_byteout()) + return 1; + if (bio_ct == 7) { bio_ct = 0; - if (bio_byteout()) - return 1; - if (bio_ct == 7) { - bio_ct = 0; - if (bio_byteout()) - return 1; - } - return 0; + if (bio_byteout()) + return 1; + } + return 0; } /* @@ -184,11 +184,11 @@ int bio_flush() */ int bio_inalign() { + bio_ct = 0; + if ((bio_buf & 0xff) == 0xff) { + if (bio_bytein()) + return 1; bio_ct = 0; - if ((bio_buf & 0xff) == 0xff) { - if (bio_bytein()) - return 1; - bio_ct = 0; - } - return 0; + } + return 0; } diff --git a/libopenjpeg/bio.h b/libopenjpeg/bio.h index adb20346..58995b05 100644 --- a/libopenjpeg/bio.h +++ b/libopenjpeg/bio.h @@ -31,7 +31,7 @@ /* * Number of bytes written. - */ + */ int bio_numbytes(); /* @@ -70,6 +70,6 @@ int bio_read(int n); */ int bio_flush(); -int bio_inalign(); /* modified to eliminated longjmp !! */ +int bio_inalign(); /* modified to eliminated longjmp !! */ #endif diff --git a/libopenjpeg/cio.c b/libopenjpeg/cio.c index b57e085f..cd44b420 100644 --- a/libopenjpeg/cio.c +++ b/libopenjpeg/cio.c @@ -27,9 +27,9 @@ #include "cio.h" #include -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 */ +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; @@ -38,7 +38,7 @@ extern jmp_buf j2k_error; */ int cio_numbytes() { - return cio_bp - cio_start; + return cio_bp - cio_start; } /* @@ -46,7 +46,7 @@ int cio_numbytes() */ int cio_tell() { - return cio_bp - cio_start; + return cio_bp - cio_start; } /* @@ -56,7 +56,7 @@ int cio_tell() */ void cio_seek(int pos) { - cio_bp = cio_start + pos; + cio_bp = cio_start + pos; } /* @@ -64,7 +64,7 @@ void cio_seek(int pos) */ int cio_numbytesleft() { - return cio_end - cio_bp; + return cio_end - cio_bp; } /* @@ -72,7 +72,7 @@ int cio_numbytesleft() */ unsigned char *cio_getbp() { - return cio_bp; + return cio_bp; } /* @@ -83,9 +83,9 @@ unsigned char *cio_getbp() */ void cio_init(unsigned char *bp, int len) { - cio_start = bp; - cio_end = bp + len; - cio_bp = bp; + cio_start = bp; + cio_end = bp + len; + cio_bp = bp; } /* @@ -93,9 +93,9 @@ void cio_init(unsigned char *bp, int len) */ void cio_byteout(unsigned char v) { - if (cio_bp >= cio_end) - longjmp(j2k_error, 1); - *cio_bp++ = v; + if (cio_bp >= cio_end) + longjmp(j2k_error, 1); + *cio_bp++ = v; } @@ -104,9 +104,9 @@ void cio_byteout(unsigned char v) */ unsigned char cio_bytein() { - if (cio_bp >= cio_end) - longjmp(j2k_error, 1); - return *cio_bp++; + if (cio_bp >= cio_end) + longjmp(j2k_error, 1); + return *cio_bp++; } /* @@ -117,10 +117,10 @@ unsigned char cio_bytein() */ void cio_write(unsigned int v, int n) { - int i; - for (i = n - 1; i >= 0; i--) { - cio_byteout((unsigned char) ((v >> (i << 3)) & 0xff)); - } + int i; + for (i = n - 1; i >= 0; i--) { + cio_byteout((unsigned char) ((v >> (i << 3)) & 0xff)); + } } /* @@ -132,13 +132,13 @@ void cio_write(unsigned int v, int n) */ unsigned int cio_read(int n) { - int i; - unsigned int v; - v = 0; - for (i = n - 1; i >= 0; i--) { - v += cio_bytein() << (i << 3); - } - return v; + int i; + unsigned int v; + v = 0; + for (i = n - 1; i >= 0; i--) { + v += cio_bytein() << (i << 3); + } + return v; } /* @@ -148,5 +148,5 @@ unsigned int cio_read(int n) */ void cio_skip(int n) { - cio_bp += n; + cio_bp += n; } diff --git a/libopenjpeg/dwt.c b/libopenjpeg/dwt.c index 5f6408c1..4ea7583d 100644 --- a/libopenjpeg/dwt.c +++ b/libopenjpeg/dwt.c @@ -46,20 +46,20 @@ /* This table contains the norms of the 5-3 wavelets for different bands. */ /* */ double dwt_norms[4][10] = { - {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3}, - {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, - {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, - {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93} + {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3}, + {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, + {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, + {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93} }; /* */ /* This table contains the norms of the 9-7 wavelets for different bands. */ /* */ double dwt_norms_real[4][10] = { - {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9}, - {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0}, - {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0}, - {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2} + {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9}, + {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0}, + {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0}, + {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2} }; /* Add Patrick */ @@ -72,11 +72,11 @@ static int lastSizeOfB = 0; void dwt_clean() { - if (b != NULL) { - free(b); - } - b = NULL; - lastSizeOfB = 0; + if (b != NULL) { + free(b); + } + b = NULL; + lastSizeOfB = 0; } /* \ Add Patrick */ @@ -86,29 +86,29 @@ void dwt_clean() /* */ void dwt_deinterleave(int *a, int n, int x, int res, int cas) { - int dn, sn, i; - sn = res; - dn = n - res; - if (lastSizeOfB != n) { - if (b != NULL) - free(b); - b = (int *) malloc(n * sizeof(int)); - lastSizeOfB = n; - } - - if (cas) { - for (i = 0; i < sn; i++) - b[i] = a[(2 * i + 1) * x]; - for (i = 0; i < dn; i++) - b[sn + i] = a[2 * i * x]; - } else { - for (i = 0; i < sn; i++) - b[i] = a[2 * i * x]; - for (i = 0; i < dn; i++) - b[sn + i] = a[(2 * i + 1) * x]; - } - for (i = 0; i < n; i++) - a[i * x] = b[i]; + 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]; } /* */ @@ -116,30 +116,30 @@ void dwt_deinterleave(int *a, int n, int x, int res, int cas) /* */ void dwt_interleave(int *a, int n, int x, int res, int cas) { - int dn, sn, i; - sn = res; - dn = n - res; - - if (lastSizeOfB != n) { - if (b != NULL) - free(b); - b = (int *) malloc(n * sizeof(int)); - lastSizeOfB = n; - } - - if (cas) { - for (i = 0; i < sn; i++) - b[2 * i + 1] = a[i * x]; - for (i = 0; i < dn; i++) - b[2 * i] = a[(sn + i) * x]; - } else { - for (i = 0; i < sn; i++) - b[2 * i] = a[i * x]; - for (i = 0; i < dn; i++) - b[2 * i + 1] = a[(sn + i) * x]; - } - for (i = 0; i < n; i++) - a[i * x] = b[i]; + 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]; } /* */ @@ -147,28 +147,28 @@ void dwt_interleave(int *a, int n, int x, int res, int cas) /* */ void dwt_encode_1(int *a, int n, int x, int res, int cas) { - int dn, sn, i = 0; - sn = res; - dn = n - res; - - if (cas) { - if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */ - S(i) *= 2; - else { - for (i = 0; i < dn; i++) - S(i) -= (DD_(i) + DD_(i - 1)) >> 1; - for (i = 0; i < sn; i++) - D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2; - } - } else { - if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ - for (i = 0; i < dn; i++) - D(i) -= (S_(i) + S_(i + 1)) >> 1; - for (i = 0; i < sn; i++) - S(i) += (D_(i - 1) + D_(i) + 2) >> 2; - } + int dn, sn, i = 0; + sn = res; + dn = n - res; + + if (cas) { + if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */ + S(i) *= 2; + else { + for (i = 0; i < dn; i++) + S(i) -= (DD_(i) + DD_(i - 1)) >> 1; + for (i = 0; i < sn; i++) + D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2; } - dwt_deinterleave(a, n, x, res, cas); + } else { + if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ + for (i = 0; i < dn; i++) + D(i) -= (S_(i) + S_(i + 1)) >> 1; + for (i = 0; i < sn; i++) + S(i) += (D_(i - 1) + D_(i) + 2) >> 2; + } + } + dwt_deinterleave(a, n, x, res, cas); } /* */ @@ -176,28 +176,28 @@ void dwt_encode_1(int *a, int n, int x, int res, int cas) /* */ void dwt_decode_1(int *a, int n, int x, int res, int cas) { - int dn, sn, i = 0; - sn = res; - dn = n - res; - - dwt_interleave(a, n, x, res, cas); - if (cas) { - if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */ - S(i) /= 2; - else { - for (i = 0; i < sn; i++) - D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2; - for (i = 0; i < dn; i++) - S(i) += (DD_(i) + DD_(i - 1)) >> 1; - } - } else { - if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ - for (i = 0; i < sn; i++) - S(i) -= (D_(i - 1) + D_(i) + 2) >> 2; - for (i = 0; i < dn; i++) - D(i) += (S_(i) + S_(i + 1)) >> 1; - } + int dn, sn, i = 0; + sn = res; + dn = n - res; + + dwt_interleave(a, n, x, res, cas); + if (cas) { + if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */ + S(i) /= 2; + else { + for (i = 0; i < sn; i++) + D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2; + for (i = 0; i < dn; i++) + S(i) += (DD_(i) + DD_(i - 1)) >> 1; + } + } else { + if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ + for (i = 0; i < sn; i++) + S(i) -= (D_(i - 1) + D_(i) + 2) >> 2; + for (i = 0; i < dn; i++) + D(i) += (S_(i) + S_(i + 1)) >> 1; } + } } /* */ @@ -205,61 +205,70 @@ void dwt_decode_1(int *a, int n, int x, int res, int cas) /* */ void dwt_encode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l) { - 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; /* 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; - - 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); - for (j = 0; j < rh; j++) - dwt_encode_1(a + j * w, rw, 1, rw1, cas_row); - } - - dwt_clean(); + 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; /* 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; + + 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); + for (j = 0; j < rh; j++) + dwt_encode_1(a + j * w, rw, 1, rw1, cas_row); + } + + dwt_clean(); } /* */ /* Inverse 5-3 wavelet tranform in 2-D. */ /* */ -void dwt_decode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l, int stop) +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 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 >= 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 */ - - 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; - - 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); - for (j = 0; j < rw; j++) - dwt_decode_1(a + j, rh, w, rh1, cas_col); - } - dwt_clean(); + 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 >= 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 */ + + 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; + + 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); + for (j = 0; j < rw; j++) + dwt_decode_1(a + j, rh, w, rh1, cas_col); + } + dwt_clean(); } /* */ @@ -267,11 +276,11 @@ void dwt_decode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l, int stop) /* */ int dwt_getgain(int orient) { - if (orient == 0) - return 0; - if (orient == 1 || orient == 2) - return 1; - return 2; + if (orient == 0) + return 0; + if (orient == 1 || orient == 2) + return 1; + return 2; } /* */ @@ -279,7 +288,7 @@ int dwt_getgain(int orient) /* */ double dwt_getnorm(int level, int orient) { - return dwt_norms[orient][level]; + return dwt_norms[orient][level]; } /* */ @@ -287,42 +296,42 @@ double dwt_getnorm(int level, int orient) /* */ void dwt_encode_1_real(int *a, int n, int x, int res, int cas) { - int dn, sn, i = 0; - dn = n - res; - sn = res; - - if (cas) { - if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */ - for (i = 0; i < dn; i++) - S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993); - for (i = 0; i < sn; i++) - D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434); - for (i = 0; i < dn; i++) - S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233); - for (i = 0; i < sn; i++) - D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633); - for (i = 0; i < dn; i++) - S(i) = fix_mul(S(i), 5038); /*5038*/ - for (i = 0; i < sn; i++) - D(i) = fix_mul(D(i), 6659); /*6660*/ - } - } else { - if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ - for (i = 0; i < dn; i++) - D(i) -= fix_mul(S_(i) + S_(i + 1), 12993); - for (i = 0; i < sn; i++) - S(i) -= fix_mul(D_(i - 1) + D_(i), 434); - for (i = 0; i < dn; i++) - D(i) += fix_mul(S_(i) + S_(i + 1), 7233); - for (i = 0; i < sn; i++) - S(i) += fix_mul(D_(i - 1) + D_(i), 3633); - for (i = 0; i < dn; i++) - D(i) = fix_mul(D(i), 5038); /*5038*/ - for (i = 0; i < sn; i++) - S(i) = fix_mul(S(i), 6659); /*6660*/ - } + int dn, sn, i = 0; + dn = n - res; + sn = res; + + if (cas) { + if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */ + for (i = 0; i < dn; i++) + S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993); + for (i = 0; i < sn; i++) + D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434); + for (i = 0; i < dn; i++) + S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233); + for (i = 0; i < sn; i++) + D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633); + for (i = 0; i < dn; i++) + S(i) = fix_mul(S(i), 5038); /*5038 */ + for (i = 0; i < sn; i++) + D(i) = fix_mul(D(i), 6659); /*6660 */ + } + } else { + if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ + for (i = 0; i < dn; i++) + D(i) -= fix_mul(S_(i) + S_(i + 1), 12993); + for (i = 0; i < sn; i++) + S(i) -= fix_mul(D_(i - 1) + D_(i), 434); + for (i = 0; i < dn; i++) + D(i) += fix_mul(S_(i) + S_(i + 1), 7233); + for (i = 0; i < sn; i++) + S(i) += fix_mul(D_(i - 1) + D_(i), 3633); + for (i = 0; i < dn; i++) + D(i) = fix_mul(D(i), 5038); /*5038 */ + for (i = 0; i < sn; i++) + S(i) = fix_mul(S(i), 6659); /*6660 */ } - dwt_deinterleave(a, n, x, res, cas); + } + dwt_deinterleave(a, n, x, res, cas); } /* */ @@ -330,41 +339,41 @@ void dwt_encode_1_real(int *a, int n, int x, int res, int cas) /* */ 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(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), 10078); /* 10076 */ - for (i = 0; i < dn; i++) - 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++) - S(i) -= fix_mul(DD_(i) + DD_(i - 1), 7233); - for (i = 0; i < sn; i++) - D(i) += fix_mul(SS_(i) + SS_(i + 1), 434); - for (i = 0; i < dn; i++) - S(i) += fix_mul(DD_(i) + DD_(i - 1), 12993); - } - } else { - if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ - for (i = 0; i < sn; i++) - S(i) = fix_mul(S(i), 10078); /* 10076 */ - for (i = 0; i < dn; i++) - 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++) - D(i) -= fix_mul(S_(i) + S_(i + 1), 7233); - for (i = 0; i < sn; i++) - S(i) += fix_mul(D_(i - 1) + D_(i), 434); - for (i = 0; i < dn; i++) - D(i) += fix_mul(S_(i) + S_(i + 1), 12993); - } + int dn, sn, i = 0; + dn = n - res; + sn = res; + 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), 10078); /* 10076 */ + for (i = 0; i < dn; i++) + 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++) + S(i) -= fix_mul(DD_(i) + DD_(i - 1), 7233); + for (i = 0; i < sn; i++) + D(i) += fix_mul(SS_(i) + SS_(i + 1), 434); + for (i = 0; i < dn; i++) + S(i) += fix_mul(DD_(i) + DD_(i - 1), 12993); } + } else { + if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ + for (i = 0; i < sn; i++) + S(i) = fix_mul(S(i), 10078); /* 10076 */ + for (i = 0; i < dn; i++) + 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++) + D(i) -= fix_mul(S_(i) + S_(i + 1), 7233); + for (i = 0; i < sn; i++) + S(i) += fix_mul(D_(i - 1) + D_(i), 434); + for (i = 0; i < dn; i++) + D(i) += fix_mul(S_(i) + S_(i + 1), 12993); + } + } } /* */ @@ -373,58 +382,67 @@ void dwt_decode_1_real(int *a, int n, int x, int res, int cas) void dwt_encode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l) { - 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; /* 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; - - 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); - for (j = 0; j < rh; j++) - dwt_encode_1_real(a + j * w, rw, 1, rw1, cas_row); - } + 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; /* 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; + + 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); + for (j = 0; j < rh; j++) + dwt_encode_1_real(a + j * w, rw, 1, rw1, cas_row); + } } /* */ /* Inverse 9-7 wavelet transform in 2-D. */ /* */ -void dwt_decode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l, int stop) +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 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 >= 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 */ - - 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; - - 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); - for (j = 0; j < rw; j++) - dwt_decode_1_real(a + j, rh, w, rh1, cas_col); - } + 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 >= 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 */ + + 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; + + 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); + for (j = 0; j < rw; j++) + dwt_decode_1_real(a + j, rh, w, rh1, cas_col); + } } /* */ @@ -432,7 +450,7 @@ void dwt_decode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l, int st /* */ int dwt_getgain_real(int orient) { - return 0; + return 0; } /* */ @@ -440,5 +458,5 @@ int dwt_getgain_real(int orient) /* */ double dwt_getnorm_real(int level, int orient) { - return dwt_norms_real[orient][level]; + return dwt_norms_real[orient][level]; } diff --git a/libopenjpeg/dwt.h b/libopenjpeg/dwt.h index 8f38bb9e..ec9860b6 100644 --- a/libopenjpeg/dwt.h +++ b/libopenjpeg/dwt.h @@ -51,7 +51,8 @@ void dwt_encode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l); * 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, int stop); +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 @@ -82,7 +83,8 @@ 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, int stop); +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) diff --git a/libopenjpeg/fix.c b/libopenjpeg/fix.c index 21466ae9..5d7f2b67 100644 --- a/libopenjpeg/fix.c +++ b/libopenjpeg/fix.c @@ -37,5 +37,5 @@ */ int fix_mul(int a, int b) { - return (int) ((int64) a * (int64) b >> 13); + return (int) ((int64) a * (int64) b >> 13); } diff --git a/libopenjpeg/int.c b/libopenjpeg/int.c index 283df097..8e82b887 100644 --- a/libopenjpeg/int.c +++ b/libopenjpeg/int.c @@ -31,7 +31,7 @@ */ int int_min(int a, int b) { - return a < b ? a : b; + return a < b ? a : b; } /* @@ -41,7 +41,7 @@ int int_min(int a, int b) */ int int_max(int a, int b) { - return a > b ? a : b; + return a > b ? a : b; } /* @@ -53,11 +53,11 @@ int int_max(int a, int b) */ int int_clamp(int a, int min, int max) { - if (a < min) - return min; - if (a > max) - return max; - return a; + if (a < min) + return min; + if (a > max) + return max; + return a; } /* @@ -65,7 +65,7 @@ int int_clamp(int a, int min, int max) */ int int_abs(int a) { - return a < 0 ? -a : a; + return a < 0 ? -a : a; } /* @@ -75,7 +75,7 @@ int int_abs(int a) */ int int_ceildiv(int a, int b) { - return (a + b - 1) / b; + return (a + b - 1) / b; } /* @@ -85,7 +85,7 @@ int int_ceildiv(int a, int b) */ int int_ceildivpow2(int a, int b) { - return (a + (1 << b) - 1) >> b; + return (a + (1 << b) - 1) >> b; } /* @@ -95,7 +95,7 @@ int int_ceildivpow2(int a, int b) */ int int_floordivpow2(int a, int b) { - return a >> b; + return a >> b; } /* @@ -105,9 +105,9 @@ int int_floordivpow2(int a, int b) */ int int_floorlog2(int a) { - int l; - for (l = 0; a > 1; l++) { - a >>= 1; - } - return l; + int l; + for (l = 0; a > 1; l++) { + a >>= 1; + } + return l; } diff --git a/libopenjpeg/j2k.c b/libopenjpeg/j2k.c index a00bab6a..80a53026 100644 --- a/libopenjpeg/j2k.c +++ b/libopenjpeg/j2k.c @@ -89,1265 +89,1331 @@ static info_image info_IM; /* Add Patrick */ void j2k_clean() { - int tileno = 0; - tcd_free_encode(j2k_img, j2k_cp, j2k_curtileno); + int tileno = 0; + tcd_free_encode(j2k_img, j2k_cp, j2k_curtileno); - if (info_IM.index_on) { - for (tileno = 0; tileno < j2k_cp->tw * j2k_cp->th; tileno++) { - free(info_IM.tile[tileno].packet); - } - free(info_IM.tile); + if (info_IM.index_on) { + for (tileno = 0; tileno < j2k_cp->tw * j2k_cp->th; tileno++) { + free(info_IM.tile[tileno].packet); } + free(info_IM.tile); + } } /* \Add Patrick */ void j2k_dump_image(j2k_image_t * img) { - int compno; - fprintf(stderr, "image {\n"); - fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0, - img->x1, img->y1); - fprintf(stderr, " numcomps=%d\n", img->numcomps); - for (compno = 0; compno < img->numcomps; compno++) { - j2k_comp_t *comp = &img->comps[compno]; - fprintf(stderr, " comp %d {\n", compno); - fprintf(stderr, " dx=%d, dy=%d\n", comp->dx, comp->dy); - fprintf(stderr, " prec=%d\n", comp->prec); - fprintf(stderr, " sgnd=%d\n", comp->sgnd); - fprintf(stderr, " }\n"); - } - fprintf(stderr, "}\n"); + int compno; + fprintf(stderr, "image {\n"); + fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0, + img->x1, img->y1); + fprintf(stderr, " numcomps=%d\n", img->numcomps); + for (compno = 0; compno < img->numcomps; compno++) { + j2k_comp_t *comp = &img->comps[compno]; + fprintf(stderr, " comp %d {\n", compno); + fprintf(stderr, " dx=%d, dy=%d\n", comp->dx, comp->dy); + fprintf(stderr, " prec=%d\n", comp->prec); + fprintf(stderr, " sgnd=%d\n", comp->sgnd); + fprintf(stderr, " }\n"); + } + fprintf(stderr, "}\n"); } void j2k_dump_cp(j2k_image_t * img, j2k_cp_t * cp) { - int tileno, compno, layno, bandno, resno, numbands; - fprintf(stderr, "coding parameters {\n"); - fprintf(stderr, " tx0=%d, ty0=%d\n", cp->tx0, cp->ty0); - fprintf(stderr, " tdx=%d, tdy=%d\n", cp->tdx, cp->tdy); - fprintf(stderr, " tw=%d, th=%d\n", cp->tw, cp->th); - for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { - j2k_tcp_t *tcp = &cp->tcps[tileno]; - fprintf(stderr, " tile %d {\n", tileno); - fprintf(stderr, " csty=%x\n", tcp->csty); - fprintf(stderr, " prg=%d\n", tcp->prg); - fprintf(stderr, " numlayers=%d\n", tcp->numlayers); - fprintf(stderr, " mct=%d\n", tcp->mct); - fprintf(stderr, " rates="); - for (layno = 0; layno < tcp->numlayers; layno++) { - fprintf(stderr, "%d ", tcp->rates[layno]); + int tileno, compno, layno, bandno, resno, numbands; + fprintf(stderr, "coding parameters {\n"); + fprintf(stderr, " tx0=%d, ty0=%d\n", cp->tx0, cp->ty0); + fprintf(stderr, " tdx=%d, tdy=%d\n", cp->tdx, cp->tdy); + fprintf(stderr, " tw=%d, th=%d\n", cp->tw, cp->th); + for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { + j2k_tcp_t *tcp = &cp->tcps[tileno]; + fprintf(stderr, " tile %d {\n", tileno); + fprintf(stderr, " csty=%x\n", tcp->csty); + fprintf(stderr, " prg=%d\n", tcp->prg); + fprintf(stderr, " numlayers=%d\n", tcp->numlayers); + fprintf(stderr, " mct=%d\n", tcp->mct); + fprintf(stderr, " rates="); + for (layno = 0; layno < tcp->numlayers; layno++) { + fprintf(stderr, "%d ", tcp->rates[layno]); + } + fprintf(stderr, "\n"); + for (compno = 0; compno < img->numcomps; compno++) { + j2k_tccp_t *tccp = &tcp->tccps[compno]; + fprintf(stderr, " comp %d {\n", compno); + fprintf(stderr, " csty=%x\n", tccp->csty); + fprintf(stderr, " numresolutions=%d\n", + tccp->numresolutions); + fprintf(stderr, " cblkw=%d\n", tccp->cblkw); + fprintf(stderr, " cblkh=%d\n", tccp->cblkh); + fprintf(stderr, " cblksty=%x\n", tccp->cblksty); + fprintf(stderr, " qmfbid=%d\n", tccp->qmfbid); + fprintf(stderr, " qntsty=%d\n", tccp->qntsty); + fprintf(stderr, " numgbits=%d\n", tccp->numgbits); + fprintf(stderr, " roishift=%d\n", tccp->roishift); + fprintf(stderr, " stepsizes="); + numbands = + tccp->qntsty == + J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2; + for (bandno = 0; bandno < numbands; bandno++) { + fprintf(stderr, "(%d,%d) ", tccp->stepsizes[bandno].mant, + tccp->stepsizes[bandno].expn); + } + fprintf(stderr, "\n"); + + if (tccp->csty & J2K_CCP_CSTY_PRT) { + fprintf(stderr, " prcw="); + for (resno = 0; resno < tccp->numresolutions; resno++) { + fprintf(stderr, "%d ", tccp->prcw[resno]); } fprintf(stderr, "\n"); - for (compno = 0; compno < img->numcomps; compno++) { - j2k_tccp_t *tccp = &tcp->tccps[compno]; - fprintf(stderr, " comp %d {\n", compno); - fprintf(stderr, " csty=%x\n", tccp->csty); - fprintf(stderr, " numresolutions=%d\n", tccp->numresolutions); - fprintf(stderr, " cblkw=%d\n", tccp->cblkw); - fprintf(stderr, " cblkh=%d\n", tccp->cblkh); - fprintf(stderr, " cblksty=%x\n", tccp->cblksty); - fprintf(stderr, " qmfbid=%d\n", tccp->qmfbid); - fprintf(stderr, " qntsty=%d\n", tccp->qntsty); - fprintf(stderr, " numgbits=%d\n", tccp->numgbits); - fprintf(stderr, " roishift=%d\n", tccp->roishift); - fprintf(stderr, " stepsizes="); - numbands = - tccp->qntsty == - J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2; - for (bandno = 0; bandno < numbands; bandno++) { - fprintf(stderr, "(%d,%d) ", tccp->stepsizes[bandno].mant, - tccp->stepsizes[bandno].expn); - } - fprintf(stderr, "\n"); - - if (tccp->csty & J2K_CCP_CSTY_PRT) { - fprintf(stderr, " prcw="); - for (resno = 0; resno < tccp->numresolutions; resno++) { - fprintf(stderr, "%d ", tccp->prcw[resno]); - } - fprintf(stderr, "\n"); - fprintf(stderr, " prch="); - for (resno = 0; resno < tccp->numresolutions; resno++) { - fprintf(stderr, "%d ", tccp->prch[resno]); - } - fprintf(stderr, "\n"); - } - fprintf(stderr, " }\n"); + fprintf(stderr, " prch="); + for (resno = 0; resno < tccp->numresolutions; resno++) { + fprintf(stderr, "%d ", tccp->prch[resno]); } - fprintf(stderr, " }\n"); + fprintf(stderr, "\n"); + } + fprintf(stderr, " }\n"); } - fprintf(stderr, "}\n"); + fprintf(stderr, " }\n"); + } + fprintf(stderr, "}\n"); } void j2k_write_soc() { - cio_write(J2K_MS_SOC, 2); + cio_write(J2K_MS_SOC, 2); } void j2k_read_soc() { - j2k_state = J2K_STATE_MHSIZ; + j2k_state = J2K_STATE_MHSIZ; } void j2k_write_siz() { - int i; - int lenp, len; - - cio_write(J2K_MS_SIZ, 2); /* SIZ */ - lenp = cio_tell(); - cio_skip(2); - cio_write(0, 2); /* Rsiz (capabilities) */ - cio_write(j2k_img->x1, 4); /* Xsiz */ - cio_write(j2k_img->y1, 4); /* Ysiz */ - cio_write(j2k_img->x0, 4); /* X0siz */ - cio_write(j2k_img->y0, 4); /* Y0siz */ - cio_write(j2k_cp->tdx, 4); /* XTsiz */ - cio_write(j2k_cp->tdy, 4); /* YTsiz */ - cio_write(j2k_cp->tx0, 4); /* XT0siz */ - cio_write(j2k_cp->ty0, 4); /* YT0siz */ - cio_write(j2k_img->numcomps, 2); /* Csiz */ - for (i = 0; i < j2k_img->numcomps; i++) { - cio_write(j2k_img->comps[i].prec - 1 + (j2k_img->comps[i].sgnd << 7), 1); /* Ssiz_i */ - cio_write(j2k_img->comps[i].dx, 1); /* XRsiz_i */ - cio_write(j2k_img->comps[i].dy, 1); /* YRsiz_i */ - } - len = cio_tell() - lenp; - cio_seek(lenp); - cio_write(len, 2); /* Lsiz */ - cio_seek(lenp + len); + int i; + int lenp, len; + + cio_write(J2K_MS_SIZ, 2); /* SIZ */ + lenp = cio_tell(); + cio_skip(2); + cio_write(0, 2); /* Rsiz (capabilities) */ + cio_write(j2k_img->x1, 4); /* Xsiz */ + cio_write(j2k_img->y1, 4); /* Ysiz */ + cio_write(j2k_img->x0, 4); /* X0siz */ + cio_write(j2k_img->y0, 4); /* Y0siz */ + cio_write(j2k_cp->tdx, 4); /* XTsiz */ + cio_write(j2k_cp->tdy, 4); /* YTsiz */ + cio_write(j2k_cp->tx0, 4); /* XT0siz */ + cio_write(j2k_cp->ty0, 4); /* YT0siz */ + cio_write(j2k_img->numcomps, 2); /* Csiz */ + for (i = 0; i < j2k_img->numcomps; i++) { + cio_write(j2k_img->comps[i].prec - 1 + (j2k_img->comps[i].sgnd << 7), 1); /* Ssiz_i */ + cio_write(j2k_img->comps[i].dx, 1); /* XRsiz_i */ + cio_write(j2k_img->comps[i].dy, 1); /* YRsiz_i */ + } + len = cio_tell() - lenp; + cio_seek(lenp); + cio_write(len, 2); /* Lsiz */ + cio_seek(lenp + len); } void j2k_read_siz() { - int len, i; - - len = cio_read(2); /* Lsiz */ - cio_read(2); /* Rsiz (capabilities) */ - j2k_img->x1 = cio_read(4); /* Xsiz */ - j2k_img->y1 = cio_read(4); /* Ysiz */ - j2k_img->x0 = cio_read(4); /* X0siz */ - j2k_img->y0 = cio_read(4); /* Y0siz */ - j2k_cp->tdx = cio_read(4); /* XTsiz */ - j2k_cp->tdy = cio_read(4); /* YTsiz */ - j2k_cp->tx0 = cio_read(4); /* XT0siz */ - j2k_cp->ty0 = cio_read(4); /* YT0siz */ - - j2k_img->numcomps = cio_read(2); /* Csiz */ - j2k_img->comps = (j2k_comp_t *) malloc(j2k_img->numcomps * sizeof(j2k_comp_t)); - for (i = 0; i < j2k_img->numcomps; i++) { - int tmp, w, h; - tmp = cio_read(1); /* Ssiz_i */ - j2k_img->comps[i].prec = (tmp & 0x7f) + 1; - j2k_img->comps[i].sgnd = tmp >> 7; - j2k_img->comps[i].dx = cio_read(1); /* XRsiz_i */ - 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].resno_decoded = 0; /* number of resolution decoded */ - j2k_img->comps[i].factor = 0; /* reducing factor by component */ - } + int len, i; + + len = cio_read(2); /* Lsiz */ + cio_read(2); /* Rsiz (capabilities) */ + j2k_img->x1 = cio_read(4); /* Xsiz */ + j2k_img->y1 = cio_read(4); /* Ysiz */ + j2k_img->x0 = cio_read(4); /* X0siz */ + j2k_img->y0 = cio_read(4); /* Y0siz */ + j2k_cp->tdx = cio_read(4); /* XTsiz */ + j2k_cp->tdy = cio_read(4); /* YTsiz */ + j2k_cp->tx0 = cio_read(4); /* XT0siz */ + j2k_cp->ty0 = cio_read(4); /* YT0siz */ + + j2k_img->numcomps = cio_read(2); /* Csiz */ + j2k_img->comps = + (j2k_comp_t *) malloc(j2k_img->numcomps * sizeof(j2k_comp_t)); + for (i = 0; i < j2k_img->numcomps; i++) { + int tmp, w, h; + tmp = cio_read(1); /* Ssiz_i */ + j2k_img->comps[i].prec = (tmp & 0x7f) + 1; + j2k_img->comps[i].sgnd = tmp >> 7; + j2k_img->comps[i].dx = cio_read(1); /* XRsiz_i */ + 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].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; + j2k_cp->tcps[i].numpocs = 0; + j2k_cp->tcps[i].first = 1; + } + + /* Initialization for PPM marker */ + j2k_cp->ppm = 0; + j2k_cp->ppm_data = NULL; + j2k_cp->ppm_previous = 0; + j2k_cp->ppm_store = 0; + + j2k_default_tcp.tccps = + (j2k_tccp_t *) calloc(sizeof(j2k_tccp_t), j2k_img->numcomps); + for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) { + j2k_cp->tcps[i].tccps = + (j2k_tccp_t *) calloc(sizeof(j2k_tccp_t), j2k_img->numcomps); + } + j2k_tile_data = + (unsigned char **) calloc(j2k_cp->tw * j2k_cp->th, sizeof(char *)); + j2k_tile_len = (int *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(int)); + j2k_state = J2K_STATE_MH; - 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; itw * j2k_cp->th; i++) - { - j2k_cp->tcps[i].POC=0; - j2k_cp->tcps[i].numpocs=0; - j2k_cp->tcps[i].first=1; - } - - /* Initialization for PPM marker */ - j2k_cp->ppm = 0; - j2k_cp->ppm_data = NULL; - j2k_cp->ppm_previous = 0; - j2k_cp->ppm_store = 0; - - j2k_default_tcp.tccps = (j2k_tccp_t *) calloc(sizeof(j2k_tccp_t), j2k_img->numcomps); - for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) { - j2k_cp->tcps[i].tccps = (j2k_tccp_t *) calloc(sizeof(j2k_tccp_t), j2k_img->numcomps); - } - j2k_tile_data = (unsigned char **) calloc(j2k_cp->tw * j2k_cp->th, sizeof(char *)); - j2k_tile_len = (int *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(int)); - j2k_state = J2K_STATE_MH; - } void j2k_write_com() { - unsigned int i; - int lenp, len; - char str[256]; - sprintf(str, "%s", j2k_cp->comment); - - cio_write(J2K_MS_COM, 2); - lenp = cio_tell(); - cio_skip(2); - cio_write(0, 2); - for (i = 0; i < strlen(str); i++) { - cio_write(str[i], 1); - } - len = cio_tell() - lenp; - cio_seek(lenp); - cio_write(len, 2); - cio_seek(lenp + len); + unsigned int i; + int lenp, len; + char str[256]; + sprintf(str, "%s", j2k_cp->comment); + + cio_write(J2K_MS_COM, 2); + lenp = cio_tell(); + cio_skip(2); + cio_write(0, 2); + for (i = 0; i < strlen(str); i++) { + cio_write(str[i], 1); + } + len = cio_tell() - lenp; + cio_seek(lenp); + cio_write(len, 2); + cio_seek(lenp + len); } void j2k_read_com() { - int len; + int len; + + len = cio_read(2); + cio_skip(len - 2); - len = cio_read(2); - cio_skip(len - 2); - } void j2k_write_cox(int compno) { - int i; - j2k_tcp_t *tcp; - j2k_tccp_t *tccp; - tcp = &j2k_cp->tcps[j2k_curtileno]; - tccp = &tcp->tccps[compno]; - - cio_write(tccp->numresolutions - 1, 1); /* SPcox (D) */ - cio_write(tccp->cblkw - 2, 1); /* SPcox (E) */ - cio_write(tccp->cblkh - 2, 1); /* SPcox (F) */ - cio_write(tccp->cblksty, 1); /* SPcox (G) */ - cio_write(tccp->qmfbid, 1); /* SPcox (H) */ - - if (tccp->csty & J2K_CCP_CSTY_PRT) { - for (i = 0; i < tccp->numresolutions; i++) { - cio_write(tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */ - } + int i; + j2k_tcp_t *tcp; + j2k_tccp_t *tccp; + tcp = &j2k_cp->tcps[j2k_curtileno]; + tccp = &tcp->tccps[compno]; + + cio_write(tccp->numresolutions - 1, 1); /* SPcox (D) */ + cio_write(tccp->cblkw - 2, 1); /* SPcox (E) */ + cio_write(tccp->cblkh - 2, 1); /* SPcox (F) */ + cio_write(tccp->cblksty, 1); /* SPcox (G) */ + cio_write(tccp->qmfbid, 1); /* SPcox (H) */ + + if (tccp->csty & J2K_CCP_CSTY_PRT) { + for (i = 0; i < tccp->numresolutions; i++) { + cio_write(tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */ } + } } void j2k_read_cox(int compno) { - int i; - j2k_tcp_t *tcp; - j2k_tccp_t *tccp; - tcp = j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; - tccp = &tcp->tccps[compno]; - tccp->numresolutions = cio_read(1) + 1; /* SPcox (D) */ - tccp->cblkw = cio_read(1) + 2; /* SPcox (E) */ - tccp->cblkh = cio_read(1) + 2; /* SPcox (F) */ - tccp->cblksty = cio_read(1); /* SPcox (G) */ - tccp->qmfbid = cio_read(1); /* SPcox (H) */ - if (tccp->csty & J2K_CP_CSTY_PRT) { - for (i = 0; i < tccp->numresolutions; i++) { - int tmp = cio_read(1); /* SPcox (I_i) */ - tccp->prcw[i] = tmp & 0xf; - tccp->prch[i] = tmp >> 4; - } + int i; + j2k_tcp_t *tcp; + j2k_tccp_t *tccp; + tcp = + j2k_state == + J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; + tccp = &tcp->tccps[compno]; + tccp->numresolutions = cio_read(1) + 1; /* SPcox (D) */ + tccp->cblkw = cio_read(1) + 2; /* SPcox (E) */ + tccp->cblkh = cio_read(1) + 2; /* SPcox (F) */ + tccp->cblksty = cio_read(1); /* SPcox (G) */ + tccp->qmfbid = cio_read(1); /* SPcox (H) */ + if (tccp->csty & J2K_CP_CSTY_PRT) { + for (i = 0; i < tccp->numresolutions; i++) { + int tmp = cio_read(1); /* SPcox (I_i) */ + tccp->prcw[i] = tmp & 0xf; + tccp->prch[i] = tmp >> 4; } + } } void j2k_write_cod() { - j2k_tcp_t *tcp; - int lenp, len; + j2k_tcp_t *tcp; + int lenp, len; - cio_write(J2K_MS_COD, 2); /* COD */ + cio_write(J2K_MS_COD, 2); /* COD */ - lenp = cio_tell(); - cio_skip(2); + lenp = cio_tell(); + cio_skip(2); - tcp = &j2k_cp->tcps[j2k_curtileno]; - cio_write(tcp->csty, 1); /* Scod */ - cio_write(tcp->prg, 1); /* SGcod (A) */ - cio_write(tcp->numlayers, 2); /* SGcod (B) */ - cio_write(tcp->mct, 1); /* SGcod (C) */ + tcp = &j2k_cp->tcps[j2k_curtileno]; + cio_write(tcp->csty, 1); /* Scod */ + cio_write(tcp->prg, 1); /* SGcod (A) */ + cio_write(tcp->numlayers, 2); /* SGcod (B) */ + cio_write(tcp->mct, 1); /* SGcod (C) */ - j2k_write_cox(0); - len = cio_tell() - lenp; - cio_seek(lenp); - cio_write(len, 2); /* Lcod */ - cio_seek(lenp + len); + j2k_write_cox(0); + len = cio_tell() - lenp; + cio_seek(lenp); + cio_write(len, 2); /* Lcod */ + cio_seek(lenp + len); } void j2k_read_cod() { - int len, i, pos; - j2k_tcp_t *tcp; - - 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) */ + int len, i, pos; + j2k_tcp_t *tcp; - pos = cio_tell(); - for (i = 0; i < j2k_img->numcomps; i++) { - tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT; - cio_seek(pos); - j2k_read_cox(i); - } + 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) */ + + pos = cio_tell(); + for (i = 0; i < j2k_img->numcomps; i++) { + tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT; + cio_seek(pos); + j2k_read_cox(i); + } } void j2k_write_coc(int compno) { - j2k_tcp_t *tcp; - int lenp, len; - - cio_write(J2K_MS_COC, 2); /* COC */ - lenp = cio_tell(); - cio_skip(2); - tcp = &j2k_cp->tcps[j2k_curtileno]; - cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Ccoc */ - cio_write(tcp->tccps[compno].csty, 1); /* Scoc */ - j2k_write_cox(compno); - len = cio_tell() - lenp; - cio_seek(lenp); - cio_write(len, 2); /* Lcoc */ - cio_seek(lenp + len); + j2k_tcp_t *tcp; + int lenp, len; + + cio_write(J2K_MS_COC, 2); /* COC */ + lenp = cio_tell(); + cio_skip(2); + tcp = &j2k_cp->tcps[j2k_curtileno]; + cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Ccoc */ + cio_write(tcp->tccps[compno].csty, 1); /* Scoc */ + j2k_write_cox(compno); + len = cio_tell() - lenp; + cio_seek(lenp); + cio_write(len, 2); /* Lcoc */ + cio_seek(lenp + len); } void j2k_read_coc() { - int len, compno; - j2k_tcp_t *tcp; - - 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 */ - tcp->tccps[compno].csty = cio_read(1); /* Scoc */ - j2k_read_cox(compno); + int len, compno; + j2k_tcp_t *tcp; + + 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 */ + tcp->tccps[compno].csty = cio_read(1); /* Scoc */ + j2k_read_cox(compno); } void j2k_write_qcx(int compno) { - j2k_tcp_t *tcp; - j2k_tccp_t *tccp; - int bandno, numbands; - int expn, mant; + j2k_tcp_t *tcp; + j2k_tccp_t *tccp; + int bandno, numbands; + int expn, mant; - tcp = &j2k_cp->tcps[j2k_curtileno]; - tccp = &tcp->tccps[compno]; + tcp = &j2k_cp->tcps[j2k_curtileno]; + tccp = &tcp->tccps[compno]; - cio_write(tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */ - numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2; + cio_write(tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */ + numbands = + tccp->qntsty == + J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2; - for (bandno = 0; bandno < numbands; bandno++) { - expn = tccp->stepsizes[bandno].expn; - mant = tccp->stepsizes[bandno].mant; + for (bandno = 0; bandno < numbands; bandno++) { + expn = tccp->stepsizes[bandno].expn; + mant = tccp->stepsizes[bandno].mant; - if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { - cio_write(expn << 3, 1); /* SPqcx_i */ - } else { - cio_write((expn << 11) + mant, 2); /* SPqcx_i */ - } + if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { + cio_write(expn << 3, 1); /* SPqcx_i */ + } else { + cio_write((expn << 11) + mant, 2); /* SPqcx_i */ } + } } void j2k_read_qcx(int compno, int len) { - int tmp; - 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 */ - tccp->qntsty = tmp & 0x1f; - tccp->numgbits = tmp >> 5; - numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT ? len - 1 : (len - 1) / 2); - for (bandno = 0; bandno < numbands; bandno++) { - int expn, mant; - if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { /* WHY STEPSIZES WHEN NOQNT ? */ - expn = cio_read(1) >> 3; /* SPqcx_i */ - mant = 0; - } else { - tmp = cio_read(2); /* SPqcx_i */ - expn = tmp >> 11; - mant = tmp & 0x7ff; - } - tccp->stepsizes[bandno].expn = expn; - tccp->stepsizes[bandno].mant = mant; + int tmp; + 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 */ + tccp->qntsty = tmp & 0x1f; + tccp->numgbits = tmp >> 5; + numbands = + tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : (tccp->qntsty == + J2K_CCP_QNTSTY_NOQNT ? + len - 1 : (len - + 1) / 2); + for (bandno = 0; bandno < numbands; bandno++) { + int expn, mant; + if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { /* WHY STEPSIZES WHEN NOQNT ? */ + expn = cio_read(1) >> 3; /* SPqcx_i */ + mant = 0; + } else { + tmp = cio_read(2); /* SPqcx_i */ + expn = tmp >> 11; + mant = tmp & 0x7ff; } + tccp->stepsizes[bandno].expn = expn; + tccp->stepsizes[bandno].mant = mant; + } } void j2k_write_qcd() { - int lenp, len; - - cio_write(J2K_MS_QCD, 2); /* QCD */ - lenp = cio_tell(); - cio_skip(2); - j2k_write_qcx(0); - len = cio_tell() - lenp; - cio_seek(lenp); - cio_write(len, 2); /* Lqcd */ - cio_seek(lenp + len); + int lenp, len; + + cio_write(J2K_MS_QCD, 2); /* QCD */ + lenp = cio_tell(); + cio_skip(2); + j2k_write_qcx(0); + len = cio_tell() - lenp; + cio_seek(lenp); + cio_write(len, 2); /* Lqcd */ + cio_seek(lenp + len); } void j2k_read_qcd() { - int len, i, pos; + int len, i, pos; - len = cio_read(2); /* Lqcd */ - pos = cio_tell(); - for (i = 0; i < j2k_img->numcomps; i++) { - cio_seek(pos); - j2k_read_qcx(i, len - 2); - } + len = cio_read(2); /* Lqcd */ + pos = cio_tell(); + for (i = 0; i < j2k_img->numcomps; i++) { + cio_seek(pos); + j2k_read_qcx(i, len - 2); + } } void j2k_write_qcc(int compno) { - int lenp, len; - - cio_write(J2K_MS_QCC, 2); /* QCC */ - lenp = cio_tell(); - cio_skip(2); - cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Cqcc */ - j2k_write_qcx(compno); - len = cio_tell() - lenp; - cio_seek(lenp); - cio_write(len, 2); /* Lqcc */ - cio_seek(lenp + len); + int lenp, len; + + cio_write(J2K_MS_QCC, 2); /* QCC */ + lenp = cio_tell(); + cio_skip(2); + cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Cqcc */ + j2k_write_qcx(compno); + len = cio_tell() - lenp; + cio_seek(lenp); + cio_write(len, 2); /* Lqcc */ + cio_seek(lenp + len); } void j2k_read_qcc() { - int len, compno; + int len, compno; - 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)); + 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)); } -void j2k_write_poc() { - int len, numpchgs, i; - j2k_tcp_t *tcp; - j2k_tccp_t *tccp; - - tcp = &j2k_cp->tcps[j2k_curtileno]; - tccp = &tcp->tccps[0]; - numpchgs = tcp->numpocs; - cio_write(J2K_MS_POC, 2); /* POC */ - len = 2 + (5 + 2*(j2k_img->numcomps <= 256 ? 1 : 2))*numpchgs; - cio_write(len, 2); /* Lpoc */ - for (i = 0; i < numpchgs; i++) - { - // MODIF - j2k_poc_t *poc; - poc = &tcp->pocs[i]; - cio_write(poc->resno0, 1); /* RSpoc_i */ - cio_write(poc->compno0, (j2k_img->numcomps <= 256 ? 1 : 2)); /* CSpoc_i */ - cio_write(poc->layno1, 2); /* LYEpoc_i */ - poc->layno1 = int_min(poc->layno1, tcp->numlayers); - cio_write(poc->resno1, 1); /* REpoc_i */ - poc->resno1 = int_min(poc->resno1, tccp->numresolutions); - cio_write(poc->compno1, (j2k_img->numcomps <= 256 ? 1 : 2)); /* CEpoc_i */ - poc->compno1 = int_min(poc->compno1, j2k_img->numcomps); - cio_write(poc->prg, 1); /* Ppoc_i */ +void j2k_write_poc() +{ + int len, numpchgs, i; + j2k_tcp_t *tcp; + j2k_tccp_t *tccp; + + tcp = &j2k_cp->tcps[j2k_curtileno]; + tccp = &tcp->tccps[0]; + numpchgs = tcp->numpocs; + cio_write(J2K_MS_POC, 2); /* POC */ + len = 2 + (5 + 2 * (j2k_img->numcomps <= 256 ? 1 : 2)) * numpchgs; + cio_write(len, 2); /* Lpoc */ + for (i = 0; i < numpchgs; i++) { + // MODIF + j2k_poc_t *poc; + poc = &tcp->pocs[i]; + cio_write(poc->resno0, 1); /* RSpoc_i */ + cio_write(poc->compno0, (j2k_img->numcomps <= 256 ? 1 : 2)); /* CSpoc_i */ + cio_write(poc->layno1, 2); /* LYEpoc_i */ + poc->layno1 = int_min(poc->layno1, tcp->numlayers); + cio_write(poc->resno1, 1); /* REpoc_i */ + poc->resno1 = int_min(poc->resno1, tccp->numresolutions); + cio_write(poc->compno1, (j2k_img->numcomps <= 256 ? 1 : 2)); /* CEpoc_i */ + poc->compno1 = int_min(poc->compno1, j2k_img->numcomps); + cio_write(poc->prg, 1); /* Ppoc_i */ } } -void j2k_read_poc() { +void j2k_read_poc() +{ int len, numpchgs, i, old_poc; j2k_tcp_t *tcp; j2k_tccp_t *tccp; - tcp = j2k_state==J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; + tcp = + j2k_state == + J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; - old_poc = tcp->POC ? tcp->numpocs+1 : 0; + old_poc = tcp->POC ? tcp->numpocs + 1 : 0; tcp->POC = 1; tccp = &tcp->tccps[0]; - len = cio_read(2); /* Lpoc */ - numpchgs = (len-2)/(5+2*(j2k_img->numcomps <= 256 ? 1 : 2)); + len = cio_read(2); /* Lpoc */ + numpchgs = (len - 2) / (5 + 2 * (j2k_img->numcomps <= 256 ? 1 : 2)); - for (i = old_poc; i < numpchgs+old_poc; i++) - { + for (i = old_poc; i < numpchgs + old_poc; i++) { j2k_poc_t *poc; poc = &tcp->pocs[i]; - poc->resno0 = cio_read(1); /* RSpoc_i */ - poc->compno0 = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* CSpoc_i */ - poc->layno1 = int_min(cio_read(2), tcp->numlayers); /* LYEpoc_i */ - 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 */ - } - - tcp->numpocs = numpchgs+old_poc-1; + poc->resno0 = cio_read(1); /* RSpoc_i */ + poc->compno0 = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* CSpoc_i */ + poc->layno1 = int_min(cio_read(2), tcp->numlayers); /* LYEpoc_i */ + 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 */ + } + + tcp->numpocs = numpchgs + old_poc - 1; } void j2k_read_crg() { - int len, i, Xcrg_i, Ycrg_i; - - len = cio_read(2); /* Lcrg */ - for (i=0;inumcomps;i++) - { - Xcrg_i = cio_read(2); /* Xcrg_i */ - Ycrg_i = cio_read(2); /* Ycrg_i */ - } + int len, i, Xcrg_i, Ycrg_i; + + len = cio_read(2); /* Lcrg */ + for (i = 0; i < j2k_img->numcomps; i++) { + Xcrg_i = cio_read(2); /* Xcrg_i */ + Ycrg_i = cio_read(2); /* Ycrg_i */ + } } void j2k_read_tlm() { - int len, Ztlm, Stlm, ST, SP, tile_tlm, i; - long int Ttlm_i, Ptlm_i; - - len = cio_read(2); /* Ltlm */ - Ztlm = cio_read(1); /* Ztlm */ - Stlm = cio_read(1); /* Stlm */ - ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02); - SP = (Stlm >> 6) & 0x01; - tile_tlm = (len-4)/((SP+1)*2+ST); - for (i=0;i> 4) & 0x01) + ((Stlm >> 4) & 0x02); + SP = (Stlm >> 6) & 0x01; + tile_tlm = (len - 4) / ((SP + 1) * 2 + ST); + for (i = 0; i < tile_tlm; i++) { + Ttlm_i = cio_read(ST); /* Ttlm_i */ + Ptlm_i = cio_read(SP ? 4 : 2); /* Ptlm_i */ + } } void j2k_read_plm() { - int len, i, Zplm, Nplm, add, packet_len = 0; - - len = cio_read(2); /* Lplm */ - Zplm = cio_read(1); /* Zplm */ - len-=3; - while (len>0) - { - Nplm = cio_read(4); /* Nplm */ - len-=4; - for (i=Nplm ; i>0 ; i--) - { - add=cio_read(1); - len--; - packet_len=(packet_len<<7) + add; /* Iplm_ij */ - if ((add & 0x80)==0) - { - /* New packet */ - packet_len=0; - } - if (len<=0) break; - } - } + int len, i, Zplm, Nplm, add, packet_len = 0; + + len = cio_read(2); /* Lplm */ + Zplm = cio_read(1); /* Zplm */ + len -= 3; + while (len > 0) { + Nplm = cio_read(4); /* Nplm */ + len -= 4; + for (i = Nplm; i > 0; i--) { + add = cio_read(1); + len--; + packet_len = (packet_len << 7) + add; /* Iplm_ij */ + if ((add & 0x80) == 0) { + /* New packet */ + packet_len = 0; + } + if (len <= 0) + break; + } + } } void j2k_read_plt() { - int len, i, Zplt, packet_len=0, add; - - len = cio_read(2); /* Lplt */ - Zplt=cio_read(1); /* Zplt */ - for (i=len-3;i>0;i--) - { - add=cio_read(1); - packet_len=(packet_len<<7) + add; /* Iplt_i */ - if ((add & 0x80)==0) - { - /* New packet */ - packet_len=0; - } - } + int len, i, Zplt, packet_len = 0, add; + + len = cio_read(2); /* Lplt */ + Zplt = cio_read(1); /* Zplt */ + for (i = len - 3; i > 0; i--) { + add = cio_read(1); + packet_len = (packet_len << 7) + add; /* Iplt_i */ + if ((add & 0x80) == 0) { + /* New packet */ + packet_len = 0; + } + } } void j2k_read_ppm() { - int len, Z_ppm, i, j; - int N_ppm; - - len = cio_read(2); - j2k_cp->ppm=1; - - Z_ppm = cio_read(1); /* Z_ppm */ - len-=3; - while (len > 0) - { - if (j2k_cp->ppm_previous==0) - { - N_ppm = cio_read(4); /* N_ppm */ - len-=4; - } else - { + int len, Z_ppm, i, j; + int N_ppm; + + len = cio_read(2); + j2k_cp->ppm = 1; + + Z_ppm = cio_read(1); /* Z_ppm */ + len -= 3; + while (len > 0) { + if (j2k_cp->ppm_previous == 0) { + N_ppm = cio_read(4); /* N_ppm */ + len -= 4; + } else { N_ppm = j2k_cp->ppm_previous; - } - - j=j2k_cp->ppm_store; - if (Z_ppm==0) /* First PPM marker */ - j2k_cp->ppm_data=(unsigned char*)calloc(N_ppm,sizeof(unsigned char)); - else /* NON-first PPM marker */ - j2k_cp->ppm_data=(unsigned char*)realloc(j2k_cp->ppm_data, (N_ppm+j2k_cp->ppm_store)*sizeof(unsigned char)); - - for (i=N_ppm ; i>0 ; i--) /* Read packet header */ - { - j2k_cp->ppm_data[j]=cio_read(1); - j++; - len--; - if (len==0) break; /* Case of non-finished packet header in present marker but finished in next one */ } - - j2k_cp->ppm_previous=i-1; - j2k_cp->ppm_store=j; + + j = j2k_cp->ppm_store; + if (Z_ppm == 0) /* First PPM marker */ + j2k_cp->ppm_data = + (unsigned char *) calloc(N_ppm, sizeof(unsigned char)); + else /* NON-first PPM marker */ + j2k_cp->ppm_data = + (unsigned char *) realloc(j2k_cp->ppm_data, + (N_ppm + + j2k_cp->ppm_store) * + sizeof(unsigned char)); + + for (i = N_ppm; i > 0; i--) { /* Read packet header */ + j2k_cp->ppm_data[j] = cio_read(1); + j++; + len--; + if (len == 0) + break; /* Case of non-finished packet header in present marker but finished in next one */ + } + + j2k_cp->ppm_previous = i - 1; + j2k_cp->ppm_store = j; } } void j2k_read_ppt() { - int len, Z_ppt, i, j=0; - j2k_tcp_t *tcp; - - len = cio_read(2); - Z_ppt = cio_read(1); - tcp=&j2k_cp->tcps[j2k_curtileno]; - tcp->ppt=1; - if (Z_ppt==0) /* First PPT marker */ - { - tcp->ppt_data=(unsigned char*)calloc(len-3,sizeof(unsigned char)); - tcp->ppt_store=0; - } - else /* NON-first PPT marker */ - tcp->ppt_data=(unsigned char*)realloc(tcp->ppt_data, (len-3+tcp->ppt_store)*sizeof(unsigned char)); - - j=tcp->ppt_store; - for (i=len-3 ; i>0 ; i--) - { - tcp->ppt_data[j]=cio_read(1); - j++; - } - tcp->ppt_store=j; + int len, Z_ppt, i, j = 0; + j2k_tcp_t *tcp; + + len = cio_read(2); + Z_ppt = cio_read(1); + tcp = &j2k_cp->tcps[j2k_curtileno]; + tcp->ppt = 1; + if (Z_ppt == 0) { /* First PPT marker */ + tcp->ppt_data = + (unsigned char *) calloc(len - 3, sizeof(unsigned char)); + tcp->ppt_store = 0; + } else /* NON-first PPT marker */ + tcp->ppt_data = + (unsigned char *) realloc(tcp->ppt_data, + (len - 3 + + tcp->ppt_store) * + sizeof(unsigned char)); + + j = tcp->ppt_store; + for (i = len - 3; i > 0; i--) { + tcp->ppt_data[j] = cio_read(1); + j++; + } + tcp->ppt_store = j; } void j2k_write_sot() { - int lenp, len; - - j2k_sot_start = cio_tell(); - cio_write(J2K_MS_SOT, 2); /* SOT */ - lenp = cio_tell(); - cio_skip(2); /* Lsot (further) */ - cio_write(j2k_curtileno, 2); /* Isot */ - cio_skip(4); /* Psot (further in j2k_write_sod) */ - cio_write(0, 1); /* TPsot */ - cio_write(1, 1); /* TNsot */ - len = cio_tell() - lenp; - cio_seek(lenp); - cio_write(len, 2); /* Lsot */ - cio_seek(lenp + len); + int lenp, len; + + j2k_sot_start = cio_tell(); + cio_write(J2K_MS_SOT, 2); /* SOT */ + lenp = cio_tell(); + cio_skip(2); /* Lsot (further) */ + cio_write(j2k_curtileno, 2); /* Isot */ + cio_skip(4); /* Psot (further in j2k_write_sod) */ + cio_write(0, 1); /* TPsot */ + cio_write(1, 1); /* TNsot */ + len = cio_tell() - lenp; + cio_seek(lenp); + cio_write(len, 2); /* Lsot */ + cio_seek(lenp + len); } void j2k_read_sot() { - int len, tileno, totlen, partno, numparts, i; - j2k_tcp_t *tcp; - j2k_tccp_t *tmp; - char status = 0; - - len = cio_read(2); - tileno = cio_read(2); - - if (j2k_cp->tileno_size == 0) - { + int len, tileno, totlen, partno, numparts, i; + j2k_tcp_t *tcp; + j2k_tccp_t *tmp; + char status = 0; + + 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++; - } - 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; - - partno = cio_read(1); - numparts = cio_read(1); + } + } - j2k_curtileno = tileno; - j2k_eot = cio_getbp() - 12 + totlen; - j2k_state = J2K_STATE_TPH; - tcp = &j2k_cp->tcps[j2k_curtileno]; - - if (tcp->first == 1) - { - tmp = tcp->tccps; - *tcp = j2k_default_tcp; - - /* Initialization PPT */ - tcp->ppt=0; - tcp->ppt_data=NULL; - - tcp->tccps = tmp; - for (i = 0; i < j2k_img->numcomps; i++) { - tcp->tccps[i] = j2k_default_tcp.tccps[i]; - } - j2k_cp->tcps[j2k_curtileno].first=0; - } + totlen = cio_read(4); + if (!totlen) + totlen = cio_numbytesleft() + 8; + + partno = cio_read(1); + numparts = cio_read(1); + + j2k_curtileno = tileno; + j2k_eot = cio_getbp() - 12 + totlen; + j2k_state = J2K_STATE_TPH; + tcp = &j2k_cp->tcps[j2k_curtileno]; + + if (tcp->first == 1) { + tmp = tcp->tccps; + *tcp = j2k_default_tcp; + + /* Initialization PPT */ + tcp->ppt = 0; + tcp->ppt_data = NULL; + + tcp->tccps = tmp; + for (i = 0; i < j2k_img->numcomps; i++) { + tcp->tccps[i] = j2k_default_tcp.tccps[i]; + } + j2k_cp->tcps[j2k_curtileno].first = 0; + } } void j2k_write_sod() { - int l, layno; - int totlen; - j2k_tcp_t *tcp; - static int j2k_sod_start; - - cio_write(J2K_MS_SOD, 2); - if (j2k_curtileno == 0) { - j2k_sod_start = cio_tell() + pos_correction; - } + int l, layno; + int totlen; + j2k_tcp_t *tcp; + static int j2k_sod_start; - /* 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) * 100,sizeof(info_packet)); - } - /* << INDEX */ + cio_write(J2K_MS_SOD, 2); + if (j2k_curtileno == 0) { + j2k_sod_start = cio_tell() + pos_correction; + } - 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)); - } + /* 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) * 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)); + } - info_IM.num = 0; - if (j2k_cp->image_type) - l = tcd_encode_tile_pxm(j2k_curtileno, cio_getbp(), cio_numbytesleft() - 2, &info_IM); - else - l = tcd_encode_tile_pgx(j2k_curtileno, cio_getbp(), cio_numbytesleft() - 2, &info_IM); - - /* Writing Psot in SOT marker */ - totlen = cio_tell() + l - j2k_sot_start; - cio_seek(j2k_sot_start + 6); - cio_write(totlen, 4); - cio_seek(j2k_sot_start + totlen); + info_IM.num = 0; + if (j2k_cp->image_type) + l = tcd_encode_tile_pxm(j2k_curtileno, cio_getbp(), + cio_numbytesleft() - 2, &info_IM); + else + l = tcd_encode_tile_pgx(j2k_curtileno, cio_getbp(), + cio_numbytesleft() - 2, &info_IM); + + /* Writing Psot in SOT marker */ + totlen = cio_tell() + l - j2k_sot_start; + cio_seek(j2k_sot_start + 6); + cio_write(totlen, 4); + cio_seek(j2k_sot_start + totlen); } void j2k_read_sod() { - int len, truncate = 0, i; - unsigned char *data; - - 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; itcps[tileno]; + j2k_tcp_t *tcp = &j2k_cp->tcps[tileno]; - 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 */ - cio_write(0, 1); /* Srgn */ - cio_write(tcp->tccps[compno].roishift, 1); /* SPrgn */ + 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 */ + cio_write(0, 1); /* Srgn */ + cio_write(tcp->tccps[compno].roishift, 1); /* SPrgn */ } void j2k_read_rgn() { - int len, compno, roisty; - j2k_tcp_t *tcp; - - 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 */ - roisty = cio_read(1); /* Srgn */ - tcp->tccps[compno].roishift = cio_read(1); /* SPrgn */ + int len, compno, roisty; + j2k_tcp_t *tcp; + + 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 */ + roisty = cio_read(1); /* Srgn */ + tcp->tccps[compno].roishift = cio_read(1); /* SPrgn */ } void j2k_write_eoc() { - /* fprintf(stderr, "%.8x: EOC\n", cio_tell() + pos_correction); */ - cio_write(J2K_MS_EOC, 2); + /* fprintf(stderr, "%.8x: EOC\n", cio_tell() + pos_correction); */ + cio_write(J2K_MS_EOC, 2); } void j2k_read_eoc() { - int i, tileno; + int i, tileno; - tcd_init(j2k_img, j2k_cp); + tcd_init(j2k_img, j2k_cp); - 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; - longjmp(j2k_error, 1); + 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; + longjmp(j2k_error, 1); } void j2k_read_unk() { - fprintf(stderr, "warning: unknown marker\n"); + fprintf(stderr, "warning: unknown marker\n"); } -LIBJ2K_API int j2k_encode(j2k_image_t * img, j2k_cp_t * cp, char *outfile, int len, char *index) +LIBJ2K_API int j2k_encode(j2k_image_t * img, j2k_cp_t * cp, char *outfile, + int len, char *index) { - int tileno, compno, layno, resno, precno, pack_nb; - char *dest; - FILE *INDEX; - FILE *f; + int tileno, compno, layno, resno, precno, pack_nb; + char *dest; + FILE *INDEX; + FILE *f; - if (setjmp(j2k_error)) { - return 0; - } + if (setjmp(j2k_error)) { + return 0; + } - f = fopen(outfile, "wb"); + f = fopen(outfile, "wb"); - if (!f) { - fprintf(stderr, "failed to open %s for writing\n", outfile); - return 1; - } + if (!f) { + fprintf(stderr, "failed to open %s for writing\n", outfile); + return 1; + } + + dest = (char *) malloc(len); + cio_init(dest, len); + + j2k_img = img; + j2k_cp = cp; + /* j2k_dump_cp(j2k_img, j2k_cp); */ + + /* INDEX >> */ + info_IM.index_on = j2k_img->index_on; + if (info_IM.index_on) { + info_IM.tile = + (info_tile *) malloc(j2k_cp->tw * j2k_cp->th * + sizeof(info_tile)); + info_IM.Im_w = j2k_img->x1 - j2k_img->x0; + info_IM.Im_h = j2k_img->y1 - j2k_img->y0; + info_IM.Prog = (&j2k_cp->tcps[0])->prg; + /* info_IM.Tile_x=j2k_cp->tw; old version parser */ + /* info_IM.Tile_y=j2k_cp->th; old version parser */ + info_IM.Tile_x = j2k_cp->tdx; /* new version parser */ + info_IM.Tile_y = j2k_cp->tdy; /* new version parser */ + info_IM.Comp = j2k_img->numcomps; + info_IM.Layer = (&j2k_cp->tcps[0])->numlayers; + info_IM.Decomposition = + (&j2k_cp->tcps[0])->tccps->numresolutions - 1; + info_IM.D_max = 0; /* ADD Marcela */ + } + /* << INDEX */ + + j2k_write_soc(); + j2k_write_siz(); + j2k_write_cod(); + j2k_write_qcd(); + for (compno = 0; compno < j2k_img->numcomps; compno++) { + j2k_tcp_t *tcp = &j2k_cp->tcps[0]; + if (tcp->tccps[compno].roishift) + j2k_write_rgn(compno, 0); + } + if (j2k_cp->comment != NULL) + j2k_write_com(); + + /* Writing the main header */ + pos_correction = cio_tell(); + fwrite(dest, 1, cio_tell(), f); + + /* INDEX >> */ + if (info_IM.index_on) { + info_IM.Main_head_end = cio_tell() - 1; + } + /* << INDEX */ + + for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { + fprintf(stderr, "\nTile number %d / %d \n", tileno + 1, + cp->tw * cp->th); + + /* new dest for each tile */ + free(dest); dest = (char *) malloc(len); cio_init(dest, len); + j2k_curtileno = tileno; + /* initialisation before tile encoding */ - j2k_img = img; - j2k_cp = cp; - /* j2k_dump_cp(j2k_img, j2k_cp); */ + if (tileno == 0) { + tcd_malloc_encode(j2k_img, j2k_cp, j2k_curtileno); + } else { + tcd_init_encode(j2k_img, j2k_cp, j2k_curtileno); + } /* INDEX >> */ - info_IM.index_on = j2k_img->index_on; if (info_IM.index_on) { - info_IM.tile = (info_tile *) malloc(j2k_cp->tw * j2k_cp->th * sizeof(info_tile)); - info_IM.Im_w = j2k_img->x1 - j2k_img->x0; - info_IM.Im_h = j2k_img->y1 - j2k_img->y0; - info_IM.Prog = (&j2k_cp->tcps[0])->prg; - /* info_IM.Tile_x=j2k_cp->tw; old version parser */ - /* info_IM.Tile_y=j2k_cp->th; old version parser */ - info_IM.Tile_x = j2k_cp->tdx; /* new version parser */ - info_IM.Tile_y = j2k_cp->tdy; /* new version parser */ - info_IM.Comp = j2k_img->numcomps; - info_IM.Layer = (&j2k_cp->tcps[0])->numlayers; - info_IM.Decomposition = (&j2k_cp->tcps[0])->tccps->numresolutions - 1; - info_IM.D_max = 0; /* ADD Marcela */ + info_IM.tile[j2k_curtileno].num_tile = j2k_curtileno; + info_IM.tile[j2k_curtileno].start_pos = + cio_tell() + pos_correction; } /* << INDEX */ + j2k_write_sot(); - j2k_write_soc(); - j2k_write_siz(); - j2k_write_cod(); - j2k_write_qcd(); - for (compno = 0; compno < j2k_img->numcomps; compno++) { - j2k_tcp_t *tcp = &j2k_cp->tcps[0]; - if (tcp->tccps[compno].roishift) - j2k_write_rgn(compno, 0); + for (compno = 1; compno < img->numcomps; compno++) { + j2k_write_coc(compno); + j2k_write_qcc(compno); } - if (j2k_cp->comment != NULL) - j2k_write_com(); - /* Writing the main header */ - pos_correction = cio_tell(); - fwrite(dest, 1, cio_tell(), f); + if (cp->tcps[tileno].numpocs) + j2k_write_poc(); + j2k_write_sod(); /* INDEX >> */ if (info_IM.index_on) { - info_IM.Main_head_end = cio_tell() - 1; + info_IM.tile[j2k_curtileno].end_pos = + cio_tell() + pos_correction - 1; } /* << INDEX */ - - for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { - fprintf(stderr, "\nTile number %d / %d \n", tileno + 1, - cp->tw * cp->th); - - /* new dest for each tile */ - free(dest); - dest = (char *) malloc(len); - cio_init(dest, len); - j2k_curtileno = tileno; - /* initialisation before tile encoding */ - - if (tileno == 0) { - tcd_malloc_encode(j2k_img, j2k_cp, j2k_curtileno); - } else { - tcd_init_encode(j2k_img, j2k_cp, j2k_curtileno); - } + /* + if (tile->PPT) BAD PPT !!! + { + FILE *PPT_file; + + int i; + PPT_file=fopen("PPT","rb"); + fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256); + for (i=0;ilen_ppt;i++) + { + unsigned char elmt; + fread(&elmt, 1, 1, PPT_file); + fwrite(&elmt,1,1,f); + } + fclose(PPT_file); + unlink("PPT"); + } + */ - /* INDEX >> */ - if (info_IM.index_on) { - info_IM.tile[j2k_curtileno].num_tile = j2k_curtileno; - info_IM.tile[j2k_curtileno].start_pos = cio_tell() + pos_correction; - } - /* << INDEX */ - j2k_write_sot(); - - for (compno = 1; compno < img->numcomps; compno++) { - j2k_write_coc(compno); - j2k_write_qcc(compno); - } + fwrite(dest, 1, cio_tell(), f); + pos_correction = cio_tell() + pos_correction; + } - if (cp->tcps[tileno].numpocs) - j2k_write_poc(); - j2k_write_sod(); + free(dest); + dest = (char *) malloc(len); + cio_init(dest, len); - /* INDEX >> */ - if (info_IM.index_on) { - info_IM.tile[j2k_curtileno].end_pos = - cio_tell() + pos_correction - 1; - } - /* << INDEX */ - - /* - if (tile->PPT) BAD PPT !!! - { - FILE *PPT_file; - - int i; - PPT_file=fopen("PPT","rb"); - fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256); - for (i=0;ilen_ppt;i++) - { - unsigned char elmt; - fread(&elmt, 1, 1, PPT_file); - fwrite(&elmt,1,1,f); - } - fclose(PPT_file); - unlink("PPT"); - } - */ - - fwrite(dest, 1, cio_tell(), f); - pos_correction = cio_tell() + pos_correction; - } + j2k_write_eoc(); - free(dest); - dest = (char *) malloc(len); - cio_init(dest, len); + fwrite(dest, 1, 2, f); + free(dest); + /* closing file *.j2k */ + fclose(f); - j2k_write_eoc(); + /* Creation of the index file */ + if (info_IM.index_on) { + info_IM.codestream_size = cio_tell() + pos_correction; /* Correction 14/4/03 suite rmq de Patrick */ + INDEX = fopen(index, "w"); - fwrite(dest, 1, 2, f); - free(dest); - /* closing file *.j2k */ - fclose(f); - - /* Creation of the index file */ - if (info_IM.index_on) { - info_IM.codestream_size = cio_tell() + pos_correction; /* Correction 14/4/03 suite rmq de Patrick */ - INDEX = fopen(index, "w"); + if (!INDEX) { + fprintf(stderr, "failed to open %s for writing\n", index); + return 1; + } - if (!INDEX) { - fprintf(stderr, "failed to open %s for writing\n", index); - return 1; + fprintf(INDEX, "%d %d\n", info_IM.Im_w, info_IM.Im_h); + fprintf(INDEX, "%d\n", info_IM.Prog); + fprintf(INDEX, "%d %d\n", info_IM.Tile_x, info_IM.Tile_y); + fprintf(INDEX, "%d\n", info_IM.Comp); + fprintf(INDEX, "%d\n", info_IM.Layer); + fprintf(INDEX, "%d\n", info_IM.Decomposition); + 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, + info_IM.tile[tileno].end_header, + info_IM.tile[tileno].end_pos); + /*for (layno=0;laynotw * j2k_cp->th; tileno++) { + pack_nb = 0; + if (info_IM.Prog == 0) { /* LRCP */ + for (layno = 0; layno < info_IM.Layer; layno++) { + for (resno = 0; resno < info_IM.Decomposition + 1; + resno++) { + for (compno = 0; compno < info_IM.Comp; compno++) { + for (precno = 0; + precno < + info_IM.tile[tileno].pw * + info_IM.tile[tileno].ph; precno++) { + 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++; + } + } + } } - - fprintf(INDEX, "%d %d\n", info_IM.Im_w, info_IM.Im_h); - fprintf(INDEX, "%d\n", info_IM.Prog); - fprintf(INDEX, "%d %d\n", info_IM.Tile_x, info_IM.Tile_y); - fprintf(INDEX, "%d\n", info_IM.Comp); - fprintf(INDEX, "%d\n", info_IM.Layer); - fprintf(INDEX, "%d\n", info_IM.Decomposition); - 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, - info_IM.tile[tileno].end_header, - info_IM.tile[tileno].end_pos); - /*for (layno=0;laynotw * j2k_cp->th; tileno++) { - pack_nb = 0; - if (info_IM.Prog == 0) { /* LRCP */ - for (layno = 0; layno < info_IM.Layer; layno++) { - for (resno = 0; resno < info_IM.Decomposition + 1; resno++) { - for (compno = 0; compno < info_IM.Comp; compno++) { - for (precno = 0; - precno < - info_IM.tile[tileno].pw * info_IM.tile[tileno].ph; - precno++) { - 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++; - } - } - } - } - } else if (info_IM.Prog == 1) { /* RLCP */ - for (resno = 0; resno < info_IM.Decomposition + 1; resno++) { - for (layno = 0; layno < info_IM.Layer; layno++) { - for (compno = 0; compno < info_IM.Comp; compno++) { - for (precno = 0; precno < info_IM.pw * info_IM.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/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++; - } - } - } - } - } else if (info_IM.Prog == 2) { /* RPCL */ - for (resno = 0; resno < info_IM.Decomposition + 1; resno++) { - for (precno = 0; precno < info_IM.pw * info_IM.ph; precno++) { - for (compno = 0; compno < info_IM.Comp; compno++) { - for (layno = 0; layno < info_IM.Layer; layno++) { - /* 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/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++; - } - } - } - } - } else if (info_IM.Prog == 3) { /* PCRL */ - for (precno = 0; precno < info_IM.pw * info_IM.ph; precno++) { - for (compno = 0; compno < info_IM.Comp; compno++) { - for (resno = 0; resno < info_IM.Decomposition + 1; resno++) { - for (layno = 0; layno < info_IM.Layer; layno++) { - /* 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/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++; - } - } - } - } - } else { /* CPRL */ - - for (compno = 0; compno < info_IM.Comp; compno++) { - for (precno = 0; precno < info_IM.pw * info_IM.ph; precno++) { - for (resno = 0; resno < info_IM.Decomposition + 1; resno++) { - for (layno = 0; layno < info_IM.Layer; layno++) { - /*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/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++; - } - } - } - } + } + } + } else if (info_IM.Prog == 2) { /* RPCL */ + for (resno = 0; resno < info_IM.Decomposition + 1; resno++) { + for (precno = 0; precno < info_IM.pw * info_IM.ph; + precno++) { + for (compno = 0; compno < info_IM.Comp; compno++) { + for (layno = 0; layno < info_IM.Layer; layno++) { + /* 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/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++; + } + } + } + } + } else if (info_IM.Prog == 3) { /* PCRL */ + for (precno = 0; precno < info_IM.pw * info_IM.ph; + precno++) { + for (compno = 0; compno < info_IM.Comp; compno++) { + for (resno = 0; resno < info_IM.Decomposition + 1; + resno++) { + for (layno = 0; layno < info_IM.Layer; layno++) { + /* 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/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++; + } } + } } - fclose(INDEX); + } else { /* CPRL */ + + for (compno = 0; compno < info_IM.Comp; compno++) { + for (precno = 0; precno < info_IM.pw * info_IM.ph; + precno++) { + for (resno = 0; resno < info_IM.Decomposition + 1; + resno++) { + for (layno = 0; layno < info_IM.Layer; layno++) { + /*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/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++; + } + } + } + } + } } + fclose(INDEX); + } - j2k_clean(); + j2k_clean(); - return cio_tell(); + return cio_tell(); } typedef struct { - int id; - int states; - void (*handler) (); + int id; + int states; + void (*handler) (); } j2k_dec_mstabent_t; j2k_dec_mstabent_t j2k_dec_mstab[] = { - {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc}, - {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot}, - {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod}, - {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc}, - {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz}, - {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod}, - {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc}, - {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn}, - {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd}, - {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc}, - {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc}, - {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm}, - {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm}, - {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt}, - {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm}, - {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt}, - {J2K_MS_SOP, 0, 0}, - {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg}, - {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com}, - {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk} + {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc}, + {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot}, + {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod}, + {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc}, + {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz}, + {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod}, + {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc}, + {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn}, + {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd}, + {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc}, + {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc}, + {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm}, + {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm}, + {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt}, + {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm}, + {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt}, + {J2K_MS_SOP, 0, 0}, + {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg}, + {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com}, + {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk} }; j2k_dec_mstabent_t *j2k_dec_mstab_lookup(int id) { - j2k_dec_mstabent_t *e; - for (e = j2k_dec_mstab; e->id != 0; e++) { - if (e->id == id) { - break; - } + j2k_dec_mstabent_t *e; + for (e = j2k_dec_mstab; e->id != 0; e++) { + if (e->id == id) { + break; } - return e; + } + return e; } -LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t ** img, j2k_cp_t ** cp, j2k_option_t option) +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)) { - if (j2k_state != J2K_STATE_MT) { - fprintf(stderr, "WARNING: incomplete bitstream\n"); - return 0; - } - return cio_numbytes(); + if (setjmp(j2k_error)) { + if (j2k_state != J2K_STATE_MT) { + fprintf(stderr, "WARNING: incomplete bitstream\n"); + return 0; } + return cio_numbytes(); + } - j2k_img = (j2k_image_t *) malloc(sizeof(j2k_image_t)); - 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); - - for (;;) { - j2k_dec_mstabent_t *e; - int id = cio_read(2); - if (id >> 8 != 0xff) { - fprintf(stderr, "%.8x: expected a marker instead of %x\n", cio_tell() - 2, id); - return 0; - } - e = j2k_dec_mstab_lookup(id); - if (!(j2k_state & e->states)) { - fprintf(stderr, "%.8x: unexpected marker %x\n", cio_tell() - 2, id); - return 0; - } - if (e->handler) { - (*e->handler) (); - } - if (j2k_state == J2K_STATE_NEOC) - break; /* RAJOUTE */ + j2k_img = (j2k_image_t *) malloc(sizeof(j2k_image_t)); + 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); + + for (;;) { + j2k_dec_mstabent_t *e; + int id = cio_read(2); + if (id >> 8 != 0xff) { + fprintf(stderr, "%.8x: expected a marker instead of %x\n", + cio_tell() - 2, id); + return 0; + } + e = j2k_dec_mstab_lookup(id); + if (!(j2k_state & e->states)) { + fprintf(stderr, "%.8x: unexpected marker %x\n", cio_tell() - 2, + id); + return 0; + } + if (e->handler) { + (*e->handler) (); } if (j2k_state == J2K_STATE_NEOC) - j2k_read_eoc(); /* RAJOUTE */ - - return 0; + break; /* RAJOUTE */ + } + if (j2k_state == J2K_STATE_NEOC) + j2k_read_eoc(); /* RAJOUTE */ + + return 0; } /* * Read a JPT-stream and decode file * */ -int j2k_decode_jpt_stream(unsigned char *src, int len, j2k_image_t ** img, j2k_cp_t ** cp) -{ - jpt_msg_header_struct_t header; - int position; - - if (setjmp(j2k_error)) { - if (j2k_state != J2K_STATE_MT) { - fprintf(stderr, "WARNING: incomplete bitstream\n"); - return 0; - } - return cio_numbytes(); - } - - j2k_img = (j2k_image_t *) malloc(sizeof(j2k_image_t)); - 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); - - /* Initialize the header */ - jpt_init_Msg_Header(&header); - /* Read the first header of the message */ - jpt_read_Msg_Header(&header); - - position = cio_tell(); - if (header.Class_Id != 6) /* 6 : Main header data-bin message */ - { - fprintf(stderr,"[JPT-stream] : Expecting Main header first [class_Id %d] !\n",header.Class_Id); - return 0; +int j2k_decode_jpt_stream(unsigned char *src, int len, j2k_image_t ** img, + j2k_cp_t ** cp) +{ + jpt_msg_header_struct_t header; + int position; + + if (setjmp(j2k_error)) { + if (j2k_state != J2K_STATE_MT) { + fprintf(stderr, "WARNING: incomplete bitstream\n"); + return 0; + } + return cio_numbytes(); } - for (;;) { - j2k_dec_mstabent_t *e; - int id; + j2k_img = (j2k_image_t *) malloc(sizeof(j2k_image_t)); + j2k_cp = (j2k_cp_t *) malloc(sizeof(j2k_cp_t)); + *img = j2k_img; + *cp = j2k_cp; - if(!cio_numbytesleft()) - { - j2k_read_eoc(); + j2k_state = J2K_STATE_MHSOC; + cio_init(src, len); + + /* Initialize the header */ + jpt_init_Msg_Header(&header); + /* Read the first header of the message */ + jpt_read_Msg_Header(&header); + + position = cio_tell(); + if (header.Class_Id != 6) { /* 6 : Main header data-bin message */ + fprintf(stderr, + "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", + header.Class_Id); return 0; - } - /* data-bin read -> need to read a new header */ - if ((cio_tell() - position) == header.Msg_length) - { - jpt_read_Msg_Header(&header); - position = cio_tell(); - if (header.Class_Id != 4) /* 4 : Tile data-bin message */ - { - fprintf(stderr,"[JPT-stream] : Expecting Tile info !\n"); + } + + for (;;) { + j2k_dec_mstabent_t *e; + int id; + + if (!cio_numbytesleft()) { + j2k_read_eoc(); return 0; - } - } + } + /* data-bin read -> need to read a new header */ + if ((cio_tell() - position) == header.Msg_length) { + jpt_read_Msg_Header(&header); + position = cio_tell(); + if (header.Class_Id != 4) { /* 4 : Tile data-bin message */ + fprintf(stderr, "[JPT-stream] : Expecting Tile info !\n"); + return 0; + } + } - id = cio_read(2); - if (id >> 8 != 0xff) { - fprintf(stderr, "%.8x: expected a marker instead of %x\n", cio_tell() - 2, id); - return 0; - } - e = j2k_dec_mstab_lookup(id); - if (!(j2k_state & e->states)) { - fprintf(stderr, "%.8x: unexpected marker %x\n", cio_tell() - 2, id); - return 0; - } - if (e->handler) { - (*e->handler) (); + id = cio_read(2); + if (id >> 8 != 0xff) { + fprintf(stderr, "%.8x: expected a marker instead of %x\n", + cio_tell() - 2, id); + return 0; + } + e = j2k_dec_mstab_lookup(id); + if (!(j2k_state & e->states)) { + fprintf(stderr, "%.8x: unexpected marker %x\n", cio_tell() - 2, + id); + return 0; + } + if (e->handler) { + (*e->handler) (); + } + if (j2k_state == J2K_STATE_NEOC) + break; /* RAJOUTE */ } if (j2k_state == J2K_STATE_NEOC) - break; /* RAJOUTE */ - } - if (j2k_state == J2K_STATE_NEOC) - j2k_read_eoc(); /* RAJOUTE */ - - return 0; + j2k_read_eoc(); /* RAJOUTE */ + + return 0; } #ifdef WIN32 #include -BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) +BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, + LPVOID lpReserved) { - switch (ul_reason_for_call) { - case DLL_PROCESS_ATTACH: - case DLL_THREAD_ATTACH: - case DLL_THREAD_DETACH: - case DLL_PROCESS_DETACH: - break; - } - return TRUE; + switch (ul_reason_for_call) { + case DLL_PROCESS_ATTACH: + case DLL_THREAD_ATTACH: + case DLL_THREAD_DETACH: + case DLL_PROCESS_DETACH: + break; + } + return TRUE; } #endif diff --git a/libopenjpeg/j2k.h b/libopenjpeg/j2k.h index f1f5f49e..796b45c9 100644 --- a/libopenjpeg/j2k.h +++ b/libopenjpeg/j2k.h @@ -41,8 +41,8 @@ #ifndef __J2K_H #define __J2K_H -#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_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,127 +59,127 @@ #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 */ + 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 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 */ + 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 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 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 */ + 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 { - int resno0, compno0; - int layno1, resno1, compno1; - int prg; - int tile; - char progorder[4]; + int resno0, compno0; + int layno1, resno1, compno1; + int prg; + int tile; + char progorder[4]; } j2k_poc_t; typedef struct { - 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 */ + 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 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; /* 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) */ - 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 */ + 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; /* 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) */ + 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; /* ADD for Marcela */ -} info_packet; /* Index struct */ + int start_pos, end_pos; /* start and end position */ + double disto; /* ADD for Marcela */ +} info_packet; /* Index struct */ typedef 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 */ + 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 */ - 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 index_on; + double D_max; /* ADD for Marcela */ + int num; /* numero of packet */ + int index_write; /* writing the packet inthe index with t2_encode_packets */ + int Im_w, Im_h; /* Image width and Height */ + int Prog; /* progression order */ + int Tile_x, Tile_y; /* Number of Tile in X and Y */ + int tw, th; + int Comp; /* Component numbers */ + int Layer; /* number of layer */ + int Decomposition; /* number of decomposition */ + int pw, ph; /* nombre precinct in X and Y */ + int pdx, pdy; /* size of precinct in X and Y */ + int Main_head_end; /* Main header position */ + int codestream_size; /* codestream's size */ + info_tile *tile; /* information concerning tiles inside image */ +} info_image; /* index struct */ /* * Encode an image into a JPEG-2000 codestream @@ -189,7 +189,8 @@ 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); */ /* @@ -199,7 +200,8 @@ LIBJ2K_API int j2k_encode(j2k_image_t * i, j2k_cp_t * cp, char *outfile, int len * 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, j2k_option_t option); +LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t ** img, + j2k_cp_t ** cp, j2k_option_t option); /* @@ -210,6 +212,7 @@ LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t ** img, j2k_c * 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); +int j2k_decode_jpt_stream(unsigned char *src, int len, j2k_image_t ** img, + j2k_cp_t ** cp); #endif diff --git a/libopenjpeg/jpt.c b/libopenjpeg/jpt.c index 04cc8264..09a429ad 100644 --- a/libopenjpeg/jpt.c +++ b/libopenjpeg/jpt.c @@ -37,37 +37,36 @@ * Read the information contains in VBAS [JPP/JPT stream message header] * Store information (7 bits) in value * - */ + */ unsigned int jpt_read_VBAS_info(unsigned int value) { - unsigned char elmt; - - elmt = cio_read(1); - while ((elmt >> 7) == 1) - { - value = (value << 7); - value |= (elmt & 0x7f); - elmt = cio_read(1); + unsigned char elmt; + + elmt = cio_read(1); + while ((elmt >> 7) == 1) { + value = (value << 7); + value |= (elmt & 0x7f); + elmt = cio_read(1); } - value = (value << 7); - value |= (elmt & 0x7f); - - return value; + value = (value << 7); + value |= (elmt & 0x7f); + + return value; } /* * Initialize the value of the message header structure * - */ -void jpt_init_Msg_Header(jpt_msg_header_struct_t *header) + */ +void jpt_init_Msg_Header(jpt_msg_header_struct_t * header) { - header->Id = 0; /* In-class Identifier */ - header->last_byte = 0; /* Last byte information */ - header->Class_Id = 0; /* Class Identifier */ - header->CSn_Id = 0; /* CSn : index identifier */ - header->Msg_offset = 0; /* Message offset */ - header->Msg_length = 0; /* Message length */ - header->Layer_nb = 0; /* Auxiliary for JPP case */ + header->Id = 0; /* In-class Identifier */ + header->last_byte = 0; /* Last byte information */ + header->Class_Id = 0; /* Class Identifier */ + header->CSn_Id = 0; /* CSn : index identifier */ + header->Msg_offset = 0; /* Message offset */ + header->Msg_length = 0; /* Message length */ + header->Layer_nb = 0; /* Auxiliary for JPP case */ } /* @@ -76,93 +75,90 @@ void jpt_init_Msg_Header(jpt_msg_header_struct_t *header) * Only parameters always present in message header * */ -void jpt_reinit_Msg_Header(jpt_msg_header_struct_t *header) +void jpt_reinit_Msg_Header(jpt_msg_header_struct_t * header) { - header->Id = 0; /* In-class Identifier */ - header->last_byte = 0; /* Last byte information */ - header->Msg_offset = 0; /* Message offset */ - header->Msg_length = 0; /* Message length */ + header->Id = 0; /* In-class Identifier */ + header->last_byte = 0; /* Last byte information */ + header->Msg_offset = 0; /* Message offset */ + header->Msg_length = 0; /* Message length */ } /* * Read the message header for a JPP/JPT - stream * */ -void jpt_read_Msg_Header(jpt_msg_header_struct_t *header) +void jpt_read_Msg_Header(jpt_msg_header_struct_t * header) { - unsigned char elmt, Class = 0, CSn = 0; - jpt_reinit_Msg_Header(header); - - /* ------------- */ - /* VBAS : Bin-ID */ - /* ------------- */ - elmt = cio_read(1); - - /* See for Class and CSn */ - switch((elmt>>5) & 0x03) - { + unsigned char elmt, Class = 0, CSn = 0; + jpt_reinit_Msg_Header(header); + + /* ------------- */ + /* VBAS : Bin-ID */ + /* ------------- */ + elmt = cio_read(1); + + /* See for Class and CSn */ + switch ((elmt >> 5) & 0x03) { case 0: - fprintf(stderr,"Forbidden value encounter in message header !!\n"); - break; + fprintf(stderr, + "Forbidden value encounter in message header !!\n"); + break; case 1: - Class = 0; - CSn = 0; - break; + Class = 0; + CSn = 0; + break; case 2: - Class = 1; - CSn = 0; - break; + Class = 1; + CSn = 0; + break; case 3: - Class = 1; - CSn = 1; - break; - default : - break; + Class = 1; + CSn = 1; + break; + default: + break; } - - /* see information on bits 'c' [p 10 : A.2.1 general, ISO/IEC FCD 15444-9] */ - if (((elmt>>3) & 0x01) == 1) - header->last_byte = 1; - - /* In-class identifier */ - header->Id |= (elmt & 0x0f); - if ((elmt>>7)==1) - header->Id = jpt_read_VBAS_info(header->Id); - - /* ------------ */ - /* VBAS : Class */ - /* ------------ */ - if (Class==1) - { - header->Class_Id = 0; - header->Class_Id = jpt_read_VBAS_info(header->Class_Id); + + /* see information on bits 'c' [p 10 : A.2.1 general, ISO/IEC FCD 15444-9] */ + if (((elmt >> 3) & 0x01) == 1) + header->last_byte = 1; + + /* In-class identifier */ + header->Id |= (elmt & 0x0f); + if ((elmt >> 7) == 1) + header->Id = jpt_read_VBAS_info(header->Id); + + /* ------------ */ + /* VBAS : Class */ + /* ------------ */ + if (Class == 1) { + header->Class_Id = 0; + header->Class_Id = jpt_read_VBAS_info(header->Class_Id); } - /* ---------- */ - /* VBAS : CSn */ - /* ---------- */ - if (CSn==1) - { - header->CSn_Id = 0; - header->CSn_Id = jpt_read_VBAS_info(header->CSn_Id); + /* ---------- */ + /* VBAS : CSn */ + /* ---------- */ + if (CSn == 1) { + header->CSn_Id = 0; + header->CSn_Id = jpt_read_VBAS_info(header->CSn_Id); } - /* ----------------- */ - /* VBAS : Msg_offset */ - /* ----------------- */ - header->Msg_offset = jpt_read_VBAS_info(header->Msg_offset); - - /* ----------------- */ - /* VBAS : Msg_length */ - /* ----------------- */ - header->Msg_length = jpt_read_VBAS_info(header->Msg_length); - - /* ---------- */ - /* VBAS : Aux */ - /* ---------- */ - if (header->CSn_Id == 1) - { - header->Layer_nb = 0; - header->Layer_nb = jpt_read_VBAS_info(header->Layer_nb); + /* ----------------- */ + /* VBAS : Msg_offset */ + /* ----------------- */ + header->Msg_offset = jpt_read_VBAS_info(header->Msg_offset); + + /* ----------------- */ + /* VBAS : Msg_length */ + /* ----------------- */ + header->Msg_length = jpt_read_VBAS_info(header->Msg_length); + + /* ---------- */ + /* VBAS : Aux */ + /* ---------- */ + if (header->CSn_Id == 1) { + header->Layer_nb = 0; + header->Layer_nb = jpt_read_VBAS_info(header->Layer_nb); } } diff --git a/libopenjpeg/jpt.h b/libopenjpeg/jpt.h index e2932dc6..0e96fb1c 100644 --- a/libopenjpeg/jpt.h +++ b/libopenjpeg/jpt.h @@ -30,13 +30,13 @@ * */ typedef struct { - unsigned int Id; /* In-class Identifier */ - unsigned int last_byte; /* Last byte information */ - unsigned int Class_Id; /* Class Identifier */ - unsigned int CSn_Id; /* CSn : index identifier */ - unsigned int Msg_offset; /* Message offset */ - unsigned int Msg_length; /* Message length */ - unsigned int Layer_nb; /* Auxiliary for JPP case */ + unsigned int Id; /* In-class Identifier */ + unsigned int last_byte; /* Last byte information */ + unsigned int Class_Id; /* Class Identifier */ + unsigned int CSn_Id; /* CSn : index identifier */ + unsigned int Msg_offset; /* Message offset */ + unsigned int Msg_length; /* Message length */ + unsigned int Layer_nb; /* Auxiliary for JPP case */ } jpt_msg_header_struct_t; /* @@ -44,8 +44,8 @@ typedef struct { * * header : Message header structure * - */ -void jpt_init_Msg_Header(jpt_msg_header_struct_t *header); + */ +void jpt_init_Msg_Header(jpt_msg_header_struct_t * header); /* * Read the message header for a JPP/JPT - stream @@ -53,4 +53,4 @@ void jpt_init_Msg_Header(jpt_msg_header_struct_t *header); * header : Message header structure * */ -void jpt_read_Msg_Header(jpt_msg_header_struct_t *header); +void jpt_read_Msg_Header(jpt_msg_header_struct_t * header); diff --git a/libopenjpeg/mct.c b/libopenjpeg/mct.c index ef371e92..54f5231f 100644 --- a/libopenjpeg/mct.c +++ b/libopenjpeg/mct.c @@ -42,19 +42,19 @@ double mct_norms_real[3] = { 1.732, 1.805, 1.573 }; /* */ void mct_encode(int *c0, int *c1, int *c2, int n) { - int i; - for (i = 0; i < n; i++) { - int r, g, b, y, u, v; - r = c0[i]; - g = c1[i]; - b = c2[i]; - y = (r + (g << 1) + b) >> 2; - u = b - g; - v = r - g; - c0[i] = y; - c1[i] = u; - c2[i] = v; - } + int i; + for (i = 0; i < n; i++) { + int r, g, b, y, u, v; + r = c0[i]; + g = c1[i]; + b = c2[i]; + y = (r + (g << 1) + b) >> 2; + u = b - g; + v = r - g; + c0[i] = y; + c1[i] = u; + c2[i] = v; + } } /* */ @@ -62,19 +62,19 @@ void mct_encode(int *c0, int *c1, int *c2, int n) /* */ void mct_decode(int *c0, int *c1, int *c2, int n) { - int i; - for (i = 0; i < n; i++) { - int y, u, v, r, g, b; - y = c0[i]; - u = c1[i]; - v = c2[i]; - g = y - ((u + v) >> 2); - r = v + g; - b = u + g; - c0[i] = r; - c1[i] = g; - c2[i] = b; - } + int i; + for (i = 0; i < n; i++) { + int y, u, v, r, g, b; + y = c0[i]; + u = c1[i]; + v = c2[i]; + g = y - ((u + v) >> 2); + r = v + g; + b = u + g; + c0[i] = r; + c1[i] = g; + c2[i] = b; + } } /* */ @@ -82,7 +82,7 @@ void mct_decode(int *c0, int *c1, int *c2, int n) /* */ double mct_getnorm(int compno) { - return mct_norms[compno]; + return mct_norms[compno]; } /* */ @@ -90,19 +90,19 @@ double mct_getnorm(int compno) /* */ void mct_encode_real(int *c0, int *c1, int *c2, int n) { - int i; - for (i = 0; i < n; i++) { - int r, g, b, y, u, v; - r = c0[i]; - g = c1[i]; - b = c2[i]; - y = fix_mul(r, 2449) + fix_mul(g, 4809) + fix_mul(b, 934); - u = -fix_mul(r, 1382) - fix_mul(g, 2714) + fix_mul(b, 4096); - v = fix_mul(r, 4096) - fix_mul(g, 3430) - fix_mul(b, 666); - c0[i] = y; - c1[i] = u; - c2[i] = v; - } + int i; + for (i = 0; i < n; i++) { + int r, g, b, y, u, v; + r = c0[i]; + g = c1[i]; + b = c2[i]; + y = fix_mul(r, 2449) + fix_mul(g, 4809) + fix_mul(b, 934); + u = -fix_mul(r, 1382) - fix_mul(g, 2714) + fix_mul(b, 4096); + v = fix_mul(r, 4096) - fix_mul(g, 3430) - fix_mul(b, 666); + c0[i] = y; + c1[i] = u; + c2[i] = v; + } } /* */ @@ -110,19 +110,19 @@ void mct_encode_real(int *c0, int *c1, int *c2, int n) /* */ void mct_decode_real(int *c0, int *c1, int *c2, int n) { - int i; - for (i = 0; i < n; i++) { - int y, u, v, r, g, b; - y = c0[i]; - u = c1[i]; - v = c2[i]; - r = y + fix_mul(v, 11485); - g = y - fix_mul(u, 2819) - fix_mul(v, 5850); - b = y + fix_mul(u, 14516); - c0[i] = r; - c1[i] = g; - c2[i] = b; - } + int i; + for (i = 0; i < n; i++) { + int y, u, v, r, g, b; + y = c0[i]; + u = c1[i]; + v = c2[i]; + r = y + fix_mul(v, 11485); + g = y - fix_mul(u, 2819) - fix_mul(v, 5850); + b = y + fix_mul(u, 14516); + c0[i] = r; + c1[i] = g; + c2[i] = b; + } } /* */ @@ -130,5 +130,5 @@ void mct_decode_real(int *c0, int *c1, int *c2, int n) /* */ double mct_getnorm_real(int compno) { - return mct_norms_real[compno]; + return mct_norms_real[compno]; } diff --git a/libopenjpeg/mqc.c b/libopenjpeg/mqc.c index 4e6c319c..57cd8151 100644 --- a/libopenjpeg/mqc.c +++ b/libopenjpeg/mqc.c @@ -33,110 +33,110 @@ /* This struct defines the state of a context. */ /* */ 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; /* */ /* This array defines all the possible states for a context. */ /* */ mqc_state_t mqc_states[47 * 2] = { - {0x5601, 0, &mqc_states[2], &mqc_states[3]}, - {0x5601, 1, &mqc_states[3], &mqc_states[2]}, - {0x3401, 0, &mqc_states[4], &mqc_states[12]}, - {0x3401, 1, &mqc_states[5], &mqc_states[13]}, - {0x1801, 0, &mqc_states[6], &mqc_states[18]}, - {0x1801, 1, &mqc_states[7], &mqc_states[19]}, - {0x0ac1, 0, &mqc_states[8], &mqc_states[24]}, - {0x0ac1, 1, &mqc_states[9], &mqc_states[25]}, - {0x0521, 0, &mqc_states[10], &mqc_states[58]}, - {0x0521, 1, &mqc_states[11], &mqc_states[59]}, - {0x0221, 0, &mqc_states[76], &mqc_states[66]}, - {0x0221, 1, &mqc_states[77], &mqc_states[67]}, - {0x5601, 0, &mqc_states[14], &mqc_states[13]}, - {0x5601, 1, &mqc_states[15], &mqc_states[12]}, - {0x5401, 0, &mqc_states[16], &mqc_states[28]}, - {0x5401, 1, &mqc_states[17], &mqc_states[29]}, - {0x4801, 0, &mqc_states[18], &mqc_states[28]}, - {0x4801, 1, &mqc_states[19], &mqc_states[29]}, - {0x3801, 0, &mqc_states[20], &mqc_states[28]}, - {0x3801, 1, &mqc_states[21], &mqc_states[29]}, - {0x3001, 0, &mqc_states[22], &mqc_states[34]}, - {0x3001, 1, &mqc_states[23], &mqc_states[35]}, - {0x2401, 0, &mqc_states[24], &mqc_states[36]}, - {0x2401, 1, &mqc_states[25], &mqc_states[37]}, - {0x1c01, 0, &mqc_states[26], &mqc_states[40]}, - {0x1c01, 1, &mqc_states[27], &mqc_states[41]}, - {0x1601, 0, &mqc_states[58], &mqc_states[42]}, - {0x1601, 1, &mqc_states[59], &mqc_states[43]}, - {0x5601, 0, &mqc_states[30], &mqc_states[29]}, - {0x5601, 1, &mqc_states[31], &mqc_states[28]}, - {0x5401, 0, &mqc_states[32], &mqc_states[28]}, - {0x5401, 1, &mqc_states[33], &mqc_states[29]}, - {0x5101, 0, &mqc_states[34], &mqc_states[30]}, - {0x5101, 1, &mqc_states[35], &mqc_states[31]}, - {0x4801, 0, &mqc_states[36], &mqc_states[32]}, - {0x4801, 1, &mqc_states[37], &mqc_states[33]}, - {0x3801, 0, &mqc_states[38], &mqc_states[34]}, - {0x3801, 1, &mqc_states[39], &mqc_states[35]}, - {0x3401, 0, &mqc_states[40], &mqc_states[36]}, - {0x3401, 1, &mqc_states[41], &mqc_states[37]}, - {0x3001, 0, &mqc_states[42], &mqc_states[38]}, - {0x3001, 1, &mqc_states[43], &mqc_states[39]}, - {0x2801, 0, &mqc_states[44], &mqc_states[38]}, - {0x2801, 1, &mqc_states[45], &mqc_states[39]}, - {0x2401, 0, &mqc_states[46], &mqc_states[40]}, - {0x2401, 1, &mqc_states[47], &mqc_states[41]}, - {0x2201, 0, &mqc_states[48], &mqc_states[42]}, - {0x2201, 1, &mqc_states[49], &mqc_states[43]}, - {0x1c01, 0, &mqc_states[50], &mqc_states[44]}, - {0x1c01, 1, &mqc_states[51], &mqc_states[45]}, - {0x1801, 0, &mqc_states[52], &mqc_states[46]}, - {0x1801, 1, &mqc_states[53], &mqc_states[47]}, - {0x1601, 0, &mqc_states[54], &mqc_states[48]}, - {0x1601, 1, &mqc_states[55], &mqc_states[49]}, - {0x1401, 0, &mqc_states[56], &mqc_states[50]}, - {0x1401, 1, &mqc_states[57], &mqc_states[51]}, - {0x1201, 0, &mqc_states[58], &mqc_states[52]}, - {0x1201, 1, &mqc_states[59], &mqc_states[53]}, - {0x1101, 0, &mqc_states[60], &mqc_states[54]}, - {0x1101, 1, &mqc_states[61], &mqc_states[55]}, - {0x0ac1, 0, &mqc_states[62], &mqc_states[56]}, - {0x0ac1, 1, &mqc_states[63], &mqc_states[57]}, - {0x09c1, 0, &mqc_states[64], &mqc_states[58]}, - {0x09c1, 1, &mqc_states[65], &mqc_states[59]}, - {0x08a1, 0, &mqc_states[66], &mqc_states[60]}, - {0x08a1, 1, &mqc_states[67], &mqc_states[61]}, - {0x0521, 0, &mqc_states[68], &mqc_states[62]}, - {0x0521, 1, &mqc_states[69], &mqc_states[63]}, - {0x0441, 0, &mqc_states[70], &mqc_states[64]}, - {0x0441, 1, &mqc_states[71], &mqc_states[65]}, - {0x02a1, 0, &mqc_states[72], &mqc_states[66]}, - {0x02a1, 1, &mqc_states[73], &mqc_states[67]}, - {0x0221, 0, &mqc_states[74], &mqc_states[68]}, - {0x0221, 1, &mqc_states[75], &mqc_states[69]}, - {0x0141, 0, &mqc_states[76], &mqc_states[70]}, - {0x0141, 1, &mqc_states[77], &mqc_states[71]}, - {0x0111, 0, &mqc_states[78], &mqc_states[72]}, - {0x0111, 1, &mqc_states[79], &mqc_states[73]}, - {0x0085, 0, &mqc_states[80], &mqc_states[74]}, - {0x0085, 1, &mqc_states[81], &mqc_states[75]}, - {0x0049, 0, &mqc_states[82], &mqc_states[76]}, - {0x0049, 1, &mqc_states[83], &mqc_states[77]}, - {0x0025, 0, &mqc_states[84], &mqc_states[78]}, - {0x0025, 1, &mqc_states[85], &mqc_states[79]}, - {0x0015, 0, &mqc_states[86], &mqc_states[80]}, - {0x0015, 1, &mqc_states[87], &mqc_states[81]}, - {0x0009, 0, &mqc_states[88], &mqc_states[82]}, - {0x0009, 1, &mqc_states[89], &mqc_states[83]}, - {0x0005, 0, &mqc_states[90], &mqc_states[84]}, - {0x0005, 1, &mqc_states[91], &mqc_states[85]}, - {0x0001, 0, &mqc_states[90], &mqc_states[86]}, - {0x0001, 1, &mqc_states[91], &mqc_states[87]}, - {0x5601, 0, &mqc_states[92], &mqc_states[92]}, - {0x5601, 1, &mqc_states[93], &mqc_states[93]}, + {0x5601, 0, &mqc_states[2], &mqc_states[3]}, + {0x5601, 1, &mqc_states[3], &mqc_states[2]}, + {0x3401, 0, &mqc_states[4], &mqc_states[12]}, + {0x3401, 1, &mqc_states[5], &mqc_states[13]}, + {0x1801, 0, &mqc_states[6], &mqc_states[18]}, + {0x1801, 1, &mqc_states[7], &mqc_states[19]}, + {0x0ac1, 0, &mqc_states[8], &mqc_states[24]}, + {0x0ac1, 1, &mqc_states[9], &mqc_states[25]}, + {0x0521, 0, &mqc_states[10], &mqc_states[58]}, + {0x0521, 1, &mqc_states[11], &mqc_states[59]}, + {0x0221, 0, &mqc_states[76], &mqc_states[66]}, + {0x0221, 1, &mqc_states[77], &mqc_states[67]}, + {0x5601, 0, &mqc_states[14], &mqc_states[13]}, + {0x5601, 1, &mqc_states[15], &mqc_states[12]}, + {0x5401, 0, &mqc_states[16], &mqc_states[28]}, + {0x5401, 1, &mqc_states[17], &mqc_states[29]}, + {0x4801, 0, &mqc_states[18], &mqc_states[28]}, + {0x4801, 1, &mqc_states[19], &mqc_states[29]}, + {0x3801, 0, &mqc_states[20], &mqc_states[28]}, + {0x3801, 1, &mqc_states[21], &mqc_states[29]}, + {0x3001, 0, &mqc_states[22], &mqc_states[34]}, + {0x3001, 1, &mqc_states[23], &mqc_states[35]}, + {0x2401, 0, &mqc_states[24], &mqc_states[36]}, + {0x2401, 1, &mqc_states[25], &mqc_states[37]}, + {0x1c01, 0, &mqc_states[26], &mqc_states[40]}, + {0x1c01, 1, &mqc_states[27], &mqc_states[41]}, + {0x1601, 0, &mqc_states[58], &mqc_states[42]}, + {0x1601, 1, &mqc_states[59], &mqc_states[43]}, + {0x5601, 0, &mqc_states[30], &mqc_states[29]}, + {0x5601, 1, &mqc_states[31], &mqc_states[28]}, + {0x5401, 0, &mqc_states[32], &mqc_states[28]}, + {0x5401, 1, &mqc_states[33], &mqc_states[29]}, + {0x5101, 0, &mqc_states[34], &mqc_states[30]}, + {0x5101, 1, &mqc_states[35], &mqc_states[31]}, + {0x4801, 0, &mqc_states[36], &mqc_states[32]}, + {0x4801, 1, &mqc_states[37], &mqc_states[33]}, + {0x3801, 0, &mqc_states[38], &mqc_states[34]}, + {0x3801, 1, &mqc_states[39], &mqc_states[35]}, + {0x3401, 0, &mqc_states[40], &mqc_states[36]}, + {0x3401, 1, &mqc_states[41], &mqc_states[37]}, + {0x3001, 0, &mqc_states[42], &mqc_states[38]}, + {0x3001, 1, &mqc_states[43], &mqc_states[39]}, + {0x2801, 0, &mqc_states[44], &mqc_states[38]}, + {0x2801, 1, &mqc_states[45], &mqc_states[39]}, + {0x2401, 0, &mqc_states[46], &mqc_states[40]}, + {0x2401, 1, &mqc_states[47], &mqc_states[41]}, + {0x2201, 0, &mqc_states[48], &mqc_states[42]}, + {0x2201, 1, &mqc_states[49], &mqc_states[43]}, + {0x1c01, 0, &mqc_states[50], &mqc_states[44]}, + {0x1c01, 1, &mqc_states[51], &mqc_states[45]}, + {0x1801, 0, &mqc_states[52], &mqc_states[46]}, + {0x1801, 1, &mqc_states[53], &mqc_states[47]}, + {0x1601, 0, &mqc_states[54], &mqc_states[48]}, + {0x1601, 1, &mqc_states[55], &mqc_states[49]}, + {0x1401, 0, &mqc_states[56], &mqc_states[50]}, + {0x1401, 1, &mqc_states[57], &mqc_states[51]}, + {0x1201, 0, &mqc_states[58], &mqc_states[52]}, + {0x1201, 1, &mqc_states[59], &mqc_states[53]}, + {0x1101, 0, &mqc_states[60], &mqc_states[54]}, + {0x1101, 1, &mqc_states[61], &mqc_states[55]}, + {0x0ac1, 0, &mqc_states[62], &mqc_states[56]}, + {0x0ac1, 1, &mqc_states[63], &mqc_states[57]}, + {0x09c1, 0, &mqc_states[64], &mqc_states[58]}, + {0x09c1, 1, &mqc_states[65], &mqc_states[59]}, + {0x08a1, 0, &mqc_states[66], &mqc_states[60]}, + {0x08a1, 1, &mqc_states[67], &mqc_states[61]}, + {0x0521, 0, &mqc_states[68], &mqc_states[62]}, + {0x0521, 1, &mqc_states[69], &mqc_states[63]}, + {0x0441, 0, &mqc_states[70], &mqc_states[64]}, + {0x0441, 1, &mqc_states[71], &mqc_states[65]}, + {0x02a1, 0, &mqc_states[72], &mqc_states[66]}, + {0x02a1, 1, &mqc_states[73], &mqc_states[67]}, + {0x0221, 0, &mqc_states[74], &mqc_states[68]}, + {0x0221, 1, &mqc_states[75], &mqc_states[69]}, + {0x0141, 0, &mqc_states[76], &mqc_states[70]}, + {0x0141, 1, &mqc_states[77], &mqc_states[71]}, + {0x0111, 0, &mqc_states[78], &mqc_states[72]}, + {0x0111, 1, &mqc_states[79], &mqc_states[73]}, + {0x0085, 0, &mqc_states[80], &mqc_states[74]}, + {0x0085, 1, &mqc_states[81], &mqc_states[75]}, + {0x0049, 0, &mqc_states[82], &mqc_states[76]}, + {0x0049, 1, &mqc_states[83], &mqc_states[77]}, + {0x0025, 0, &mqc_states[84], &mqc_states[78]}, + {0x0025, 1, &mqc_states[85], &mqc_states[79]}, + {0x0015, 0, &mqc_states[86], &mqc_states[80]}, + {0x0015, 1, &mqc_states[87], &mqc_states[81]}, + {0x0009, 0, &mqc_states[88], &mqc_states[82]}, + {0x0009, 1, &mqc_states[89], &mqc_states[83]}, + {0x0005, 0, &mqc_states[90], &mqc_states[84]}, + {0x0005, 1, &mqc_states[91], &mqc_states[85]}, + {0x0001, 0, &mqc_states[90], &mqc_states[86]}, + {0x0001, 1, &mqc_states[91], &mqc_states[87]}, + {0x5601, 0, &mqc_states[92], &mqc_states[92]}, + {0x5601, 1, &mqc_states[93], &mqc_states[93]}, }; #define MQC_NUMCTXS 32 @@ -155,7 +155,7 @@ mqc_state_t **mqc_curctx; /* */ int mqc_numbytes() { - return mqc_bp - mqc_start; + return mqc_bp - mqc_start; } /* */ @@ -164,33 +164,33 @@ int mqc_numbytes() /* */ void mqc_byteout() { - if (*mqc_bp == 0xff) { + if (*mqc_bp == 0xff) { + mqc_bp++; + *mqc_bp = mqc_c >> 20; + mqc_c &= 0xfffff; + mqc_ct = 7; + } else { + if ((mqc_c & 0x8000000) == 0) { /* ((mqc_c&0x8000000)==0) CHANGE */ + mqc_bp++; + *mqc_bp = mqc_c >> 19; + mqc_c &= 0x7ffff; + mqc_ct = 8; + } else { + (*mqc_bp)++; + if (*mqc_bp == 0xff) { + mqc_c &= 0x7ffffff; mqc_bp++; *mqc_bp = mqc_c >> 20; mqc_c &= 0xfffff; mqc_ct = 7; - } else { - if ((mqc_c & 0x8000000) == 0) { /* ((mqc_c&0x8000000)==0) CHANGE */ - mqc_bp++; - *mqc_bp = mqc_c >> 19; - mqc_c &= 0x7ffff; - mqc_ct = 8; - } else { - (*mqc_bp)++; - if (*mqc_bp == 0xff) { - mqc_c &= 0x7ffffff; - mqc_bp++; - *mqc_bp = mqc_c >> 20; - mqc_c &= 0xfffff; - mqc_ct = 7; - } else { - mqc_bp++; - *mqc_bp = mqc_c >> 19; - mqc_c &= 0x7ffff; - mqc_ct = 8; - } - } + } else { + mqc_bp++; + *mqc_bp = mqc_c >> 19; + mqc_c &= 0x7ffff; + mqc_ct = 8; + } } + } } /* */ @@ -198,14 +198,14 @@ void mqc_byteout() /* */ void mqc_renorme() { - do { - mqc_a <<= 1; - mqc_c <<= 1; - mqc_ct--; - if (mqc_ct == 0) { - mqc_byteout(); - } - } while ((mqc_a & 0x8000) == 0); + do { + mqc_a <<= 1; + mqc_c <<= 1; + mqc_ct--; + if (mqc_ct == 0) { + mqc_byteout(); + } + } while ((mqc_a & 0x8000) == 0); } /* */ @@ -213,18 +213,18 @@ void mqc_renorme() /* */ void mqc_codemps() { - mqc_a -= (*mqc_curctx)->qeval; - if ((mqc_a & 0x8000) == 0) { - if (mqc_a < (*mqc_curctx)->qeval) { - mqc_a = (*mqc_curctx)->qeval; - } else { - mqc_c += (*mqc_curctx)->qeval; - } - *mqc_curctx = (*mqc_curctx)->nmps; - mqc_renorme(); + mqc_a -= (*mqc_curctx)->qeval; + if ((mqc_a & 0x8000) == 0) { + if (mqc_a < (*mqc_curctx)->qeval) { + mqc_a = (*mqc_curctx)->qeval; } else { - mqc_c += (*mqc_curctx)->qeval; + mqc_c += (*mqc_curctx)->qeval; } + *mqc_curctx = (*mqc_curctx)->nmps; + mqc_renorme(); + } else { + mqc_c += (*mqc_curctx)->qeval; + } } /* */ @@ -232,14 +232,14 @@ void mqc_codemps() /* */ void mqc_codelps() { - mqc_a -= (*mqc_curctx)->qeval; - if (mqc_a < (*mqc_curctx)->qeval) { - mqc_c += (*mqc_curctx)->qeval; - } else { - mqc_a = (*mqc_curctx)->qeval; - } - *mqc_curctx = (*mqc_curctx)->nlps; - mqc_renorme(); + mqc_a -= (*mqc_curctx)->qeval; + if (mqc_a < (*mqc_curctx)->qeval) { + mqc_c += (*mqc_curctx)->qeval; + } else { + mqc_a = (*mqc_curctx)->qeval; + } + *mqc_curctx = (*mqc_curctx)->nlps; + mqc_renorme(); } /* */ @@ -248,15 +248,15 @@ void mqc_codelps() /* Output buffer. */ void mqc_init_enc(unsigned char *bp) { - mqc_setcurctx(0); - mqc_a = 0x8000; - mqc_c = 0; - mqc_bp = bp - 1; - mqc_ct = 12; - if (*mqc_bp == 0xff) { - mqc_ct = 13; - } - mqc_start = bp; + mqc_setcurctx(0); + mqc_a = 0x8000; + mqc_c = 0; + mqc_bp = bp - 1; + mqc_ct = 12; + if (*mqc_bp == 0xff) { + mqc_ct = 13; + } + mqc_start = bp; } /* */ @@ -265,7 +265,7 @@ void mqc_init_enc(unsigned char *bp) /* Context number. */ void mqc_setcurctx(int ctxno) { - mqc_curctx = &mqc_ctxs[ctxno]; + mqc_curctx = &mqc_ctxs[ctxno]; } /* */ @@ -274,11 +274,11 @@ void mqc_setcurctx(int ctxno) /* The symbol to be encoded (0 or 1). */ void mqc_encode(int d) { - if ((*mqc_curctx)->mps == d) { - mqc_codemps(); - } else { - mqc_codelps(); - } + if ((*mqc_curctx)->mps == d) { + mqc_codemps(); + } else { + mqc_codelps(); + } } /* */ @@ -286,11 +286,11 @@ void mqc_encode(int d) /* */ void mqc_setbits() { - unsigned int tempc = mqc_c + mqc_a; - mqc_c |= 0xffff; - if (mqc_c >= tempc) { - mqc_c -= 0x8000; - } + unsigned int tempc = mqc_c + mqc_a; + mqc_c |= 0xffff; + if (mqc_c >= tempc) { + mqc_c -= 0x8000; + } } /* */ @@ -298,15 +298,15 @@ void mqc_setbits() /* */ void mqc_flush() { - mqc_setbits(); - mqc_c <<= mqc_ct; - mqc_byteout(); - mqc_c <<= mqc_ct; - mqc_byteout(); - - if (*mqc_bp != 0xff) { - mqc_bp++; - } + mqc_setbits(); + mqc_c <<= mqc_ct; + mqc_byteout(); + mqc_c <<= mqc_ct; + mqc_byteout(); + + if (*mqc_bp != 0xff) { + mqc_bp++; + } } /* */ @@ -316,11 +316,11 @@ void mqc_flush() /* */ void mqc_bypass_init_enc() { - mqc_c = 0; - mqc_ct = 8; - /*if (*mqc_bp == 0xff) { - mqc_ct = 7; - }*/ + mqc_c = 0; + mqc_ct = 8; + /*if (*mqc_bp == 0xff) { + mqc_ct = 7; + } */ } /* */ @@ -330,18 +330,17 @@ void mqc_bypass_init_enc() /* */ 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; - } + 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; + } } /* */ @@ -350,23 +349,23 @@ void mqc_bypass_enc(int d) /* */ int mqc_bypass_flush_enc() { - unsigned char bit_padding; + unsigned char bit_padding; - bit_padding = 0; + bit_padding = 0; - if (mqc_ct != 0) { - while (mqc_ct > 0) { - mqc_ct--; - mqc_c += bit_padding << mqc_ct; - bit_padding = (bit_padding + 1) & 0x01; - } - mqc_bp++; - *mqc_bp = mqc_c; - mqc_ct = 8; - mqc_c = 0; + if (mqc_ct != 0) { + while (mqc_ct > 0) { + mqc_ct--; + mqc_c += bit_padding << mqc_ct; + bit_padding = (bit_padding + 1) & 0x01; } + mqc_bp++; + *mqc_bp = mqc_c; + mqc_ct = 8; + mqc_c = 0; + } - return 1; + return 1; } /* */ @@ -374,10 +373,10 @@ int mqc_bypass_flush_enc() /* */ void mqc_reset_enc() { - mqc_resetstates(); - mqc_setstate(18, 0, 46); - mqc_setstate(0, 0, 3); - mqc_setstate(1, 0, 4); + mqc_resetstates(); + mqc_setstate(18, 0, 46); + mqc_setstate(0, 0, 3); + mqc_setstate(1, 0, 4); } /* */ @@ -385,19 +384,19 @@ void mqc_reset_enc() /* */ int mqc_restart_enc() { - int correction = 1; + int correction = 1; - /* */ - int n = 27 - 15 - mqc_ct; - mqc_c <<= mqc_ct; - while (n > 0) { - mqc_byteout(); - n -= mqc_ct; - mqc_c <<= mqc_ct; - } + /* */ + int n = 27 - 15 - mqc_ct; + mqc_c <<= mqc_ct; + while (n > 0) { mqc_byteout(); + n -= mqc_ct; + mqc_c <<= mqc_ct; + } + mqc_byteout(); - return correction; + return correction; } /* */ @@ -405,15 +404,15 @@ int mqc_restart_enc() /* */ void mqc_restart_init_enc() { - /* */ - mqc_setcurctx(0); - mqc_a = 0x8000; - mqc_c = 0; - mqc_ct = 12; - mqc_bp--; - if (*mqc_bp == 0xff) { - mqc_ct = 13; - } + /* */ + mqc_setcurctx(0); + mqc_a = 0x8000; + mqc_c = 0; + mqc_ct = 12; + mqc_bp--; + if (*mqc_bp == 0xff) { + mqc_ct = 13; + } } @@ -422,18 +421,18 @@ void mqc_restart_init_enc() /* */ void mqc_erterm_enc() { - int k = 11 - mqc_ct + 1; + int k = 11 - mqc_ct + 1; - while (k > 0) { - mqc_c <<= mqc_ct; - mqc_ct = 0; - mqc_byteout(); - k -= mqc_ct; - } + while (k > 0) { + mqc_c <<= mqc_ct; + mqc_ct = 0; + mqc_byteout(); + k -= mqc_ct; + } - if (*mqc_bp != 0xff) { - mqc_byteout(); - } + if (*mqc_bp != 0xff) { + mqc_byteout(); + } } /* */ @@ -441,44 +440,44 @@ void mqc_erterm_enc() /* */ void mqc_segmark_enc() { - int i; - mqc_setcurctx(18); + int i; + mqc_setcurctx(18); - for (i = 1; i < 5; i++) { - mqc_encode(i % 2); - } + for (i = 1; i < 5; i++) { + mqc_encode(i % 2); + } } /* */ /* */ int mqc_mpsexchange() { - int d; - if (mqc_a < (*mqc_curctx)->qeval) { - d = 1 - (*mqc_curctx)->mps; - *mqc_curctx = (*mqc_curctx)->nlps; - } else { - d = (*mqc_curctx)->mps; - *mqc_curctx = (*mqc_curctx)->nmps; - } - return d; + int d; + if (mqc_a < (*mqc_curctx)->qeval) { + d = 1 - (*mqc_curctx)->mps; + *mqc_curctx = (*mqc_curctx)->nlps; + } else { + d = (*mqc_curctx)->mps; + *mqc_curctx = (*mqc_curctx)->nmps; + } + return d; } /* */ /* */ int mqc_lpsexchange() { - int d; - if (mqc_a < (*mqc_curctx)->qeval) { - mqc_a = (*mqc_curctx)->qeval; - d = (*mqc_curctx)->mps; - *mqc_curctx = (*mqc_curctx)->nmps; - } else { - mqc_a = (*mqc_curctx)->qeval; - d = 1 - (*mqc_curctx)->mps; - *mqc_curctx = (*mqc_curctx)->nlps; - } - return d; + int d; + if (mqc_a < (*mqc_curctx)->qeval) { + mqc_a = (*mqc_curctx)->qeval; + d = (*mqc_curctx)->mps; + *mqc_curctx = (*mqc_curctx)->nmps; + } else { + mqc_a = (*mqc_curctx)->qeval; + d = 1 - (*mqc_curctx)->mps; + *mqc_curctx = (*mqc_curctx)->nlps; + } + return d; } /* */ @@ -486,31 +485,31 @@ int mqc_lpsexchange() /* */ void mqc_bytein() { - if (mqc_bp != mqc_end) { - unsigned int c; - if (mqc_bp + 1 != mqc_end) { - c = *(mqc_bp + 1); - } else { - c = 0xff; - } - if (*mqc_bp == 0xff) { - if (c > 0x8f) { - mqc_c += 0xff00; - mqc_ct = 8; - } else { - mqc_bp++; - mqc_c += c << 9; - mqc_ct = 7; - } - } else { - mqc_bp++; - mqc_c += c << 8; - mqc_ct = 8; - } + if (mqc_bp != mqc_end) { + unsigned int c; + if (mqc_bp + 1 != mqc_end) { + c = *(mqc_bp + 1); } else { + c = 0xff; + } + if (*mqc_bp == 0xff) { + if (c > 0x8f) { mqc_c += 0xff00; mqc_ct = 8; + } else { + mqc_bp++; + mqc_c += c << 9; + mqc_ct = 7; + } + } else { + mqc_bp++; + mqc_c += c << 8; + mqc_ct = 8; } + } else { + mqc_c += 0xff00; + mqc_ct = 8; + } } /* */ @@ -518,14 +517,14 @@ void mqc_bytein() /* */ void mqc_renormd() { - do { - if (mqc_ct == 0) { - mqc_bytein(); - } - mqc_a <<= 1; - mqc_c <<= 1; - mqc_ct--; - } while (mqc_a < 0x8000); + do { + if (mqc_ct == 0) { + mqc_bytein(); + } + mqc_a <<= 1; + mqc_c <<= 1; + mqc_ct--; + } while (mqc_a < 0x8000); } /* */ @@ -533,15 +532,15 @@ void mqc_renormd() /* */ void mqc_init_dec(unsigned char *bp, int len) { - mqc_setcurctx(0); - mqc_start = bp; - mqc_end = bp + len; - mqc_bp = bp; - mqc_c = *mqc_bp << 16; - mqc_bytein(); - mqc_c <<= 7; - mqc_ct -= 7; - mqc_a = 0x8000; + mqc_setcurctx(0); + mqc_start = bp; + mqc_end = bp + len; + mqc_bp = bp; + mqc_c = *mqc_bp << 16; + mqc_bytein(); + mqc_c <<= 7; + mqc_ct -= 7; + mqc_a = 0x8000; } /* */ @@ -549,21 +548,21 @@ void mqc_init_dec(unsigned char *bp, int len) /* */ int mqc_decode() { - int d; - mqc_a -= (*mqc_curctx)->qeval; - if ((mqc_c >> 16) < (*mqc_curctx)->qeval) { - d = mqc_lpsexchange(); - mqc_renormd(); + int d; + mqc_a -= (*mqc_curctx)->qeval; + if ((mqc_c >> 16) < (*mqc_curctx)->qeval) { + d = mqc_lpsexchange(); + mqc_renormd(); + } else { + mqc_c -= (*mqc_curctx)->qeval << 16; + if ((mqc_a & 0x8000) == 0) { + d = mqc_mpsexchange(); + mqc_renormd(); } else { - mqc_c -= (*mqc_curctx)->qeval << 16; - if ((mqc_a & 0x8000) == 0) { - d = mqc_mpsexchange(); - mqc_renormd(); - } else { - d = (*mqc_curctx)->mps; - } + d = (*mqc_curctx)->mps; } - return d; + } + return d; } /* */ @@ -571,10 +570,10 @@ int mqc_decode() /* */ void mqc_resetstates() { - int i; - for (i = 0; i < MQC_NUMCTXS; i++) { - mqc_ctxs[i] = mqc_states; - } + int i; + for (i = 0; i < MQC_NUMCTXS; i++) { + mqc_ctxs[i] = mqc_states; + } } /* */ @@ -585,5 +584,5 @@ void mqc_resetstates() /* Index to the probability of symbols */ void mqc_setstate(int ctxno, int msb, int prob) { - mqc_ctxs[ctxno] = &mqc_states[msb + (prob << 1)]; + mqc_ctxs[ctxno] = &mqc_states[msb + (prob << 1)]; } diff --git a/libopenjpeg/pi.c b/libopenjpeg/pi.c index 42bfab29..acee203c 100644 --- a/libopenjpeg/pi.c +++ b/libopenjpeg/pi.c @@ -36,103 +36,110 @@ * */ pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno) { - int p, q, i; - int compno, resno, pino; - int maxres = 0; - pi_iterator_t *pi; - j2k_tcp_t *tcp; - j2k_tccp_t *tccp; + 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)); + tcp = &cp->tcps[tileno]; + pi = (pi_iterator_t *) malloc((tcp->numpocs + 1) * + sizeof(pi_iterator_t)); - for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */ - p = tileno % cp->tw; - q = tileno / cp->tw; + for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */ + p = tileno % cp->tw; + q = tileno / cp->tw; - 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].tx0 = int_max(cp->tx0 + p * cp->tdx, img->x0); + pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, img->y0); + pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1); + pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1); + pi[pino].numcomps = img->numcomps; + pi[pino].comps = + (pi_comp_t *) malloc(img->numcomps * sizeof(pi_comp_t)); - for (compno = 0; compno < pi->numcomps; compno++) { - int tcx0, tcy0, tcx1, tcy1; - pi_comp_t *comp = &pi[pino].comps[compno]; - tccp = &tcp->tccps[compno]; - comp->dx = img->comps[compno].dx; - comp->dy = img->comps[compno].dy; - comp->numresolutions = tccp->numresolutions; - comp->resolutions = (pi_resolution_t *) malloc(comp->numresolutions * sizeof(pi_resolution_t)); - tcx0 = int_ceildiv(pi->tx0, comp->dx); - tcy0 = int_ceildiv(pi->ty0, comp->dy); - tcx1 = int_ceildiv(pi->tx1, comp->dx); - tcy1 = int_ceildiv(pi->ty1, comp->dy); - if (comp->numresolutions > maxres) { - maxres = comp->numresolutions; - } - for (resno = 0; resno < comp->numresolutions; resno++) { - int levelno; - int rx0, ry0, rx1, ry1; - int px0, py0, px1, py1; - pi_resolution_t *res = &comp->resolutions[resno]; - if (tccp->csty & J2K_CCP_CSTY_PRT) { - res->pdx = tccp->prcw[resno]; - res->pdy = tccp->prch[resno]; - } else { - res->pdx = 15; - res->pdy = 15; - } - levelno = comp->numresolutions - 1 - resno; - rx0 = int_ceildivpow2(tcx0, levelno); - ry0 = int_ceildivpow2(tcy0, levelno); - rx1 = int_ceildivpow2(tcx1, levelno); - ry1 = int_ceildivpow2(tcy1, levelno); - px0 = int_floordivpow2(rx0, res->pdx) << res->pdx; - py0 = int_floordivpow2(ry0, res->pdy) << res->pdy; - px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx; - py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy; - res->pw = (px1 - px0) >> res->pdx; - res->ph = (py1 - py0) >> res->pdy; - } + for (compno = 0; compno < pi->numcomps; compno++) { + int tcx0, tcy0, tcx1, tcy1; + pi_comp_t *comp = &pi[pino].comps[compno]; + tccp = &tcp->tccps[compno]; + comp->dx = img->comps[compno].dx; + comp->dy = img->comps[compno].dy; + comp->numresolutions = tccp->numresolutions; + comp->resolutions = + (pi_resolution_t *) malloc(comp->numresolutions * + sizeof(pi_resolution_t)); + tcx0 = int_ceildiv(pi->tx0, comp->dx); + tcy0 = int_ceildiv(pi->ty0, comp->dy); + tcx1 = int_ceildiv(pi->tx1, comp->dx); + tcy1 = int_ceildiv(pi->ty1, comp->dy); + if (comp->numresolutions > maxres) { + maxres = comp->numresolutions; + } + for (resno = 0; resno < comp->numresolutions; resno++) { + int levelno; + int rx0, ry0, rx1, ry1; + int px0, py0, px1, py1; + pi_resolution_t *res = &comp->resolutions[resno]; + if (tccp->csty & J2K_CCP_CSTY_PRT) { + res->pdx = tccp->prcw[resno]; + res->pdy = tccp->prch[resno]; + } else { + res->pdx = 15; + res->pdy = 15; } - - 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; + levelno = comp->numresolutions - 1 - resno; + rx0 = int_ceildivpow2(tcx0, levelno); + ry0 = int_ceildivpow2(tcy0, levelno); + rx1 = int_ceildivpow2(tcx1, levelno); + ry1 = int_ceildivpow2(tcy1, levelno); + px0 = int_floordivpow2(rx0, res->pdx) << res->pdx; + py0 = int_floordivpow2(ry0, res->pdy) << res->pdy; + px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx; + py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy; + res->pw = (px1 - px0) >> res->pdx; + res->ph = (py1 - py0) >> res->pdy; + } + } - if (pino==0) - { - pi[pino].include = (short int*)malloc(img->numcomps*maxres*tcp->numlayers*100*sizeof(short int)); - for (i=0 ; inumcomps*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; + 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; - if (tcp->POC == 0) { - pi[pino].first = 1; - pi[pino].poc.resno0 = 0; - pi[pino].poc.compno0 = 0; - pi[pino].poc.layno1 = tcp->numlayers; - pi[pino].poc.resno1 = maxres; - pi[pino].poc.compno1 = img->numcomps; - pi[pino].poc.prg = tcp->prg; - } else { - pi[pino].first = 1; - pi[pino].poc.resno0 = tcp->pocs[pino].resno0; - pi[pino].poc.compno0 = tcp->pocs[pino].compno0; - pi[pino].poc.layno1 = tcp->pocs[pino].layno1; - pi[pino].poc.resno1 = tcp->pocs[pino].resno1; - pi[pino].poc.compno1 = tcp->pocs[pino].compno1; - pi[pino].poc.prg = tcp->pocs[pino].prg; - } + 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; + + if (tcp->POC == 0) { + pi[pino].first = 1; + pi[pino].poc.resno0 = 0; + pi[pino].poc.compno0 = 0; + pi[pino].poc.layno1 = tcp->numlayers; + pi[pino].poc.resno1 = maxres; + pi[pino].poc.compno1 = img->numcomps; + pi[pino].poc.prg = tcp->prg; + } else { + pi[pino].first = 1; + pi[pino].poc.resno0 = tcp->pocs[pino].resno0; + pi[pino].poc.compno0 = tcp->pocs[pino].compno0; + pi[pino].poc.layno1 = tcp->pocs[pino].layno1; + pi[pino].poc.resno1 = tcp->pocs[pino].resno1; + pi[pino].poc.compno1 = tcp->pocs[pino].compno1; + pi[pino].poc.prg = tcp->pocs[pino].prg; } - return pi; + } + return pi; } /* @@ -142,36 +149,45 @@ pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno) * */ int pi_next_lrcp(pi_iterator_t * pi) { - pi_comp_t *comp; - pi_resolution_t *res; + pi_comp_t *comp; + pi_resolution_t *res; - if (!pi->first) { + if (!pi->first) { + comp = &pi->comps[pi->compno]; + res = &comp->resolutions[pi->resno]; + goto skip; + } else { + pi->first = 0; + } + for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { + for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; + pi->resno++) { + for (pi->compno = pi->poc.compno0; + pi->compno < pi->poc.compno1; pi->compno++) { comp = &pi->comps[pi->compno]; + if (pi->resno >= comp->numresolutions) { + continue; + } res = &comp->resolutions[pi->resno]; - goto skip; - } else { - pi->first = 0; - } - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { - for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) { - for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { - comp = &pi->comps[pi->compno]; - if (pi->resno >= comp->numresolutions) { - continue; - } - res = &comp->resolutions[pi->resno]; - for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) - { - if (!pi->include[pi->layno*pi->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:; + 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; + } + return 0; } /* @@ -181,35 +197,44 @@ int pi_next_lrcp(pi_iterator_t * pi) * */ int pi_next_rlcp(pi_iterator_t * pi) { - pi_comp_t *comp; - pi_resolution_t *res; - if (!pi->first) { + pi_comp_t *comp; + pi_resolution_t *res; + if (!pi->first) { + comp = &pi->comps[pi->compno]; + res = &comp->resolutions[pi->resno]; + goto skip; + } else { + pi->first = 0; + } + for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; + pi->resno++) { + for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { + for (pi->compno = pi->poc.compno0; + pi->compno < pi->poc.compno1; pi->compno++) { comp = &pi->comps[pi->compno]; + if (pi->resno >= comp->numresolutions) { + continue; + } res = &comp->resolutions[pi->resno]; - goto skip; - } else { - pi->first = 0; - } - for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) { - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { - for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; - pi->compno++) { - comp = &pi->comps[pi->compno]; - if (pi->resno >= comp->numresolutions) { - continue; - } - res = &comp->resolutions[pi->resno]; - for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) { - if (!pi->include[pi->layno*pi->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:; - } - } + 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; + } + return 0; } /* @@ -219,68 +244,96 @@ int pi_next_rlcp(pi_iterator_t * pi) * */ int pi_next_rpcl(pi_iterator_t * pi) { - pi_comp_t *comp; - pi_resolution_t *res; - if (!pi->first) { - goto skip; - } else { - int compno, resno; - pi->first = 0; - pi->dx = 0; - pi->dy = 0; - for (compno = 0; compno < pi->numcomps; compno++) { - comp = &pi->comps[compno]; - for (resno = 0; resno < comp->numresolutions; resno++) { - int dx, dy; - res = &comp->resolutions[resno]; - dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); - dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); - pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); - pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); - } - } + pi_comp_t *comp; + pi_resolution_t *res; + if (!pi->first) { + goto skip; + } else { + int compno, resno; + pi->first = 0; + pi->dx = 0; + pi->dy = 0; + for (compno = 0; compno < pi->numcomps; compno++) { + comp = &pi->comps[compno]; + for (resno = 0; resno < comp->numresolutions; resno++) { + int dx, dy; + res = &comp->resolutions[resno]; + dx = comp->dx * + (1 << (res->pdx + comp->numresolutions - 1 - resno)); + dy = comp->dy * + (1 << (res->pdy + comp->numresolutions - 1 - resno)); + pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); + pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); + } } - for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) { - for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) { - for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) { - for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { - int levelno; - int trx0, try0; - int rpx, rpy; - int prci, prcj; - comp = &pi->comps[pi->compno]; - if (pi->resno >= comp->numresolutions) { - continue; - } - res = &comp->resolutions[pi->resno]; - levelno = comp->numresolutions - 1 - pi->resno; - trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); - try0 = int_ceildiv(pi->ty0, comp->dy << levelno); - rpx = res->pdx + levelno; - rpy = res->pdy + levelno; - if (!(pi->x % (comp->dx << rpx) == 0 || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) { - continue; - } - if (!(pi->y % (comp->dy << rpy) == 0 || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) { - continue; - } - prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), - res->pdx) - int_floordivpow2(trx0, res->pdx); - prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), - res->pdy) - int_floordivpow2(try0, res->pdy); - pi->precno = prci + prcj * res->pw; - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { - if (!pi->include[pi->layno*pi->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:; - } - } + } + for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; + pi->resno++) { + for (pi->y = pi->ty0; pi->y < pi->ty1; + pi->y += pi->dy - (pi->y % pi->dy)) { + for (pi->x = pi->tx0; pi->x < pi->tx1; + pi->x += pi->dx - (pi->x % pi->dx)) { + for (pi->compno = pi->poc.compno0; + pi->compno < pi->poc.compno1; pi->compno++) { + int levelno; + int trx0, try0; + int rpx, rpy; + int prci, prcj; + comp = &pi->comps[pi->compno]; + if (pi->resno >= comp->numresolutions) { + continue; + } + res = &comp->resolutions[pi->resno]; + levelno = comp->numresolutions - 1 - pi->resno; + trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); + try0 = int_ceildiv(pi->ty0, comp->dy << levelno); + rpx = res->pdx + levelno; + rpy = res->pdy + levelno; + if (! + (pi->x % (comp->dx << rpx) == 0 + || (pi->x == pi->tx0 + && (trx0 << levelno) % (1 << rpx)))) { + continue; + } + if (! + (pi->y % (comp->dy << rpy) == 0 + || (pi->y == pi->ty0 + && (try0 << levelno) % (1 << rpx)))) { + continue; + } + prci = + int_floordivpow2(int_ceildiv + (pi->x, comp->dx << levelno), + res->pdx) - int_floordivpow2(trx0, + res-> + pdx); + prcj = + int_floordivpow2(int_ceildiv + (pi->y, comp->dy << levelno), + res->pdy) - int_floordivpow2(try0, + res-> + pdy); + pi->precno = prci + prcj * res->pw; + for (pi->layno = 0; pi->layno < pi->poc.layno1; + pi->layno++) { + if (!pi-> + include[pi->layno * pi->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; + } + return 0; } /* @@ -290,64 +343,96 @@ int pi_next_rpcl(pi_iterator_t * pi) * */ int pi_next_pcrl(pi_iterator_t * pi) { - pi_comp_t *comp; - pi_resolution_t *res; - if (!pi->first) { - comp = &pi->comps[pi->compno]; - goto skip; - } else { - int compno, resno; - pi->first = 0; - pi->dx = 0; - pi->dy = 0; - for (compno = 0; compno < pi->numcomps; compno++) { - comp = &pi->comps[compno]; - for (resno = 0; resno < comp->numresolutions; resno++) { - int dx, dy; - res = &comp->resolutions[resno]; - dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); - dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); - pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); - pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); - } - } + pi_comp_t *comp; + pi_resolution_t *res; + if (!pi->first) { + comp = &pi->comps[pi->compno]; + goto skip; + } else { + int compno, resno; + pi->first = 0; + pi->dx = 0; + pi->dy = 0; + for (compno = 0; compno < pi->numcomps; compno++) { + comp = &pi->comps[compno]; + for (resno = 0; resno < comp->numresolutions; resno++) { + int dx, dy; + res = &comp->resolutions[resno]; + dx = comp->dx * + (1 << (res->pdx + comp->numresolutions - 1 - resno)); + dy = comp->dy * + (1 << (res->pdy + comp->numresolutions - 1 - resno)); + pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); + pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); + } } - for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) { - for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) { - for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { - comp = &pi->comps[pi->compno]; - for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) { - int levelno; - int trx0, try0; - int rpx, rpy; - int prci, prcj; - res = &comp->resolutions[pi->resno]; - levelno = comp->numresolutions - 1 - pi->resno; - trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); - try0 = int_ceildiv(pi->ty0, comp->dy << levelno); - rpx = res->pdx + levelno; - rpy = res->pdy + levelno; - if (!(pi->x % (comp->dx << rpx) == 0 || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) { - continue; - } - if (!(pi->y % (comp->dy << rpy) == 0 || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) { - continue; - } - prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) - int_floordivpow2(trx0, res->pdx); - prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) - int_floordivpow2(try0, res->pdy); - pi->precno = prci + prcj * res->pw; - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { - if (! pi->include[pi->layno*pi->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:; - } - } + } + for (pi->y = pi->ty0; pi->y < pi->ty1; + pi->y += pi->dy - (pi->y % pi->dy)) { + for (pi->x = pi->tx0; pi->x < pi->tx1; + pi->x += pi->dx - (pi->x % pi->dx)) { + for (pi->compno = pi->poc.compno0; + pi->compno < pi->poc.compno1; pi->compno++) { + comp = &pi->comps[pi->compno]; + for (pi->resno = pi->poc.resno0; + pi->resno < int_min(pi->poc.resno1, + comp->numresolutions); + pi->resno++) { + int levelno; + int trx0, try0; + int rpx, rpy; + int prci, prcj; + res = &comp->resolutions[pi->resno]; + levelno = comp->numresolutions - 1 - pi->resno; + trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); + try0 = int_ceildiv(pi->ty0, comp->dy << levelno); + rpx = res->pdx + levelno; + rpy = res->pdy + levelno; + if (! + (pi->x % (comp->dx << rpx) == 0 + || (pi->x == pi->tx0 + && (trx0 << levelno) % (1 << rpx)))) { + continue; + } + if (! + (pi->y % (comp->dy << rpy) == 0 + || (pi->y == pi->ty0 + && (try0 << levelno) % (1 << rpx)))) { + continue; + } + prci = + int_floordivpow2(int_ceildiv + (pi->x, comp->dx << levelno), + res->pdx) - int_floordivpow2(trx0, + res-> + pdx); + prcj = + int_floordivpow2(int_ceildiv + (pi->y, comp->dy << levelno), + res->pdy) - int_floordivpow2(try0, + res-> + pdy); + pi->precno = prci + prcj * res->pw; + for (pi->layno = 0; pi->layno < pi->poc.layno1; + pi->layno++) { + if (!pi-> + include[pi->layno * pi->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; + } + return 0; } /* @@ -357,66 +442,93 @@ int pi_next_pcrl(pi_iterator_t * pi) * */ int pi_next_cprl(pi_iterator_t * pi) { - pi_comp_t *comp; - pi_resolution_t *res; - if (!pi->first) { - comp = &pi->comps[pi->compno]; - goto skip; - } else { - pi->first = 0; + pi_comp_t *comp; + pi_resolution_t *res; + if (!pi->first) { + comp = &pi->comps[pi->compno]; + goto skip; + } else { + pi->first = 0; + } + for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; + pi->compno++) { + int resno; + comp = &pi->comps[pi->compno]; + pi->dx = 0; + pi->dy = 0; + for (resno = 0; resno < comp->numresolutions; resno++) { + int dx, dy; + res = &comp->resolutions[resno]; + dx = comp->dx * + (1 << (res->pdx + comp->numresolutions - 1 - resno)); + dy = comp->dy * + (1 << (res->pdy + comp->numresolutions - 1 - resno)); + pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); + pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); } - for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; - pi->compno++) { - int resno; - comp = &pi->comps[pi->compno]; - pi->dx = 0; - pi->dy = 0; - for (resno = 0; resno < comp->numresolutions; resno++) { - int dx, dy; - res = &comp->resolutions[resno]; - dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); - dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); - pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); - pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); - } - for (pi->y = pi->ty0; pi->y < pi->ty1; - pi->y += pi->dy - (pi->y % pi->dy)) { - for (pi->x = pi->tx0; pi->x < pi->tx1; - pi->x += pi->dx - (pi->x % pi->dx)) { - for (pi->resno = pi->poc.resno0; - pi->resno < int_min(pi->poc.resno1, comp->numresolutions); - pi->resno++) { - int levelno; - int trx0, try0; - int rpx, rpy; - int prci, prcj; - res = &comp->resolutions[pi->resno]; - levelno = comp->numresolutions - 1 - pi->resno; - trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); - try0 = int_ceildiv(pi->ty0, comp->dy << levelno); - rpx = res->pdx + levelno; - rpy = res->pdy + levelno; - if (!(pi->x % (comp->dx << rpx) == 0 || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) { - continue; - } - if (!(pi->y % (comp->dy << rpy) == 0 || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) { - continue; - } - prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) - int_floordivpow2(trx0, res->pdx); - prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) - int_floordivpow2(try0, res->pdy); - pi->precno = prci + prcj * res->pw; - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { - if (! pi->include[pi->layno*pi->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:; - } - } + for (pi->y = pi->ty0; pi->y < pi->ty1; + pi->y += pi->dy - (pi->y % pi->dy)) { + for (pi->x = pi->tx0; pi->x < pi->tx1; + pi->x += pi->dx - (pi->x % pi->dx)) { + for (pi->resno = pi->poc.resno0; + pi->resno < int_min(pi->poc.resno1, + comp->numresolutions); + pi->resno++) { + int levelno; + int trx0, try0; + int rpx, rpy; + int prci, prcj; + res = &comp->resolutions[pi->resno]; + levelno = comp->numresolutions - 1 - pi->resno; + trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); + try0 = int_ceildiv(pi->ty0, comp->dy << levelno); + rpx = res->pdx + levelno; + rpy = res->pdy + levelno; + if (! + (pi->x % (comp->dx << rpx) == 0 + || (pi->x == pi->tx0 + && (trx0 << levelno) % (1 << rpx)))) { + continue; + } + if (! + (pi->y % (comp->dy << rpy) == 0 + || (pi->y == pi->ty0 + && (try0 << levelno) % (1 << rpx)))) { + continue; + } + prci = + int_floordivpow2(int_ceildiv + (pi->x, comp->dx << levelno), + res->pdx) - int_floordivpow2(trx0, + res-> + pdx); + prcj = + int_floordivpow2(int_ceildiv + (pi->y, comp->dy << levelno), + res->pdy) - int_floordivpow2(try0, + res-> + pdy); + pi->precno = prci + prcj * res->pw; + for (pi->layno = 0; pi->layno < pi->poc.layno1; + pi->layno++) { + if (!pi-> + include[pi->layno * pi->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; + } + return 0; } /* @@ -426,17 +538,17 @@ int pi_next_cprl(pi_iterator_t * pi) * */ int pi_next(pi_iterator_t * pi) { - switch (pi->poc.prg) { - case 0: - return pi_next_lrcp(pi); - case 1: - return pi_next_rlcp(pi); - case 2: - return pi_next_rpcl(pi); - case 3: - return pi_next_pcrl(pi); - case 4: - return pi_next_cprl(pi); - } - return 0; + switch (pi->poc.prg) { + case 0: + return pi_next_lrcp(pi); + case 1: + return pi_next_rlcp(pi); + case 2: + return pi_next_rpcl(pi); + case 3: + return pi_next_pcrl(pi); + case 4: + return pi_next_cprl(pi); + } + return 0; } diff --git a/libopenjpeg/pi.h b/libopenjpeg/pi.h index f450304a..84000e5b 100644 --- a/libopenjpeg/pi.h +++ b/libopenjpeg/pi.h @@ -31,27 +31,27 @@ #include "tcd.h" typedef struct { - int pdx, pdy; - int pw, ph; + int pdx, pdy; + int pw, ph; } pi_resolution_t; typedef struct { - int dx, dy; - int numresolutions; - pi_resolution_t *resolutions; + int dx, dy; + int numresolutions; + pi_resolution_t *resolutions; } pi_comp_t; typedef struct { - 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; /* 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 */ + 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; /* 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 */ /* * Create a packet iterator diff --git a/libopenjpeg/raw.c b/libopenjpeg/raw.c index 180a1dec..390146ce 100644 --- a/libopenjpeg/raw.c +++ b/libopenjpeg/raw.c @@ -27,20 +27,20 @@ #include "raw.h" -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 */ +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 */ /* * Return the number of bytes already encoded. */ int raw_numbytes() { - return raw_bp - raw_start; + return raw_bp - raw_start; } /* @@ -51,11 +51,11 @@ int raw_numbytes() */ void raw_init_dec(unsigned char *bp, int len) { - raw_start = bp; - raw_lenmax = len; - raw_len = 0; - raw_c = 0; - raw_ct = 0; + raw_start = bp; + raw_lenmax = len; + raw_len = 0; + raw_c = 0; + raw_ct = 0; } /* @@ -63,19 +63,19 @@ void raw_init_dec(unsigned char *bp, int len) */ int raw_decode() { - int d; - if (raw_ct == 0) { - raw_ct = 8; - if (raw_len == raw_lenmax) - raw_c = 0xff; - else { - if (raw_c == 0xff) - raw_ct = 7; - raw_c = *(raw_start + raw_len); - raw_len++; - } + int d; + if (raw_ct == 0) { + raw_ct = 8; + if (raw_len == raw_lenmax) + raw_c = 0xff; + else { + if (raw_c == 0xff) + raw_ct = 7; + raw_c = *(raw_start + raw_len); + raw_len++; } - raw_ct--; - d = (raw_c >> raw_ct) & 0x01; - return d; + } + raw_ct--; + d = (raw_c >> raw_ct) & 0x01; + return d; } diff --git a/libopenjpeg/t1.c b/libopenjpeg/t1.c index 9aa024e2..186ab3f2 100644 --- a/libopenjpeg/t1.c +++ b/libopenjpeg/t1.c @@ -29,7 +29,7 @@ #include "t1.h" #include "j2k.h" #include "mqc.h" -#include "raw.h" /* Antonin */ +#include "raw.h" /* Antonin */ #include "int.h" #include "mct.h" #include "dwt.h" @@ -97,718 +97,824 @@ static int t1_flags[T1_MAXCBLKH + 2][T1_MAXCBLKH + 2]; int t1_getctxno_zc(int f, int orient) { - return t1_lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)]; + return t1_lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)]; } int t1_getctxno_sc(int f) { - return t1_lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4]; + return t1_lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4]; } 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) { - return t1_lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4]; + return t1_lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4]; } 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)]; - else - return t1_lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)]; + if (bitpos > T1_NMSEDEC_FRACBITS) + 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)]; } 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)]; - else - return t1_lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)]; + if (bitpos > T1_NMSEDEC_FRACBITS) + 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)]; } void t1_updateflags(int *fp, int s) { - int *np = fp - (T1_MAXCBLKW + 2); - int *sp = fp + (T1_MAXCBLKW + 2); - np[-1] |= T1_SIG_SE; - np[1] |= T1_SIG_SW; - sp[-1] |= T1_SIG_NE; - sp[1] |= T1_SIG_NW; - *np |= T1_SIG_S; - *sp |= T1_SIG_N; - fp[-1] |= T1_SIG_E; - fp[1] |= T1_SIG_W; - if (s) { - *np |= T1_SGN_S; - *sp |= T1_SGN_N; - fp[-1] |= T1_SGN_E; - fp[1] |= T1_SGN_W; - } + int *np = fp - (T1_MAXCBLKW + 2); + int *sp = fp + (T1_MAXCBLKW + 2); + np[-1] |= T1_SIG_SE; + np[1] |= T1_SIG_SW; + sp[-1] |= T1_SIG_NE; + sp[1] |= T1_SIG_NW; + *np |= T1_SIG_S; + *sp |= T1_SIG_N; + fp[-1] |= T1_SIG_E; + fp[1] |= T1_SIG_W; + if (s) { + *np |= T1_SGN_S; + *sp |= T1_SGN_N; + fp[-1] |= T1_SGN_E; + fp[1] |= T1_SGN_W; + } } -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); - 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_bypass_enc(v); - } else { - mqc_setcurctx(t1_getctxno_zc(flag, orient)); - mqc_encode(v); - } - if (v) { - v = *dp < 0 ? 1 : 0; - *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); - } else { - mqc_setcurctx(t1_getctxno_sc(flag)); - mqc_encode(v ^ t1_getspb(flag)); - } - t1_updateflags(fp, v); - *fp |= T1_SIG; - } - *fp |= T1_VISIT; + int v, flag; + 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_bypass_enc(v); + } else { + mqc_setcurctx(t1_getctxno_zc(flag, orient)); + mqc_encode(v); + } + if (v) { + v = *dp < 0 ? 1 : 0; + *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); + } else { + mqc_setcurctx(t1_getctxno_sc(flag)); + mqc_encode(v ^ t1_getspb(flag)); + } + t1_updateflags(fp, v); + *fp |= T1_SIG; } + *fp |= T1_VISIT; + } } -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 = - 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))) { - if (type == T1_TYPE_RAW) { - if (raw_decode()) { - v = raw_decode(); /* ESSAI */ - *dp = v ? -oneplushalf : oneplushalf; - t1_updateflags(fp, v); - *fp |= T1_SIG; - } - } else { - mqc_setcurctx(t1_getctxno_zc(flag, orient)); - if (mqc_decode()) { - mqc_setcurctx(t1_getctxno_sc(flag)); - v = mqc_decode() ^ t1_getspb(flag); - *dp = v ? -oneplushalf : oneplushalf; - t1_updateflags(fp, v); - *fp |= T1_SIG; - } - } - *fp |= T1_VISIT; + int v, flag; + 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))) { + if (type == T1_TYPE_RAW) { + if (raw_decode()) { + v = raw_decode(); /* ESSAI */ + *dp = v ? -oneplushalf : oneplushalf; + t1_updateflags(fp, v); + *fp |= T1_SIG; + } + } else { + mqc_setcurctx(t1_getctxno_zc(flag, orient)); + if (mqc_decode()) { + mqc_setcurctx(t1_getctxno_sc(flag)); + v = mqc_decode() ^ t1_getspb(flag); + *dp = v ? -oneplushalf : oneplushalf; + t1_updateflags(fp, v); + *fp |= T1_SIG; + } } -} /* VSC and BYPASS by Antonin */ + *fp |= T1_VISIT; + } +} /* 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; - one = 1 << (bpno + T1_NMSEDEC_FRACBITS); - 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); - } - } + int i, j, k, one, vsc; + *nmsedec = 0; + one = 1 << (bpno + T1_NMSEDEC_FRACBITS); + 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); + } } + } } -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++) { + 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); + 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); - if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) { - *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_bypass_enc(v); - } else { - mqc_setcurctx(t1_getctxno_mag(flag)); - mqc_encode(v); - } - *fp |= T1_REFINE; + int v, flag; + 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); + v = int_abs(*dp) & one ? 1 : 0; + if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ + mqc_setcurctx(t1_getctxno_mag(flag)); /* ESSAI */ + mqc_bypass_enc(v); + } else { + mqc_setcurctx(t1_getctxno_mag(flag)); + mqc_encode(v); } + *fp |= T1_REFINE; + } } -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); - if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) { - if (type == T1_TYPE_RAW) { - mqc_setcurctx(t1_getctxno_mag(flag)); /* ESSAI */ - v = raw_decode(); - } else { - mqc_setcurctx(t1_getctxno_mag(flag)); - v = mqc_decode(); - } - t = v ? poshalf : neghalf; - *dp += *dp < 0 ? -t : t; - *fp |= T1_REFINE; + int v, t, flag; + 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 */ + v = raw_decode(); + } else { + mqc_setcurctx(t1_getctxno_mag(flag)); + v = mqc_decode(); } -} /* VSC and BYPASS by Antonin */ + t = v ? poshalf : neghalf; + *dp += *dp < 0 ? -t : t; + *fp |= T1_REFINE; + } +} /* 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; - one = 1 << (bpno + T1_NMSEDEC_FRACBITS); - 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); - } - } + int i, j, k, one, vsc; + *nmsedec = 0; + one = 1 << (bpno + T1_NMSEDEC_FRACBITS); + 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); + } } + } } void t1_dec_refpass(int w, int h, int bpno, char type, int cblksty) { - int i, j, k, one, poshalf, neghalf; - int vsc; - one = 1 << bpno; - poshalf = one >> 1; - neghalf = bpno > 0 ? -poshalf : -1; - 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); - } - } + int i, j, k, one, poshalf, neghalf; + int vsc; + one = 1 << bpno; + poshalf = one >> 1; + neghalf = bpno > 0 ? -poshalf : -1; + 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 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); - if (partial) - goto label_partial; - if (!(*fp & (T1_SIG | T1_VISIT))) { - mqc_setcurctx(t1_getctxno_zc(flag, orient)); - v = int_abs(*dp) & one ? 1 : 0; - mqc_encode(v); - if (v) { - label_partial: - *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)); - t1_updateflags(fp, v); - *fp |= T1_SIG; - } + int v, flag; + 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))) { + mqc_setcurctx(t1_getctxno_zc(flag, orient)); + v = int_abs(*dp) & one ? 1 : 0; + mqc_encode(v); + if (v) { + label_partial: + *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)); + t1_updateflags(fp, v); + *fp |= T1_SIG; } - *fp &= ~T1_VISIT; + } + *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); - if (partial) - goto label_partial; - if (!(flag & (T1_SIG | T1_VISIT))) { - mqc_setcurctx(t1_getctxno_zc(flag, orient)); - if (mqc_decode()) { - label_partial: - mqc_setcurctx(t1_getctxno_sc(flag)); - v = mqc_decode() ^ t1_getspb(flag); - *dp = v ? -oneplushalf : oneplushalf; - t1_updateflags(fp, v); - *fp |= T1_SIG; - } + int v, flag; + 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))) { + mqc_setcurctx(t1_getctxno_zc(flag, orient)); + if (mqc_decode()) { + label_partial: + mqc_setcurctx(t1_getctxno_sc(flag)); + v = mqc_decode() ^ t1_getspb(flag); + *dp = v ? -oneplushalf : oneplushalf; + t1_updateflags(fp, v); + *fp |= T1_SIG; } - *fp &= ~T1_VISIT; -} /* VSC and BYPASS by Antonin */ + } + *fp &= ~T1_VISIT; +} /* 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; - one = 1 << (bpno + T1_NMSEDEC_FRACBITS); - for (k = 0; k < h; k += 4) { - for (i = 0; i < w; i++) { - 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)); - } 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)); - } - } else { - agg = 0; - } - if (agg) { - for (runlen = 0; runlen < 4; runlen++) { - if (int_abs(t1_data[k + runlen][i]) & one) - break; - } - mqc_setcurctx(T1_CTXNO_AGG); - mqc_encode(runlen != 4); - if (runlen == 4) { - continue; - } - mqc_setcurctx(T1_CTXNO_UNI); - mqc_encode(runlen >> 1); - mqc_encode(runlen & 1); - } else { - 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); - } + int i, j, k, one, agg, runlen, vsc; + *nmsedec = 0; + one = 1 << (bpno + T1_NMSEDEC_FRACBITS); + for (k = 0; k < h; k += 4) { + for (i = 0; i < w; i++) { + 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)); + } 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)); + } + } else { + agg = 0; + } + if (agg) { + for (runlen = 0; runlen < 4; runlen++) { + if (int_abs(t1_data[k + runlen][i]) & one) + break; + } + mqc_setcurctx(T1_CTXNO_AGG); + mqc_encode(runlen != 4); + if (runlen == 4) { + continue; } + mqc_setcurctx(T1_CTXNO_UNI); + mqc_encode(runlen >> 1); + mqc_encode(runlen & 1); + } else { + 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); + } } + } } void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty) { - int i, j, k, one, half, oneplushalf, agg, runlen, vsc; - int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; - one = 1 << bpno; - half = one >> 1; - oneplushalf = one | half; - for (k = 0; k < h; k += 4) { - for (i = 0; i < w; i++) { - 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)); - } 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)); - } - } else { - agg = 0; - } - if (agg) { - mqc_setcurctx(T1_CTXNO_AGG); - if (!mqc_decode()) { - continue; - } - mqc_setcurctx(T1_CTXNO_UNI); - runlen = mqc_decode(); - runlen = (runlen << 1) | mqc_decode(); - } else { - 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); - } + int i, j, k, one, half, oneplushalf, agg, runlen, vsc; + int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; + one = 1 << bpno; + half = one >> 1; + oneplushalf = one | half; + for (k = 0; k < h; k += 4) { + for (i = 0; i < w; i++) { + 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)); + } 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)); + } + } else { + agg = 0; + } + if (agg) { + mqc_setcurctx(T1_CTXNO_AGG); + if (!mqc_decode()) { + continue; } - } - if (segsym) { - int v = 0; mqc_setcurctx(T1_CTXNO_UNI); - v = mqc_decode(); - v = (v << 1) | mqc_decode(); - v = (v << 1) | mqc_decode(); - v = (v << 1) | mqc_decode(); - /* if (v!=0xa) - { - fprintf(stderr, "warning: bad segmentation symbol %x\n",v); - } */ + runlen = mqc_decode(); + runlen = (runlen << 1) | mqc_decode(); + } else { + 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 and BYPASS by Antonin */ + } + if (segsym) { + int v = 0; + mqc_setcurctx(T1_CTXNO_UNI); + v = mqc_decode(); + v = (v << 1) | mqc_decode(); + v = (v << 1) | mqc_decode(); + v = (v << 1) | mqc_decode(); + /* if (v!=0xa) + { + fprintf(stderr, "warning: bad segmentation symbol %x\n",v); + } */ + } +} /* 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) { - w1 = mct_getnorm(compno); - w2 = dwt_getnorm(level, orient); - } else { /* if (qmfbid == 0) */ - w1 = mct_getnorm_real(compno); - w2 = dwt_getnorm_real(level, orient); - } - wmsedec = w1 * w2 * stepsize * (1 << bpno); - wmsedec *= wmsedec * nmsedec / 8192.0; - return wmsedec; + double w1, w2, wmsedec; + if (qmfbid == 1) { + w1 = mct_getnorm(compno); + w2 = dwt_getnorm(level, orient); + } else { /* if (qmfbid == 0) */ + w1 = mct_getnorm_real(compno); + w2 = dwt_getnorm_real(level, orient); + } + wmsedec = w1 * w2 * stepsize * (1 << bpno); + wmsedec *= wmsedec * nmsedec / 8192.0; + 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; - int passno; - int bpno, passtype; - int max; - int nmsedec; - double cumwmsedec = 0; - char type = T1_TYPE_MQ; - - w = cblk->x1 - cblk->x0; - h = cblk->y1 - cblk->y0; - - max = 0; - for (j = 0; j < h; j++) { - for (i = 0; i < w; i++) { + int i, j; + int w, h; + int passno; + int bpno, passtype; + int max; + int nmsedec; + double cumwmsedec = 0; + char type = T1_TYPE_MQ; + + w = cblk->x1 - cblk->x0; + h = cblk->y1 - cblk->y0; + + max = 0; + for (j = 0; j < h; j++) { + 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; + cblk->numbps = + max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0; - for (i = 0; i < sizeof(t1_flags) / sizeof(int); i++) - ((int *) t1_flags)[i] = 0; - bpno = cblk->numbps - 1; - passtype = 2; + for (i = 0; i < sizeof(t1_flags) / sizeof(int); i++) + ((int *) t1_flags)[i] = 0; + bpno = cblk->numbps - 1; + passtype = 2; - mqc_resetstates(); - mqc_setstate(T1_CTXNO_UNI, 0, 46); - mqc_setstate(T1_CTXNO_AGG, 0, 3); - mqc_setstate(T1_CTXNO_ZC, 0, 4); - mqc_init_enc(cblk->data); + mqc_resetstates(); + mqc_setstate(T1_CTXNO_UNI, 0, 46); + mqc_setstate(T1_CTXNO_AGG, 0, 3); + mqc_setstate(T1_CTXNO_ZC, 0, 4); + mqc_init_enc(cblk->data); - for (passno = 0; bpno >= 0; passno++) { - 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; + for (passno = 0; bpno >= 0; passno++) { + 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; - switch (passtype) { - case 0: - t1_enc_sigpass(w, h, bpno, orient, &nmsedec, type, cblksty); - break; - case 1: - t1_enc_refpass(w, h, bpno, &nmsedec, type, cblksty); - break; - case 2: - t1_enc_clnpass(w, h, bpno, orient, &nmsedec, cblksty); - /* code switch SEGMARK (i.e. SEGSYM) */ - if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) - mqc_segmark_enc(); - break; - } + switch (passtype) { + case 0: + t1_enc_sigpass(w, h, bpno, orient, &nmsedec, type, cblksty); + break; + case 1: + t1_enc_refpass(w, h, bpno, &nmsedec, type, cblksty); + break; + case 2: + t1_enc_clnpass(w, h, bpno, orient, &nmsedec, cblksty); + /* code switch SEGMARK (i.e. SEGSYM) */ + if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) + mqc_segmark_enc(); + break; + } - 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) - { - 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; - if (type == T1_TYPE_RAW) - mqc_bypass_init_enc(); - else - mqc_restart_init_enc(); + 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) { + 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; + } + } - pass->distortiondec = cumwmsedec; - pass->rate = mqc_numbytes() + correction; /* FIXME */ - pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate); + if (++passtype == 3) { + passtype = 0; + bpno--; + } - /* Code-switch "RESET" */ - if (cblksty & J2K_CCP_CBLKSTY_RESET) - mqc_reset_enc(); + if (pass->term && bpno > 0) { + type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) + && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : + T1_TYPE_MQ; + if (type == T1_TYPE_RAW) + mqc_bypass_init_enc(); + else + mqc_restart_init_enc(); } - /* Code switch "ERTERM" (i.e. PTERM) */ - if (cblksty & J2K_CCP_CBLKSTY_PTERM) - mqc_erterm_enc(); - else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) - mqc_flush(); + pass->distortiondec = cumwmsedec; + pass->rate = mqc_numbytes() + correction; /* FIXME */ + pass->len = + pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate); + + /* Code-switch "RESET" */ + if (cblksty & J2K_CCP_CBLKSTY_RESET) + mqc_reset_enc(); + } - cblk->totalpasses = passno; + /* Code switch "ERTERM" (i.e. PTERM) */ + if (cblksty & J2K_CCP_CBLKSTY_PTERM) + mqc_erterm_enc(); + else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) + mqc_flush(); + + 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; - int bpno, passtype; - int segno, passno; + int i; + int w, h; + int bpno, passtype; + int segno, passno; + /* add TONY */ + char type = T1_TYPE_MQ; + /* dda */ + + for (i = 0; i < sizeof(t1_data) / sizeof(int); i++) + ((int *) t1_data)[i] = 0; + for (i = 0; i < sizeof(t1_flags) / sizeof(int); i++) + ((int *) t1_flags)[i] = 0; + + w = cblk->x1 - cblk->x0; + h = cblk->y1 - cblk->y0; + bpno = roishift + cblk->numbps - 1; + passtype = 2; + + mqc_resetstates(); + mqc_setstate(T1_CTXNO_UNI, 0, 46); + mqc_setstate(T1_CTXNO_AGG, 0, 3); + mqc_setstate(T1_CTXNO_ZC, 0, 4); + + for (segno = 0; segno < cblk->numsegs; segno++) { + tcd_seg_t *seg = &cblk->segs[segno]; + /* add TONY */ - char type = T1_TYPE_MQ; + type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) + && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : + T1_TYPE_MQ; + if (type == T1_TYPE_RAW) + raw_init_dec(seg->data, seg->len); + else + mqc_init_dec(seg->data, seg->len); /* dda */ - for (i = 0; i < sizeof(t1_data) / sizeof(int); i++) - ((int *) t1_data)[i] = 0; - for (i = 0; i < sizeof(t1_flags) / sizeof(int); i++) - ((int *) t1_flags)[i] = 0; - - w = cblk->x1 - cblk->x0; - h = cblk->y1 - cblk->y0; - bpno = roishift + cblk->numbps - 1; - passtype = 2; - - mqc_resetstates(); - mqc_setstate(T1_CTXNO_UNI, 0, 46); - mqc_setstate(T1_CTXNO_AGG, 0, 3); - mqc_setstate(T1_CTXNO_ZC, 0, 4); - - for (segno = 0; segno < cblk->numsegs; segno++) { - tcd_seg_t *seg = &cblk->segs[segno]; - - /* add TONY */ - type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) - && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; - if (type == T1_TYPE_RAW) - raw_init_dec(seg->data, seg->len); - else - mqc_init_dec(seg->data, seg->len); - /* dda */ - - for (passno = 0; passno < seg->numpasses; passno++) { - switch (passtype) { - case 0: - t1_dec_sigpass(w, h, bpno, orient, type, cblksty); - break; - case 1: - t1_dec_refpass(w, h, bpno, type, cblksty); - break; - case 2: - t1_dec_clnpass(w, h, bpno, orient, cblksty); - break; - } + for (passno = 0; passno < seg->numpasses; passno++) { + switch (passtype) { + case 0: + t1_dec_sigpass(w, h, bpno, orient, type, cblksty); + break; + case 1: + t1_dec_refpass(w, h, bpno, type, cblksty); + break; + case 2: + t1_dec_clnpass(w, h, bpno, orient, cblksty); + break; + } - if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) - mqc_reset_enc(); + if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) + mqc_reset_enc(); - if (++passtype == 3) { - passtype = 0; - bpno--; - } - } + if (++passtype == 3) { + passtype = 0; + bpno--; + } } + } } 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; - } - } - } + 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].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]; - } + + 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; + } + } + } } - } 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); + + 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); + } + } } - } } - } } } } @@ -818,167 +924,195 @@ void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp) int t1_init_ctxno_zc(int f, int orient) { - int h, v, d, n, t, hv; - 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); - switch (orient) { - case 2: - t = h; - h = v; - v = t; - case 0: - case 1: - if (!h) { - if (!v) { - if (!d) - n = 0; - else if (d == 1) - n = 1; - else - n = 2; - } else if (v == 1) - n = 3; - else - n = 4; - } else if (h == 1) { - if (!v) { - if (!d) - n = 5; - else - n = 6; - } else - n = 7; - } else - n = 8; - break; - case 3: - hv = h + v; - if (!d) { - if (!hv) - n = 0; - else if (hv == 1) - n = 1; - else - n = 2; - } else if (d == 1) { - if (!hv) - n = 3; - else if (hv == 1) - n = 4; - else - n = 5; - } else if (d == 2) { - if (!hv) - n = 6; - else - n = 7; - } else - n = 8; - break; - } - return T1_CTXNO_ZC + n; + int h, v, d, n, t, hv; + 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); + switch (orient) { + case 2: + t = h; + h = v; + v = t; + case 0: + case 1: + if (!h) { + if (!v) { + if (!d) + n = 0; + else if (d == 1) + n = 1; + else + n = 2; + } else if (v == 1) + n = 3; + else + n = 4; + } else if (h == 1) { + if (!v) { + if (!d) + n = 5; + else + n = 6; + } else + n = 7; + } else + n = 8; + break; + case 3: + hv = h + v; + if (!d) { + if (!hv) + n = 0; + else if (hv == 1) + n = 1; + else + n = 2; + } else if (d == 1) { + if (!hv) + n = 3; + else if (hv == 1) + n = 4; + else + n = 5; + } else if (d == 2) { + if (!hv) + n = 6; + else + n = 7; + } else + n = 8; + break; + } + return T1_CTXNO_ZC + n; } 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); - if (hc < 0) { - hc = -hc; - vc = -vc; - } - if (!hc) { - if (vc == -1) - n = 1; - else if (!vc) - n = 0; - else - n = 1; - } else if (hc == 1) { - if (vc == -1) - n = 2; - else if (!vc) - n = 3; - else - n = 4; - } - return T1_CTXNO_SC + n; + 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); + if (hc < 0) { + hc = -hc; + vc = -vc; + } + if (!hc) { + if (vc == -1) + n = 1; + else if (!vc) + n = 0; + else + n = 1; + } else if (hc == 1) { + if (vc == -1) + n = 2; + else if (!vc) + n = 3; + else + n = 4; + } + return T1_CTXNO_SC + n; } int t1_init_ctxno_mag(int f) { - int n; - if (!(f & T1_REFINE)) - n = (f & (T1_SIG_OTH)) ? 1 : 0; - else - n = 2; - return T1_CTXNO_MAG + n; + int n; + if (!(f & T1_REFINE)) + n = (f & (T1_SIG_OTH)) ? 1 : 0; + else + n = 2; + return T1_CTXNO_MAG + n; } 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); - if (!hc && !vc) - n = 0; - else - n = (!(hc > 0 || (!hc && vc > 0))); - return n; + 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); + if (!hc && !vc) + n = 0; + else + n = (!(hc > 0 || (!hc && vc > 0))); + return n; } void t1_init_luts() { - int i, j; - double u, v, t; - for (j = 0; j < 4; j++) { - for (i = 0; i < 256; ++i) { - t1_lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j); - } - } - for (i = 0; i < 256; i++) { - t1_lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4); - } - for (j = 0; j < 2; j++) { - for (i = 0; i < 2048; ++i) { - t1_lut_ctxno_mag[(j << 11) + i] = - t1_init_ctxno_mag((j ? T1_REFINE : 0) | i); - } - } + int i, j; + double u, v, t; + for (j = 0; j < 4; j++) { for (i = 0; i < 256; ++i) { - t1_lut_spb[i] = t1_init_spb(i << 4); + t1_lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j); } - /* FIXME FIXME FIXME */ - /* printf("nmsedec luts:\n"); */ - for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) { - 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)); - 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)); + } + for (i = 0; i < 256; i++) { + t1_lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4); + } + for (j = 0; j < 2; j++) { + for (i = 0; i < 2048; ++i) { + t1_lut_ctxno_mag[(j << 11) + i] = + t1_init_ctxno_mag((j ? T1_REFINE : 0) | i); } + } + for (i = 0; i < 256; ++i) { + t1_lut_spb[i] = t1_init_spb(i << 4); + } + /* FIXME FIXME FIXME */ + /* printf("nmsedec luts:\n"); */ + for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) { + 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)); + 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)); + } } diff --git a/libopenjpeg/t2.c b/libopenjpeg/t2.c index bbdbac07..a7cb228e 100644 --- a/libopenjpeg/t2.c +++ b/libopenjpeg/t2.c @@ -45,18 +45,18 @@ extern jmp_buf j2k_error; void t2_putcommacode(int n) { - while (--n >= 0) { - bio_write(1, 1); - } - bio_write(0, 1); + while (--n >= 0) { + bio_write(1, 1); + } + bio_write(0, 1); } int t2_getcommacode() { - int n; - for (n = 0; bio_read(1); n++) { - } - return n; + int n; + for (n = 0; bio_read(1); n++) { + } + return n; } /* */ @@ -65,31 +65,31 @@ int t2_getcommacode() /* <\summary> */ void t2_putnumpasses(int n) { - if (n == 1) { - bio_write(0, 1); - } else if (n == 2) { - bio_write(2, 2); - } else if (n <= 5) { - bio_write(0xc | (n - 3), 4); - } else if (n <= 36) { - bio_write(0x1e0 | (n - 6), 9); - } else if (n <= 164) { - bio_write(0xff80 | (n - 37), 16); - } + if (n == 1) { + bio_write(0, 1); + } else if (n == 2) { + bio_write(2, 2); + } else if (n <= 5) { + bio_write(0xc | (n - 3), 4); + } else if (n <= 36) { + bio_write(0x1e0 | (n - 6), 9); + } else if (n <= 164) { + bio_write(0xff80 | (n - 37), 16); + } } int t2_getnumpasses() { - int n; - if (!bio_read(1)) - return 1; - if (!bio_read(1)) - return 2; - if ((n = bio_read(2)) != 3) - return 3 + n; - if ((n = bio_read(5)) != 31) - return 6 + n; - return 37 + bio_read(7); + int n; + if (!bio_read(1)) + return 1; + if (!bio_read(1)) + return 2; + if ((n = bio_read(2)) != 3) + return 3 + n; + if ((n = bio_read(5)) != 31) + return 6 + n; + return 37 + bio_read(7); } /* @@ -106,173 +106,183 @@ int t2_getnumpasses() * 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 t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno, + int resno, int precno, int layno, unsigned char *dest, int len, info_image * info_IM, int tileno) { - int bandno, cblkno; - unsigned char *sop = 0, *eph = 0; - tcd_tilecomp_t *tilec = &tile->comps[compno]; - tcd_resolution_t *res = &tilec->resolutions[resno]; - unsigned char *c = dest; + int bandno, cblkno; + unsigned char *sop = 0, *eph = 0; + tcd_tilecomp_t *tilec = &tile->comps[compno]; + tcd_resolution_t *res = &tilec->resolutions[resno]; + unsigned char *c = dest; + + /* */ + if (tcp->csty & J2K_CP_CSTY_SOP) { + sop = (unsigned char *) malloc(6 * sizeof(unsigned char)); + sop[0] = 255; + sop[1] = 145; + sop[2] = 0; + sop[3] = 4; + sop[4] = (info_IM->num % 65536) / 256; + sop[5] = (info_IM->num % 65536) % 256; + memcpy(c, sop, 6); + free(sop); + c += 6; + } + /* */ - /* */ - if (tcp->csty & J2K_CP_CSTY_SOP) { - sop = (unsigned char *) malloc(6 * sizeof(unsigned char)); - sop[0] = 255; - sop[1] = 145; - sop[2] = 0; - sop[3] = 4; - sop[4] = (info_IM->num % 65536) / 256; - sop[5] = (info_IM->num % 65536) % 256; - memcpy(c, sop, 6); - free(sop); - c += 6; - } - /* */ - - if (!layno) { - for (bandno = 0; bandno < res->numbands; bandno++) { - tcd_band_t *band = &res->bands[bandno]; - tcd_precinct_t *prc = &band->precincts[precno]; - tgt_reset(prc->incltree); - tgt_reset(prc->imsbtree); - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - tcd_cblk_t *cblk = &prc->cblks[cblkno]; - cblk->numpasses = 0; - tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps); - } - } + if (!layno) { + for (bandno = 0; bandno < res->numbands; bandno++) { + tcd_band_t *band = &res->bands[bandno]; + tcd_precinct_t *prc = &band->precincts[precno]; + tgt_reset(prc->incltree); + tgt_reset(prc->imsbtree); + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + tcd_cblk_t *cblk = &prc->cblks[cblkno]; + cblk->numpasses = 0; + tgt_setvalue(prc->imsbtree, cblkno, + band->numbps - cblk->numbps); + } } + } - bio_init_enc(c, len); - bio_write(1, 1); /* Empty header bit */ - - /* Writing Packet header */ - for (bandno = 0; bandno < res->numbands; bandno++) { - tcd_band_t *band = &res->bands[bandno]; - tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - tcd_cblk_t *cblk = &prc->cblks[cblkno]; - tcd_layer_t *layer = &cblk->layers[layno]; - if (!cblk->numpasses && layer->numpasses) { - tgt_setvalue(prc->incltree, cblkno, layno); - } + bio_init_enc(c, len); + bio_write(1, 1); /* Empty header bit */ + + /* Writing Packet header */ + for (bandno = 0; bandno < res->numbands; bandno++) { + tcd_band_t *band = &res->bands[bandno]; + tcd_precinct_t *prc = &band->precincts[precno]; + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + tcd_cblk_t *cblk = &prc->cblks[cblkno]; + tcd_layer_t *layer = &cblk->layers[layno]; + if (!cblk->numpasses && layer->numpasses) { + tgt_setvalue(prc->incltree, cblkno, layno); + } + } + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + tcd_cblk_t *cblk = &prc->cblks[cblkno]; + tcd_layer_t *layer = &cblk->layers[layno]; + int increment = 0; + int nump = 0; + int len = 0, passno; + /* cblk inclusion bits */ + if (!cblk->numpasses) { + tgt_encode(prc->incltree, cblkno, layno + 1); + } else { + bio_write(layer->numpasses != 0, 1); + } + /* if cblk not included, go to the next cblk */ + if (!layer->numpasses) { + continue; + } + /* if first instance of cblk --> zero bit-planes information */ + if (!cblk->numpasses) { + cblk->numlenbits = 3; + tgt_encode(prc->imsbtree, cblkno, 999); + } + /* number of coding passes included */ + t2_putnumpasses(layer->numpasses); + + /* computation of the increase of the length indicator and insertion in the header */ + for (passno = cblk->numpasses; + passno < cblk->numpasses + layer->numpasses; passno++) { + tcd_pass_t *pass = &cblk->passes[passno]; + nump++; + len += pass->len; + if (pass->term + || passno == + (cblk->numpasses + layer->numpasses) - 1) { + increment = + int_max(increment, + int_floorlog2(len) + 1 - + (cblk->numlenbits + int_floorlog2(nump))); + len = 0; + nump = 0; } - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - tcd_cblk_t *cblk = &prc->cblks[cblkno]; - tcd_layer_t *layer = &cblk->layers[layno]; - int increment = 0; - int nump = 0; - int len = 0, passno; - /* cblk inclusion bits */ - if (!cblk->numpasses) { - tgt_encode(prc->incltree, cblkno, layno + 1); - } else { - bio_write(layer->numpasses != 0, 1); - } - /* if cblk not included, go to the next cblk */ - if (!layer->numpasses) { - continue; - } - /* if first instance of cblk --> zero bit-planes information */ - if (!cblk->numpasses) { - cblk->numlenbits = 3; - tgt_encode(prc->imsbtree, cblkno, 999); - } - /* number of coding passes included */ - t2_putnumpasses(layer->numpasses); - - /* computation of the increase of the length indicator and insertion in the header */ - for (passno = cblk->numpasses; - passno < cblk->numpasses + layer->numpasses; passno++) { - tcd_pass_t *pass = &cblk->passes[passno]; - nump++; - len += pass->len; - if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { - increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump))); - len = 0; - nump = 0; - } - } - t2_putcommacode(increment); - /* computation of the new Length indicator */ - cblk->numlenbits += increment; - /* insertion of the codeword segment length */ - - for (passno = cblk->numpasses; - passno < cblk->numpasses + layer->numpasses; passno++) { - tcd_pass_t *pass = &cblk->passes[passno]; - nump++; - len += pass->len; - if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { - bio_write(len, cblk->numlenbits + int_floorlog2(nump)); - len = 0; - nump = 0; - } - } + } + t2_putcommacode(increment); + /* computation of the new Length indicator */ + cblk->numlenbits += increment; + /* insertion of the codeword segment length */ + + for (passno = cblk->numpasses; + passno < cblk->numpasses + layer->numpasses; passno++) { + tcd_pass_t *pass = &cblk->passes[passno]; + nump++; + len += pass->len; + if (pass->term + || passno == + (cblk->numpasses + layer->numpasses) - 1) { + bio_write(len, cblk->numlenbits + int_floorlog2(nump)); + len = 0; + nump = 0; } + } } + } - if (bio_flush()) - return -999; /* modified to eliminate longjmp !! */ + if (bio_flush()) + return -999; /* modified to eliminate longjmp !! */ - c += bio_numbytes(); - - /* */ - if (tcp->csty & J2K_CP_CSTY_EPH) { - eph = (unsigned char *) malloc(2 * sizeof(unsigned char)); - eph[0] = 255; - eph[1] = 146; - memcpy(c, eph, 2); - free(eph); - c += 2; - } - /* */ + c += bio_numbytes(); - /* Writing the packet body */ + /* */ + if (tcp->csty & J2K_CP_CSTY_EPH) { + eph = (unsigned char *) malloc(2 * sizeof(unsigned char)); + eph[0] = 255; + eph[1] = 146; + memcpy(c, eph, 2); + free(eph); + c += 2; + } + /* */ + + /* Writing the packet body */ + + for (bandno = 0; bandno < res->numbands; bandno++) { + tcd_band_t *band = &res->bands[bandno]; + tcd_precinct_t *prc = &band->precincts[precno]; + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + tcd_cblk_t *cblk = &prc->cblks[cblkno]; + tcd_layer_t *layer = &cblk->layers[layno]; + if (!layer->numpasses) { + continue; + } + if (c + layer->len > dest + len) { + return -999; + } - for (bandno = 0; bandno < res->numbands; bandno++) { - tcd_band_t *band = &res->bands[bandno]; - tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - tcd_cblk_t *cblk = &prc->cblks[cblkno]; - tcd_layer_t *layer = &cblk->layers[layno]; - if (!layer->numpasses) { - continue; - } - if (c + layer->len > dest + len) { - return -999; - } - - memcpy(c, layer->data, layer->len); - cblk->numpasses += layer->numpasses; - c += layer->len; - /* ADD for index Cfr. Marcela --> delta disto by packet */ - if (info_IM->index_write && info_IM->index_on) { - info_tile *info_TL = &info_IM->tile[tileno]; - info_packet *info_PK = &info_TL->packet[info_IM->num]; - info_PK->disto += layer->disto; - if (info_IM->D_max < info_PK->disto) - info_IM->D_max = info_PK->disto; - } /* */ - } + memcpy(c, layer->data, layer->len); + cblk->numpasses += layer->numpasses; + c += layer->len; + /* ADD for index Cfr. Marcela --> delta disto by packet */ + if (info_IM->index_write && info_IM->index_on) { + info_tile *info_TL = &info_IM->tile[tileno]; + info_packet *info_PK = &info_TL->packet[info_IM->num]; + info_PK->disto += layer->disto; + if (info_IM->D_max < info_PK->disto) + info_IM->D_max = info_PK->disto; + } /* */ } - return c - dest; + } + return c - dest; } void t2_init_seg(tcd_seg_t * seg, int cblksty, int first) { - seg->numpasses = 0; - seg->len = 0; - if (cblksty & J2K_CCP_CBLKSTY_TERMALL) - seg->maxpasses = 1; - else if (cblksty & J2K_CCP_CBLKSTY_LAZY) { - if (first) - seg->maxpasses = 10; - else - seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1; - } else - seg->maxpasses = 109; + seg->numpasses = 0; + seg->len = 0; + if (cblksty & J2K_CCP_CBLKSTY_TERMALL) + seg->maxpasses = 1; + else if (cblksty & J2K_CCP_CBLKSTY_LAZY) { + if (first) + seg->maxpasses = 10; + else + seg->maxpasses = (((seg - 1)->maxpasses == 1) + || ((seg - 1)->maxpasses == 10)) ? 2 : 1; + } else + seg->maxpasses = 109; } /* @@ -288,113 +298,103 @@ void t2_init_seg(tcd_seg_t * seg, int cblksty, int first) * 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 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; - 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");} - c += 6; + + /* 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"); } - bio_init_dec(c, src + len - c); + c += 6; } - } - - 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; + bio_init_dec(c, src + len - c); } - - /* PPT and PPM dealing */ - if (cp->ppm == 1) /* PPM */ - { - cp->ppm_data=c; - return 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; + if (tcp->ppt == 1) { /* PPT */ + tcp->ppt_data = c; + return 0; } - return c - src; + 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 */ + 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); + included = bio_read(1); } - /* if cblk not included */ - if (!included) - { - cblk->numnewpasses = 0; - continue; + /* 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; + /* 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(); @@ -402,105 +402,109 @@ int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile, j2k_cp_t * /* length indicator increment */ cblk->numlenbits += increment; if (!cblk->numsegs) { - seg = &cblk->segs[0]; - t2_init_seg(seg, tcp->tccps[compno].cblksty, 1); + 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); - } + 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); - } + 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; + } + if (bio_inalign()) + return -999; - c += bio_numbytes(); + 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"); } - c += 2; + if (tcp->csty & J2K_CP_CSTY_EPH) { /* EPH marker */ + if ((*c) != 255 || (*(c + 1) != 146)) { + printf("Error : expected EPH marker [2]!!!\n"); } - - /* 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 += 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); - } 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); + } + 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]; + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + tcd_cblk_t *cblk = &prc->cblks[cblkno]; + tcd_seg_t *seg; + if (!cblk->numnewpasses) + continue; + if (!cblk->numsegs) { + seg = &cblk->segs[cblk->numsegs++]; + cblk->len = 0; + } else { + seg = &cblk->segs[cblk->numsegs - 1]; + if (seg->numpasses == seg->maxpasses) { + seg++; + cblk->numsegs++; } } - - for (bandno = 0; bandno < res->numbands; bandno++) { - tcd_band_t *band = &res->bands[bandno]; - tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - tcd_cblk_t *cblk = &prc->cblks[cblkno]; - tcd_seg_t *seg; - if (!cblk->numnewpasses) - continue; - if (!cblk->numsegs) { - seg = &cblk->segs[cblk->numsegs++]; - cblk->len = 0; - } else { - seg = &cblk->segs[cblk->numsegs - 1]; - if (seg->numpasses == seg->maxpasses) { - seg++; - cblk->numsegs++; - } - } - do { - if (c + seg->newlen > src + len){ - return -999;} - - memcpy(cblk->data + cblk->len, c, seg->newlen); - if (seg->numpasses == 0) { - seg->data = cblk->data + cblk->len; - } - c += seg->newlen; - cblk->len += seg->newlen; - seg->len += seg->newlen; - seg->numpasses += seg->numnewpasses; - cblk->numnewpasses -= seg->numnewpasses; - if (cblk->numnewpasses > 0) { - seg++; - cblk->numsegs++; - } - } while (cblk->numnewpasses > 0); + do { + if (c + seg->newlen > src + len) { + return -999; + } + + memcpy(cblk->data + cblk->len, c, seg->newlen); + if (seg->numpasses == 0) { + seg->data = cblk->data + cblk->len; + } + c += seg->newlen; + cblk->len += seg->newlen; + seg->len += seg->newlen; + seg->numpasses += seg->numnewpasses; + cblk->numnewpasses -= seg->numnewpasses; + if (cblk->numnewpasses > 0) { + seg++; + cblk->numsegs++; } + } while (cblk->numnewpasses > 0); } + } - return c - src; + return c - src; } @@ -517,60 +521,63 @@ int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile, j2k_cp_t * * 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) { - unsigned char *c = dest; - int e = 0; - pi_iterator_t *pi; - int pino, compno; - - pi = pi_create(img, cp, tileno); - - for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) { - while (pi_next(&pi[pino])) { - if (pi[pino].layno < maxlayers) { - e = t2_encode_packet(tile, &cp->tcps[tileno], pi[pino].compno, - pi[pino].resno, pi[pino].precno, pi[pino].layno, - c, dest + len - c, info_IM, tileno); - if (e == -999) { - break; - } else - c += e; - /* INDEX >> */ - if (info_IM->index_write && info_IM->index_on) { - info_tile *info_TL = &info_IM->tile[tileno]; - info_packet *info_PK = &info_TL->packet[info_IM->num]; - if (!info_IM->num) { - info_PK->start_pos = info_TL->end_header + 1; - } else { - info_PK->start_pos = - info_TL->packet[info_IM->num - 1].end_pos + 1; - } - info_PK->end_pos = info_PK->start_pos + e - 1; - - } - /* << INDEX */ - if ((info_IM->index_write - && cp->tcps[tileno].csty & J2K_CP_CSTY_SOP) - || (info_IM->index_write && info_IM->index_on)) { - info_IM->num++; - } - } + unsigned char *c = dest; + int e = 0; + pi_iterator_t *pi; + int pino, compno; + + pi = pi_create(img, cp, tileno); + + for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) { + while (pi_next(&pi[pino])) { + if (pi[pino].layno < maxlayers) { + e = t2_encode_packet(tile, &cp->tcps[tileno], + pi[pino].compno, pi[pino].resno, + pi[pino].precno, pi[pino].layno, c, + dest + len - c, info_IM, tileno); + if (e == -999) { + break; + } else + c += e; + /* INDEX >> */ + if (info_IM->index_write && info_IM->index_on) { + info_tile *info_TL = &info_IM->tile[tileno]; + info_packet *info_PK = &info_TL->packet[info_IM->num]; + if (!info_IM->num) { + info_PK->start_pos = info_TL->end_header + 1; + } else { + info_PK->start_pos = + info_TL->packet[info_IM->num - 1].end_pos + 1; + } + info_PK->end_pos = info_PK->start_pos + e - 1; } - - /* FREE space memory taken by pi */ - for (compno = 0; compno < pi[pino].numcomps; compno++) { - free(pi[pino].comps[compno].resolutions); + /* << INDEX */ + if ((info_IM->index_write + && cp->tcps[tileno].csty & J2K_CP_CSTY_SOP) + || (info_IM->index_write && info_IM->index_on)) { + info_IM->num++; } - free(pi[pino].comps); + } + } - free(pi[0].include); - free(pi); - if (e == -999) - return e; - else - return c - dest; + + /* 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 - dest; } @@ -585,42 +592,49 @@ int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno, tcd_tile_t * * 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) { - 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); - - /* 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) { /* 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); + 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); + + /* 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) { /* 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[pino].comps); - } - free(pi[0].include); - free(pi); - - if (e == -999) - return e; - else - return c - src; + free(pi[0].include); + free(pi); + + if (e == -999) + return e; + else + return c - src; } diff --git a/libopenjpeg/t2.h b/libopenjpeg/t2.h index 79ccf993..795fb53f 100644 --- a/libopenjpeg/t2.h +++ b/libopenjpeg/t2.h @@ -44,7 +44,9 @@ * 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 @@ -56,6 +58,7 @@ int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno, tcd_tile_t * * 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 diff --git a/libopenjpeg/tcd.c b/libopenjpeg/tcd.c index ab4fdb17..c97cc620 100644 --- a/libopenjpeg/tcd.c +++ b/libopenjpeg/tcd.c @@ -60,1263 +60,1548 @@ extern jmp_buf j2k_error; void tcd_dump(tcd_image_t * img, int curtileno) { - int tileno, compno, resno, bandno, precno, cblkno; - /* fprintf(stderr, "image {\n"); */ - fprintf(stderr, " tw=%d, th=%d x0 %d x1 %d\n", img->tw, img->th, - tcd_img->x0, tcd_img->x1); - for (tileno = 0; tileno < 1; tileno++) { - tcd_tile_t *tile = &tcd_image.tiles[curtileno]; - /* fprintf(stderr, " tile {\n"); */ - /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n", tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps); */ - for (compno = 0; compno < tile->numcomps; compno++) { - tcd_tilecomp_t *tilec = &tile->comps[compno]; - /* fprintf(stderr, " tilec {\n"); */ - /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n", tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions); */ - for (resno = 0; resno < tilec->numresolutions; resno++) { - tcd_resolution_t *res = &tilec->resolutions[resno]; - /* fprintf(stderr, "\n res {\n"); */ - /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n", res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands); */ - for (bandno = 0; bandno < res->numbands; bandno++) { - tcd_band_t *band = &res->bands[bandno]; - /* fprintf(stderr, " band {\n"); */ - /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%d, numbps=%d\n", band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps); */ - for (precno = 0; precno < res->pw * res->ph; precno++) { - tcd_precinct_t *prec = &band->precincts[precno]; - /* fprintf(stderr, " prec {\n"); */ - /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n", prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch); */ - for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) { - /* tcd_cblk_t *cblk=&prec->cblks[cblkno]; */ - /* fprintf(stderr, " cblk {\n"); */ - /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d\n", cblk->x0, cblk->y0, cblk->x1, cblk->y1); */ - /* fprintf(stderr, " }\n"); */ - } - /* fprintf(stderr, " }\n"); */ - } - /* fprintf(stderr, " }\n"); */ - } - /* fprintf(stderr, " }\n"); */ + int tileno, compno, resno, bandno, precno, cblkno; + /* fprintf(stderr, "image {\n"); */ + fprintf(stderr, " tw=%d, th=%d x0 %d x1 %d\n", img->tw, img->th, + tcd_img->x0, tcd_img->x1); + for (tileno = 0; tileno < 1; tileno++) { + tcd_tile_t *tile = &tcd_image.tiles[curtileno]; + /* fprintf(stderr, " tile {\n"); */ + /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n", tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps); */ + for (compno = 0; compno < tile->numcomps; compno++) { + tcd_tilecomp_t *tilec = &tile->comps[compno]; + /* fprintf(stderr, " tilec {\n"); */ + /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n", tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions); */ + for (resno = 0; resno < tilec->numresolutions; resno++) { + tcd_resolution_t *res = &tilec->resolutions[resno]; + /* fprintf(stderr, "\n res {\n"); */ + /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n", res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands); */ + for (bandno = 0; bandno < res->numbands; bandno++) { + tcd_band_t *band = &res->bands[bandno]; + /* fprintf(stderr, " band {\n"); */ + /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%d, numbps=%d\n", band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps); */ + for (precno = 0; precno < res->pw * res->ph; precno++) { + tcd_precinct_t *prec = &band->precincts[precno]; + /* fprintf(stderr, " prec {\n"); */ + /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n", prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch); */ + for (cblkno = 0; cblkno < prec->cw * prec->ch; + cblkno++) { + /* tcd_cblk_t *cblk=&prec->cblks[cblkno]; */ + /* fprintf(stderr, " cblk {\n"); */ + /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d\n", cblk->x0, cblk->y0, cblk->x1, cblk->y1); */ + /* fprintf(stderr, " }\n"); */ } - /* fprintf(stderr, " }\n"); */ + /* fprintf(stderr, " }\n"); */ + } + /* fprintf(stderr, " }\n"); */ } - /* fprintf(stderr, " }\n"); */ + /* fprintf(stderr, " }\n"); */ + } + /* fprintf(stderr, " }\n"); */ } - /* fprintf(stderr, "}\n"); */ + /* fprintf(stderr, " }\n"); */ + } + /* fprintf(stderr, "}\n"); */ } void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno) { - int tileno, compno, resno, bandno, precno, cblkno; - tcd_img = img; - tcd_cp = cp; - tcd_image.tw = cp->tw; - tcd_image.th = cp->th; - tcd_image.tiles = (tcd_tile_t *) malloc(sizeof(tcd_tile_t)); - - for (tileno = 0; tileno < 1; tileno++) { - j2k_tcp_t *tcp = &cp->tcps[curtileno]; - int j; - /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ - int p = curtileno % cp->tw; /* si numerotation matricielle .. */ - int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */ - /* tcd_tile_t *tile=&tcd_image.tiles[tileno]; */ - tile = tcd_image.tiles; - /* 4 borders of the tile rescale on the image if necessary */ - tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0); - tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0); - tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1); - tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1); - tile->numcomps = img->numcomps; - /* tile->PPT=img->PPT; */ - /* Modification of the RATE >> */ - for (j = 0; j < tcp->numlayers; j++) { - tcp->rates[j] = ceil(tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * img->comps[0].prec / (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy)); - if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) { - tcp->rates[j] = tcp->rates[j - 1] + 20; - } else { - if (!j && tcp->rates[j] < 30) - tcp->rates[j] = 30; - } + int tileno, compno, resno, bandno, precno, cblkno; + tcd_img = img; + tcd_cp = cp; + tcd_image.tw = cp->tw; + tcd_image.th = cp->th; + tcd_image.tiles = (tcd_tile_t *) malloc(sizeof(tcd_tile_t)); + + for (tileno = 0; tileno < 1; tileno++) { + j2k_tcp_t *tcp = &cp->tcps[curtileno]; + int j; + /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ + int p = curtileno % cp->tw; /* si numerotation matricielle .. */ + int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */ + /* tcd_tile_t *tile=&tcd_image.tiles[tileno]; */ + tile = tcd_image.tiles; + /* 4 borders of the tile rescale on the image if necessary */ + tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0); + tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0); + tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1); + tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1); + tile->numcomps = img->numcomps; + /* tile->PPT=img->PPT; */ + /* Modification of the RATE >> */ + for (j = 0; j < tcp->numlayers; j++) { + tcp->rates[j] = + ceil(tile->numcomps * (tile->x1 - tile->x0) * + (tile->y1 - + tile->y0) * img->comps[0].prec / (tcp->rates[j] * 8 * + img->comps[0].dx * + img->comps[0].dy)); + if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) { + tcp->rates[j] = tcp->rates[j - 1] + 20; + } else { + if (!j && tcp->rates[j] < 30) + tcp->rates[j] = 30; + } + } + /* << Modification of the RATE */ + + tile->comps = + (tcd_tilecomp_t *) malloc(img->numcomps * + sizeof(tcd_tilecomp_t)); + for (compno = 0; compno < tile->numcomps; compno++) { + j2k_tccp_t *tccp = &tcp->tccps[compno]; + /* tcd_tilecomp_t *tilec=&tile->comps[compno]; */ + tilec = &tile->comps[compno]; + /* border of each tile component (global) */ + tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx); + + tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy); + tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx); + tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy); + + 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++) { + int pdx, pdy; + int levelno = tilec->numresolutions - 1 - resno; + int tlprcxstart, tlprcystart, brprcxend, brprcyend; + int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; + int cbgwidthexpn, cbgheightexpn; + int cblkwidthexpn, cblkheightexpn; + /* tcd_resolution_t *res=&tilec->resolutions[resno]; */ + + res = &tilec->resolutions[resno]; + + /* border for each resolution level (global) */ + res->x0 = int_ceildivpow2(tilec->x0, levelno); + res->y0 = int_ceildivpow2(tilec->y0, levelno); + res->x1 = int_ceildivpow2(tilec->x1, levelno); + res->y1 = int_ceildivpow2(tilec->y1, levelno); + + res->numbands = resno == 0 ? 1 : 3; + /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ + if (tccp->csty & J2K_CCP_CSTY_PRT) { + pdx = tccp->prcw[resno]; + pdy = tccp->prch[resno]; + } else { + pdx = 15; + pdy = 15; + } + /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ + tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; + tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; + brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; + brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; + + res->pw = (brprcxend - tlprcxstart) >> pdx; + res->ph = (brprcyend - tlprcystart) >> pdy; + + if (resno == 0) { + tlcbgxstart = tlprcxstart; + tlcbgystart = tlprcystart; + brcbgxend = brprcxend; + brcbgyend = brprcyend; + cbgwidthexpn = pdx; + cbgheightexpn = pdy; + } else { + tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); + tlcbgystart = int_ceildivpow2(tlprcystart, 1); + brcbgxend = int_ceildivpow2(brprcxend, 1); + brcbgyend = int_ceildivpow2(brprcyend, 1); + cbgwidthexpn = pdx - 1; + cbgheightexpn = pdy - 1; } - /* << Modification of the RATE */ - - tile->comps = (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t)); - for (compno = 0; compno < tile->numcomps; compno++) { - j2k_tccp_t *tccp = &tcp->tccps[compno]; - /* tcd_tilecomp_t *tilec=&tile->comps[compno]; */ - tilec = &tile->comps[compno]; - /* border of each tile component (global) */ - tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx); - - tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy); - tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx); - tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy); - - 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++) { - int pdx, pdy; - int levelno = tilec->numresolutions - 1 - resno; - int tlprcxstart, tlprcystart, brprcxend, brprcyend; - int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; - int cbgwidthexpn, cbgheightexpn; - int cblkwidthexpn, cblkheightexpn; - /* tcd_resolution_t *res=&tilec->resolutions[resno]; */ - - res = &tilec->resolutions[resno]; - - /* border for each resolution level (global) */ - res->x0 = int_ceildivpow2(tilec->x0, levelno); - res->y0 = int_ceildivpow2(tilec->y0, levelno); - res->x1 = int_ceildivpow2(tilec->x1, levelno); - res->y1 = int_ceildivpow2(tilec->y1, levelno); - - res->numbands = resno == 0 ? 1 : 3; - /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ - if (tccp->csty & J2K_CCP_CSTY_PRT) { - pdx = tccp->prcw[resno]; - pdy = tccp->prch[resno]; - } else { - pdx = 15; - pdy = 15; - } - /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ - tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; - tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; - brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; - brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; - - res->pw = (brprcxend - tlprcxstart) >> pdx; - res->ph = (brprcyend - tlprcystart) >> pdy; - - if (resno == 0) { - tlcbgxstart = tlprcxstart; - tlcbgystart = tlprcystart; - brcbgxend = brprcxend; - brcbgyend = brprcyend; - cbgwidthexpn = pdx; - cbgheightexpn = pdy; - } else { - tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); - tlcbgystart = int_ceildivpow2(tlprcystart, 1); - brcbgxend = int_ceildivpow2(brprcxend, 1); - brcbgyend = int_ceildivpow2(brprcyend, 1); - cbgwidthexpn = pdx - 1; - cbgheightexpn = pdy - 1; - } - - cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn); - cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn); - - for (bandno = 0; bandno < res->numbands; bandno++) { - int x0b, y0b, i; - int gain, numbps; - j2k_stepsize_t *ss; - band = &res->bands[bandno]; - band->bandno = resno == 0 ? 0 : bandno + 1; - x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0; - y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0; - - if (band->bandno == 0) { - /* band border (global) */ - band->x0 = int_ceildivpow2(tilec->x0, levelno); - band->y0 = int_ceildivpow2(tilec->y0, levelno); - band->x1 = int_ceildivpow2(tilec->x1, levelno); - band->y1 = int_ceildivpow2(tilec->y1, levelno); - } else { - /* band border (global) */ - band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1); - band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1); - band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1); - band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1); - - } - - ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1]; - gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno); - numbps = img->comps[compno].prec + gain; - band->stepsize = (int) floor((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn) * 8192.0); - band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ - - band->precincts = (tcd_precinct_t *) malloc(3 * res->pw * res->ph * sizeof(tcd_precinct_t)); - - for (i = 0; i < res->pw * res->ph * 3; i++) { - band->precincts[i].imsbtree = NULL; - band->precincts[i].incltree = NULL; - } - - for (precno = 0; precno < res->pw * res->ph; precno++) { - int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; - int cbgxstart = - tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn); - int cbgystart = - tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn); - int cbgxend = cbgxstart + (1 << cbgwidthexpn); - int cbgyend = cbgystart + (1 << cbgheightexpn); - /* tcd_precinct_t *prc=&band->precincts[precno]; */ - prc = &band->precincts[precno]; - /* precinct size (global) */ - prc->x0 = int_max(cbgxstart, band->x0); - prc->y0 = int_max(cbgystart, band->y0); - prc->x1 = int_min(cbgxend, band->x1); - prc->y1 = int_min(cbgyend, band->y1); - - tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn; - tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn; - brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn; - brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn; - prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; - prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; - - prc->cblks = (tcd_cblk_t *) malloc((prc->cw * prc->ch) * sizeof(tcd_cblk_t)); - prc->incltree = tgt_create(prc->cw, prc->ch); - prc->imsbtree = tgt_create(prc->cw, prc->ch); - - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn); - int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn); - int cblkxend = cblkxstart + (1 << cblkwidthexpn); - int cblkyend = cblkystart + (1 << cblkheightexpn); - - cblk = &prc->cblks[cblkno]; - /* code-block size (global) */ - cblk->x0 = int_max(cblkxstart, prc->x0); - cblk->y0 = int_max(cblkystart, prc->y0); - cblk->x1 = int_min(cblkxend, prc->x1); - cblk->y1 = int_min(cblkyend, prc->y1); - } - } - } + + cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn); + cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn); + + for (bandno = 0; bandno < res->numbands; bandno++) { + int x0b, y0b, i; + int gain, numbps; + j2k_stepsize_t *ss; + band = &res->bands[bandno]; + band->bandno = resno == 0 ? 0 : bandno + 1; + x0b = (band->bandno == 1) + || (band->bandno == 3) ? 1 : 0; + y0b = (band->bandno == 2) + || (band->bandno == 3) ? 1 : 0; + + if (band->bandno == 0) { + /* band border (global) */ + band->x0 = int_ceildivpow2(tilec->x0, levelno); + band->y0 = int_ceildivpow2(tilec->y0, levelno); + band->x1 = int_ceildivpow2(tilec->x1, levelno); + band->y1 = int_ceildivpow2(tilec->y1, levelno); + } else { + /* band border (global) */ + band->x0 = + int_ceildivpow2(tilec->x0 - + (1 << levelno) * x0b, + levelno + 1); + band->y0 = + int_ceildivpow2(tilec->y0 - + (1 << levelno) * y0b, + levelno + 1); + band->x1 = + int_ceildivpow2(tilec->x1 - + (1 << levelno) * x0b, + levelno + 1); + band->y1 = + int_ceildivpow2(tilec->y1 - + (1 << levelno) * y0b, + levelno + 1); + + } + + ss = &tccp->stepsizes[resno == + 0 ? 0 : 3 * (resno - 1) + + bandno + 1]; + gain = + tccp->qmfbid == + 0 ? dwt_getgain_real(band-> + bandno) : dwt_getgain(band-> + bandno); + numbps = img->comps[compno].prec + gain; + band->stepsize = + (int) floor((1.0 + ss->mant / 2048.0) * + pow(2.0, numbps - ss->expn) * 8192.0); + band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ + + band->precincts = + (tcd_precinct_t *) malloc(3 * res->pw * res->ph * + sizeof(tcd_precinct_t)); + + for (i = 0; i < res->pw * res->ph * 3; i++) { + band->precincts[i].imsbtree = NULL; + band->precincts[i].incltree = NULL; + } + + for (precno = 0; precno < res->pw * res->ph; precno++) { + int tlcblkxstart, tlcblkystart, brcblkxend, + brcblkyend; + int cbgxstart = + tlcbgxstart + + (precno % res->pw) * (1 << cbgwidthexpn); + int cbgystart = + tlcbgystart + + (precno / res->pw) * (1 << cbgheightexpn); + int cbgxend = cbgxstart + (1 << cbgwidthexpn); + int cbgyend = cbgystart + (1 << cbgheightexpn); + /* tcd_precinct_t *prc=&band->precincts[precno]; */ + prc = &band->precincts[precno]; + /* precinct size (global) */ + prc->x0 = int_max(cbgxstart, band->x0); + prc->y0 = int_max(cbgystart, band->y0); + prc->x1 = int_min(cbgxend, band->x1); + prc->y1 = int_min(cbgyend, band->y1); + + tlcblkxstart = + int_floordivpow2(prc->x0, + cblkwidthexpn) << + cblkwidthexpn; + tlcblkystart = + int_floordivpow2(prc->y0, + cblkheightexpn) << + cblkheightexpn; + brcblkxend = + int_ceildivpow2(prc->x1, + cblkwidthexpn) << + cblkwidthexpn; + brcblkyend = + int_ceildivpow2(prc->y1, + cblkheightexpn) << + cblkheightexpn; + prc->cw = + (brcblkxend - tlcblkxstart) >> cblkwidthexpn; + prc->ch = + (brcblkyend - tlcblkystart) >> cblkheightexpn; + + prc->cblks = + (tcd_cblk_t *) malloc((prc->cw * prc->ch) * + sizeof(tcd_cblk_t)); + prc->incltree = tgt_create(prc->cw, prc->ch); + prc->imsbtree = tgt_create(prc->cw, prc->ch); + + for (cblkno = 0; cblkno < prc->cw * prc->ch; + cblkno++) { + int cblkxstart = + tlcblkxstart + + (cblkno % prc->cw) * (1 << cblkwidthexpn); + int cblkystart = + tlcblkystart + + (cblkno / prc->cw) * (1 << cblkheightexpn); + int cblkxend = + cblkxstart + (1 << cblkwidthexpn); + int cblkyend = + cblkystart + (1 << cblkheightexpn); + + cblk = &prc->cblks[cblkno]; + /* code-block size (global) */ + cblk->x0 = int_max(cblkxstart, prc->x0); + cblk->y0 = int_max(cblkystart, prc->y0); + cblk->x1 = int_min(cblkxend, prc->x1); + cblk->y1 = int_min(cblkyend, prc->y1); } + } } + } } - /* tcd_dump(&tcd_image,curtileno); */ + } + /* tcd_dump(&tcd_image,curtileno); */ } void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno) { - int tileno, compno, resno, bandno, precno; - tcd_img = img; - tcd_cp = cp; - tcd_image.tw = cp->tw; - tcd_image.th = cp->th; - for (tileno = 0; tileno < 1; tileno++) { - /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */ - tile = tcd_image.tiles; - 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]; - - if (prc->incltree != NULL) - tgt_destroy(prc->incltree); - if (prc->imsbtree != NULL) - tgt_destroy(prc->imsbtree); - free(prc->cblks); - } /* for (precno */ - free(band->precincts); - } /* for (bandno */ - } /* for (resno */ - free(tilec->resolutions); - } /* for (compno */ - free(tile->comps); - } /* for (tileno */ - free(tcd_image.tiles); + int tileno, compno, resno, bandno, precno; + tcd_img = img; + tcd_cp = cp; + tcd_image.tw = cp->tw; + tcd_image.th = cp->th; + for (tileno = 0; tileno < 1; tileno++) { + /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */ + tile = tcd_image.tiles; + 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]; + + if (prc->incltree != NULL) + tgt_destroy(prc->incltree); + if (prc->imsbtree != NULL) + tgt_destroy(prc->imsbtree); + free(prc->cblks); + } /* for (precno */ + free(band->precincts); + } /* for (bandno */ + } /* for (resno */ + free(tilec->resolutions); + } /* for (compno */ + free(tile->comps); + } /* for (tileno */ + free(tcd_image.tiles); } void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno) { - int tileno, compno, resno, bandno, precno, cblkno; - - for (tileno = 0; tileno < 1; tileno++) { - j2k_tcp_t *tcp = &cp->tcps[curtileno]; - int j; - // int previous_x0, previous_x1, previous_y0, previous_y1; - /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ - int p = curtileno % cp->tw; - int q = curtileno / cp->tw; - tile = tcd_image.tiles; - - /* 4 borders of the tile rescale on the image if necessary */ - tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0); - tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0); - tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1); - tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1); - - tile->numcomps = img->numcomps; - /* tile->PPT=img->PPT; */ - /* Modification of the RATE >> */ - for (j = 0; j < tcp->numlayers; j++) { - tcp->rates[j] = ceil(tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * img->comps[0].prec - / (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy)); - if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) { - tcp->rates[j] = tcp->rates[j - 1] + 20; - } else { - if (!j && tcp->rates[j] < 30) - tcp->rates[j] = 30; - } + int tileno, compno, resno, bandno, precno, cblkno; + + for (tileno = 0; tileno < 1; tileno++) { + j2k_tcp_t *tcp = &cp->tcps[curtileno]; + int j; + // int previous_x0, previous_x1, previous_y0, previous_y1; + /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ + int p = curtileno % cp->tw; + int q = curtileno / cp->tw; + tile = tcd_image.tiles; + + /* 4 borders of the tile rescale on the image if necessary */ + tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0); + tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0); + tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1); + tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1); + + tile->numcomps = img->numcomps; + /* tile->PPT=img->PPT; */ + /* Modification of the RATE >> */ + for (j = 0; j < tcp->numlayers; j++) { + tcp->rates[j] = + ceil(tile->numcomps * (tile->x1 - tile->x0) * + (tile->y1 - + tile->y0) * img->comps[0].prec / (tcp->rates[j] * 8 * + img->comps[0].dx * + img->comps[0].dy)); + if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) { + tcp->rates[j] = tcp->rates[j - 1] + 20; + } else { + if (!j && tcp->rates[j] < 30) + tcp->rates[j] = 30; + } + } + /* << Modification of the RATE */ + /* tile->comps=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */ + for (compno = 0; compno < tile->numcomps; compno++) { + j2k_tccp_t *tccp = &tcp->tccps[compno]; + /* int realloc_op; */ + + tilec = &tile->comps[compno]; + /* border of each tile component (global) */ + tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx); + tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy); + tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx); + tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy); + + 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++) { + int pdx, pdy; + int levelno = tilec->numresolutions - 1 - resno; + int tlprcxstart, tlprcystart, brprcxend, brprcyend; + int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; + int cbgwidthexpn, cbgheightexpn; + int cblkwidthexpn, cblkheightexpn; + + res = &tilec->resolutions[resno]; + /* border for each resolution level (global) */ + res->x0 = int_ceildivpow2(tilec->x0, levelno); + res->y0 = int_ceildivpow2(tilec->y0, levelno); + res->x1 = int_ceildivpow2(tilec->x1, levelno); + res->y1 = int_ceildivpow2(tilec->y1, levelno); + + res->numbands = resno == 0 ? 1 : 3; + /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ + if (tccp->csty & J2K_CCP_CSTY_PRT) { + pdx = tccp->prcw[resno]; + pdy = tccp->prch[resno]; + } else { + pdx = 15; + pdy = 15; } - /* << Modification of the RATE */ - /* tile->comps=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */ - for (compno = 0; compno < tile->numcomps; compno++) { - j2k_tccp_t *tccp = &tcp->tccps[compno]; - /* int realloc_op; */ - - tilec = &tile->comps[compno]; - /* border of each tile component (global) */ - tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx); - tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy); - tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx); - tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy); - - 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++) { - int pdx, pdy; - int levelno = tilec->numresolutions - 1 - resno; - int tlprcxstart, tlprcystart, brprcxend, brprcyend; - int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; - int cbgwidthexpn, cbgheightexpn; - int cblkwidthexpn, cblkheightexpn; - - res = &tilec->resolutions[resno]; - /* border for each resolution level (global) */ - res->x0 = int_ceildivpow2(tilec->x0, levelno); - res->y0 = int_ceildivpow2(tilec->y0, levelno); - res->x1 = int_ceildivpow2(tilec->x1, levelno); - res->y1 = int_ceildivpow2(tilec->y1, levelno); - - res->numbands = resno == 0 ? 1 : 3; - /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ - if (tccp->csty & J2K_CCP_CSTY_PRT) { - pdx = tccp->prcw[resno]; - pdy = tccp->prch[resno]; - } else { - pdx = 15; - pdy = 15; - } - /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ - tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; - tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; - brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; - brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; - - res->pw = (brprcxend - tlprcxstart) >> pdx; - res->ph = (brprcyend - tlprcystart) >> pdy; - - if (resno == 0) { - tlcbgxstart = tlprcxstart; - tlcbgystart = tlprcystart; - brcbgxend = brprcxend; - brcbgyend = brprcyend; - cbgwidthexpn = pdx; - cbgheightexpn = pdy; - } else { - tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); - tlcbgystart = int_ceildivpow2(tlprcystart, 1); - brcbgxend = int_ceildivpow2(brprcxend, 1); - brcbgyend = int_ceildivpow2(brprcyend, 1); - cbgwidthexpn = pdx - 1; - cbgheightexpn = pdy - 1; - } - - cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn); - cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn); - - for (bandno = 0; bandno < res->numbands; bandno++) { - int x0b, y0b; - int gain, numbps; - j2k_stepsize_t *ss; - band = &res->bands[bandno]; - band->bandno = resno == 0 ? 0 : bandno + 1; - x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0; - y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0; - - if (band->bandno == 0) { - /* band border */ - band->x0 = int_ceildivpow2(tilec->x0, levelno); - band->y0 = int_ceildivpow2(tilec->y0, levelno); - band->x1 = int_ceildivpow2(tilec->x1, levelno); - band->y1 = int_ceildivpow2(tilec->y1, levelno); - } else { - band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1); - band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1); - band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1); - band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1); - } - - ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1]; - gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno); - numbps = img->comps[compno].prec + gain; - band->stepsize = (int) floor((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn) * 8192.0); - band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ - - for (precno = 0; precno < res->pw * res->ph; precno++) { - int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; - int cbgxstart = - tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn); - int cbgystart = - tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn); - int cbgxend = cbgxstart + (1 << cbgwidthexpn); - int cbgyend = cbgystart + (1 << cbgheightexpn); - - prc = &band->precincts[precno]; - /* precinct size (global) */ - prc->x0 = int_max(cbgxstart, band->x0); - prc->y0 = int_max(cbgystart, band->y0); - prc->x1 = int_min(cbgxend, band->x1); - prc->y1 = int_min(cbgyend, band->y1); - - tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn; - tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn; - brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn; - brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn; - prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; - prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; - - free(prc->cblks); - prc->cblks = (tcd_cblk_t *) malloc(prc->cw * prc->ch * sizeof(tcd_cblk_t)); - - if (prc->incltree != NULL) - tgt_destroy(prc->incltree); - if (prc->imsbtree != NULL) - tgt_destroy(prc->imsbtree); - - prc->incltree = tgt_create(prc->cw, prc->ch); - prc->imsbtree = tgt_create(prc->cw, prc->ch); - - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn); - int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn); - int cblkxend = cblkxstart + (1 << cblkwidthexpn); - int cblkyend = cblkystart + (1 << cblkheightexpn); - cblk = &prc->cblks[cblkno]; - - /* code-block size (global) */ - cblk->x0 = int_max(cblkxstart, prc->x0); - cblk->y0 = int_max(cblkystart, prc->y0); - cblk->x1 = int_min(cblkxend, prc->x1); - cblk->y1 = int_min(cblkyend, prc->y1); - - } - } - } + /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ + tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; + tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; + brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; + brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; + + res->pw = (brprcxend - tlprcxstart) >> pdx; + res->ph = (brprcyend - tlprcystart) >> pdy; + + if (resno == 0) { + tlcbgxstart = tlprcxstart; + tlcbgystart = tlprcystart; + brcbgxend = brprcxend; + brcbgyend = brprcyend; + cbgwidthexpn = pdx; + cbgheightexpn = pdy; + } else { + tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); + tlcbgystart = int_ceildivpow2(tlprcystart, 1); + brcbgxend = int_ceildivpow2(brprcxend, 1); + brcbgyend = int_ceildivpow2(brprcyend, 1); + cbgwidthexpn = pdx - 1; + cbgheightexpn = pdy - 1; + } + + cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn); + cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn); + + for (bandno = 0; bandno < res->numbands; bandno++) { + int x0b, y0b; + int gain, numbps; + j2k_stepsize_t *ss; + band = &res->bands[bandno]; + band->bandno = resno == 0 ? 0 : bandno + 1; + x0b = (band->bandno == 1) + || (band->bandno == 3) ? 1 : 0; + y0b = (band->bandno == 2) + || (band->bandno == 3) ? 1 : 0; + + if (band->bandno == 0) { + /* band border */ + band->x0 = int_ceildivpow2(tilec->x0, levelno); + band->y0 = int_ceildivpow2(tilec->y0, levelno); + band->x1 = int_ceildivpow2(tilec->x1, levelno); + band->y1 = int_ceildivpow2(tilec->y1, levelno); + } else { + band->x0 = + int_ceildivpow2(tilec->x0 - + (1 << levelno) * x0b, + levelno + 1); + band->y0 = + int_ceildivpow2(tilec->y0 - + (1 << levelno) * y0b, + levelno + 1); + band->x1 = + int_ceildivpow2(tilec->x1 - + (1 << levelno) * x0b, + levelno + 1); + band->y1 = + int_ceildivpow2(tilec->y1 - + (1 << levelno) * y0b, + levelno + 1); + } + + ss = &tccp->stepsizes[resno == + 0 ? 0 : 3 * (resno - 1) + + bandno + 1]; + gain = + tccp->qmfbid == + 0 ? dwt_getgain_real(band-> + bandno) : dwt_getgain(band-> + bandno); + numbps = img->comps[compno].prec + gain; + band->stepsize = + (int) floor((1.0 + ss->mant / 2048.0) * + pow(2.0, numbps - ss->expn) * 8192.0); + band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ + + for (precno = 0; precno < res->pw * res->ph; precno++) { + int tlcblkxstart, tlcblkystart, brcblkxend, + brcblkyend; + int cbgxstart = + tlcbgxstart + + (precno % res->pw) * (1 << cbgwidthexpn); + int cbgystart = + tlcbgystart + + (precno / res->pw) * (1 << cbgheightexpn); + int cbgxend = cbgxstart + (1 << cbgwidthexpn); + int cbgyend = cbgystart + (1 << cbgheightexpn); + + prc = &band->precincts[precno]; + /* precinct size (global) */ + prc->x0 = int_max(cbgxstart, band->x0); + prc->y0 = int_max(cbgystart, band->y0); + prc->x1 = int_min(cbgxend, band->x1); + prc->y1 = int_min(cbgyend, band->y1); + + tlcblkxstart = + int_floordivpow2(prc->x0, + cblkwidthexpn) << + cblkwidthexpn; + tlcblkystart = + int_floordivpow2(prc->y0, + cblkheightexpn) << + cblkheightexpn; + brcblkxend = + int_ceildivpow2(prc->x1, + cblkwidthexpn) << + cblkwidthexpn; + brcblkyend = + int_ceildivpow2(prc->y1, + cblkheightexpn) << + cblkheightexpn; + prc->cw = + (brcblkxend - tlcblkxstart) >> cblkwidthexpn; + prc->ch = + (brcblkyend - tlcblkystart) >> cblkheightexpn; + + free(prc->cblks); + prc->cblks = + (tcd_cblk_t *) malloc(prc->cw * prc->ch * + sizeof(tcd_cblk_t)); + + if (prc->incltree != NULL) + tgt_destroy(prc->incltree); + if (prc->imsbtree != NULL) + tgt_destroy(prc->imsbtree); + + prc->incltree = tgt_create(prc->cw, prc->ch); + prc->imsbtree = tgt_create(prc->cw, prc->ch); + + for (cblkno = 0; cblkno < prc->cw * prc->ch; + cblkno++) { + int cblkxstart = + tlcblkxstart + + (cblkno % prc->cw) * (1 << cblkwidthexpn); + int cblkystart = + tlcblkystart + + (cblkno / prc->cw) * (1 << cblkheightexpn); + int cblkxend = + cblkxstart + (1 << cblkwidthexpn); + int cblkyend = + cblkystart + (1 << cblkheightexpn); + cblk = &prc->cblks[cblkno]; + + /* code-block size (global) */ + cblk->x0 = int_max(cblkxstart, prc->x0); + cblk->y0 = int_max(cblkystart, prc->y0); + cblk->x1 = int_min(cblkxend, prc->x1); + cblk->y1 = int_min(cblkyend, prc->y1); + } + } } + } } - /* tcd_dump(&tcd_image,0); */ + } + /* tcd_dump(&tcd_image,0); */ } void tcd_init(j2k_image_t * img, j2k_cp_t * cp) { - int tileno, compno, resno, bandno, precno, cblkno, i; - unsigned int x0=0 , y0=0, x1 = 0, y1 = 0, w, h, j, p, q; - 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++) { - j2k_tcp_t *tcp = &cp->tcps[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) */ - p = tileno % cp->tw; /* si numerotation matricielle .. */ - q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */ - - /* 4 borders of the tile rescale on the image if necessary */ - tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0); - tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0); - tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1); - tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1); - - tile->numcomps = img->numcomps; - tile->comps = (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t)); - for (compno = 0; compno < tile->numcomps; compno++) { - j2k_tccp_t *tccp = &tcp->tccps[compno]; - tcd_tilecomp_t *tilec = &tile->comps[compno]; - /* border of each tile component (global) */ - tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx); - tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy); - tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx); - tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy); - - 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++) { - int pdx, pdy; - int levelno = tilec->numresolutions - 1 - resno; - int tlprcxstart, tlprcystart, brprcxend, brprcyend; - int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; - int cbgwidthexpn, cbgheightexpn; - int cblkwidthexpn, cblkheightexpn; - tcd_resolution_t *res = &tilec->resolutions[resno]; - - /* border for each resolution level (global) */ - res->x0 = int_ceildivpow2(tilec->x0, levelno); - res->y0 = int_ceildivpow2(tilec->y0, levelno); - res->x1 = int_ceildivpow2(tilec->x1, levelno); - res->y1 = int_ceildivpow2(tilec->y1, levelno); - - res->numbands = resno == 0 ? 1 : 3; - /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ - if (tccp->csty & J2K_CCP_CSTY_PRT) { - pdx = tccp->prcw[resno]; - pdy = tccp->prch[resno]; - } else { - pdx = 15; - pdy = 15; - } - /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ - tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; - tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; - brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; - brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; - res->pw = (brprcxend - tlprcxstart) >> pdx; - res->ph = (brprcyend - tlprcystart) >> pdy; - - if (resno == 0) { - tlcbgxstart = tlprcxstart; - tlcbgystart = tlprcystart; - brcbgxend = brprcxend; - brcbgyend = brprcyend; - cbgwidthexpn = pdx; - cbgheightexpn = pdy; - } else { - tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); - tlcbgystart = int_ceildivpow2(tlprcystart, 1); - brcbgxend = int_ceildivpow2(brprcxend, 1); - brcbgyend = int_ceildivpow2(brprcyend, 1); - cbgwidthexpn = pdx - 1; - cbgheightexpn = pdy - 1; - } - - cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn); - cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn); - - for (bandno = 0; bandno < res->numbands; bandno++) { - int x0b, y0b; - int gain, numbps; - j2k_stepsize_t *ss; - tcd_band_t *band = &res->bands[bandno]; - band->bandno = resno == 0 ? 0 : bandno + 1; - x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0; - y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0; - - if (band->bandno == 0) { - /* band border (global) */ - band->x0 = int_ceildivpow2(tilec->x0, levelno); - band->y0 = int_ceildivpow2(tilec->y0, levelno); - band->x1 = int_ceildivpow2(tilec->x1, levelno); - band->y1 = int_ceildivpow2(tilec->y1, levelno); - } else { - /* band border (global) */ - band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1); - band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1); - band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1); - band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1); - } - - ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1]; - gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno); - numbps = img->comps[compno].prec + gain; - band->stepsize = (int) floor((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn) * 8192.0); - band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ - - band->precincts = (tcd_precinct_t *) malloc(res->pw * res->ph * sizeof(tcd_precinct_t)); - - for (precno = 0; precno < res->pw * res->ph; precno++) { - int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; - int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn); - int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn); - int cbgxend = cbgxstart + (1 << cbgwidthexpn); - int cbgyend = cbgystart + (1 << cbgheightexpn); - tcd_precinct_t *prc = &band->precincts[precno]; - /* precinct size (global) */ - prc->x0 = int_max(cbgxstart, band->x0); - prc->y0 = int_max(cbgystart, band->y0); - prc->x1 = int_min(cbgxend, band->x1); - prc->y1 = int_min(cbgyend, band->y1); - - tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn; - tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn; - brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn; - brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn; - prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; - prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; - - prc->cblks = (tcd_cblk_t *) malloc(prc->cw * prc->ch * sizeof(tcd_cblk_t)); - - prc->incltree = tgt_create(prc->cw, prc->ch); - prc->imsbtree = tgt_create(prc->cw, prc->ch); - - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn); - int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn); - int cblkxend = cblkxstart + (1 << cblkwidthexpn); - int cblkyend = cblkystart + (1 << cblkheightexpn); - tcd_cblk_t *cblk = &prc->cblks[cblkno]; - /* code-block size (global) */ - cblk->x0 = int_max(cblkxstart, prc->x0); - cblk->y0 = int_max(cblkystart, prc->y0); - cblk->x1 = int_min(cblkxend, prc->x1); - cblk->y1 = int_min(cblkyend, prc->y1); - } - } - } + int tileno, compno, resno, bandno, precno, cblkno, i; + unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h, j, p, q; + 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++) { + j2k_tcp_t *tcp = &cp->tcps[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) */ + p = tileno % cp->tw; /* si numerotation matricielle .. */ + q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */ + + /* 4 borders of the tile rescale on the image if necessary */ + tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0); + tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0); + tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1); + tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1); + + tile->numcomps = img->numcomps; + tile->comps = + (tcd_tilecomp_t *) malloc(img->numcomps * + sizeof(tcd_tilecomp_t)); + for (compno = 0; compno < tile->numcomps; compno++) { + j2k_tccp_t *tccp = &tcp->tccps[compno]; + tcd_tilecomp_t *tilec = &tile->comps[compno]; + /* border of each tile component (global) */ + tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx); + tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy); + tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx); + tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy); + + 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++) { + int pdx, pdy; + int levelno = tilec->numresolutions - 1 - resno; + int tlprcxstart, tlprcystart, brprcxend, brprcyend; + int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; + int cbgwidthexpn, cbgheightexpn; + int cblkwidthexpn, cblkheightexpn; + tcd_resolution_t *res = &tilec->resolutions[resno]; + + /* border for each resolution level (global) */ + res->x0 = int_ceildivpow2(tilec->x0, levelno); + res->y0 = int_ceildivpow2(tilec->y0, levelno); + res->x1 = int_ceildivpow2(tilec->x1, levelno); + res->y1 = int_ceildivpow2(tilec->y1, levelno); + + res->numbands = resno == 0 ? 1 : 3; + /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ + if (tccp->csty & J2K_CCP_CSTY_PRT) { + pdx = tccp->prcw[resno]; + pdy = tccp->prch[resno]; + } else { + pdx = 15; + pdy = 15; + } + /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ + tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; + tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; + brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; + brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; + res->pw = (brprcxend - tlprcxstart) >> pdx; + res->ph = (brprcyend - tlprcystart) >> pdy; + + if (resno == 0) { + tlcbgxstart = tlprcxstart; + tlcbgystart = tlprcystart; + brcbgxend = brprcxend; + brcbgyend = brprcyend; + cbgwidthexpn = pdx; + cbgheightexpn = pdy; + } else { + tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); + tlcbgystart = int_ceildivpow2(tlprcystart, 1); + brcbgxend = int_ceildivpow2(brprcxend, 1); + brcbgyend = int_ceildivpow2(brprcyend, 1); + cbgwidthexpn = pdx - 1; + cbgheightexpn = pdy - 1; + } + + cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn); + cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn); + + for (bandno = 0; bandno < res->numbands; bandno++) { + int x0b, y0b; + int gain, numbps; + j2k_stepsize_t *ss; + tcd_band_t *band = &res->bands[bandno]; + band->bandno = resno == 0 ? 0 : bandno + 1; + x0b = (band->bandno == 1) + || (band->bandno == 3) ? 1 : 0; + y0b = (band->bandno == 2) + || (band->bandno == 3) ? 1 : 0; + + if (band->bandno == 0) { + /* band border (global) */ + band->x0 = int_ceildivpow2(tilec->x0, levelno); + band->y0 = int_ceildivpow2(tilec->y0, levelno); + band->x1 = int_ceildivpow2(tilec->x1, levelno); + band->y1 = int_ceildivpow2(tilec->y1, levelno); + } else { + /* band border (global) */ + band->x0 = + int_ceildivpow2(tilec->x0 - + (1 << levelno) * x0b, + levelno + 1); + band->y0 = + int_ceildivpow2(tilec->y0 - + (1 << levelno) * y0b, + levelno + 1); + band->x1 = + int_ceildivpow2(tilec->x1 - + (1 << levelno) * x0b, + levelno + 1); + band->y1 = + int_ceildivpow2(tilec->y1 - + (1 << levelno) * y0b, + levelno + 1); + } + + ss = &tccp->stepsizes[resno == + 0 ? 0 : 3 * (resno - 1) + + bandno + 1]; + gain = + tccp->qmfbid == + 0 ? dwt_getgain_real(band-> + bandno) : dwt_getgain(band-> + bandno); + numbps = img->comps[compno].prec + gain; + band->stepsize = + (int) floor((1.0 + ss->mant / 2048.0) * + pow(2.0, numbps - ss->expn) * 8192.0); + band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ + + band->precincts = + (tcd_precinct_t *) malloc(res->pw * res->ph * + sizeof(tcd_precinct_t)); + + for (precno = 0; precno < res->pw * res->ph; precno++) { + int tlcblkxstart, tlcblkystart, brcblkxend, + brcblkyend; + int cbgxstart = + tlcbgxstart + + (precno % res->pw) * (1 << cbgwidthexpn); + int cbgystart = + tlcbgystart + + (precno / res->pw) * (1 << cbgheightexpn); + int cbgxend = cbgxstart + (1 << cbgwidthexpn); + int cbgyend = cbgystart + (1 << cbgheightexpn); + tcd_precinct_t *prc = &band->precincts[precno]; + /* precinct size (global) */ + prc->x0 = int_max(cbgxstart, band->x0); + prc->y0 = int_max(cbgystart, band->y0); + prc->x1 = int_min(cbgxend, band->x1); + prc->y1 = int_min(cbgyend, band->y1); + + tlcblkxstart = + int_floordivpow2(prc->x0, + cblkwidthexpn) << + cblkwidthexpn; + tlcblkystart = + int_floordivpow2(prc->y0, + cblkheightexpn) << + cblkheightexpn; + brcblkxend = + int_ceildivpow2(prc->x1, + cblkwidthexpn) << + cblkwidthexpn; + brcblkyend = + int_ceildivpow2(prc->y1, + cblkheightexpn) << + cblkheightexpn; + prc->cw = + (brcblkxend - tlcblkxstart) >> cblkwidthexpn; + prc->ch = + (brcblkyend - tlcblkystart) >> cblkheightexpn; + + prc->cblks = + (tcd_cblk_t *) malloc(prc->cw * prc->ch * + sizeof(tcd_cblk_t)); + + prc->incltree = tgt_create(prc->cw, prc->ch); + prc->imsbtree = tgt_create(prc->cw, prc->ch); + + for (cblkno = 0; cblkno < prc->cw * prc->ch; + cblkno++) { + int cblkxstart = + tlcblkxstart + + (cblkno % prc->cw) * (1 << cblkwidthexpn); + int cblkystart = + tlcblkystart + + (cblkno / prc->cw) * (1 << cblkheightexpn); + int cblkxend = + cblkxstart + (1 << cblkwidthexpn); + int cblkyend = + cblkystart + (1 << cblkheightexpn); + tcd_cblk_t *cblk = &prc->cblks[cblkno]; + /* code-block size (global) */ + cblk->x0 = int_max(cblkxstart, prc->x0); + cblk->y0 = int_max(cblkystart, prc->y0); + cblk->x1 = int_min(cblkxend, prc->x1); + cblk->y1 = int_min(cblkyend, prc->y1); } + } } + } } - /* tcd_dump(&tcd_image,0); */ - + } + /* 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++) { + /* 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; + 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, resno, bandno, precno, cblkno; - int value; //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; - int matrice[10][10][3]; - int i,j,k; - - /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int));*/ - - for (compno=0; compnonumcomps; compno++) - { - tcd_tilecomp_t *tilec=&tcd_tile->comps[compno]; - for (i=0;inumlayers;i++) { - for (j=0;jnumresolutions;j++) { - for (k=0;k<3;k++) { - matrice[i][j][k]=(int)(tcd_cp->matrice[i*tilec->numresolutions*3+j*3+k]*(float)(tcd_img->comps[compno].prec/16.0));}}} - - for (resno=0; resnonumresolutions; resno++) { - tcd_resolution_t *res=&tilec->resolutions[resno]; - for (bandno=0; bandnonumbands; bandno++) { - tcd_band_t *band=&res->bands[bandno]; - for (precno=0; precnopw*res->ph; precno++) { - tcd_precinct_t *prc=&band->precincts[precno]; - for (cblkno=0; cblknocw*prc->ch; cblkno++) { - tcd_cblk_t *cblk=&prc->cblks[cblkno]; - tcd_layer_t *layer=&cblk->layers[layno]; - int n; - int imsb=tcd_img->comps[compno].prec-cblk->numbps; /* number of bit-plan equal to zero */ - /* Correction of the matrix of coefficient to include the IMSB information */ - - if (layno==0) - { - value=matrice[layno][resno][bandno]; - if (imsb>=value) - value=0; - else - value-=imsb; - } else - { - value=matrice[layno][resno][bandno]-matrice[layno-1][resno][bandno]; - if (imsb>=matrice[layno-1][resno][bandno]) - { - value-=(imsb-matrice[layno-1][resno][bandno]); - if (value<0) value=0; - } - } - - if (layno==0) - cblk->numpassesinlayers=0; - - n=cblk->numpassesinlayers; - if (cblk->numpassesinlayers==0) - { - if (value!=0) - n=3*value-2+cblk->numpassesinlayers; - else - n=cblk->numpassesinlayers; - } else - n=3*value+cblk->numpassesinlayers; - - layer->numpasses=n-cblk->numpassesinlayers; - - if (!layer->numpasses) - continue; - - if (cblk->numpassesinlayers==0) - { - layer->len=cblk->passes[n-1].rate; - layer->data=cblk->data; - } else - { - layer->len=cblk->passes[n-1].rate-cblk->passes[cblk->numpassesinlayers-1].rate; - layer->data=cblk->data+cblk->passes[cblk->numpassesinlayers-1].rate; - } - if (final) - cblk->numpassesinlayers=n; +void tcd_makelayer_fixed(int layno, int final) +{ + int compno, resno, bandno, precno, cblkno; + int value; //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; + int matrice[10][10][3]; + int i, j, k; + + /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */ + + for (compno = 0; compno < tcd_tile->numcomps; compno++) { + tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; + for (i = 0; i < tcd_tcp->numlayers; i++) { + for (j = 0; j < tilec->numresolutions; j++) { + for (k = 0; k < 3; k++) { + matrice[i][j][k] = + (int) (tcd_cp-> + matrice[i * tilec->numresolutions * 3 + + j * 3 + + k] * + (float) (tcd_img->comps[compno].prec / + 16.0)); + }}} + + 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++) { + tcd_cblk_t *cblk = &prc->cblks[cblkno]; + tcd_layer_t *layer = &cblk->layers[layno]; + int n; + int imsb = tcd_img->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */ + /* Correction of the matrix of coefficient to include the IMSB information */ + + if (layno == 0) { + value = matrice[layno][resno][bandno]; + if (imsb >= value) + value = 0; + else + value -= imsb; + } else { + value = + matrice[layno][resno][bandno] - + matrice[layno - 1][resno][bandno]; + if (imsb >= matrice[layno - 1][resno][bandno]) { + value -= + (imsb - + matrice[layno - 1][resno][bandno]); + if (value < 0) + value = 0; + } + } + + if (layno == 0) + cblk->numpassesinlayers = 0; + + n = cblk->numpassesinlayers; + if (cblk->numpassesinlayers == 0) { + if (value != 0) + n = 3 * value - 2 + + cblk->numpassesinlayers; + else + n = cblk->numpassesinlayers; + } else + n = 3 * value + cblk->numpassesinlayers; + + layer->numpasses = n - cblk->numpassesinlayers; + + if (!layer->numpasses) + continue; + + if (cblk->numpassesinlayers == 0) { + layer->len = cblk->passes[n - 1].rate; + layer->data = cblk->data; + } else { + layer->len = + cblk->passes[n - 1].rate - + cblk->passes[cblk->numpassesinlayers - + 1].rate; + layer->data = + cblk->data + + cblk->passes[cblk->numpassesinlayers - + 1].rate; + } + if (final) + cblk->numpassesinlayers = n; + } + } } - } } - } } } -void tcd_rateallocate_fixed() { - int layno; +void tcd_rateallocate_fixed() +{ + int layno; - for (layno=0; laynonumlayers; layno++) - { - tcd_makelayer_fixed(layno, 1); + for (layno = 0; layno < tcd_tcp->numlayers; layno++) { + tcd_makelayer_fixed(layno, 1); } } void tcd_makelayer(int layno, double thresh, int final) { - int compno, resno, bandno, precno, cblkno, passno; - for (compno = 0; compno < tcd_tile->numcomps; compno++) { - tcd_tilecomp_t *tilec = &tcd_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++) { - tcd_cblk_t *cblk = &prc->cblks[cblkno]; - tcd_layer_t *layer = &cblk->layers[layno]; - int n; - - if (layno == 0) { - cblk->numpassesinlayers = 0; - } - n = cblk->numpassesinlayers; - for (passno = cblk->numpassesinlayers; - passno < cblk->totalpasses; passno++) { - int dr; - double dd; - tcd_pass_t *pass = &cblk->passes[passno]; - if (n == 0) { - dr = pass->rate; - dd = pass->distortiondec; - } else { - dr = pass->rate - cblk->passes[n - 1].rate; - dd = pass->distortiondec - cblk->passes[n - 1].distortiondec; - } - if (dr == 0) { - if (dd != 0) - n = passno + 1; - continue; - } - if (dd / dr > thresh) - n = passno + 1; - } - layer->numpasses = n - cblk->numpassesinlayers; - if (!layer->numpasses) - continue; - if (cblk->numpassesinlayers == 0) { - layer->len = cblk->passes[n - 1].rate; - layer->data = cblk->data; - layer->disto = cblk->passes[n - 1].distortiondec; - } else { - layer->len = cblk->passes[n - 1].rate - - cblk->passes[cblk->numpassesinlayers - 1].rate; - layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate; - layer->disto = cblk->passes[n - 1].distortiondec - - cblk->passes[cblk->numpassesinlayers - 1].distortiondec; - } - - if (final) - cblk->numpassesinlayers = n; - } - } + int compno, resno, bandno, precno, cblkno, passno; + for (compno = 0; compno < tcd_tile->numcomps; compno++) { + tcd_tilecomp_t *tilec = &tcd_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++) { + tcd_cblk_t *cblk = &prc->cblks[cblkno]; + tcd_layer_t *layer = &cblk->layers[layno]; + int n; + + if (layno == 0) { + cblk->numpassesinlayers = 0; + } + n = cblk->numpassesinlayers; + for (passno = cblk->numpassesinlayers; + passno < cblk->totalpasses; passno++) { + int dr; + double dd; + tcd_pass_t *pass = &cblk->passes[passno]; + if (n == 0) { + dr = pass->rate; + dd = pass->distortiondec; + } else { + dr = pass->rate - cblk->passes[n - 1].rate; + dd = pass->distortiondec - cblk->passes[n - + 1]. + distortiondec; + } + if (dr == 0) { + if (dd != 0) + n = passno + 1; + continue; + } + if (dd / dr > thresh) + n = passno + 1; + } + layer->numpasses = n - cblk->numpassesinlayers; + if (!layer->numpasses) + continue; + if (cblk->numpassesinlayers == 0) { + layer->len = cblk->passes[n - 1].rate; + layer->data = cblk->data; + layer->disto = + cblk->passes[n - 1].distortiondec; + } else { + layer->len = cblk->passes[n - 1].rate - + cblk->passes[cblk->numpassesinlayers - + 1].rate; + layer->data = + cblk->data + + cblk->passes[cblk->numpassesinlayers - + 1].rate; + layer->disto = + cblk->passes[n - 1].distortiondec - + cblk->passes[cblk->numpassesinlayers - + 1].distortiondec; } + + if (final) + cblk->numpassesinlayers = n; + } } + } } + } } void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM) { - int compno, resno, bandno, precno, cblkno, passno, layno; - double min, max; - min = DBL_MAX; - max = 0; - - for (compno = 0; compno < tcd_tile->numcomps; compno++) { - tcd_tilecomp_t *tilec = &tcd_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++) { - tcd_cblk_t *cblk = &prc->cblks[cblkno]; - for (passno = 0; passno < cblk->totalpasses; passno++) { - tcd_pass_t *pass = &cblk->passes[passno]; - int dr; - double dd, rdslope; - if (passno == 0) { - dr = pass->rate; - dd = pass->distortiondec; - } else { - dr = pass->rate - cblk->passes[passno - 1].rate; - dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec; - } - if (dr == 0) { - continue; - } - rdslope = dd / dr; - if (rdslope < min) { - min = rdslope; - } - if (rdslope > max) { - max = rdslope; - } - } /* passno */ - } /* cbklno */ - } /* precno */ - } /* bandno */ - } /* resno */ - } /* compno */ - if (info_IM->index_on) { /* Threshold for Marcela Index */ - info_IM->tile[tcd_tileno].thresh=(double*)malloc(tcd_tcp->numlayers*sizeof(double)); - } - for (layno = 0; layno < tcd_tcp->numlayers; layno++) { - volatile double lo = min; - volatile double hi = max; - volatile int success = 0; - volatile int maxlen = int_min(tcd_tcp->rates[layno], len); - volatile double goodthresh; - volatile int goodlen; - volatile int i; - - for (i = 0; i < 32; i++) { - volatile double thresh = (lo + hi) / 2; - int l; - - tcd_makelayer(layno, thresh, 0); - - l = t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile, layno + 1, dest, maxlen, info_IM); - /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */ - if (l == -999) { - lo = thresh; + int compno, resno, bandno, precno, cblkno, passno, layno; + double min, max; + min = DBL_MAX; + max = 0; + + for (compno = 0; compno < tcd_tile->numcomps; compno++) { + tcd_tilecomp_t *tilec = &tcd_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++) { + tcd_cblk_t *cblk = &prc->cblks[cblkno]; + for (passno = 0; passno < cblk->totalpasses; + passno++) { + tcd_pass_t *pass = &cblk->passes[passno]; + int dr; + double dd, rdslope; + if (passno == 0) { + dr = pass->rate; + dd = pass->distortiondec; + } else { + dr = pass->rate - cblk->passes[passno - + 1].rate; + dd = pass->distortiondec - + cblk->passes[passno - 1].distortiondec; + } + if (dr == 0) { continue; - } + } + rdslope = dd / dr; + if (rdslope < min) { + min = rdslope; + } + if (rdslope > max) { + max = rdslope; + } + } /* passno */ + } /* cbklno */ + } /* precno */ + } /* bandno */ + } /* resno */ + } /* compno */ + if (info_IM->index_on) { /* Threshold for Marcela Index */ + info_IM->tile[tcd_tileno].thresh = + (double *) malloc(tcd_tcp->numlayers * sizeof(double)); + } + for (layno = 0; layno < tcd_tcp->numlayers; layno++) { + volatile double lo = min; + volatile double hi = max; + volatile int success = 0; + volatile int maxlen = int_min(tcd_tcp->rates[layno], len); + volatile double goodthresh; + volatile int goodlen; + volatile int i; + + for (i = 0; i < 32; i++) { + volatile double thresh = (lo + hi) / 2; + int l; + + tcd_makelayer(layno, thresh, 0); + + l = t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile, + layno + 1, dest, maxlen, info_IM); + /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */ + if (l == -999) { + lo = thresh; + continue; + } - hi = thresh; - success = 1; - goodthresh = thresh; - goodlen = l; - } + hi = thresh; + success = 1; + goodthresh = thresh; + goodlen = l; + } - if (!success) { - longjmp(j2k_error, 1); - } - - if (info_IM->index_on) { /* Threshold for Marcela Index */ - info_IM->tile[tcd_tileno].thresh[layno]=goodthresh; - } - tcd_makelayer(layno, goodthresh, 1); + if (!success) { + longjmp(j2k_error, 1); + } + + if (info_IM->index_on) { /* Threshold for Marcela Index */ + info_IM->tile[tcd_tileno].thresh[layno] = goodthresh; } + tcd_makelayer(layno, goodthresh, 1); + } } -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) { - int compno; - int l; - clock_t time7; - tcd_tile_t *tile; - j2k_tcp_t *tcp = &tcd_cp->tcps[0]; - j2k_tccp_t *tccp = &tcp->tccps[0]; - - tcd_tileno = tileno; - tcd_tile = tcd_image.tiles; - tcd_tcp = &tcd_cp->tcps[tileno]; - tile = tcd_tile; - /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */ - if (info_IM->index_on) { - tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* old parser version */ - tcd_resolution_t *res_idx = &tilec_idx->resolutions[0]; /* old parser version */ - - info_IM->tile[tileno].pw = res_idx->pw; - info_IM->tile[tileno].ph = res_idx->ph; - - info_IM->pw = res_idx->pw; /* old parser version */ - info_IM->ph = res_idx->ph; /* old parser version */ - info_IM->pdx = 1 << tccp->prcw[tccp->numresolutions - 1]; - info_IM->pdy = 1 << tccp->prch[tccp->numresolutions - 1]; - } - /* << INDEX */ + int compno; + int l; + clock_t time7; + tcd_tile_t *tile; + j2k_tcp_t *tcp = &tcd_cp->tcps[0]; + j2k_tccp_t *tccp = &tcp->tccps[0]; + + tcd_tileno = tileno; + tcd_tile = tcd_image.tiles; + tcd_tcp = &tcd_cp->tcps[tileno]; + tile = tcd_tile; + /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */ + if (info_IM->index_on) { + tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* old parser version */ + tcd_resolution_t *res_idx = &tilec_idx->resolutions[0]; /* old parser version */ + + info_IM->tile[tileno].pw = res_idx->pw; + info_IM->tile[tileno].ph = res_idx->ph; + + info_IM->pw = res_idx->pw; /* old parser version */ + info_IM->ph = res_idx->ph; /* old parser version */ + info_IM->pdx = 1 << tccp->prcw[tccp->numresolutions - 1]; + info_IM->pdy = 1 << tccp->prch[tccp->numresolutions - 1]; + } + /* << INDEX */ /*---------------TILE-------------------*/ - time7 = clock(); + time7 = clock(); + + for (compno = 0; compno < tile->numcomps; compno++) { + FILE *src; + char tmp[256]; + int k; + unsigned char elmt; + int i, j; + int tw, w; + tcd_tilecomp_t *tilec = &tile->comps[compno]; + int adjust = + tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno]. + prec - 1); + int offset_x, offset_y; + + offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx); + offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy); + tw = tilec->x1 - tilec->x0; + w = int_ceildiv(tcd_img->x1 - tcd_img->x0, + tcd_img->comps[compno].dx); + sprintf(tmp, "Compo%d", compno); /* component file */ + src = fopen(tmp, "rb"); + if (!src) { + fprintf(stderr, "failed to open %s for reading\n", tmp); + return 1; + } - for (compno = 0; compno < tile->numcomps; compno++) { - FILE *src; - char tmp[256]; - int k; - unsigned char elmt; - int i, j; - int tw, w; - tcd_tilecomp_t *tilec = &tile->comps[compno]; - int adjust = tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec - 1); - int offset_x, offset_y; - - offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx); - offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy); - tw = tilec->x1 - tilec->x0; - w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx); - sprintf(tmp, "Compo%d", compno); /* component file */ - src = fopen(tmp, "rb"); - if (!src) { - fprintf(stderr, "failed to open %s for reading\n", tmp); - return 1; + /* read the Compo file to extract data of the tile */ + k = 0; + fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w, + SEEK_SET); + k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w; + for (j = tilec->y0; j < tilec->y1; j++) { + for (i = tilec->x0; i < tilec->x1; i++) { + if (tcd_tcp->tccps[compno].qmfbid == 1) { + elmt = fgetc(src); + tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] = + elmt - adjust; + k++; + } else if (tcd_tcp->tccps[compno].qmfbid == 0) { + elmt = fgetc(src); + tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] = + (elmt - adjust) << 13; + k++; } + } + fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k, + SEEK_CUR); + k = tilec->x0 - offset_x + (j + 1 - offset_y) * w; - /* read the Compo file to extract data of the tile */ - k = 0; - fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w, SEEK_SET); - k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w; - for (j = tilec->y0; j < tilec->y1; j++) { - for (i = tilec->x0; i < tilec->x1; i++) { - if (tcd_tcp->tccps[compno].qmfbid == 1) { - elmt = fgetc(src); - tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] = elmt - adjust; - k++; - } else if (tcd_tcp->tccps[compno].qmfbid == 0) { - elmt = fgetc(src); - tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] = (elmt - adjust) << 13; - k++; - } - } - fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k, SEEK_CUR); - k = tilec->x0 - offset_x + (j + 1 - offset_y) * w; - - } - fclose(src); } + fclose(src); + } /*----------------MCT-------------------*/ - if (tcd_tcp->mct) { - if (tcd_tcp->tccps[0].qmfbid == 0) { - mct_encode_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)); - } else { - mct_encode(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->mct) { + if (tcd_tcp->tccps[0].qmfbid == 0) { + mct_encode_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)); + } else { + mct_encode(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)); } + } /*----------------DWT---------------------*/ - /* time3=clock(); */ - for (compno = 0; compno < tile->numcomps; compno++) { - tcd_tilecomp_t *tilec = &tile->comps[compno]; - if (tcd_tcp->tccps[compno].qmfbid == 1) { - dwt_encode(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1); - } else if (tcd_tcp->tccps[compno].qmfbid == 0) { - dwt_encode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1); - } + /* time3=clock(); */ + for (compno = 0; compno < tile->numcomps; compno++) { + tcd_tilecomp_t *tilec = &tile->comps[compno]; + if (tcd_tcp->tccps[compno].qmfbid == 1) { + dwt_encode(tilec->data, tilec->x1 - tilec->x0, + tilec->y1 - tilec->y0, tilec, + tilec->numresolutions - 1); + } else if (tcd_tcp->tccps[compno].qmfbid == 0) { + dwt_encode_real(tilec->data, tilec->x1 - tilec->x0, + tilec->y1 - tilec->y0, tilec, + tilec->numresolutions - 1); } + } /*------------------TIER1-----------------*/ - t1_init_luts(); - t1_encode_cblks(tile, tcd_tcp); + t1_init_luts(); + t1_encode_cblks(tile, tcd_tcp); /*-----------RATE-ALLOCATE------------------*/ - info_IM->index_write = 0; /* INDEX */ - - if (tcd_cp->disto_alloc) - /* Normal Rate/distortion allocation */ - tcd_rateallocate(dest, len, info_IM); - else - /* Fixed layer allocation */ - tcd_rateallocate_fixed(); - + info_IM->index_write = 0; /* INDEX */ + + if (tcd_cp->disto_alloc) + /* Normal Rate/distortion allocation */ + tcd_rateallocate(dest, len, info_IM); + else + /* Fixed layer allocation */ + tcd_rateallocate_fixed(); + /*--------------TIER2------------------*/ - info_IM->index_write = 1; /* INDEX */ - l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile, tcd_tcp->numlayers, dest, len, info_IM); + info_IM->index_write = 1; /* INDEX */ + 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); + time7 = clock() - time7; + printf("total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC, + (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC); - /* cleaning memory */ - for (compno = 0; compno < tile->numcomps; compno++) { - tilec = &tile->comps[compno]; - free(tilec->data); - } + /* cleaning memory */ + for (compno = 0; compno < tile->numcomps; compno++) { + tilec = &tile->comps[compno]; + free(tilec->data); + } - return l; + return l; } -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) { - int compno; - int l; - clock_t time; - tcd_tile_t *tile; - j2k_tcp_t *tcp = &tcd_cp->tcps[0]; - j2k_tccp_t *tccp = &tcp->tccps[0]; - - tcd_tileno = tileno; - tcd_tile = tcd_image.tiles; - tcd_tcp = &tcd_cp->tcps[tileno]; - tile = tcd_tile; - /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */ - if (info_IM->index_on) { - tcd_tilecomp_t *tilec_idx = &tile->comps[0]; - tcd_resolution_t *res_idx = &tilec_idx->resolutions[0]; - info_IM->tile[tileno].pw = res_idx->pw; - info_IM->tile[tileno].ph = res_idx->ph; - info_IM->pw = res_idx->pw; /* old parser version */ - info_IM->ph = res_idx->ph; /* old parser version */ - info_IM->pdx = 1 << tccp->prcw[tccp->numresolutions - 1]; - info_IM->pdy = 1 << tccp->prch[tccp->numresolutions - 1]; - } - /* << INDEX */ + int compno; + int l; + clock_t time; + tcd_tile_t *tile; + j2k_tcp_t *tcp = &tcd_cp->tcps[0]; + j2k_tccp_t *tccp = &tcp->tccps[0]; + + tcd_tileno = tileno; + tcd_tile = tcd_image.tiles; + tcd_tcp = &tcd_cp->tcps[tileno]; + tile = tcd_tile; + /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */ + if (info_IM->index_on) { + tcd_tilecomp_t *tilec_idx = &tile->comps[0]; + tcd_resolution_t *res_idx = &tilec_idx->resolutions[0]; + info_IM->tile[tileno].pw = res_idx->pw; + info_IM->tile[tileno].ph = res_idx->ph; + info_IM->pw = res_idx->pw; /* old parser version */ + info_IM->ph = res_idx->ph; /* old parser version */ + info_IM->pdx = 1 << tccp->prcw[tccp->numresolutions - 1]; + info_IM->pdy = 1 << tccp->prch[tccp->numresolutions - 1]; + } + /* << INDEX */ /*---------------TILE-------------------*/ - time = clock(); - - for (compno = 0; compno < tile->numcomps; compno++) { - FILE *src; - char tmp[256]; - int k; - int elmt; - int i, j; - int tw, w; - tcd_tilecomp_t *tilec = &tile->comps[compno]; - int adjust = tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec - 1); - int offset_x, offset_y; - - offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx); - offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy); - tw = tilec->x1 - tilec->x0; - w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx); - sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1); /* bandtile file opening */ - src = fopen(tmp, "rb"); - if (!src) { - fprintf(stderr, "failed to open %s for reading\n", tmp); - return 1; - } - /* Extract data from bandtile file limited to the current tile */ - k = 0; - while (k < tilec->x0 - offset_x) { - k++; - fscanf(src, "%d", &elmt); - } + time = clock(); + + for (compno = 0; compno < tile->numcomps; compno++) { + FILE *src; + char tmp[256]; + int k; + int elmt; + int i, j; + int tw, w; + tcd_tilecomp_t *tilec = &tile->comps[compno]; + int adjust = + tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno]. + prec - 1); + int offset_x, offset_y; + + offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx); + offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy); + tw = tilec->x1 - tilec->x0; + w = int_ceildiv(tcd_img->x1 - tcd_img->x0, + tcd_img->comps[compno].dx); + sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1); /* bandtile file opening */ + src = fopen(tmp, "rb"); + if (!src) { + fprintf(stderr, "failed to open %s for reading\n", tmp); + return 1; + } + /* Extract data from bandtile file limited to the current tile */ + k = 0; + while (k < tilec->x0 - offset_x) { + k++; + fscanf(src, "%d", &elmt); + } - for (j = 0; j < tilec->y1 - tilec->y0; j++) { - for (i = tilec->x0; i < tilec->x1; i++) { - if (tcd_tcp->tccps[compno].qmfbid == 1) { - fscanf(src, "%d", &elmt); - tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust; - k++; - } else if (tcd_tcp->tccps[compno].qmfbid == 0) { - fscanf(src, "%d", &elmt); - tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13; - k++; - } - } - while (k < tilec->x0 - offset_x + (j + 1) * w) { - k++; - fscanf(src, "%d", &elmt); - } + for (j = 0; j < tilec->y1 - tilec->y0; j++) { + for (i = tilec->x0; i < tilec->x1; i++) { + if (tcd_tcp->tccps[compno].qmfbid == 1) { + fscanf(src, "%d", &elmt); + tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust; + k++; + } else if (tcd_tcp->tccps[compno].qmfbid == 0) { + fscanf(src, "%d", &elmt); + tilec->data[i - tilec->x0 + (j) * tw] = + (elmt - adjust) << 13; + k++; } - fclose(src); + } + while (k < tilec->x0 - offset_x + (j + 1) * w) { + k++; + fscanf(src, "%d", &elmt); + } } + fclose(src); + } /*----------------MCT-------------------*/ - if (tcd_tcp->mct) { - if (tcd_tcp->tccps[0].qmfbid == 0) { - mct_encode_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)); - } else { - mct_encode(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->mct) { + if (tcd_tcp->tccps[0].qmfbid == 0) { + mct_encode_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)); + } else { + mct_encode(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)); } + } /*----------------DWT---------------------*/ - for (compno = 0; compno < tile->numcomps; compno++) { - tcd_tilecomp_t *tilec = &tile->comps[compno]; - if (tcd_tcp->tccps[compno].qmfbid == 1) { - dwt_encode(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1); - } else if (tcd_tcp->tccps[compno].qmfbid == 0) { - dwt_encode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1); - } + for (compno = 0; compno < tile->numcomps; compno++) { + tcd_tilecomp_t *tilec = &tile->comps[compno]; + if (tcd_tcp->tccps[compno].qmfbid == 1) { + dwt_encode(tilec->data, tilec->x1 - tilec->x0, + tilec->y1 - tilec->y0, tilec, + tilec->numresolutions - 1); + } else if (tcd_tcp->tccps[compno].qmfbid == 0) { + dwt_encode_real(tilec->data, tilec->x1 - tilec->x0, + tilec->y1 - tilec->y0, tilec, + tilec->numresolutions - 1); } + } /*------------------TIER1-----------------*/ - - t1_init_luts(); - t1_encode_cblks(tile, tcd_tcp); + + t1_init_luts(); + t1_encode_cblks(tile, tcd_tcp); /*-----------RATE-ALLOCATE------------------*/ - info_IM->index_write = 0; /* INDEX */ + info_IM->index_write = 0; /* INDEX */ + + info_IM->index_write = 0; /* INDEX */ - info_IM->index_write=0; /* INDEX */ - - if (tcd_cp->disto_alloc) - /* Normal Rate/distortion allocation */ - tcd_rateallocate(dest, len, info_IM); - else - /* Fixed layer allocation */ - tcd_rateallocate_fixed(); + if (tcd_cp->disto_alloc) + /* Normal Rate/distortion allocation */ + tcd_rateallocate(dest, len, info_IM); + else + /* Fixed layer allocation */ + tcd_rateallocate_fixed(); /*--------------TIER2------------------*/ - info_IM->index_write = 1; /* INDEX */ + info_IM->index_write = 1; /* INDEX */ - l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile, tcd_tcp->numlayers, dest, len, info_IM); + l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile, + tcd_tcp->numlayers, dest, len, info_IM); /*---------------CLEAN-------------------*/ - time = clock() - time; - printf("total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC, (time % 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]; - free(tilec->data); - } + for (compno = 0; compno < tile->numcomps; compno++) { + tilec = &tile->comps[compno]; + free(tilec->data); + } - return l; + return l; } int tcd_decode_tile(unsigned char *src, int len, int tileno) { - int l; - int compno; - int eof = 0; - clock_t time; - tcd_tile_t *tile; + int l; + int compno; + int eof = 0; + clock_t time; + tcd_tile_t *tile; - tcd_tileno = tileno; - tcd_tile = &tcd_image.tiles[tileno]; - tcd_tcp = &tcd_cp->tcps[tileno]; - tile = tcd_tile; + tcd_tileno = tileno; + tcd_tile = &tcd_image.tiles[tileno]; + tcd_tcp = &tcd_cp->tcps[tileno]; + tile = tcd_tile; - time = clock(); + time = clock(); - fprintf(stderr,"tile decoding time %d/%d: ", tileno + 1, tcd_cp->tw * tcd_cp->th); + fprintf(stderr, "tile decoding time %d/%d: ", tileno + 1, + tcd_cp->tw * tcd_cp->th); /*--------------TIER2------------------*/ - l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile); + l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile); - if (l == -999) { - eof = 1; - fprintf(stderr, "tcd_decode: incomplete bistream\n"); - } + if (l == -999) { + eof = 1; + fprintf(stderr, "tcd_decode: incomplete bistream\n"); + } /*------------------TIER1-----------------*/ - t1_init_luts(); - t1_decode_cblks(tile, tcd_tcp); + t1_init_luts(); + t1_decode_cblks(tile, tcd_tcp); /*----------------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, - 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); - } - - if (tile->comps[compno].numresolutions > 0) - tcd_img->comps[compno].factor = tile->comps[compno].numresolutions - (tcd_img->comps[compno].resno_decoded + 1); - } - + 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, + 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); + } + + if (tile->comps[compno].numresolutions > 0) + tcd_img->comps[compno].factor = + tile->comps[compno].numresolutions - + (tcd_img->comps[compno].resno_decoded + 1); + } + /*----------------MCT-------------------*/ - if (tcd_tcp->mct) { - 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_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->mct) { + 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_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)); } + } /*---------------TILE-------------------*/ - 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 (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); + 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); } - } } + } - time = clock() - time; - fprintf(stderr,"total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC, (time % 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); - } + if (eof) { + longjmp(j2k_error, 1); + } - return l; + return l; } diff --git a/libopenjpeg/tcd.h b/libopenjpeg/tcd.h index 9401e968..2b0ee94c 100644 --- a/libopenjpeg/tcd.h +++ b/libopenjpeg/tcd.h @@ -33,82 +33,82 @@ #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; /* Number of passes in the layer */ - int len; /* len of information */ - double disto; /* add for index (Cfr. Marcela) */ - unsigned char *data; /* 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; /* 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 */ + 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; /* 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 */ + 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; /* 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; + 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; /* 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 */ + 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; /* 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 */ + 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; /* 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 */ + 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; /* number of tiles in width and heigth */ - tcd_tile_t *tiles; /* Tiles information */ + int tw, th; /* number of tiles in width and heigth */ + tcd_tile_t *tiles; /* Tiles information */ } tcd_image_t; /* @@ -152,7 +152,8 @@ void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno); * 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); /* @@ -162,7 +163,8 @@ int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len, info_image * i * 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 diff --git a/libopenjpeg/tgt.c b/libopenjpeg/tgt.c index 4d4e2beb..26c33af8 100644 --- a/libopenjpeg/tgt.c +++ b/libopenjpeg/tgt.c @@ -34,16 +34,16 @@ /* */ void tgt_reset(tgt_tree_t * tree) { - int i; - /* new */ - if (!tree || tree == NULL) - return; - - for (i = 0; i < tree->numnodes; i++) { - tree->nodes[i].value = 999; - tree->nodes[i].low = 0; - tree->nodes[i].known = 0; - } + int i; + /* new */ + if (!tree || tree == NULL) + return; + + for (i = 0; i < tree->numnodes; i++) { + tree->nodes[i].value = 999; + tree->nodes[i].low = 0; + tree->nodes[i].known = 0; + } } /* */ @@ -51,69 +51,70 @@ void tgt_reset(tgt_tree_t * tree) /* */ tgt_tree_t *tgt_create(int numleafsh, int numleafsv) { - int nplh[32]; - int nplv[32]; - tgt_node_t *node; - tgt_node_t *parentnode; - tgt_node_t *parentnode0; - tgt_tree_t *tree; - int i, j, k; - int numlvls; - int n; - - tree = (tgt_tree_t *) malloc(sizeof(tgt_tree_t)); - tree->numleafsh = numleafsh; - tree->numleafsv = numleafsv; - - numlvls = 0; - nplh[0] = numleafsh; - nplv[0] = numleafsv; - tree->numnodes = 0; - do { - n = nplh[numlvls] * nplv[numlvls]; - nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2; - nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2; - tree->numnodes += n; - ++numlvls; - } while (n > 1); - - /* ADD */ - if (tree->numnodes == 0) { - free(tree); - return NULL; - } - - tree->nodes = (tgt_node_t *) malloc(tree->numnodes * sizeof(tgt_node_t)); - - node = tree->nodes; - parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv]; - parentnode0 = parentnode; - - for (i = 0; i < numlvls - 1; ++i) { - for (j = 0; j < nplv[i]; ++j) { - k = nplh[i]; - while (--k >= 0) { - node->parent = parentnode; - ++node; - if (--k >= 0) { - node->parent = parentnode; - ++node; - } - ++parentnode; - } - if ((j & 1) || j == nplv[i] - 1) { - parentnode0 = parentnode; - } else { - parentnode = parentnode0; - parentnode0 += nplh[i]; - } + int nplh[32]; + int nplv[32]; + tgt_node_t *node; + tgt_node_t *parentnode; + tgt_node_t *parentnode0; + tgt_tree_t *tree; + int i, j, k; + int numlvls; + int n; + + tree = (tgt_tree_t *) malloc(sizeof(tgt_tree_t)); + tree->numleafsh = numleafsh; + tree->numleafsv = numleafsv; + + numlvls = 0; + nplh[0] = numleafsh; + nplv[0] = numleafsv; + tree->numnodes = 0; + do { + n = nplh[numlvls] * nplv[numlvls]; + nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2; + nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2; + tree->numnodes += n; + ++numlvls; + } while (n > 1); + + /* ADD */ + if (tree->numnodes == 0) { + free(tree); + return NULL; + } + + tree->nodes = + (tgt_node_t *) malloc(tree->numnodes * sizeof(tgt_node_t)); + + node = tree->nodes; + parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv]; + parentnode0 = parentnode; + + for (i = 0; i < numlvls - 1; ++i) { + for (j = 0; j < nplv[i]; ++j) { + k = nplh[i]; + while (--k >= 0) { + node->parent = parentnode; + ++node; + if (--k >= 0) { + node->parent = parentnode; + ++node; } + ++parentnode; + } + if ((j & 1) || j == nplv[i] - 1) { + parentnode0 = parentnode; + } else { + parentnode = parentnode0; + parentnode0 += nplh[i]; + } } - node->parent = 0; + } + node->parent = 0; - tgt_reset(tree); + tgt_reset(tree); - return tree; + return tree; } /* */ @@ -121,8 +122,8 @@ tgt_tree_t *tgt_create(int numleafsh, int numleafsv) /* */ void tgt_destroy(tgt_tree_t * t) { - free(t->nodes); - free(t); + free(t->nodes); + free(t); } /* */ @@ -130,12 +131,12 @@ void tgt_destroy(tgt_tree_t * t) /* */ void tgt_setvalue(tgt_tree_t * tree, int leafno, int value) { - tgt_node_t *node; - node = &tree->nodes[leafno]; - while (node && node->value > value) { - node->value = value; - node = node->parent; - } + tgt_node_t *node; + node = &tree->nodes[leafno]; + while (node && node->value > value) { + node->value = value; + node = node->parent; + } } /* */ @@ -143,44 +144,44 @@ void tgt_setvalue(tgt_tree_t * tree, int leafno, int value) /* */ void tgt_encode(tgt_tree_t * tree, int leafno, int threshold) { - tgt_node_t *stk[31]; - tgt_node_t **stkptr; - tgt_node_t *node; - int low; - - stkptr = stk; - node = &tree->nodes[leafno]; - while (node->parent) { - *stkptr++ = node; - node = node->parent; + tgt_node_t *stk[31]; + tgt_node_t **stkptr; + tgt_node_t *node; + int low; + + stkptr = stk; + node = &tree->nodes[leafno]; + while (node->parent) { + *stkptr++ = node; + node = node->parent; + } + + low = 0; + for (;;) { + if (low > node->low) { + node->low = low; + } else { + low = node->low; } - low = 0; - for (;;) { - if (low > node->low) { - node->low = low; - } else { - low = node->low; + while (low < threshold) { + if (low >= node->value) { + if (!node->known) { + bio_write(1, 1); + node->known = 1; } - - while (low < threshold) { - if (low >= node->value) { - if (!node->known) { - bio_write(1, 1); - node->known = 1; - } - break; - } - bio_write(0, 1); - ++low; - } - - node->low = low; - if (stkptr == stk) - break; - node = *--stkptr; + break; + } + bio_write(0, 1); + ++low; } + node->low = low; + if (stkptr == stk) + break; + node = *--stkptr; + } + } /* */ @@ -188,38 +189,38 @@ void tgt_encode(tgt_tree_t * tree, int leafno, int threshold) /* */ int tgt_decode(tgt_tree_t * tree, int leafno, int threshold) { - tgt_node_t *stk[31]; - tgt_node_t **stkptr; - tgt_node_t *node; - int low; - - stkptr = stk; - node = &tree->nodes[leafno]; - while (node->parent) { - *stkptr++ = node; - node = node->parent; + tgt_node_t *stk[31]; + tgt_node_t **stkptr; + tgt_node_t *node; + int low; + + stkptr = stk; + node = &tree->nodes[leafno]; + while (node->parent) { + *stkptr++ = node; + node = node->parent; + } + + low = 0; + for (;;) { + if (low > node->low) { + node->low = low; + } else { + low = node->low; } - - low = 0; - for (;;) { - if (low > node->low) { - node->low = low; - } else { - low = node->low; - } - while (low < threshold && low < node->value) { - if (bio_read(1)) { - node->value = low; - } else { - ++low; - } - } - node->low = low; - if (stkptr == stk) { - break; - } - node = *--stkptr; + while (low < threshold && low < node->value) { + if (bio_read(1)) { + node->value = low; + } else { + ++low; + } + } + node->low = low; + if (stkptr == stk) { + break; } + node = *--stkptr; + } - return (node->value < threshold) ? 1 : 0; + return (node->value < threshold) ? 1 : 0; } diff --git a/libopenjpeg/tgt.h b/libopenjpeg/tgt.h index 230b0e25..bae85b62 100644 --- a/libopenjpeg/tgt.h +++ b/libopenjpeg/tgt.h @@ -28,17 +28,17 @@ #define __TGT_H typedef struct tgt_node { - struct tgt_node *parent; - int value; - int low; - int known; + struct tgt_node *parent; + int value; + int low; + int known; } tgt_node_t; typedef struct { - int numleafsh; - int numleafsv; - int numnodes; - tgt_node_t *nodes; + int numleafsh; + int numleafsv; + int numnodes; + tgt_node_t *nodes; } tgt_tree_t; -- 2.30.2