Update for version 1.0

This commit is contained in:
Yannick Verschueren 2004-02-13 09:54:31 +00:00
parent 8b1431ecce
commit 38828e8dcd
16 changed files with 2261 additions and 1293 deletions

View File

@ -42,19 +42,6 @@ int bio_numbytes() {
return bio_bp-bio_start; return bio_bp-bio_start;
} }
/// <summary>
/// Init encoder.
/// </summary>
/// <param name="bp">Output buffer</param>
/// <param name="len">Output buffer length</param>
void bio_init_enc(unsigned char *bp, int len) {
bio_start=bp;
bio_end=bp+len;
bio_bp=bp;
bio_buf=0;
bio_ct=8;
}
/// <summary> /// <summary>
/// Init decoder. /// Init decoder.
/// </summary> /// </summary>
@ -68,15 +55,14 @@ void bio_init_dec(unsigned char *bp, int len) {
bio_ct=0; bio_ct=0;
} }
/// <summary> int bio_byteout()
/// Write byte. {
/// </summary> bio_buf = (bio_buf << 8) & 0xffff;
int bio_byteout() { bio_ct = bio_buf == 0xff00 ? 7 : 8;
bio_buf=(bio_buf<<8)&0xffff; if (bio_bp >= bio_end)
bio_ct=bio_buf==0xff00?7:8; return 1;
if (bio_bp>=bio_end) return 1; //longjmp(j2k_error, 1); *bio_bp++ = bio_buf >> 8;
*bio_bp++=bio_buf>>8; return 0;
return 0;
} }
/// <summary> /// <summary>
@ -90,18 +76,6 @@ int bio_bytein() {
return 0; return 0;
} }
/// <summary>
/// Write bit.
/// </summary>
/// <param name="b">Bit to write (0 or 1)</param>
void bio_putbit(int b) {
if (bio_ct==0) {
bio_byteout();
}
bio_ct--;
bio_buf|=b<<bio_ct;
}
/// <summary> /// <summary>
/// Read bit. /// Read bit.
/// </summary> /// </summary>
@ -113,18 +87,6 @@ int bio_getbit() {
return (bio_buf>>bio_ct)&1; return (bio_buf>>bio_ct)&1;
} }
/// <summary>
/// Write bits.
/// </summary>
/// <param name="v">Value of bits</param>
/// <param name="n">Number of bits to write</param>
void bio_write(int v, int n) {
int i;
for (i=n-1; i>=0; i--) {
bio_putbit((v>>i)&1);
}
}
/// <summary> /// <summary>
/// Read bits. /// Read bits.
/// </summary> /// </summary>

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2001-2002, David Janssens * Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren * Copyright (c) 2003-2004, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -30,9 +30,7 @@
#define __BIO_H #define __BIO_H
int bio_numbytes(); int bio_numbytes();
void bio_init_enc(unsigned char *bp, int len);
void bio_init_dec(unsigned char *bp, int len); void bio_init_dec(unsigned char *bp, int len);
void bio_write(int v, int n);
int bio_read(int n); int bio_read(int n);
int bio_flush(); int bio_flush();
int bio_inalign(); int bio_inalign();

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2001-2002, David Janssens * Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren * Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -109,12 +109,14 @@ void cio_write(long long v, int n) {
/// <summary> /// <summary>
/// Read some bytes. /// Read some bytes.
/// </summary> /// </summary>
unsigned int cio_read(int n) { /* unsigned int cio_read(int n) { */
long long cio_read(int n) {
int i; int i;
unsigned int v; /*unsigned int v;*/
long long v;
v=0; v=0;
for (i=n-1; i>=0; i--) { for (i=n-1; i>=0; i--) {
v+=cio_bytein()<<(i<<3); v+=cio_bytein()<<(i<<3);
} }
return v; return v;
} }

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2001-2002, David Janssens * Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren * Copyright (c) 2003-2004, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -35,9 +35,10 @@ int cio_numbytes();
int cio_numbytesleft(); int cio_numbytesleft();
unsigned char *cio_getbp(); unsigned char *cio_getbp();
void cio_init(unsigned char *bp, int len); void cio_init(unsigned char *bp, int len);
//void cio_write(unsigned int v, int n); /* void cio_write(unsigned int v, int n); */
void cio_write(long long v, int n); void cio_write(long long v, int n);
unsigned int cio_read(int n); /* unsigned int cio_read(int n); */
long long cio_read(int n);
void cio_skip(int n); void cio_skip(int n);
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2001-2002, David Janssens * Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren * Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2001-2002, David Janssens * Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren * Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2001-2002, David Janssens * Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren * Copyright (c) 2003-2004, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -60,40 +60,40 @@
typedef struct typedef struct
{ {
int dx, dy; // XRsiz, YRsiz int dx, dy; /* XRsiz, YRsiz */
int prec; // precision int prec; /* precision */
int bpp; // deapth of image in bits int bpp; /* deapth of image in bits */
int sgnd; // signed int sgnd; /* signed */
int *data; // image-component data int *data; /* image-component data */
} j2k_comp_t; } j2k_comp_t;
typedef struct { typedef struct {
int x0, y0; // XOsiz, YOsiz int version;
int x1, y1; // Xsiz, Ysiz int x0, y0; /* XOsiz, YOsiz */
int numcomps; // number of components int x1, y1; /* Xsiz, Ysiz */
int index_on; // 0 = no index || 1 = index int numcomps; /* number of components */
//int PPT; int index_on; /* 0 = no index || 1 = index */
j2k_comp_t *comps; // image-components j2k_comp_t *comps; /* image-components */
} j2k_image_t; } j2k_image_t;
typedef struct { typedef struct {
int expn; // exponent int expn; /* exponent */
int mant; // mantissa int mant; /* mantissa */
} j2k_stepsize_t; } j2k_stepsize_t;
typedef struct { typedef struct {
int csty; // coding style int csty; /* coding style */
int numresolutions; // number of resolutions int numresolutions; /* number of resolutions */
int cblkw; // width of code-blocks int cblkw; /* width of code-blocks */
int cblkh; // height of code-blocks int cblkh; /* height of code-blocks */
int cblksty; // code-block coding style int cblksty; /* code-block coding style */
int qmfbid; // discrete wavelet transform identifier int qmfbid; /* discrete wavelet transform identifier */
int qntsty; // quantisation style int qntsty; /* quantisation style */
j2k_stepsize_t stepsizes[J2K_MAXBANDS]; // stepsizes used for quantisation j2k_stepsize_t stepsizes[J2K_MAXBANDS]; /* stepsizes used for quantisation */
int numgbits; // number of guard bits int numgbits; /* number of guard bits */
int roishift; // Region Of Interest shift int roishift; /* Region of Interest shift */
int prcw[J2K_MAXRLVLS]; // Precinct width int prcw[J2K_MAXRLVLS]; /* Precinct width */
int prch[J2K_MAXRLVLS]; // Precinct height int prch[J2K_MAXRLVLS]; /* Precinct height */
} j2k_tccp_t; } j2k_tccp_t;
typedef struct { typedef struct {
@ -105,118 +105,185 @@ typedef struct {
} j2k_poc_t; } j2k_poc_t;
typedef struct { typedef struct {
int csty; // coding style //int first;
int prg; // progression order int csty; /* coding style */
int numlayers; // number of layers int prg; /* progression order */
int mct; // multi-component transform identifier int numlayers; /* number of layers */
int rates[100]; // rates of layers int mct; /* multi-component transform identifier */
int numpocs; // number of progression order changes int rates[100]; /* rates of layers */
j2k_poc_t pocs[32]; // progression order changes int numpocs; /* number of progression order changes */
j2k_tccp_t *tccps; // tile-component coding parameters 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; } j2k_tcp_t;
typedef struct { typedef struct {
int tx0, ty0; // XTOsiz, YTOsiz int tx0, ty0; /* XTOsiz, YTOsiz */
int tdx, tdy; // XTsiz, YTsiz int tdx, tdy; /* XTsiz, YTsiz */
int tw, th; int tw, th;
j2k_tcp_t *tcps; // tile coding parameters unsigned char *ppm_data; /* packet header store there for futur use in t2_decode_packet */
int ppm; /* If ppm == 1 --> there was a PPM marker for the present tile */
int ppm_store; /* Use in case of multiple marker PPM (number of info already store) */
int ppm_previous; /* Use in case of multiple marker PPM (case on non-finished previous info) */
j2k_tcp_t *tcps; /* tile coding parameters */
} j2k_cp_t; } j2k_cp_t;
/* Packet information : Layer level */
typedef struct { typedef struct {
int len; int len; /* Length of the body of the packet */
int len_header; int len_header; /* Length of the header of the packet */
int offset; int offset; /* Offset of the body of the packet */
int offset_header; /* Offset of the header of the packet */
} info_layer_t; } info_layer_t;
/* Access to packet information : precinct level */
typedef struct { typedef struct {
info_layer_t *layer; info_layer_t *layer;
} info_prec_t; } info_prec_t;
/* Access to packet information : resolution level */
typedef struct { typedef struct {
info_prec_t *prec; info_prec_t *prec;
} info_reso_t; } info_reso_t;
/* Access to packet information : component level */
typedef struct { typedef struct {
info_reso_t *reso; info_reso_t *reso;
} info_compo_t; } info_compo_t;
typedef struct {
int num_tile; // Number of Tile
int start_pos; // Start position
int end_header; // End position of the header
int end_pos; // End position
int pw, ph; // number of precinct by tile
int num_packet; // number of packet in the tile
info_compo_t *compo; // component [packet]
} info_tile_t; // index struct
/* Information about the marker */
typedef struct { typedef struct {
int type; // type of marker [SIZ, QCD, POC, PPM, CRG] appearing only once int type; /* type of marker [SIZ, QCD, POC, PPM, CRG, COD] appearing only once */
int start_pos; // Start position of the marker int start_pos; /* Start position of the marker */
int len; // Length of the marker int len; /* Length of the marker */
} info_marker_t; // index struct } info_marker_t;
/* Multiple marker in tile header */
typedef struct{ typedef struct{
info_marker_t *COD; info_marker_t *COC; /* COC markers */
int num_COD; int num_COC; /* Number of COC marker */
info_marker_t *COC; int CzCOC; /* Current size of the vector COC */
int num_COC;
info_marker_t *RGN;
int num_RGN;
info_marker_t *QCC;
int num_QCC;
info_marker_t *TLM;
int num_TLM;
info_marker_t *PLM;
int num_PLM;
info_marker_t *PPM;
int num_PPM;
info_marker_t *COM;
int num_COM;
} info_marker_mul_t; // index struct
info_marker_t *RGN; /* RGN markers */
int num_RGN; /* Number of RGN marker */
int CzRGN; /* Current size of the vector RGN */
info_marker_t *QCC; /* QCC markers */
int num_QCC; /* Number of QCC marker */
int CzQCC; /* Current size of the vector QCC */
info_marker_t *PLT; /* PLT markers */
int num_PLT; /* Number of PLT marker */
int CzPLT; /* Current size of the vector PLT */
info_marker_t *PPT; /* PPT markers */
int num_PPT; /* Number of PPT marker */
int CzPPT; /* Current size of the vector PPT */
info_marker_t *COM; /* COM markers */
int num_COM; /* Number of COM marker */
int CzCOM; /* Current size of the vector COC */
} info_marker_mul_tile_t;
/* Information about each tile_part for a particulary tile */
typedef struct{
int start_pos; /* Start position of the tile_part */
int length; /* Length of the tile_part header + body */
int length_header; /* Length of the header */
int end_pos; /* End position of the tile part */
int end_header; /* End position of the tile part header */
int num_reso_AUX; /* Number of resolution level completed */
} info_tile_part_t;
/* Information about each tile */
typedef struct { typedef struct {
int index_on; int num_tile; /* Number of Tile */
int num; // numero of packet int pw, ph; /* number of precinct by tile */
int index_write; // writing the packet inthe index with t2_encode_packets int num_packet; /* number of packet in the tile */
int Im_w, Im_h; // Image width and Height info_compo_t *compo; /* component [packet] */
int Prog; // progression order
int Tile_x, Tile_y; // Number of Tile in X and Y info_marker_t *marker; /* information concerning markers inside image [only one apparition] */
info_marker_mul_tile_t marker_mul; /* information concerning markers inside image [multiple apparition] */
int num_marker; /* number of marker */
int numparts; /* number of tile_part for this tile */
info_tile_part_t *tile_parts; /* Information about each tile_part */
int Cztile_parts; /* Current size of the tile_parts vector */
} info_tile_t; /* index struct */
/* Multiple marker in main header */
typedef struct{
info_marker_t *COC; /* COC markers */
int num_COC; /* Number of COC marker */
int CzCOC; /* Current size of the vector COC */
info_marker_t *RGN; /* RGN markers */
int num_RGN; /* Number of RGN marker */
int CzRGN; /* Current size of the vector RGN */
info_marker_t *QCC; /* QCC markers */
int num_QCC; /* Number of QCC marker */
int CzQCC; /* Current size of the vector QCC */
info_marker_t *TLM; /* TLM markers */
int num_TLM; /* Number of TLM marker */
int CzTLM; /* Current size of the vector TLM */
info_marker_t *PLM; /* PLM markers */
int num_PLM; /* Number of PLM marker */
int CzPLM; /* Current size of the vector PLM */
info_marker_t *PPM; /* PPM markers */
int num_PPM; /* Number of PPM marker */
int CzPPM; /* Current size of the vector PPM */
info_marker_t *COM; /* COM markers */
int num_COM; /* Number of COM marker */
int CzCOM; /* Current size of the vector COM */
} info_marker_mul_t; /* index struct */
/* Information about image */
typedef struct {
int Im_w, Im_h; /* Image width and Height */
int Tile_x, Tile_y; /* Number of Tile in X and Y */
int tw, th; int tw, th;
int Comp; // Component numbers int pw, ph; /* nombre precinct in X and Y */
int Layer; // number of layer int pdx, pdy; /* size of precinct in X and Y */
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_t *tile; // information concerning tiles inside image
info_marker_t *marker; // information concerning markers inside image [only one apparition]
info_marker_mul_t marker_mul; // information concerning markers inside image [multiple apparition]
int num_marker; // number of marker
int num_packet_max; // MAximum number of packet
} info_image_t; // index struct
int Prog; /* progression order */
int Comp; /* Component numbers */
int Layer; /* number of layer */
int Decomposition; /* number of decomposition */
int Main_head_end; /* Main header position */
int codestream_size; /* codestream's size */
/* info_marker_t *marker; /* information concerning markers inside image [only one apparition] */
* Encode an image into a JPEG-2000 codestream info_marker_mul_t marker_mul; /* information concerning markers inside image [multiple apparition] */
* i: image to encode int num_marker; /* number of marker */
* cp: coding parameters
* dest: destination buffer int num_packet_max; /* Maximum number of packet */
* len: length of destination buffer
* index : index file name int num_max_tile_parts; /* Maximum number of tile-part */
*/ info_tile_t *tile; /* information concerning tiles inside image */
LIBJ2K_API int j2k_encode(j2k_image_t *i, j2k_cp_t *cp,char *outfile, int len,char *index); } info_image_t; /* index struct */
//LIBJ2K_API int j2k_encode(j2k_image_t *i, j2k_cp_t *cp,unsigned char *dest, int len);
/*
* Decode an image from a JPEG-2000 codestream
* src: source buffer
* len: length of source buffer
* i: decode image
* cp: coding parameters that were used to encode the image
*/
LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t **i, j2k_cp_t **cp);
#endif #endif

View File

@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2003, Yannick Verschueren * Copyright (c) 2003-2004, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -28,9 +28,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <setjmp.h>
#include <math.h>
#include <unistd.h>
#include "j2k.h" #include "j2k.h"
#include "cio.h" #include "cio.h"
@ -176,13 +173,19 @@ void jp2_write_colr(int BPC_ok, j2k_image_t *j2k_img)
cio_seek(lenp+len); cio_seek(lenp+len);
} }
/*
* Write the JP2H box
*
* JP2 Header box
*
*/
void jp2_write_jp2h(j2k_image_t *j2k_img) void jp2_write_jp2h(j2k_image_t *j2k_img)
{ {
int len, lenp, BPC_ok; int len, lenp, BPC_ok;
lenp=cio_tell(); lenp=cio_tell();
cio_skip(4); cio_skip(4);
cio_write(JP2_JP2H, 4); // JP2H cio_write(JP2_JP2H, 4); /* JP2H */
BPC_ok=jp2_write_ihdr(j2k_img); BPC_ok=jp2_write_ihdr(j2k_img);
@ -192,29 +195,53 @@ void jp2_write_jp2h(j2k_image_t *j2k_img)
len=cio_tell()-lenp; len=cio_tell()-lenp;
cio_seek(lenp); cio_seek(lenp);
cio_write(len,4); // L cio_write(len,4); /* L */
cio_seek(lenp+len); cio_seek(lenp+len);
} }
/*
* Write the FTYP box
*
* File type box
*
*/
void jp2_write_ftyp() void jp2_write_ftyp()
{ {
int len, lenp; int len, lenp;
lenp=cio_tell(); lenp=cio_tell();
cio_skip(4); cio_skip(4);
cio_write(JP2_FTYP, 4); // FTYP cio_write(JP2_FTYP, 4); /* FTYP */
cio_write(JP2,4); // BR cio_write(JP2,4); /* BR */
cio_write(0,4); // MinV cio_write(0,4); /* MinV */
cio_write(JP2,4); // CL0 : JP2 cio_write(JP2,4); /* CL0 : JP2 */
cio_write(JPIP_JPIP,4); // CL1 : JPIP cio_write(JPIP_JPIP,4); /* CL1 : JPIP */
len=cio_tell()-lenp; len=cio_tell()-lenp;
cio_seek(lenp); cio_seek(lenp);
cio_write(len,4); // L cio_write(len,4); /* L */
cio_seek(lenp+len); cio_seek(lenp+len);
} }
/*
* Read the FTYP box
*
* File type box
*
*/
void jp2_read_ftyp(int length)
{
int BR, MinV, type, i;
BR = cio_read(4); /* BR */
MinV = cio_read(4); /* MinV */
length-=8;
for (i=length/4;i>0;i--)
type = cio_read(4); /* CLi : JP2, JPIP */
}
int jp2_write_jp2c(char *J2K_file) int jp2_write_jp2c(char *J2K_file)
{ {
int len, lenp, totlen, i; int len, lenp, totlen, i;
@ -257,3 +284,18 @@ void jp2_write_jp()
cio_write(len,4); // L cio_write(len,4); // L
cio_seek(lenp+len); cio_seek(lenp+len);
} }
/*
* Read the JP box
*
* JPEG 2000 signature
*
* return 1 if error else 0
*/
int jp2_read_jp()
{
if (0x0d0a870a!=cio_read(4))
return 1;
else
return 0;
}

View File

@ -24,6 +24,8 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef __JP2_H
#define __JP2_H
#include "j2k.h" #include "j2k.h"
@ -38,3 +40,5 @@ void jp2_write_ftyp();
int jp2_write_jp2c(char *J2K_file); int jp2_write_jp2c(char *J2K_file);
void jp2_write_jp(); void jp2_write_jp();
#endif

View File

@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2003, Yannick Verschueren * Copyright (c) 2003-2004, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -37,187 +37,355 @@
#include "tcd.h" #include "tcd.h"
#include "int.h" #include "int.h"
#define JPIP_CIDX 0x63696478 #define JPIP_CIDX 0x63696478 /* Codestream index */
#define JPIP_CPTR 0x63707472 #define JPIP_CPTR 0x63707472 /* Codestream Finder Box */
#define JPIP_MANF 0x6d616e66 #define JPIP_MANF 0x6d616e66 /* Manifest Box */
#define JPIP_FAIX 0x66616978 #define JPIP_FAIX 0x66616978 /* Fragment array Index box */
#define JPIP_MHIX 0x6d686978 #define JPIP_MHIX 0x6d686978 /* Main Header Index Table */
#define JPIP_TPIX 0x74706978 #define JPIP_TPIX 0x74706978 /* Tile-part Index Table box */
#define JPIP_THIX 0x74686978 #define JPIP_THIX 0x74686978 /* Tile header Index Table box */
#define JPIP_PPIX 0x70706978 #define JPIP_PPIX 0x70706978 /* Precinct Packet Index Table box */
#define JPIP_PHIX 0x70686978 #define JPIP_PHIX 0x70686978 /* Packet Header index Table */
#define JPIP_FIDX 0x66696478 #define JPIP_FIDX 0x66696478 /* File Index */
#define JPIP_FPTR 0x66707472 #define JPIP_FPTR 0x66707472 /* File Finder */
#define JPIP_PRXY 0x70727879 #define JPIP_PRXY 0x70727879 /* Proxy boxes */
#define JPIP_IPTR 0x69707472 #define JPIP_IPTR 0x69707472 /* Index finder box */
#define JPIP_PHLD 0x70686c64 /* Place holder */
#define JP2C 0x6a703263 #define JP2C 0x6a703263
static info_marker_t marker_jpip[32], marker_local_jpip[32]; // SIZE to precise ! //static info_marker_t marker_jpip[32], marker_local_jpip[32]; /* SIZE to precise ! */
static int num_marker_jpip, num_marker_local_jpip; //static int num_marker_jpip, num_marker_local_jpip;
void jpip_write_cptr(int offset, info_image_t img) // Codestream finder box (box) /*
* Write the CPTR box
*
* Codestream finder box (box)
*
*/
void jpip_write_cptr(int offset, info_image_t img)
{ {
int len, lenp; int len, lenp;
lenp=cio_tell(); lenp=cio_tell();
cio_skip(4); // L [at the end] cio_skip(4); /* L [at the end] */
cio_write(JPIP_CPTR,4); // T cio_write(JPIP_CPTR,4); /* T */
cio_write(0,2); // DR A PRECISER !! cio_write(0,2); /* DR A PRECISER !! */
cio_write(0,2); // CONT cio_write(0,2); /* CONT */
cio_write(offset,8); // COFF A PRECISER !! cio_write(offset,8); /* COFF A PRECISER !! */
cio_write(img.codestream_size,8); // CLEN cio_write(img.codestream_size,8); /* CLEN */
len=cio_tell()-lenp; len=cio_tell()-lenp;
cio_seek(lenp); cio_seek(lenp);
cio_write(len, 4); // L cio_write(len, 4); /* L */
cio_seek(lenp+len); cio_seek(lenp+len);
} }
void jpip_write_manf(int second, int v, info_marker_t *marker) // Manifest box (box) /*
* Read the CPTR box
*
* Codestream finder box (box)
*
*/
void jpip_read_cptr()
{
int DR, CONT;
long long Coff, codestream_size;
DR = cio_read(2); /* DR */
CONT = cio_read(2); /* CONT */
Coff = cio_read(8); /* COFF */
codestream_size = cio_read(8); /* CLEN */
}
/*
* Write the MANF box
*
* Manifest box (box)
*
*/
void jpip_write_manf(int second, int v, info_marker_t *marker)
{ {
int len, lenp, i; int len, lenp, i;
lenp=cio_tell(); lenp=cio_tell();
cio_skip(4); // L [at the end] cio_skip(4); /* L [at the end] */
cio_write(JPIP_MANF,4); // T cio_write(JPIP_MANF,4); /* T */
if (second) // Write only during the second pass if (second) /* Write only during the second pass */
{ {
for(i=0;i<v;i++) for(i=0;i<v;i++)
{ {
cio_write(marker[i].len,4); cio_write(marker[i].len,4); /* Marker length */
cio_write(marker[i].type,4); cio_write(marker[i].type,4); /* Marker type */
} }
} }
len=cio_tell()-lenp; len=cio_tell()-lenp;
cio_seek(lenp); cio_seek(lenp);
cio_write(len, 4); // L cio_write(len, 4); /* L */
cio_seek(lenp+len); cio_seek(lenp+len);
} }
int jpip_write_mhix(info_image_t img) // Main Header Index Table (box) /*
* Read the MANF box
*
* Manifest box (box)
*
*/
void jpip_read_manf(int len)
{
int i, v, marker_len, marker_type;
v = (len - 8)/ 8;
for(i=0;i<v;i++)
{
marker_len = cio_read(4); /* Marker length */
marker_type = cio_read(4); /* Marker type */
}
}
/*
* Write the MHIX box
*
* Main Header Index Table (box)
*
*/
int jpip_write_mhix(info_image_t img, int status, int tileno)
{ {
int len, lenp, i; int len, lenp, i;
info_tile_t *tile;
lenp=cio_tell(); lenp=cio_tell();
cio_skip(4); // L [at the end] cio_skip(4); /* L [at the end] */
cio_write(JPIP_MHIX, 4); // CIDX cio_write(JPIP_MHIX, 4); /* MHIX */
cio_write(img.Main_head_end,8); // TLEN if (status) /* MAIN HEADER */
for(i=0;i<img.num_marker;i++) // Marker restricted to 1 apparition
{ {
cio_write(img.marker[i].type,2); cio_write(img.Main_head_end,8); /* TLEN */
cio_write(0,2);
cio_write(img.marker[i].start_pos,8); for(i = 0; i < img.num_marker; i++) /* Marker restricted to 1 apparition */
cio_write(img.marker[i].len,2); {
cio_write(img.marker[i].type, 2);
cio_write(0, 2);
cio_write(img.marker[i].start_pos, 8);
cio_write(img.marker[i].len, 2);
}
/* Marker NOT restricted to 1 apparition */
for(i = img.marker_mul.num_COC - 1; i >= 0; i--) /* COC */
{
cio_write(img.marker_mul.COC[i].type, 2);
cio_write(i, 2);
cio_write(img.marker_mul.COC[i].start_pos, 8);
cio_write(img.marker_mul.COC[i].len, 2);
}
for(i = img.marker_mul.num_RGN - 1; i >= 0; i--) /* RGN */
{
cio_write(img.marker_mul.RGN[i].type, 2);
cio_write(i, 2);
cio_write(img.marker_mul.RGN[i].start_pos, 8);
cio_write(img.marker_mul.RGN[i].len, 2);
}
for(i = img.marker_mul.num_QCC - 1; i >= 0; i--) /* QCC */
{
cio_write(img.marker_mul.QCC[i].type, 2);
cio_write(i, 2);
cio_write(img.marker_mul.QCC[i].start_pos, 8);
cio_write(img.marker_mul.QCC[i].len, 2);
}
for(i = img.marker_mul.num_TLM - 1; i >= 0; i--) /* TLM */
{
cio_write(img.marker_mul.TLM[i].type, 2);
cio_write(i, 2);
cio_write(img.marker_mul.TLM[i].start_pos, 8);
cio_write(img.marker_mul.TLM[i].len, 2);
}
for(i = img.marker_mul.num_PLM - 1; i >= 0; i--) /* PLM */
{
cio_write(img.marker_mul.PLM[i].type, 2);
cio_write(i, 2);
cio_write(img.marker_mul.PLM[i].start_pos, 8);
cio_write(img.marker_mul.PLM[i].len, 2);
}
for(i = img.marker_mul.num_PPM - 1; i >= 0; i--) /* PPM */
{
cio_write(img.marker_mul.PPM[i].type, 2);
cio_write(i, 2);
cio_write(img.marker_mul.PPM[i].start_pos, 8);
cio_write(img.marker_mul.PPM[i].len, 2);
}
for(i = img.marker_mul.num_COM - 1; i >= 0; i--) /* COM */
{
cio_write(img.marker_mul.COM[i].type, 2);
cio_write(i, 2);
cio_write(img.marker_mul.COM[i].start_pos, 8);
cio_write(img.marker_mul.COM[i].len, 2);
}
} }
else /* TILE HEADER */
// Marker NOT restricted to 1 apparition
for(i=img.marker_mul.num_COD-1;i>=0;i--) // COD
{ {
cio_write(img.marker_mul.COD[i].type,2); tile = &img.tile[tileno];
cio_write(i,2); cio_write(tile->tile_parts[0].length_header, 8); /* TLEN */
cio_write(img.marker_mul.COD[i].start_pos,8);
cio_write(img.marker_mul.COD[i].len,2);
}
for(i=img.marker_mul.num_COC-1;i>=0;i--) // COC for(i = 0; i < tile->num_marker; i++) /* Marker restricted to 1 apparition */
{ {
cio_write(img.marker_mul.COC[i].type,2); cio_write(tile->marker[i].type, 2);
cio_write(i,2); cio_write(0, 2);
cio_write(img.marker_mul.COC[i].start_pos,8); cio_write(tile->marker[i].start_pos, 8);
cio_write(img.marker_mul.COC[i].len,2); cio_write(tile->marker[i].len, 2);
} }
for(i=img.marker_mul.num_RGN-1;i>=0;i--) // RGN /* Marker NOT restricted to 1 apparition */
{ for(i = tile->marker_mul.num_COC - 1; i >= 0; i--) /* COC */
cio_write(img.marker_mul.RGN[i].type,2); {
cio_write(i,2); cio_write(tile->marker_mul.COC[i].type, 2);
cio_write(img.marker_mul.RGN[i].start_pos,8); cio_write(i, 2);
cio_write(img.marker_mul.RGN[i].len,2); cio_write(tile->marker_mul.COC[i].start_pos, 8);
} cio_write(tile->marker_mul.COC[i].len, 2);
}
for(i=img.marker_mul.num_QCC-1;i>=0;i--) // QCC for(i = tile->marker_mul.num_RGN - 1; i >= 0; i--) /* RGN */
{ {
cio_write(img.marker_mul.QCC[i].type,2); cio_write(tile->marker_mul.RGN[i].type, 2);
cio_write(i,2); cio_write(i, 2);
cio_write(img.marker_mul.QCC[i].start_pos,8); cio_write(tile->marker_mul.RGN[i].start_pos, 8);
cio_write(img.marker_mul.QCC[i].len,2); cio_write(tile->marker_mul.RGN[i].len, 2);
} }
for(i=img.marker_mul.num_TLM-1;i>=0;i--) // TLM for(i = tile->marker_mul.num_QCC - 1; i >= 0; i--) /* QCC */
{ {
cio_write(img.marker_mul.TLM[i].type,2); cio_write(tile->marker_mul.QCC[i].type, 2);
cio_write(i,2); cio_write(i, 2);
cio_write(img.marker_mul.TLM[i].start_pos,8); cio_write(tile->marker_mul.QCC[i].start_pos, 8);
cio_write(img.marker_mul.TLM[i].len,2); cio_write(tile->marker_mul.QCC[i].len, 2);
} }
for(i=img.marker_mul.num_PLM-1;i>=0;i--) // PLM for(i = tile->marker_mul.num_PLT - 1; i >= 0; i--) /* PLT */
{ {
cio_write(img.marker_mul.PLM[i].type,2); cio_write(tile->marker_mul.PLT[i].type,2);
cio_write(i,2); cio_write(i,2);
cio_write(img.marker_mul.PLM[i].start_pos,8); cio_write(tile->marker_mul.PLT[i].start_pos,8);
cio_write(img.marker_mul.PLM[i].len,2); cio_write(tile->marker_mul.PLT[i].len,2);
} }
for(i=img.marker_mul.num_COM-1;i>=0;i--) // COM for(i = tile->marker_mul.num_PPT - 1; i >= 0; i--) /* PPT */
{ {
cio_write(img.marker_mul.COM[i].type,2); cio_write(tile->marker_mul.PPT[i].type, 2);
cio_write(i,2); cio_write(i, 2);
cio_write(img.marker_mul.COM[i].start_pos,8); cio_write(tile->marker_mul.PPT[i].start_pos, 8);
cio_write(img.marker_mul.COM[i].len,2); cio_write(tile->marker_mul.PPT[i].len, 2);
}
for(i = tile->marker_mul.num_COM - 1; i >= 0; i--) /* COM */
{
cio_write(tile->marker_mul.COM[i].type, 2);
cio_write(i, 2);
cio_write(tile->marker_mul.COM[i].start_pos, 8);
cio_write(tile->marker_mul.COM[i].len, 2);
}
} }
len=cio_tell()-lenp; len=cio_tell()-lenp;
cio_seek(lenp); cio_seek(lenp);
cio_write(len, 4); // L cio_write(len, 4); /* L */
cio_seek(lenp+len); cio_seek(lenp+len);
return len; return len;
} }
int jpip_write_faix(int v, int compno, info_image_t img, j2k_cp_t *j2k_cp) // Fragment array Index box /*
* Read the MHIX box
*
* Main Header Index Table (box)
*
*/
void jpip_read_mhix(int len)
{ {
int len, lenp, i; int i, v, marker_type, marker_start_pos, marker_len, marker_remains;
int version = 0;
v = (len - 8) / 14;
for (i=0; i<v ; i++)
{
marker_type = cio_read(2); /* Type of the marker */
marker_remains = cio_read(2); /* Number of same markers following */
marker_start_pos = cio_read(2); /* Start position of the marker */
marker_len = cio_read(2); /* Length of the marker */
}
}
/*
* Write the FAIX box
*
* Fragment array Index box (box)
*
*/
int jpip_write_faix(int v, int compno, info_image_t img, j2k_cp_t *j2k_cp, int version)
{
int len, lenp, i, j;
/*int version = 0;*/
int tileno, resno, precno, layno, num_packet=0; int tileno, resno, precno, layno, num_packet=0;
lenp=cio_tell(); lenp=cio_tell();
cio_skip(4); // L [at the end] cio_skip(4); /* L [at the end] */
cio_write(JPIP_FAIX, 4); // MHIX cio_write(JPIP_FAIX, 4); /* FAIX */
cio_write(version,1); // Version 0 = 4 bytes cio_write(version,1); /* Version 0 = 4 bytes */
switch(v) switch(v)
{ {
case 0: // TPIX case 0: /* TPIX */
cio_write(1,version?8:4); // NMAX cio_write(img.num_max_tile_parts,(version & 0x01)?8:4); /* NMAX */
cio_write(img.tw*img.th,version?8:4); // M cio_write(img.tw*img.th,(version & 0x01)?8:4); /* M */
for (i=0;i<img.tw*img.th;i++) for (i = 0; i < img.tw*img.th; i++)
{ {
cio_write(img.tile[i].start_pos,version?8:4); for (j = 0; j < img.tile[i].numparts ; j++)
cio_write(img.tile[i].end_pos-img.tile[i].start_pos,version?8:4); {
cio_write(img.tile[i].tile_parts[j].start_pos,(version & 0x01)?8:4); /* start position */
cio_write(img.tile[i].tile_parts[j].length,(version & 0x01)?8:4); /* length */
if (version & 0x02)
cio_write(img.tile[i].tile_parts[j].num_reso_AUX,4); /* Aux_i,j : Auxiliary value */
//cio_write(0,4);
}
/* PADDING */
while (j < img.num_max_tile_parts)
{
cio_write(0,(version & 0x01)?8:4); /* start position */
cio_write(0,(version & 0x01)?8:4); /* length */
if (version & 0x02)
cio_write(0,4); /* Aux_i,j : Auxiliary value */
j++;
}
} }
break; break;
case 1: // THIX /* case 1: */ /* THIX */
cio_write(1,version?8:4); // NMAX /* cio_write(1,(version & 0x01)?8:4); */ /* NMAX */
cio_write(img.tw*img.th,version?8:4); // M /* cio_write(img.tw*img.th,(version & 0x01)?8:4); */ /* M */
for (i=0;i<img.tw*img.th;i++) /* for (i=0;i<img.tw*img.th;i++) */
{ /* { */
cio_write(img.tile[i].start_pos,version?8:4); /* cio_write(img.tile[i].start_pos,(version & 0x01)?8:4); */ /* start position */
cio_write(img.tile[i].end_header-img.tile[i].start_pos,version?8:4); /* cio_write(img.tile[i].end_header-img.tile[i].start_pos,(version & 0x01)?8:4); */ /* length */
} /* if (version & 0x02)*/
break; /* cio_write(0,4); */ /* Aux_i,j : Auxiliary value */
/* } */
/* break; */
case 2: // PPIX NOT FINISHED !! case 2: /* PPIX NOT FINISHED !! */
cio_write(img.num_packet_max,version?8:4); // NMAX cio_write(img.num_packet_max,(version & 0x01)?8:4); /* NMAX */
cio_write(img.tw*img.th,version?8:4); // M cio_write(img.tw*img.th,(version & 0x01)?8:4); /* M */
for(tileno=0;tileno<img.tw*img.th;tileno++) for(tileno=0;tileno<img.tw*img.th;tileno++)
{ {
info_tile_t *tile_Idx = &img.tile[tileno]; info_tile_t *tile_Idx = &img.tile[tileno];
info_compo_t *compo_Idx = &tile_Idx->compo[compno]; info_compo_t *compo_Idx = &tile_Idx->compo[compno];
int correction; int correction;
num_packet=0;
if(j2k_cp->tcps[tileno].csty&J2K_CP_CSTY_EPH) if(j2k_cp->tcps[tileno].csty&J2K_CP_CSTY_EPH)
correction=3; correction=3;
else else
@ -231,30 +399,37 @@ int jpip_write_faix(int v, int compno, info_image_t img, j2k_cp_t *j2k_cp) // Fr
for(layno=0;layno<img.Layer;layno++) for(layno=0;layno<img.Layer;layno++)
{ {
info_layer_t *layer_Idx = &prec_Idx->layer[layno]; info_layer_t *layer_Idx = &prec_Idx->layer[layno];
cio_write(layer_Idx->offset,version?8:4); cio_write(layer_Idx->offset,(version & 0x01)?8:4); /* start position */
cio_write((layer_Idx->len_header-correction)?0:layer_Idx->len,version?8:4); cio_write((layer_Idx->len_header-correction)?0:layer_Idx->len,(version & 0x01)?8:4); /* length */
if (version & 0x02)
cio_write(0,4); /* Aux_i,j : Auxiliary value */
num_packet++; num_packet++;
} }
} }
} }
/* PADDING */
while (num_packet < img.num_packet_max)
{
cio_write(0,(version & 0x01)?8:4); /* start position */
cio_write(0,(version & 0x01)?8:4); /* length */
if (version & 0x02)
cio_write(0,4); /* Aux_i,j : Auxiliary value */
num_packet++;
}
} }
// PADDING
while (num_packet<img.num_packet_max)
{
cio_write(0,version?8:4);
cio_write(0,version?8:4);
num_packet++;
}
break; break;
case 3: // PHIX NOT FINISHED !! case 3: /* PHIX NOT FINISHED !! */
cio_write(img.num_packet_max,version?8:4); // NMAX cio_write(img.num_packet_max,(version & 0x01)?8:4); /* NMAX */
cio_write(img.tw*img.th,version?8:4); // M cio_write(img.tw*img.th,(version & 0x01)?8:4); /* M */
for(tileno=0;tileno<img.tw*img.th;tileno++) for(tileno=0;tileno<img.tw*img.th;tileno++)
{ {
info_tile_t *tile_Idx = &img.tile[tileno]; info_tile_t *tile_Idx = &img.tile[tileno];
info_compo_t *compo_Idx = &tile_Idx->compo[compno]; info_compo_t *compo_Idx = &tile_Idx->compo[compno];
int correction; int correction;
num_packet = 0;
if(j2k_cp->tcps[tileno].csty&J2K_CP_CSTY_EPH) if(j2k_cp->tcps[tileno].csty&J2K_CP_CSTY_EPH)
correction=3; correction=3;
else else
@ -268,126 +443,209 @@ int jpip_write_faix(int v, int compno, info_image_t img, j2k_cp_t *j2k_cp) // Fr
for(layno=0;layno<img.Layer;layno++) for(layno=0;layno<img.Layer;layno++)
{ {
info_layer_t *layer_Idx = &prec_Idx->layer[layno]; info_layer_t *layer_Idx = &prec_Idx->layer[layno];
cio_write(layer_Idx->offset,version?8:4); cio_write(layer_Idx->offset_header,(version & 0x01)?8:4); /* start position */
cio_write((layer_Idx->len_header-correction)?0:layer_Idx->len_header,version?8:4); cio_write((layer_Idx->len_header-correction)?0:layer_Idx->len_header,(version & 0x01)?8:4); /* length */
if (version & 0x02)
cio_write(0,4); /* Aux_i,j : Auxiliary value */
num_packet++; num_packet++;
} }
} }
} }
} /* PADDING */
// PADDING while (num_packet<img.num_packet_max)
while (num_packet<img.num_packet_max) {
{ cio_write(0,(version & 0x01)?8:4); /* start position */
cio_write(0,version?8:4); cio_write(0,(version & 0x01)?8:4); /* length */
cio_write(0,version?8:4); if (version & 0x02)
num_packet++; cio_write(0,4); /* Aux_i,j : Auxiliary value */
num_packet++;
}
} }
break; break;
} }
len=cio_tell()-lenp; len=cio_tell()-lenp;
cio_seek(lenp); cio_seek(lenp);
cio_write(len, 4); // L cio_write(len, 4); /* L */
cio_seek(lenp+len); cio_seek(lenp+len);
return len; return len;
} }
int jpip_write_tpix(info_image_t img, j2k_cp_t *j2k_cp) // Tile-part Index Table box (superbox) /*
* Write the TPIX box
*
* Tile-part Index table box (superbox)
*
*/
int jpip_write_tpix(info_image_t img, j2k_cp_t *j2k_cp, int version)
{ {
int len, lenp; int len, lenp;
lenp=cio_tell(); lenp=cio_tell();
cio_skip(4); // L [at the end] cio_skip(4); /* L [at the end] */
cio_write(JPIP_TPIX, 4); // TPIX cio_write(JPIP_TPIX, 4); /* TPIX */
jpip_write_faix(0,0,img, j2k_cp); jpip_write_faix(0,0,img, j2k_cp, version);
len=cio_tell()-lenp; len=cio_tell()-lenp;
cio_seek(lenp); cio_seek(lenp);
cio_write(len, 4); // L cio_write(len, 4); /* L */
cio_seek(lenp+len); cio_seek(lenp+len);
return len; return len;
} }
int jpip_write_thix(info_image_t img, j2k_cp_t *j2k_cp) // Tile header Index Table box (superbox) /*
* Write the THIX box
*
* Tile header Index table box (superbox)
*
*/
//int jpip_write_thix(info_image_t img, j2k_cp_t *j2k_cp)
// {
// int len, lenp;
// lenp=cio_tell();
// cio_skip(4); /* L [at the end] */
// cio_write(JPIP_THIX, 4); /* THIX */
// jpip_write_faix(1,0,img, j2k_cp);
// len=cio_tell()-lenp;
// cio_seek(lenp);
// cio_write(len, 4); /* L */
// cio_seek(lenp+len);
// return len;
//}
int jpip_write_thix(info_image_t img, j2k_cp_t *j2k_cp)
{ {
int len, lenp; int len, lenp, i;
lenp=cio_tell(); int tileno;
cio_skip(4); // L [at the end] info_marker_t *marker;
cio_write(JPIP_THIX, 4); // THIX int num_marker_local_jpip;
jpip_write_faix(1,0,img, j2k_cp); marker = (info_marker_t*)calloc(sizeof(info_marker_t), j2k_cp->tw*j2k_cp->th);
len=cio_tell()-lenp; for ( i = 0; i < 2 ; i++ )
cio_seek(lenp); {
cio_write(len, 4); // L if (i) cio_seek(lenp);
cio_seek(lenp+len);
lenp = cio_tell();
cio_skip(4); /* L [at the end] */
cio_write(JPIP_THIX, 4); /* THIX */
jpip_write_manf(i, j2k_cp->tw*j2k_cp->th, marker);
num_marker_local_jpip=img.Comp;
for (tileno = 0; tileno < j2k_cp->tw*j2k_cp->th; tileno++)
{
marker[tileno].len = jpip_write_mhix(img, 1, tileno);
marker[tileno].type = JPIP_MHIX;
}
len=cio_tell()-lenp;
cio_seek(lenp);
cio_write(len, 4); /* L */
cio_seek(lenp+len);
}
free(marker);
return len; return len;
} }
/*
int jpip_write_ppix(info_image_t img,j2k_cp_t *j2k_cp) // Precinct Packet Index Table box (superbox) * Write the PPIX box
*
* Precinct Packet Index table box (superbox)
*
*/
int jpip_write_ppix(info_image_t img,j2k_cp_t *j2k_cp)
{ {
int len, lenp, compno, i; int len, lenp, compno, i;
info_marker_t *marker;
int num_marker_local_jpip;
marker = (info_marker_t*)calloc(sizeof(info_marker_t), img.Comp);
for (i=0;i<2;i++) for (i=0;i<2;i++)
{ {
if (i) cio_seek(lenp); if (i) cio_seek(lenp);
lenp=cio_tell(); lenp=cio_tell();
cio_skip(4); // L [at the end] cio_skip(4); /* L [at the end] */
cio_write(JPIP_PPIX, 4); // PPIX cio_write(JPIP_PPIX, 4); /* PPIX */
jpip_write_manf(i,img.Comp,marker_local_jpip); jpip_write_manf(i,img.Comp,marker);
num_marker_local_jpip=img.Comp; num_marker_local_jpip=img.Comp;
for (compno=0; compno<img.Comp; compno++) for (compno=0; compno<img.Comp; compno++)
{ {
marker_local_jpip[compno].len=jpip_write_faix(2,compno,img, j2k_cp); marker[compno].len=jpip_write_faix(2,compno,img, j2k_cp, 0);
marker_local_jpip[compno].type=JPIP_FAIX; marker[compno].type=JPIP_FAIX;
} }
len=cio_tell()-lenp; len=cio_tell()-lenp;
cio_seek(lenp); cio_seek(lenp);
cio_write(len, 4); // L cio_write(len, 4); /* L */
cio_seek(lenp+len); cio_seek(lenp+len);
} }
free(marker);
return len; return len;
} }
int jpip_write_phix(info_image_t img, j2k_cp_t *j2k_cp) // Packet Header Index Table Box (superbox) /*
* Write the PHIX box
*
* Packet Header Index table box (superbox)
*
*/
int jpip_write_phix(info_image_t img, j2k_cp_t *j2k_cp)
{ {
int len, lenp=0, compno, i; int len, lenp=0, compno, i;
info_marker_t *marker;
marker = (info_marker_t*)calloc(sizeof(info_marker_t), img.Comp);
for (i=0;i<2;i++) for (i=0;i<2;i++)
{ {
if (i) cio_seek(lenp); if (i) cio_seek(lenp);
lenp=cio_tell(); lenp=cio_tell();
cio_skip(4); // L [at the end] cio_skip(4); /* L [at the end] */
cio_write(JPIP_PHIX, 4); // PHIX cio_write(JPIP_PHIX, 4); /* PHIX */
jpip_write_manf(i,img.Comp,marker_local_jpip); jpip_write_manf(i,img.Comp,marker);
for (compno=0; compno<img.Comp; compno++) for (compno=0; compno<img.Comp; compno++)
{ {
marker_local_jpip[compno].len=jpip_write_faix(3,compno,img, j2k_cp); marker[compno].len=jpip_write_faix(3,compno,img, j2k_cp, 0);
marker_local_jpip[compno].type=JPIP_FAIX; marker[compno].type=JPIP_FAIX;
} }
len=cio_tell()-lenp; len=cio_tell()-lenp;
cio_seek(lenp); cio_seek(lenp);
cio_write(len, 4); // L cio_write(len, 4); /* L */
cio_seek(lenp+len); cio_seek(lenp+len);
} }
free(marker);
return len; return len;
} }
int jpip_write_cidx(int offset, info_image_t img, j2k_cp_t *j2k_cp) // Codestream index box (superbox) /*
* Write the CIDX box
*
* Codestream Index box (superbox)
*
*/
int jpip_write_cidx(int offset, info_image_t img, j2k_cp_t *j2k_cp, int version)
{ {
int len, lenp=0, i; int len, lenp = 0, i;
num_marker_jpip=0; info_marker_t *marker_jpip;
int num_marker_jpip = 0;
marker_jpip = (info_marker_t*)calloc(sizeof(info_marker_t), 32);
for (i=0;i<2;i++) for (i=0;i<2;i++)
{ {
@ -396,17 +654,18 @@ int jpip_write_cidx(int offset, info_image_t img, j2k_cp_t *j2k_cp) // Codestrea
lenp=cio_tell(); lenp=cio_tell();
cio_skip(4); // L [at the end] cio_skip(4); /* L [at the end] */
cio_write(JPIP_CIDX, 4); // CIDX cio_write(JPIP_CIDX, 4); /* CIDX */
jpip_write_cptr(offset, img); jpip_write_cptr(offset, img);
jpip_write_manf(i,num_marker_jpip, marker_jpip); // A definir
jpip_write_manf(i,num_marker_jpip, marker_jpip);
num_marker_jpip=0; num_marker_jpip=0;
marker_jpip[num_marker_jpip].len=jpip_write_mhix(img); marker_jpip[num_marker_jpip].len=jpip_write_mhix(img, 0, 0);
marker_jpip[num_marker_jpip].type=JPIP_MHIX; marker_jpip[num_marker_jpip].type=JPIP_MHIX;
num_marker_jpip++; num_marker_jpip++;
marker_jpip[num_marker_jpip].len=jpip_write_tpix(img, j2k_cp); marker_jpip[num_marker_jpip].len=jpip_write_tpix(img, j2k_cp, version);
marker_jpip[num_marker_jpip].type=JPIP_TPIX; marker_jpip[num_marker_jpip].type=JPIP_TPIX;
num_marker_jpip++; num_marker_jpip++;
@ -424,65 +683,86 @@ int jpip_write_cidx(int offset, info_image_t img, j2k_cp_t *j2k_cp) // Codestrea
len=cio_tell()-lenp; len=cio_tell()-lenp;
cio_seek(lenp); cio_seek(lenp);
cio_write(len, 4); // L cio_write(len, 4); /* L */
cio_seek(lenp+len); cio_seek(lenp+len);
} }
free(marker_jpip);
return len; return len;
} }
/*
* Write the IPTR box
*
* Index Finder box
*
*/
void jpip_write_iptr(int offset, int length) void jpip_write_iptr(int offset, int length)
{ {
int len, lenp; int len, lenp;
lenp=cio_tell(); lenp=cio_tell();
cio_skip(4); // L [at the end] cio_skip(4); /* L [at the end] */
cio_write(JPIP_IPTR, 4); // IPTR cio_write(JPIP_IPTR, 4); /* IPTR */
cio_write(offset,8); cio_write(offset,8);
cio_write(length,8); cio_write(length,8);
len=cio_tell()-lenp; len=cio_tell()-lenp;
cio_seek(lenp); cio_seek(lenp);
cio_write(len, 4); // L cio_write(len, 4); /* L */
cio_seek(lenp+len); cio_seek(lenp+len);
} }
/*
* Write the PRXY box
*
* proxy (box)
*
*/
void jpip_write_prxy(int offset_jp2c, int length_jp2c, int offset_idx, int length_idx) void jpip_write_prxy(int offset_jp2c, int length_jp2c, int offset_idx, int length_idx)
{ {
int len, lenp; int len, lenp;
lenp=cio_tell(); lenp=cio_tell();
cio_skip(4); // L [at the end] cio_skip(4); /* L [at the end] */
cio_write(JPIP_PRXY, 4); // IPTR cio_write(JPIP_PRXY, 4); /* IPTR */
cio_write(offset_jp2c,8); // OOFF cio_write(offset_jp2c,8); /* OOFF */
cio_write(length_jp2c,4); // OBH part 1 cio_write(length_jp2c,4); /* OBH part 1 */
cio_write(JP2C,4); // OBH part 2 cio_write(JP2C,4); /* OBH part 2 */
cio_write(1,1); // NI cio_write(1,1); /* NI */
cio_write(offset_idx,8); // IOFF cio_write(offset_idx,8); /* IOFF */
cio_write(length_idx,4); // IBH part 1 cio_write(length_idx,4); /* IBH part 1 */
cio_write(JPIP_CIDX,4); // IBH part 2 cio_write(JPIP_CIDX,4); /* IBH part 2 */
len=cio_tell()-lenp; len=cio_tell()-lenp;
cio_seek(lenp); cio_seek(lenp);
cio_write(len, 4); // L cio_write(len, 4); /* L */
cio_seek(lenp+len); cio_seek(lenp+len);
} }
/*
* Write the FIDX box
*
* File Index (superbox)
*
*/
int jpip_write_fidx(int offset_jp2c, int length_jp2c, int offset_idx, int length_idx) int jpip_write_fidx(int offset_jp2c, int length_jp2c, int offset_idx, int length_idx)
{ {
int len, lenp; int len, lenp;
lenp=cio_tell(); lenp=cio_tell();
cio_skip(4); // L [at the end] cio_skip(4); /* L [at the end] */
cio_write(JPIP_FIDX, 4); // IPTR cio_write(JPIP_FIDX, 4); /* IPTR */
jpip_write_prxy(offset_jp2c, length_jp2c, offset_idx, offset_jp2c); jpip_write_prxy(offset_jp2c, length_jp2c, offset_idx, offset_jp2c);
len=cio_tell()-lenp; len=cio_tell()-lenp;
cio_seek(lenp); cio_seek(lenp);
cio_write(len, 4); // L cio_write(len, 4); /* L */
cio_seek(lenp+len); cio_seek(lenp+len);
return len; return len;

View File

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

View File

@ -1,7 +1,5 @@
/* /*
* Copyright (c) 2001-2002, David Janssens * Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -33,26 +31,27 @@
#include "tcd.h" #include "tcd.h"
typedef struct { typedef struct {
int pdx, pdy; int pdx, pdy;
int pw, ph; int pw, ph;
} pi_resolution_t; } pi_resolution_t;
typedef struct { typedef struct {
int dx, dy; int dx, dy;
int numresolutions; int numresolutions;
pi_resolution_t *resolutions; pi_resolution_t *resolutions;
} pi_comp_t; } pi_comp_t;
typedef struct { typedef struct {
int include[10][10][3][99]; short int *include;
int compno, resno, precno, layno; // component, resolution, precinct and layer that indentify the packet int step_l, step_r, step_c, step_p;
int first; int compno, resno, precno, layno; /* component, resolution, precinct and layer that indentify the packet */
j2k_poc_t poc; int first;
int numcomps; j2k_poc_t poc;
pi_comp_t *comps; int numcomps;
int tx0, ty0, tx1, ty1; pi_comp_t *comps;
int x, y, dx, dy; int tx0, ty0, tx1, ty1;
} pi_iterator_t; // packet iterator int x, y, dx, dy;
} pi_iterator_t; /* packet iterator */
/* /*
* Create a packet iterator * Create a packet iterator
@ -61,13 +60,13 @@ typedef struct {
* tileno: number that identifies the tile for which to list the packets * tileno: number that identifies the tile for which to list the packets
* return value: returns a packet iterator that points to the first packet of the tile * return value: returns a packet iterator that points to the first packet of the tile
*/ */
pi_iterator_t *pi_create(j2k_image_t *img, j2k_cp_t *cp, int tileno); pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno);
/* /*
* Modify the packet iterator to point to the next packet * Modify the packet iterator to point to the next packet
* pi: packet iterator to modify * pi: packet iterator to modify
* return value: returns 0 if pi pointed to the last packet or else returns 1 * return value: returns 0 if pi pointed to the last packet or else returns 1
*/ */
int pi_next(pi_iterator_t *pi); int pi_next(pi_iterator_t * pi);
#endif #endif

View File

@ -65,183 +65,318 @@ void t2_init_seg(tcd_seg_t *seg, int cblksty) {
seg->maxpasses=cblksty&J2K_CCP_CBLKSTY_TERMALL?1:100; seg->maxpasses=cblksty&J2K_CCP_CBLKSTY_TERMALL?1:100;
} }
int t2_decode_packet(unsigned char *src, int len, tcd_tile_t *tile, j2k_tcp_t *tcp, int compno, int resno, int precno, int layno, info_layer_t *layer_Idx) { 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, info_layer_t *layer_Idx) {
int bandno, cblkno; int bandno, cblkno;
tcd_tilecomp_t *tilec=&tile->comps[compno]; tcd_tilecomp_t *tilec = &tile->comps[compno];
tcd_resolution_t *res=&tilec->resolutions[resno]; tcd_resolution_t *res = &tilec->resolutions[resno];
unsigned char *c=src; unsigned char *c = src;
unsigned char *d=c; unsigned char *d = c;
int e; int e;
int present; int present;
if (layno==0) { if (layno == 0) {
for (bandno=0; bandno<res->numbands; bandno++) { for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band=&res->bands[bandno]; tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc=&band->precincts[precno]; tcd_precinct_t *prc = &band->precincts[precno];
tgt_reset(prc->incltree); tgt_reset(prc->incltree);
tgt_reset(prc->imsbtree); tgt_reset(prc->imsbtree);
for (cblkno=0; cblkno<prc->cw*prc->ch; cblkno++) { for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
tcd_cblk_t *cblk=&prc->cblks[cblkno]; tcd_cblk_t *cblk = &prc->cblks[cblkno];
cblk->numsegs=0; cblk->numsegs = 0;
} }
} }
} }
/* INDEX */
layer_Idx->len_header = 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;
d = c;
bio_init_dec(c, 1000);
} else
{
if (tcp->ppt == 1) /* PPT */
{
c = tcp->ppt_data;
d = c;
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;
}
bio_init_dec(c, src + len - c);
layer_Idx->len_header = -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;
}
/* INDEX */
layer_Idx->len_header += (c-d);
/* PPT and PPM dealing */
if (cp->ppm == 1) /* PPM */
{
cp->ppm_data = c;
return 0;
}
if (tcp->ppt == 1) /* PPT */
{
tcp->ppt_data = c;
return 0;
}
return c - src;
}
if (tcp->csty&J2K_CP_CSTY_SOP) {
c+=6;
}
bio_init_dec(c, src+len-c);
present=bio_read(1);
if (!present) {
bio_inalign();
c+=bio_numbytes();
return c-src;
}
for (bandno=0; bandno<res->numbands; bandno++) { for (bandno=0; bandno<res->numbands; bandno++) {
tcd_band_t *band=&res->bands[bandno]; tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc=&band->precincts[precno]; tcd_precinct_t *prc = &band->precincts[precno];
for (cblkno=0; cblkno<prc->cw*prc->ch; cblkno++) { for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
int included, increment, n; int included, increment, n;
tcd_cblk_t *cblk=&prc->cblks[cblkno]; tcd_cblk_t *cblk = &prc->cblks[cblkno];
tcd_seg_t *seg; tcd_seg_t *seg;
if (!cblk->numsegs) { if (!cblk->numsegs) {
included=tgt_decode(prc->incltree, cblkno, layno+1); included = tgt_decode(prc->incltree, cblkno, layno+1);
} else { } else {
included=bio_read(1); included = bio_read(1);
} }
if (!included) { if (!included) {
cblk->numnewpasses=0; cblk->numnewpasses = 0;
continue; continue;
} }
if (!cblk->numsegs) { if (!cblk->numsegs) {
int i, numimsbs; int i, numimsbs;
for (i=0; !tgt_decode(prc->imsbtree, cblkno, i); i++) {} for (i = 0; !tgt_decode(prc->imsbtree, cblkno, i); i++) {}
numimsbs=i-1; numimsbs = i-1;
cblk->numbps=band->numbps-numimsbs; cblk->numbps = band->numbps - numimsbs;
cblk->numlenbits=3; cblk->numlenbits = 3;
} }
cblk->numnewpasses=t2_getnumpasses(); cblk->numnewpasses = t2_getnumpasses();
increment=t2_getcommacode(); increment = t2_getcommacode();
cblk->numlenbits+=increment; cblk->numlenbits += increment;
if (!cblk->numsegs) { if (!cblk->numsegs) {
seg=&cblk->segs[0]; seg = &cblk->segs[0];
t2_init_seg(seg, tcp->tccps[compno].cblksty); t2_init_seg(seg, tcp->tccps[compno].cblksty);
} else { } else {
seg=&cblk->segs[cblk->numsegs-1]; seg = &cblk->segs[cblk->numsegs - 1];
if (seg->numpasses==seg->maxpasses) { if (seg->numpasses == seg->maxpasses) {
t2_init_seg(++seg, tcp->tccps[compno].cblksty); t2_init_seg(++seg, tcp->tccps[compno].cblksty);
} }
} }
n=cblk->numnewpasses; n = cblk->numnewpasses;
do { do {
seg->numnewpasses=int_min(seg->maxpasses-seg->numpasses, n); seg->numnewpasses = int_min(seg->maxpasses-seg->numpasses, n);
seg->newlen=bio_read(cblk->numlenbits+int_floorlog2(seg->numnewpasses)); seg->newlen = bio_read(cblk->numlenbits + int_floorlog2(seg->numnewpasses));
n-=seg->numnewpasses; n -= seg->numnewpasses;
if (n>0) { if (n > 0) {
t2_init_seg(++seg, tcp->tccps[compno].cblksty); t2_init_seg(++seg, tcp->tccps[compno].cblksty);
} }
} while (n>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) {
c+=2; if (tcp->csty & J2K_CP_CSTY_EPH) { /* EPH marker */
if ((*c) != 255 || (*(c+1) != 146)) {printf("Error : expected EPH marker [2]!!!\n"); }
c += 2;
} }
// INDEX /* INDEX */
layer_Idx->len_header=c-d; layer_Idx->len_header += (c-d);
if (tcp->csty&J2K_CP_CSTY_SOP)
layer_Idx->len_header-=6;
// \INDEX --> END OF HEADER !!
for (bandno=0; bandno<res->numbands; bandno++) { /* PPT Step 2 : see above for details */
tcd_band_t *band=&res->bands[bandno]; if (cp->ppm == 1)
tcd_precinct_t *prc=&band->precincts[precno]; {
for (cblkno=0; cblkno<prc->cw*prc->ch; cblkno++) { cp->ppm_data = c; /* Update pointer */
tcd_cblk_t *cblk=&prc->cblks[cblkno];
/* INDEX */
layer_Idx->len_header = c-d;
c = src;
d = c;
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 */
/* INDEX */
layer_Idx->len_header = c-d;
c = src;
d = c;
if (tcp->csty & J2K_CP_CSTY_SOP) /* SOP marker */
{
if ((*c) != 255 || (*(c+1) != 145)) {printf("Error : expected SOP marker [2] !!!\n"); }
c += 6;
}
bio_init_dec(c, src + len - c);
}
}
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
for (cblkno = 0; cblkno < prc->cw*prc->ch; cblkno++) {
tcd_cblk_t *cblk = &prc->cblks[cblkno];
tcd_seg_t *seg; tcd_seg_t *seg;
if (!cblk->numnewpasses) continue; if (!cblk->numnewpasses) continue;
if (!cblk->numsegs) { if (!cblk->numsegs) {
seg=&cblk->segs[cblk->numsegs++]; seg = &cblk->segs[cblk->numsegs++];
cblk->len=0; cblk->len = 0;
} else { } else {
seg=&cblk->segs[cblk->numsegs-1]; seg = &cblk->segs[cblk->numsegs-1];
if (seg->numpasses==seg->maxpasses) { if (seg->numpasses == seg->maxpasses) {
seg++; seg++;
cblk->numsegs++; cblk->numsegs++;
} }
} }
do { do {
if (c+seg->newlen>src+len) return -999; if (c + seg->newlen > src + len) return -999;
memcpy(cblk->data+cblk->len, c, seg->newlen); memcpy(cblk->data + cblk->len, c, seg->newlen);
if (seg->numpasses==0) { if (seg->numpasses == 0) {
seg->data=cblk->data+cblk->len; seg->data = cblk->data + cblk->len;
} }
c+=seg->newlen; c += seg->newlen;
cblk->len+=seg->newlen; cblk->len += seg->newlen;
seg->len+=seg->newlen; seg->len += seg->newlen;
seg->numpasses+=seg->numnewpasses; seg->numpasses += seg->numnewpasses;
cblk->numnewpasses-=seg->numnewpasses; cblk->numnewpasses -= seg->numnewpasses;
if (cblk->numnewpasses>0) { if (cblk->numnewpasses > 0) {
seg++; seg++;
cblk->numsegs++; cblk->numsegs++;
} }
} while (cblk->numnewpasses>0); } while (cblk->numnewpasses > 0);
} }
} }
//<INDEX> /* <INDEX> */
e=c-d; e = c-d;
layer_Idx->len=e; layer_Idx->len = e;
//</INDEX> /* </INDEX> */
return c-src; return c-src;
} }
void t2_init_info_packets(info_image_t *img, j2k_cp_t *cp)
{
int compno, tileno, resno, precno, layno;
for(compno = 0; compno < img->Comp; compno++)
{
for(tileno = 0; tileno < img->tw*img->th; tileno++)
{
info_tile_t *tile_Idx = &img->tile[tileno];
info_compo_t *compo_Idx = &tile_Idx->compo[compno];
for(resno = 0; resno < img->Decomposition + 1 ; resno++)
{
info_reso_t *reso_Idx = &compo_Idx->reso[resno];
for (precno = 0; precno < img->tile[tileno].pw * img->tile[tileno].ph; precno++)
{
info_prec_t *prec_Idx = &reso_Idx->prec[precno];
for(layno = 0; layno < img->Layer ; layno++)
{
info_layer_t *layer_Idx = &prec_Idx->layer[layno];
layer_Idx->offset = 0; /* start position */
layer_Idx->len_header = 0; /* length */
}
}
}
}
}
}
int t2_decode_packets(unsigned char *src, int len, j2k_image_t *img, j2k_cp_t *cp, int tileno, tcd_tile_t *tile, info_image_t *imgg) { int t2_decode_packets(unsigned char *src, int len, j2k_image_t *img, j2k_cp_t *cp, int tileno, tcd_tile_t *tile, info_image_t *imgg) {
unsigned char *c=src; unsigned char *c = src;
pi_iterator_t *pi; pi_iterator_t *pi;
int pino, compno,e; int pino, compno,e;
int position=imgg->tile[tileno].end_header+1; int partno;
info_tile_part_t *tile_part;
int position;
int length_read;
info_tile_t *tile_Idx; info_tile_t *tile_Idx;
info_compo_t *compo_Idx; info_compo_t *compo_Idx;
info_reso_t *reso_Idx; info_reso_t *reso_Idx;
info_prec_t *prec_Idx; info_prec_t *prec_Idx;
info_layer_t *layer_Idx; info_layer_t *layer_Idx;
tile_Idx=&imgg->tile[tileno]; t2_init_info_packets(imgg, cp); /* Initialize the packets information : LEN and OFFSET to 0 */
tile_Idx->num_packet=0;
pi=pi_create(img, cp, tileno);
for (pino=0; pino<=cp->tcps[tileno].numpocs;pino++) tile_Idx = &imgg->tile[tileno];
tile_Idx->num_packet = 0;
pi = pi_create(img, cp, tileno);
partno = 0;
tile_part = &tile_Idx->tile_parts[partno];
position = tile_part->end_header + 1;
length_read = 0;
for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++)
{ {
while (pi_next(&pi[pino]))
{
compo_Idx = &tile_Idx->compo[pi[pino].compno];
reso_Idx = &compo_Idx->reso[pi[pino].resno];
prec_Idx = &reso_Idx->prec[pi[pino].precno];
layer_Idx = &prec_Idx->layer[pi[pino].layno];
while (pi_next(&pi[pino])) { layer_Idx->offset = position;
compo_Idx=&tile_Idx->compo[pi[pino].compno]; layer_Idx->offset_header = position;
reso_Idx=&compo_Idx->reso[pi[pino].resno];
prec_Idx=&reso_Idx->prec[pi[pino].precno];
layer_Idx=&prec_Idx->layer[pi[pino].layno];
layer_Idx->offset=position; 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,layer_Idx);
if (e == -999)
{
break;
} else
c += e;
position += e;
e=t2_decode_packet(c, src+len-c, tile, &cp->tcps[tileno], pi[pino].compno, pi[pino].resno, pi[pino].precno, pi[pino].layno,layer_Idx); /* Update position in case of multiple tile-parts for a tile >> */
length_read += e;
if (length_read >= (tile_part->end_pos - tile_part->end_header))
{
partno++;
tile_part = &tile_Idx->tile_parts[partno];
position = tile_part->end_header + 1;
length_read = 0;
}
/* << end_update */
if (e==-999) tile_Idx->num_packet++;
{ }
break;
} else
c+=e;
position+=e;
tile_Idx->num_packet++;
}
// FREE space memory taken by pi // FREE space memory taken by pi
for (compno=0; compno<pi[pino].numcomps; compno++) for (compno = 0; compno < pi[pino].numcomps; compno++)
{ {
free(pi[pino].comps[compno].resolutions); free(pi[pino].comps[compno].resolutions);
} }
free(pi[pino].comps); free(pi[pino].comps);
} }
free(pi[0].include);
free(pi); free(pi);
if (e==-999) if (e==-999)

View File

@ -42,10 +42,6 @@ static tcd_image_t tcd_image;
static j2k_image_t *tcd_img; static j2k_image_t *tcd_img;
static j2k_cp_t *tcd_cp; static j2k_cp_t *tcd_cp;
static tcd_tile_t *tcd_tile;
static j2k_tcp_t *tcd_tcp;
static int tcd_tileno;
extern jmp_buf j2k_error; extern jmp_buf j2k_error;
void tcd_init(j2k_image_t *img, j2k_cp_t *cp, info_image_t *imgg) { void tcd_init(j2k_image_t *img, j2k_cp_t *cp, info_image_t *imgg) {
@ -268,14 +264,11 @@ void tcd_free(j2k_image_t *img, j2k_cp_t *cp) {
int tcd_decode_tile(unsigned char *src, int len, int tileno, info_image_t *imgg) { int tcd_decode_tile(unsigned char *src, int len, int tileno, info_image_t *imgg) {
int l; int l;
int eof=0; int eof=0;
tcd_tile_t *tile; tcd_tile_t *tile;
tcd_tileno=tileno;
tcd_tile=&tcd_image.tiles[tileno];
tcd_tcp=&tcd_cp->tcps[tileno];
tile=tcd_tile;
l=t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile, imgg); tile = &tcd_image.tiles[tileno];
l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile, imgg);
if (l==-999) if (l==-999)
{ {

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2001-2002, David Janssens * Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren * Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without