TRUNK:added a new indexer functionality to the library. With the new -jpip option...
[openjpeg.git] / libopenjpeg / jp2.c
index cea826bb6bc0b08638b26af8f073ffac237106d9..1029746cc686a6badea42f52419df90b81c0e15f 100644 (file)
@@ -5,6 +5,7 @@
  * Copyright (c) 2002-2003, Yannick Verschueren
  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2010-2011, Kaori Hagihara
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -124,6 +125,32 @@ Collect colour specification data
 */
 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
     opj_jp2_box_t *box, opj_jp2_color_t *color);
+/**
+Write file Index (superbox)
+@param[in] offset_jp2c offset of jp2c box
+@param[in] length_jp2c length of jp2c box
+@param[in] offset_idx  offset of cidx box
+@param[in] length_idx  length of cidx box
+@param[in] cio         file output handle
+@return                length of fidx box
+*/
+static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
+/**
+Write index Finder box
+@param[in] offset offset of fidx box
+@param[in] length length of fidx box
+@param[in] cio         file output handle
+*/
+static void write_iptr( int offset, int length, opj_cio_t *cio);
+/**
+Write proxy box
+@param[in] offset_jp2c offset of jp2c box
+@param[in] length_jp2c length of jp2c box
+@param[in] offset_idx  offset of cidx box
+@param[in] length_idx  length of cidx box
+@param[in] cio         file output handle
+*/
+static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
 /*@}*/
 
 /*@}*/
@@ -281,11 +308,10 @@ static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
        cio_write(cio, jp2->precedence, 1);     /* PRECEDENCE */
        cio_write(cio, jp2->approx, 1);         /* APPROX */
 
-       if (jp2->meth == 1) {
-               cio_write(cio, jp2->enumcs, 4); /* EnumCS */
-       } else {
-               cio_write(cio, 0, 1);                   /* PROFILE (??) */
-       }
+       if(jp2->meth == 2)
+        jp2->enumcs = 0;
+
+       cio_write(cio, jp2->enumcs, 4); /* EnumCS */
 
        box.length = cio_tell(cio) - box.init_pos;
        cio_seek(cio, box.init_pos);
@@ -399,13 +425,16 @@ static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
        unsigned short i, j;
        unsigned char uc;
 
+       OPJ_ARG_NOT_USED(box);
+       OPJ_ARG_NOT_USED(jp2);
+
 /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
  * a JP2 Header box' :
 */
        if(color->jp2_pclr) return OPJ_FALSE;
 
-       nr_entries = cio_read(cio, 2); /* NE */
-       nr_channels = cio_read(cio, 1);/* NPC */
+       nr_entries = (unsigned short)cio_read(cio, 2); /* NE */
+       nr_channels = (unsigned short)cio_read(cio, 1);/* NPC */
 
        entries = (unsigned int*)
         opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
@@ -447,6 +476,9 @@ static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
        opj_jp2_cmap_comp_t *cmap;
        unsigned short i, nr_channels;
 
+       OPJ_ARG_NOT_USED(box);
+       OPJ_ARG_NOT_USED(jp2);
+
 /* Need nr_channels: */
        if(color->jp2_pclr == NULL) return OPJ_FALSE;
 
@@ -461,7 +493,7 @@ static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
 
        for(i = 0; i < nr_channels; ++i)
    {
-       cmap[i].cmp = cio_read(cio, 2);
+       cmap[i].cmp = (unsigned short)cio_read(cio, 2);
        cmap[i].mtyp = cio_read(cio, 1);
        cmap[i].pcol = cio_read(cio, 1);
 
@@ -512,12 +544,15 @@ static opj_bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
        opj_jp2_cdef_info_t *info;
        unsigned short i, n;
 
+       OPJ_ARG_NOT_USED(box);
+       OPJ_ARG_NOT_USED(jp2);
+
 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
  * inside a JP2 Header box.' 
 */
        if(color->jp2_cdef) return OPJ_FALSE;
 
-       if((n = cio_read(cio, 2)) == 0) return OPJ_FALSE; /* szukw000: FIXME */
+       if((n = (unsigned short)cio_read(cio, 2)) == 0) return OPJ_FALSE; /* szukw000: FIXME */
 
        info = (opj_jp2_cdef_info_t*)
         opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
@@ -528,9 +563,9 @@ static opj_bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
 
        for(i = 0; i < n; ++i)
    {
-       info[i].cn = cio_read(cio, 2);
-       info[i].typ = cio_read(cio, 2);
-       info[i].asoc = cio_read(cio, 2);
+       info[i].cn = (unsigned short)cio_read(cio, 2);
+       info[i].typ = (unsigned short)cio_read(cio, 2);
+       info[i].asoc = (unsigned short)cio_read(cio, 2);
 
    }
        return OPJ_TRUE;
@@ -557,7 +592,8 @@ static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
    {
        jp2->enumcs = cio_read(cio, 4); /* EnumCS */
    } 
-       else 
+       else
+       if (jp2->meth == 2) 
    {
 /* skip PROFILE */
        skip_len = box->init_pos + box->length - cio_tell(cio);
@@ -916,6 +952,67 @@ static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
        return OPJ_TRUE;
 }
 
+
+static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
+{  
+  int len, lenp;
+  
+  lenp = cio_tell( cio);
+  cio_skip( cio, 4);              /* L [at the end] */
+  cio_write( cio, JPIP_FIDX, 4);  /* IPTR           */
+  
+  write_prxy( offset_jp2c, length_jp2c, offset_idx, offset_jp2c, cio);
+
+  len = cio_tell( cio)-lenp;
+  cio_seek( cio, lenp);
+  cio_write( cio, len, 4);        /* L              */
+  cio_seek( cio, lenp+len);  
+
+  return len;
+}
+
+static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
+{
+  int len, lenp;
+
+  lenp = cio_tell( cio);
+  cio_skip( cio, 4);              /* L [at the end] */
+  cio_write( cio, JPIP_PRXY, 4);  /* IPTR           */
+  
+  cio_write( cio, offset_jp2c, 8); /* OOFF           */
+  cio_write( cio, length_jp2c, 4); /* OBH part 1     */
+  cio_write( cio, JP2_JP2C, 4);        /* OBH part 2     */
+  
+  cio_write( cio, 1,1);           /* NI             */
+
+  cio_write( cio, offset_idx, 8);  /* IOFF           */
+  cio_write( cio, length_idx, 4);  /* IBH part 1     */
+  cio_write( cio, JPIP_CIDX, 4);   /* IBH part 2     */
+
+  len = cio_tell( cio)-lenp;
+  cio_seek( cio, lenp);
+  cio_write( cio, len, 4);        /* L              */
+  cio_seek( cio, lenp+len);
+}
+
+static void write_iptr( int offset, int length, opj_cio_t *cio)
+{
+  int len, lenp;
+  
+  lenp = cio_tell( cio);
+  cio_skip( cio, 4);              /* L [at the end] */
+  cio_write( cio, JPIP_IPTR, 4);  /* IPTR           */
+  
+  cio_write( cio, offset, 8);
+  cio_write( cio, length, 8);
+
+  len = cio_tell( cio)-lenp;
+  cio_seek( cio, lenp);
+  cio_write( cio, len, 4);        /* L             */
+  cio_seek( cio, lenp+len);
+}
+
+
 /* ----------------------------------------------------------------------- */
 /* JP2 decoder interface                                             */
 /* ----------------------------------------------------------------------- */
@@ -1042,31 +1139,23 @@ void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_
        for (i = 0; i < image->numcomps; i++) {
                jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
        }
-
-       /* Colour Specification box */
-
-       if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
-               jp2->meth = 1;  /* METH: Enumerated colourspace */
-       } else {
-               jp2->meth = 2;  /* METH: Restricted ICC profile */
-       }
-       if (jp2->meth == 1) {
-               if (image->color_space == 1)
-                       jp2->enumcs = 16;       /* sRGB as defined by IEC 61966-2.1 */
-               else if (image->color_space == 2)
-                       jp2->enumcs = 17;       /* greyscale */
-               else if (image->color_space == 3)
-                       jp2->enumcs = 18;       /* YUV */
-       } else {
-               jp2->enumcs = 0;                /* PROFILE (??) */
-       }
+       jp2->meth = 1;
+       if (image->color_space == 1)
+               jp2->enumcs = 16;       /* sRGB as defined by IEC 61966-2.1 */
+       else if (image->color_space == 2)
+               jp2->enumcs = 17;       /* greyscale */
+       else if (image->color_space == 3)
+               jp2->enumcs = 18;       /* YUV */
        jp2->precedence = 0;    /* PRECEDENCE */
        jp2->approx = 0;                /* APPROX */
-
+       
+       jp2->jpip_on = parameters->jpip_on;
 }
 
 opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
 
+       int pos_iptr, pos_cidx, pos_jp2c, len_jp2c, end_pos, pos_fidx, len_fidx;
+
        /* JP2 encoding */
 
        /* JPEG 2000 Signature box */
@@ -1076,14 +1165,34 @@ opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_
        /* JP2 Header box */
        jp2_write_jp2h(jp2, cio);
 
+       if( jp2->jpip_on){
+         pos_iptr = cio_tell( cio);
+         cio_skip( cio, 24); /* IPTR further ! */
+         
+         pos_jp2c = cio_tell( cio);
+       }
+
        /* J2K encoding */
+       if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
+           opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
+           return OPJ_FALSE;
+       }
 
-       if(!jp2_write_jp2c(jp2, cio, image, cstr_info)) {
-               opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
-               return OPJ_FALSE;
+       if( jp2->jpip_on){
+         pos_cidx = cio_tell( cio);
+         
+         write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8);
+         
+         pos_fidx = cio_tell( cio);
+         len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, cio_tell(cio), cio);
+         
+         end_pos = cio_tell( cio);
+         
+         cio_seek( cio, pos_iptr);
+         write_iptr( pos_fidx, len_fidx, cio);
+         
+         cio_seek( cio, end_pos);
        }
 
        return OPJ_TRUE;
 }
-
-