Esempio n. 1
0
File: update.c Progetto: Booley/nbis
/***********************************************************************
************************************************************************
#cat: update_ANSI_NIST_record_LEN - Takes a specified logical record in
#cat:              an ANSI/NIST file structure and recomputes and updates
#cat:              the length/size of the record and parent ANSI/NIST
#cat:              structure.

   Input:
      record_i   - integer index of desired record structure
      ansi_nist  - ANSI/NIST file structure to be modified
   Return Code:
      Zero       - successful completion
      Negative   - system error
************************************************************************/
int update_ANSI_NIST_record_LEN(ANSI_NIST *ansi_nist, const int record_i)
{
   int ret;
   RECORD *record;
   FIELD *lenfield;
   int lenfield_i;
   ITEM *lenitem;
   int orig_recordlen;

   /* If record index out of range ... */
   if((record_i < 0) || (record_i >= ansi_nist->num_records)){
      fprintf(stderr, "ERROR : update_ANSI_NIST_record_LEN : "
	      "record index [%d] out of range [1..%d]\n",
              record_i+1, ansi_nist->num_records);
      return(-2);
   }
   record = ansi_nist->records[record_i];

   /* If LEN "01" field not found ... */
   if(!lookup_ANSI_NIST_field(&lenfield, &lenfield_i, LEN_ID, record)){
      /* Assume we have a partial tagged field record. For example, an   */
      /* fmttext file may have been parsed that specifies only a single  */
      /* field or subfield.  This happens in applications when inserting */
      /* or substituting single fields or subfields.                     */
      /* Ignore the update request. */
      return(0);
   }

   /* If LEN subfield or item not found ... */
   if((lenfield->num_subfields != 1) ||
      (lenfield->subfields[0]->num_items != 1)){
      fprintf(stderr, "ERROR : update_ANSI_NIST_record_LEN : "
	      "LEN field index [%d.%d] format error in record [Type-%d.%03d]\n",
              record_i+1, lenfield_i+1, record->type, lenfield->field_int);
      return(-4);
   }

   /* Convert original LEN to numeric integer. */
   lenitem = lenfield->subfields[0]->items[0];
   orig_recordlen = atoi((char *)lenitem->value);

   /* If original LEN != byte size of record ... */
   if(orig_recordlen != record->num_bytes){
      /* If a binary record ... */
      if(binary_record(record->type)){
         /* Update binary field record with fixed LEN field ... */
         if((ret = update_ANSI_NIST_binary_record_LEN(record)))
            return(ret);

         fprintf(stderr, "LEN field index [%d.%d] [Type-%d.%03d] updated "
		 "(%d now %d)\n",
                 record_i+1, lenfield_i+1, record->type, lenfield->field_int,
		 orig_recordlen, record->num_bytes);

         /* Return normally. */
         return(0);
      }

      /* Otherwise, update tagged field record with variable LEN field ... */
      if((ret = update_ANSI_NIST_tagged_record_LEN(record)))
         return(ret);

      ansi_nist->num_bytes += (record->num_bytes - orig_recordlen);

      fprintf(stderr, "LEN field index [%d.%d] [Type-%d.%03d] updated "
	      "(%d now %d)\n",
              record_i+1, lenfield_i+1, record->type, lenfield->field_int,
	      orig_recordlen, record->num_bytes);

   }
   /* Return normally. */
   return(0);
}
Esempio n. 2
0
/*************************************************************************
**************************************************************************
#cat:   decode_binary_field_image() - Takes an ANSI/NIST binary field
#cat:                image record and decodes its image data (if necessary)
#cat:                and returns the reconstructed pixmap and its attributes.

   Input:
      ansi_nist   - ANSI/NIST file structure
      imgrecord_i - index of record to be decoded
   Output:
      odata  - points to reconstructed pixmap
      ow     - pixel width of pixmap
      oh     - pixel height of pixmap
      od     - pixel depth of pixmap
      oppmm  - scan resolution of pixmap in pixels/mm
   Return Code:
      TRUE     - successful image reconstruction
      FALSE    - image record ignored
      Negative - system error
**************************************************************************/
int decode_binary_field_image(unsigned char **odata,
                     int *ow, int *oh, int *od, double *oppmm,
                     const ANSI_NIST *ansi_nist, const int imgrecord_i)
{
   int ret;
   RECORD *imgrecord;
   FIELD *field;
   int field_i;
   char *img_comp;
   unsigned char *idata1, *idata2;
   int ilen, iw1, ih1, id1;
   double ppmm;
   int iw2, ih2, id2, ppi, lossyflag;

   /* If image record index is out of range ... */
   if((imgrecord_i < 1) || (imgrecord_i > ansi_nist->num_records)){
      fprintf(stderr, "ERROR : decode_binary_field_image : "
	      "record index [%d] out of range [1..%d]\n",
              imgrecord_i+1, ansi_nist->num_records+1);
      return(-2);
   }

   /* Set image record pointer. */
   imgrecord = ansi_nist->records[imgrecord_i];

   /* If for some reason someone's passes a Type-8 record ... */
   if(imgrecord->type == TYPE_8_ID){
      /* Post warning and ignore record Type-8. */
      fprintf(stderr, "WARNING : decode_binary_field_image : "
	      "Type-8 record [%d] not supported.\n"
	      "Image record ignored.\n", imgrecord_i+1);
      return(FALSE);
   }

   /* 1. Determine Compression */
   /* Lookup binary compression algorithm (BIN_CA_ID). */
   if(lookup_ANSI_NIST_field(&field, &field_i, BIN_CA_ID, imgrecord) == 0){
      fprintf(stderr, "ERROR : decode_binary_field_image : "
	      "BIN_CA field not found in record index [%d] [Type-%d.%03d]\n",
              imgrecord_i+1, imgrecord->type, BIN_CA_ID);
      return(-3);
   }
   img_comp = (char *)field->subfields[0]->items[0]->value;

   /* If Type-5,6 ... ignore if compressed. */
   if((imgrecord->type == TYPE_5_ID) ||
      (imgrecord->type == TYPE_6_ID)){
      /* If the image data is compressed ... */
      if(strcmp(img_comp, BIN_COMP_NONE) != 0){
         fprintf(stderr, "WARNING : decode_binary_field_image : "
		   "binary image compression of "
		   "record index [%d] [Type-%d] is unsupported.\n"
		   "Image record ignored.\n",
                 imgrecord_i+1, imgrecord->type);
         /* Ignore image record ... */
         return(FALSE);
      }
   }
         
   /* 2. Determine image W */
   /* Lookup horizontal line length (HLL_ID). */
   if(lookup_ANSI_NIST_field(&field, &field_i, HLL_ID, imgrecord) == 0){
      fprintf(stderr, "ERROR : decode_binary_field_image : "
	      "HLL field not found in record index [%d] [Type-%d.%03d]\n",
              imgrecord_i+1, imgrecord->type, HLL_ID);
      return(-4);
   }
   iw1 = atoi((char *)field->subfields[0]->items[0]->value);

   /* 3. Determine image H */
   /* Lookup vertical line length (VLL_ID). */
   if(lookup_ANSI_NIST_field(&field, &field_i, VLL_ID, imgrecord) == 0){
      fprintf(stderr, "ERROR : decode_binary_field_image : "
	      "VLL field not found in record index [%d] [Type-%d.%03d]\n",
              imgrecord_i+1, imgrecord->type, VLL_ID);
      return(-5);
   }
   ih1 = atoi((char *)field->subfields[0]->items[0]->value);

   /* 4. Determine pixel depth */
   switch(imgrecord->type){
      case TYPE_3_ID:
      case TYPE_4_ID:
           /* Image record is grayscale, so set pixel depth = 8. */
           id1 = 8;
           break;
      case TYPE_5_ID:
      case TYPE_6_ID:
           /* Image record is bi-level, so set pixel depth = 1. */
           id1 = 1;
           break;
      default:
           fprintf(stderr, "ERROR : decode_binary_field_image : "
		   "illegal binary image record type = %d\n", imgrecord->type);
           return(-6);
   }

   /* 5. Determine ppmm */
   /* Lookup image record's pixel/mm scan resolution. */
   if((ret = lookup_binary_field_image_ppmm(&ppmm, ansi_nist, imgrecord_i)) !=0)
      return(ret);

   /* Set image pointer to last field value in record. */
   field = imgrecord->fields[imgrecord->num_fields-1];
   idata1 = field->subfields[0]->items[0]->value;
   ilen = field->subfields[0]->items[0]->num_bytes;

   /* If the image data is uncompressed ... */
   if(strcmp(img_comp, BIN_COMP_NONE) == 0){
      idata2 = (unsigned char *)malloc((size_t)ilen);
      if(idata2 == NULL){
         fprintf(stderr, "ERROR : decode_binary_field_image : "
		 "malloc : idata2 (%d bytes)\n", ilen);
         return(-7);
      }
      memcpy(idata2, idata1, (size_t)ilen);

      *odata = idata2;
      *ow = iw1;
      *oh = ih1;
      *od = id1;
      *oppmm = ppmm;

      return(TRUE);
   }
   /* Otherwise, binary field image data is compressed ... */
   /* If WSQ compressed ... */
   else if(strcmp(img_comp, BIN_COMP_WSQ) == 0){
      if((ret = wsq_decode_mem(&idata2, &iw2, &ih2, &id2, &ppi, &lossyflag,
			       idata1, ilen)) != 0)
         return(ret);
   }
   /* Otherwise, unsupported compression algorithm. */
   else{
      fprintf(stderr, "WARNING : decode_binary_field_image : "
	      "unsupported compression algorithm %.10s%s in "
	      "image record index [%d] [Type-%d].\n"
	      "Image record ignored.\n",
              img_comp, strlen(img_comp) > 10 ? "..." : "",
	      imgrecord_i+1, imgrecord->type);
      /* Ignore image record ... */
      return(FALSE);
   }

   if(iw1 != iw2){
      fprintf(stderr, "ERROR : decode_binary_field_image : "
	      "[HLL field (from file) = %d] != "
	      "[image width (from decoder) = %d]\n",
	      iw1, iw2);
      free(idata2);
      return(-8);
   }
   if(ih1 != ih2){
      fprintf(stderr, "ERROR : decode_binary_field_image : "
	      "[VLL field (from file) = %d] != "
	      "[image height (from decoder) = %d]\n", ih1, ih2);
      free(idata2);
      return(-9);
   }
   if(id1 != id2){
      fprintf(stderr, "ERROR : decode_binary_field_image : "
	      "[pixel depth (from record Type-%d) = %d] != "
	      "[pixel depth (from decoder) = %d]\n",
	      id2, imgrecord->type, id1);
      free(idata2);
      return(-10);
   }

   *odata = idata2;
   *ow = iw1;
   *oh = ih1;
   *od = id1;
   *oppmm = ppmm;

   return(TRUE);
}
Esempio n. 3
0
/*************************************************************************
**************************************************************************
#cat:   decode_tagged_field_image() - Takes an ANSI/NIST tagged field
#cat:                image record and decodes its image data (if necessary)
#cat:                and returns the reconstructed pixmap and its attributes.

   Input:
      ansi_nist   - ANSI/NIST file structure
      imgrecord_i - index of record to be decoded
      intrlvflag  - if image data is RGB, then this flagged designates
                    whether the returned pixmap should be interleaved or not
   Output:
      odata  - points to reconstructed pixmap
      ow     - pixel width of pixmap
      oh     - pixel height of pixmap
      od     - pixel depth of pixmap
      oppmm  - scan resolution of pixmap in pixels/mm
   Return Code:
      TRUE     - successful image reconstruction
      FALSE    - image record ignored
      Negative - system error
**************************************************************************/
int decode_tagged_field_image(unsigned char **odata,
                     int *ow, int *oh, int *od, double *oppmm,
                     const ANSI_NIST *ansi_nist, const int imgrecord_i,
                     const int intrlvflag)
{
   int i, ret;
   RECORD *imgrecord;
   FIELD *field;
   int field_i;
   char *img_comp, *img_csp;
   unsigned char *idata1, *idata2;
   int ilen1, iw1, ih1, id1;
   double ppmm;
   int ilen2, iw2, ih2, id2, ppi, lossyflag;
   IMG_DAT *img_dat;
   int hor_sampfctr[MAX_CMPNTS], vrt_sampfctr[MAX_CMPNTS];
   int n_cmpnts;

   /* If image record index is out of range ... */
   if((imgrecord_i < 1) || (imgrecord_i > ansi_nist->num_records)){
      fprintf(stderr, "ERROR : decode_tagged_field_image : "
	      "record index [%d] out of range [1..%d]\n",
              imgrecord_i+1, ansi_nist->num_records+1);
      return(-2);
   }

   /* Set image record pointer. */
   imgrecord = ansi_nist->records[imgrecord_i];

   /* If NOT Type-10,13,14,15,16 ... */
   if(tagged_image_record(ansi_nist->records[imgrecord_i]->type) == 0){
      fprintf(stderr, "ERROR : decode_tagged_field_image : "
	      "record index [%d] [Type-%d] is not a tagged file image record\n",
              imgrecord_i+1, imgrecord->type);
      return(-3);
   }

   /* 1. Determine Compression */
   /* Lookup grayscale compression algorithm (TAG_CA_ID). */
   if(lookup_ANSI_NIST_field(&field, &field_i, TAG_CA_ID, imgrecord) == 0){
      fprintf(stderr, "ERROR : decode_tagged_field_image : "
	      "TAG_CA field not found in record index [%d] [Type-%d.%03d]\n",
              imgrecord_i+1, imgrecord->type, TAG_CA_ID);
      return(-4);
   }
   img_comp = (char *)field->subfields[0]->items[0]->value;

   /* 2. Determine image W */
   /* Lookup horizontal line length (HLL_ID). */
   if(lookup_ANSI_NIST_field(&field, &field_i, HLL_ID, imgrecord) == 0){
      fprintf(stderr, "ERROR : decode_tagged_field_image : "
	      "HLL field not found in record index [%d] [Type-%d.%03d]\n",
              imgrecord_i+1, imgrecord->type, HLL_ID);
      return(-5);
   }
   iw1 = atoi((char *)field->subfields[0]->items[0]->value);

   /* 3. Determine image H */
   /* Lookup vertical line length (VLL_ID). */
   if(lookup_ANSI_NIST_field(&field, &field_i, VLL_ID, imgrecord) == 0){
      fprintf(stderr, "ERROR : decode_tagged_field_image : "
	      "VLL field not found in record index [%d] [Type-%d.%03d]\n",
              imgrecord_i+1, imgrecord->type, VLL_ID);
      return(-6);
   }
   ih1 = atoi((char *)field->subfields[0]->items[0]->value);

   /* 4. Determine pixel depth */
   /* Lookup bits per pixel or colorspace (BPX_ID==CSP_ID). */
   if(lookup_ANSI_NIST_field(&field, &field_i, BPX_ID, imgrecord) == 0){
      fprintf(stderr, "ERROR : decode_tagged_field_image : "
	      "BPX field not found in record index [%d] [Type-%d.%03d]\n",
              imgrecord_i+1, imgrecord->type, BPX_ID);
      return(-7);
   }
   /* if Type-10 & 17... */
   if((imgrecord->type == TYPE_10_ID) || (imgrecord->type == TYPE_17_ID)){
      if (imgrecord->type == TYPE_10_ID){
      /* Grayscale? CSP_ID = 12, "GRAY" */
         if(lookup_ANSI_NIST_field(&field, &field_i, CSP_ID, imgrecord) == 0){
            fprintf(stderr, "ERROR : dpyan2k_tagged_record : "
		    "CSP field in record index [%d] [Type-%d.%03d] not found\n",
                    imgrecord_i+1, imgrecord->type, CSP_ID);
            return(-2);  
         }               
      }     
      if (imgrecord->type == TYPE_17_ID){
      /* Grayscale? CSP_ID_Type_17 = 13, "GRAY" */
         if(lookup_ANSI_NIST_field(&field, &field_i, CSP_ID_Type_17, imgrecord)
	    == 0){
            fprintf(stderr, "ERROR : dpyan2k_tagged_record : "
		    "CSP field in record index [%d] [Type-%d.%03d] not found\n",
                    imgrecord_i+1, imgrecord->type, CSP_ID_Type_17);
            return(-2);
         } 
      }    

      img_csp = (char *)field->subfields[0]->items[0]->value;
      if(strcmp(img_csp, CSP_GRAY) == 0)
         /* Set grayscale image attributes. */
         id1 = 8;
      else if((strcmp(img_csp, CSP_RGB) == 0) || 
              (strcmp(img_csp, CSP_YCC) == 0) ||
              (strcmp(img_csp, CSP_SRGB) == 0) ||
              (strcmp(img_csp, CSP_SYCC) == 0))
         /* Set color image attributes. */
         id1 = 24;
      /* Otherwise, unsupported color space, so ignore image record. */
      else{
         fprintf(stderr, "WARNING : decode_tagged_field_image : "
		 "colorspace \"%.10s%s\" not supported in "
		 "in record [%d] [Type-%d.%03d].\n"
		 "Image record ignored.\n", 
		 img_csp, strlen(img_csp) > 10 ? "..." : "",
		 imgrecord_i+1, imgrecord->type, CSP_ID);
         return(FALSE);
      }
   }
   /* Otherwise, Type-13,14,15,16 ... so current value == BPX_ID. */
   else{
      /* Set image pixel depth = BPX_ID. */
      id1 = atoi((char *)field->subfields[0]->items[0]->value);
   }

   /* 5. Determine ppmm */
   /* Lookup image record's pixel/mm scan resolution. */
   ret = lookup_tagged_field_image_ppmm(&ppmm, imgrecord);
   /* If error or IGNORE ... */
   if(ret <= 0)
      return(ret);

   /* Set image pointer to last field value in record. */
   field = imgrecord->fields[imgrecord->num_fields-1];
   idata1 = field->subfields[0]->items[0]->value;
   ilen1 = field->subfields[0]->items[0]->num_bytes;

   /* If the image data is uncompressed ... */
   if(strcmp(img_comp, COMP_NONE) == 0){
      idata2 = (unsigned char *)malloc((size_t)ilen1);
      if(idata2 == NULL){
         fprintf(stderr, "ERROR : decode_tagged_field_image : "
		 "malloc : idata2 (%d bytes)\n", ilen1);
         return(-8);
      }
      memcpy(idata2, idata1, (size_t)ilen1);

      /* For uncompressed RGB, the ANSI/NIST Standard dictates that */
      /* the pixels will be stored noninterleaved in 3 separate     */
      /* component planes. */
      /* So, if flag set to interleaved ... */
      if((id1 == 24) && (intrlvflag != 0)){
         /* There is no downsampling of RGB planes. */
         n_cmpnts = 3;
         for(i = 0; i < n_cmpnts; i++){
            hor_sampfctr[i] = 1;
            vrt_sampfctr[i] = 1;
         }
         if((ret = not2intrlv_mem(&idata1, &ilen1, idata2, iw1, ih1, id1,
				  hor_sampfctr, vrt_sampfctr, n_cmpnts)) != 0){
            free(idata2);
            return(ret);
         }
         free(idata2);
         idata2 = idata1;
         ilen2 = ilen1;
      }

      *odata = idata2;
      *ow = iw1;
      *oh = ih1;
      *od = id1;
      *oppmm = ppmm;

      return(TRUE);
   }
   /* If WSQ compressed ... */
   else if(strcmp(img_comp, COMP_WSQ) == 0){
      if((ret = wsq_decode_mem(&idata2, &iw2, &ih2, &id2, &ppi, &lossyflag,
                              idata1, ilen1)) != 0)
         return(ret);
   }
#if 0
   /* If JPEGB compressed ... */
   else if(strcmp(img_comp, COMP_JPEGB) == 0){
      if((ret = jpegb_decode_mem(&idata2, &iw2, &ih2, &id2, &ppi, &lossyflag,
                                 idata1, ilen1)) != 0)
         return(ret);
      /* For 3 component color, JPEGB's decoder returns interleaved RGB. */
      /* So, if flag set to NOT interleaved ... */
      if((id2 == 24) && (intrlvflag == 0)){
         /* There is no downsampling of RGB planes. */
         n_cmpnts = 3;
         for(i = 0; i < n_cmpnts; i++){
            hor_sampfctr[i] = 1;
            vrt_sampfctr[i] = 1;
         }
         if((ret = intrlv2not_mem(&idata1, &ilen1, idata2, iw2, ih2, id2,
                                 hor_sampfctr, vrt_sampfctr, n_cmpnts)) != 0){
            free(idata2);
            return(ret);
         }
         free(idata2);
         idata2 = idata1;
         ilen2 = ilen1;
      }
   }
#endif
#if 0
   /* If JPEGL compressed ... */
   else if(strcmp(img_comp, COMP_JPEGL) == 0){
      if((ret = jpegl_decode_mem(&img_dat, &lossyflag, idata1, ilen1)) != 0)
         return(ret);
      if((ret = get_IMG_DAT_image(&idata2, &ilen2, &iw2, &ih2, &id2, &ppi,
                                 img_dat)) != 0){
         free_IMG_DAT(img_dat, FREE_IMAGE);
         return(ret);
      }
      /* For 3 component color, JPEGL's decoder returns non-interleaved  */
      /* components planes.  So, if flag set to interleaved ...          */
      if((id2 == 24) && (intrlvflag != 0)){
         /* Note that this is set up to handle downsampled Cb and Cr planes */
         /* if the units encoded were in the YCbCr color space.  However    */
         /* the color space would need to be converted to RGB upon return   */
         /* of this routine. */
         /* The operational assumption is that the encoded color space */
         /* was RGB. */
         if((ret = not2intrlv_mem(&idata1, &ilen1, idata2,
                                 img_dat->max_width, img_dat->max_height,
                                 img_dat->pix_depth, img_dat->hor_sampfctr,
                                 img_dat->vrt_sampfctr, img_dat->n_cmpnts)) !=0){
            free_IMG_DAT(img_dat, FREE_IMAGE);
            free(idata2);
            return(ret);
         }

         free(idata2);
         idata2 = idata1;
         ilen2 = ilen1;
      }
      free_IMG_DAT(img_dat, FREE_IMAGE);
   }
#endif
#if 0 /*__NBIS_JASPER__*/
   /* If JPEG2K compress ... */
   else if((strcmp(img_comp, COMP_JPEG2K) == 0) || 
           (strcmp(img_comp, COMP_JPEG2KL) == 0)){
      /* JPEG2K decoder always return 3 components planes. */
      /* id1 = 24; -- What's in the record has already been copied
	              into id1, and it isn't necessarily 24 - jck */
 
      if((ret = jpeg2k_decode_mem(&img_dat, &lossyflag, idata1, ilen1)) != 0)
         return(ret);
  
      if((ret = get_IMG_DAT_image(&idata2, &ilen2, &iw2, &ih2, &id2, &ppi,
                                 img_dat)) != 0){
         free_IMG_DAT(img_dat, FREE_IMAGE);
         return(ret);
      }
      /* For 3 component color, JPEG2K's decoder returns non-interleaved  */
      /* components planes.  So, if flag set to interleaved ...          */
      if((id2 == 24) && (intrlvflag != 0)){
         if((ret = not2intrlv_mem(&idata1, &ilen1, idata2,
                                  img_dat->max_width, img_dat->max_height,
                                  img_dat->pix_depth, img_dat->hor_sampfctr,
                                  img_dat->vrt_sampfctr, img_dat->n_cmpnts)) != 0){
            free_IMG_DAT(img_dat, FREE_IMAGE);
            free(idata2);
            return(ret);

         }
         free_IMG_DAT(img_dat, FREE_IMAGE);
         free(idata2);
         idata2 = idata1;
         ilen2 = ilen1;
      }
   }
#endif
#if 0 /*__NBIS_OPENJPEG__*/
   /* If JPEG2K compress ... */
   else if((strcmp(img_comp, COMP_JPEG2K) == 0) ||
           (strcmp(img_comp, COMP_JPEG2KL) == 0)){
      /* JPEG2K decoder always return 3 components planes. */
      /* id1 = 24; -- What's in the record has already been copied
                      into id1, and it isn't necessarily 24 - jck */

      if((ret = openjpeg2k_decode_mem(&img_dat, &lossyflag, idata1, ilen1)) != 0)
         return(ret);
 
      if((ret = get_IMG_DAT_image(&idata2, &ilen2, &iw2, &ih2, &id2, &ppi,
                                 img_dat)) != 0){
         free_IMG_DAT(img_dat, FREE_IMAGE);
         return(ret);
      }
      /* For 3 component color, JPEG2K's decoder returns non-interleaved  */
      /* components planes.  So, if flag set to interleaved ...          */
      if((id2 == 24) && (intrlvflag != 0)){
         if((ret = not2intrlv_mem(&idata1, &ilen1, idata2,
                                  img_dat->max_width, img_dat->max_height,
                                  img_dat->pix_depth, img_dat->hor_sampfctr,
                                  img_dat->vrt_sampfctr, img_dat->n_cmpnts)) != 0){
            free_IMG_DAT(img_dat, FREE_IMAGE);
            free(idata2);
            return(ret);

         }
         free_IMG_DAT(img_dat, FREE_IMAGE);
         free(idata2);
         idata2 = idata1;
         ilen2 = ilen1;
      }
   }
#endif
#if 0 /*__NBIS_PNG__*/
   /* If PNG compressed ... */
   else if(strcmp(img_comp, COMP_PNG) == 0){
      if((ret = png_decode_mem(&img_dat, &lossyflag, idata1, ilen1)) != 0)
         return(ret);
      if((ret = get_IMG_DAT_image(&idata2, &ilen2, &iw2, &ih2, &id2, &ppi,
                                 img_dat)) != 0){
         free_IMG_DAT(img_dat, FREE_IMAGE);
         return(ret);
      }
      /* For 3 component color, PNG's decoder returns non-interleaved  */
      /* components planes.  So, if flag set to interleaved ...          */
      if((id2 == 24) && (intrlvflag != 0)){
         if((ret = not2intrlv_mem(&idata1, &ilen1, idata2,
                                  img_dat->max_width, img_dat->max_height,
                                  img_dat->pix_depth, img_dat->hor_sampfctr,
                                  img_dat->vrt_sampfctr, img_dat->n_cmpnts)) !=  0){
            free_IMG_DAT(img_dat, FREE_IMAGE); 
            free(idata2);
            return(ret);
         }
         free_IMG_DAT(img_dat, FREE_IMAGE);
         free(idata2);
         idata2 = idata1;
         ilen2 = ilen1;
      }
   }
#endif
   /* Otherwise, unsupported compression algorithm. */
   else{
      fprintf(stderr, "WARNING : decode_tagged_field_image : "
	      "unsupported compression algorithm %.10s%s in "
	      "image record index [%d] [Type-%d].\n"
	      "Image record ignored.\n",
              img_comp, strlen(img_comp) > 10 ? "..." : "",
              imgrecord_i+1, imgrecord->type);
      /* Ignore image record ... */
      return(FALSE);
   }

   /* Code change by MDG on 07/02/04 to handle discrepancies in   */
   /* image records where the values in fields HLL and VLL differ */
   /* from what was actually decoded from the compressed image    */
   /* block.  In these cases, we post a warning and then set      */
   /* image attributes to be consistent with what was decoded.    */
   if(iw1 != iw2){
      fprintf(stderr, "WARNING : decode_tagged_field_image : "
	      "[HLL field (from file) = %d] != "
	      "[image width (from decoder) = %d]\n"
	      "Will continue with operating assumption "
	      "that image width is %d\n", iw1, iw2, iw2);
      iw1 = iw2;
   }
   if(ih1 != ih2){
      fprintf(stderr, "WARNING : decode_tagged_field_image : "
	      "[VLL field (from file) = %d] != "
	      "[image height (from decoder) = %d]\n"
	      "Will continue with operating assumption "
	      "that image height is %d\n", ih1, ih2, ih2);
      ih1 = ih2;
   }
   if(id1 != id2){
      fprintf(stderr, "WARNING : decode_tagged_field_image : "
	      "[pixel depth (from field value) = %d] != "
	      "[pixel depth (from decoder) = %d]\n"
	      "Will continue with operating assumption "
	      "that image depth is %d\n", id1, id2, id2);
      id1 = id2;
   }

   *odata = idata2;
   *ow = iw1;
   *oh = ih1;
   *od = id1;
   *oppmm = ppmm;

   return(TRUE);
}
Esempio n. 4
0
static int
update_type1(ANSI_NIST *ansi_nist, RECORD *record, unsigned int type, 
		unsigned int idc)
{
	SUBFIELD *subfield = NULL;
	FIELD *field = NULL;
	ITEM *item = NULL;
	int field_idx;
	int saved_len;
	char buf[8];

	if (lookup_ANSI_NIST_field(&field, &field_idx, CNT_ID, record) == FALSE)
		ERR_OUT("locating CNT field of Type-1 record");

	// Save away the current length of the field so we can add
	// the change in length to the record
	saved_len = field->num_bytes;

	// Create a new subfield to contain the new logical record identifier
	snprintf(buf, sizeof(buf), "%d", type);
	if (value2subfield(&subfield, buf) != 0)
		ERR_OUT("creating new subfield");

	// Add the second item to the subfield, the IDC
	snprintf(buf, sizeof(buf), IDC_FMT, idc);
	if (value2item(&item, buf) != 0)
		ERR_OUT("creating new item");
	if (append_ANSI_NIST_subfield(subfield, item) != 0)
		ERR_OUT("appending item to subfield");

	// Add the subfield to the field
	if (append_ANSI_NIST_field(field, subfield) != 0)
		ERR_OUT("adding subfield to field");

	// Update the record length with the change in field length
	record->num_bytes += field->num_bytes - saved_len;
	if (update_ANSI_NIST_tagged_record_LEN(record) != 0)
		goto err_out2;

	// Update the sum of the Type-2 to Type-16 logical records, contained
	// in the first subfield of the Type-1 record.
	// The index numbeers are off-by-one.
	if (increment_numeric_item(0,		// record index
				   2,		// field index
				   0,		// subfield index
				   1,		// item index
				   ansi_nist,	// ansi_nist record
				   NULL) < 0)
		goto err_out2;

	return 0;

err_out:
	if (item != NULL)
		free(item);
	if (subfield != NULL)
		free(subfield);
	
	return -1;

err_out2:	// This exit point doesn't free any memory because the
		// fields have been shoved into the record
	return -1;
}