Esempio n. 1
0
int getc_ppi_wsq(int *oppi, unsigned char *idata, const int ilen)
{
   int ret;
   int ppi;
   char *value;
   NISTCOM *nistcom;

   /* Get ppi from NISTCOM, if one exists ... */
   if((ret = getc_nistcom_wsq(&nistcom, idata, ilen)))
      return(ret);
   if(nistcom != (NISTCOM *)NULL){
      if((ret = extractfet_ret(&value, NCM_PPI, nistcom))){
         freefet(nistcom);
         return(ret);
      }
      if(value != (char *)NULL){
         ppi = atoi(value);
         free(value);
      }
      /* Otherwise, PPI not in NISTCOM, so ppi = -1. */
      else
         ppi = -1;
      freefet(nistcom);
   }
   /* Otherwise, NISTCOM does NOT exist, so ppi = -1. */
   else
      ppi = -1;

   *oppi = ppi;

   return(0);
}
Esempio n. 2
0
int readfetfile_ret(FET **ofet, char *file)
{
   int ret;
   FILE *fp;
   FET *fet;
   char c,buf[MAXFETLENGTH];
   size_t len = 0;

   if ((fp = fopen(file,"rb")) == (FILE *)NULL){
      fprintf(stderr, "ERROR : readfetfile_ret : fopen : %s\n", file);
      return(-2);
   }

   if((ret = allocfet_ret(&fet, MAXFETS))){
      fclose(fp);
      return(ret);
   }

   while (fscanf(fp,"%s",buf) != EOF){
      while(((c = getc(fp)) == ' ') || (c == '\t'));
      ungetc(c, fp);
      if (fet->num >= fet->alloc){
         if((ret = reallocfet_ret(&fet, fet->alloc + MAXFETS))){
            fclose(fp);
            freefet(fet);
            return(ret);
         }
      }
      len = strlen(buf) + 1;
      fet->names[fet->num] = malloc(len);
      if(fet->names[fet->num] == (char *)NULL){
         fprintf(stderr, "ERROR : readfetfile_ret : malloc : fet->names[]\n");
         fclose(fp);
         freefet(fet);
         return(-3);
      }
      strncpy(fet->names[fet->num], buf, len);
      fgets(buf,MAXFETLENGTH-1,fp);
      buf[strlen(buf)-1] = '\0';
      len = strlen(buf) + 1;
      fet->values[fet->num] = malloc(len);
      if(fet->values[fet->num] == (char *)NULL){
         fprintf(stderr, "ERROR : readfetfile_ret : malloc : fet->values[]\n");
         fclose(fp);
         freefet(fet);
         return(-4);
      }
      strncpy(fet->values[fet->num], buf, len);
      (fet->num)++;
   }
   fclose(fp);
   *ofet = fet;

   return(0);
}
Esempio n. 3
0
File: strfet.c Progetto: E3V3A/JMRTD
int string2fet(FET **ofet, char *istr)
{
   int ret;
   char *iptr, *optr;
   char name[MAXFETLENGTH], value[MAXFETLENGTH];
   FET *fet;

   ret = allocfet_ret(&fet, MAXFETS);
   if(ret)
      return(ret);

   iptr = istr;
   while(*iptr != '\0'){
      /* Get next name */
      optr = name;
      while((*iptr != '\0')&&(*iptr != ' ')&&(*iptr != '\t'))
         *optr++ = *iptr++;
      *optr = '\0';

      /* Skip white space */
      while((*iptr != '\0')&&
            ((*iptr == ' ')||(*iptr == '\t')))
         iptr++;

      /* Get next value */
      optr = value;
      while((*iptr != '\0')&&(*iptr != '\n'))
         *optr++ = *iptr++;
      *optr = '\0';

      /* Skip white space */
      while((*iptr != '\0')&&
            ((*iptr == ' ')||(*iptr == '\t')||(*iptr == '\n')))
         iptr++;

      /* Test (name,value) pair */
      if(strlen(name) == 0){
         fprintf(stderr, "ERROR : string2fet : empty name string found\n");
         return(-2);
      }

      /* Store name and value pair into FET. */
      ret = updatefet_ret(name, value, fet);
      if(ret){
         freefet(fet);
         return(ret);
      }
   }

   *ofet = fet;
   return(0);
}
Esempio n. 4
0
int main( int argc, char **argv)
{
   int ret;
   int rawflag;                   /* raw input data or Ihead image */
   char *outext;                  /* ouput file extension */
   char *ifile, ofile[MAXPATHLEN];  /* file names */
   int ilen;
   int width, height;             /* image parameters */
   int depth, ppi;
   unsigned char *idata, *odata;  /* image pointers */
   int lossyflag;                 /* data loss flag */
   NISTCOM *nistcom;              /* NIST Comment */
   char *ppi_str;


   procargs(argc, argv, &outext, &ifile, &rawflag);

   ret = read_raw_from_filesize(ifile, &idata, &ilen);
   if(ret)
      exit(ret);

   ret = wsq_decode_mem(&odata, &width, &height, &depth, &ppi, &lossyflag, idata, ilen);
   if(ret){
      free(idata);
      exit(ret);
   }

   if(debug > 1)
      fprintf(stderr, "Image pixmap constructed\n");

   fileroot(ifile);
   sprintf(ofile, "%s.%s", ifile, NCM_EXT);

   /* Get NISTCOM from compressed data file */
   ret = getc_nistcom_wsq(&nistcom, idata, ilen);
   if(ret){
      free(idata);
      exit(ret);
   }
   free(idata);
   /* WSQ decoder always returns ppi=-1, so believe PPI in NISTCOM, */
   /* if it already exists. */
   ppi_str = (char *)NULL;
   if(nistcom != (NISTCOM *)NULL){
      ret = extractfet_ret(&ppi_str, NCM_PPI, nistcom);
      if(ret){
         free(odata);
         freefet(nistcom);
         exit(ret);
      }
   }
   if(ppi_str != (char *)NULL){
      ppi = atoi(ppi_str);
      free(ppi_str);
   }

   /* Combine NISTCOM with image features */
   ret = combine_wsq_nistcom(&nistcom, width, height, depth, ppi, lossyflag,
		   0.0 /* will be deleted next */);
   if(ret){
     free(odata);
     if(nistcom != (NISTCOM *)NULL)
        freefet(nistcom);
     exit(ret);
   }
   ret = del_wsq_nistcom(nistcom);
   if(ret){
     free(odata);
     freefet(nistcom);
     exit(ret);
   }

   /* Write NISTCOM */
   ret = writefetfile_ret(ofile, nistcom);
   if(ret){
     free(odata);
     freefet(nistcom);
     exit(ret);
   }
   freefet(nistcom);

   /* Write decoded image file. */
   sprintf(ofile, "%s.%s", ifile, outext);
   ret = write_raw_or_ihead(!rawflag, ofile, odata, width, height, depth, ppi);
   if(ret){
      free(odata);
      exit(ret);
   }
   free(odata);

   if(debug > 1)
      fprintf(stdout, "Image pixmap written to %s\n", ofile);

   exit(0);
}
Esempio n. 5
0
File: j2wsq.c Progetto: E3V3A/JMRTD
/**
 * Decodes an encoded WSQ byte array to a buffered image.
 * Based on dwsq.c.
 *
 * @param in a byte array holding the encoded WSQ image
 *
 * @return a buffered image
 */
JNIEXPORT jobject JNICALL Java_org_jmrtd_imageio_WSQImageReader_decodeWSQ
 (JNIEnv *env, jobject obj, jbyteArray in, jobject metadata) {
   /* Java classes and objects */
  jclass    ioexceptionClazz = (*env)->FindClass(env, "java/io/IOException");
  jclass    metadataClazz = (*env)->GetObjectClass(env, metadata);
  jclass    imgClazz = (*env)->FindClass(env, "java/awt/image/BufferedImage");
  jclass    writableRasterClazz = (*env)->FindClass(env, "java/awt/image/WritableRaster");
  jmethodID metadataSetPropertyMethodID = (*env)->GetMethodID(env, metadataClazz, "setProperty", "(Ljava/lang/String;Ljava/lang/String;)Z");  
  jmethodID imgInitMethodID = (*env)->GetMethodID(env, imgClazz, "<init>", "(III)V");
  jmethodID imgGetRasterMethodID = (*env)->GetMethodID(env, imgClazz, "getRaster", "()Ljava/awt/image/WritableRaster;");
  jmethodID rasterSetDataElementsMethodID = (*env)->GetMethodID(env, writableRasterClazz, "setDataElements", "(IIIILjava/lang/Object;)V");
  jfieldID  imgTypeByteGrayFieldID = (*env)->GetStaticFieldID(env, imgClazz, "TYPE_BYTE_GRAY", "I");
  jint      TYPE_BYTE_GRAY = (*env)->GetStaticIntField(env, imgClazz, imgTypeByteGrayFieldID);
  jobject   imgObject, rasterObject;    
  
  /* WSQ-Compressed input buffer */
  unsigned char *idata = (unsigned char*)(*env)->GetByteArrayElements(env, in, JNI_FALSE);
  int idata_len = (*env)->GetArrayLength(env, in);
  
  /* Uncompressed Pixel Buffer */
  unsigned char *odata = NULL;
  jbyteArray jodata = NULL;
  /* Image parameters */
  NISTCOM *nistcom = NULL;
  int width, height, depth, ppi, lossy; 
  int wsq_decore_ret, nistcom_ret;
  /* Parse WSQ Image */
  wsq_decore_ret = wsq_decode_mem(&odata, &width, &height, &depth, &ppi, &lossy, idata, idata_len);
  if (wsq_decore_ret==0) {
    jodata = (*env)->NewByteArray(env, width*height);
    (*env)->SetByteArrayRegion(env, jodata, 0, width*height, (jbyte*)odata);
    free(odata);
    odata = NULL;
  } else {
    (*env)->ReleaseByteArrayElements(env, in, (jbyte*)idata, JNI_FALSE);
    (*env)->ThrowNew(env, ioexceptionClazz, "(In native C code) function wsq_decode_mem failed");
    return NULL;
  }
  
  
  /* WSQ Metadata */
  
  nistcom_ret    = getc_nistcom_wsq(&nistcom, idata, idata_len);
  if (nistcom_ret==0 && nistcom != NULL) {
    int i;
    for (i=0; i<nistcom->num; i++) {
      if (nistcom->names[i]) {
        (*env)->CallBooleanMethod(
          env, 
          metadata, 
          metadataSetPropertyMethodID,
          (*env)->NewStringUTF(env, nistcom->names[i]), 
          nistcom->values[i] ? (*env)->NewStringUTF(env, nistcom->values[i]) : NULL );
      }
    }
    freefet(nistcom);
  }
  (*env)->ReleaseByteArrayElements(env, in, (jbyte*)idata, JNI_FALSE);
  
  
  /* Construct a BufferedImage from the byte array:
      BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
      WritableRaster raster = img.getRaster();
      raster.setDataElements(0, 0, width, height, decodedBytes); 
  */
  imgObject = (*env)->NewObject(env, imgClazz, imgInitMethodID, width, height, TYPE_BYTE_GRAY);
  rasterObject = (*env)->CallObjectMethod(env, imgObject, imgGetRasterMethodID);
  (*env)->CallVoidMethod(env, rasterObject, rasterSetDataElementsMethodID, 0, 0, width, height, jodata);
 
  return imgObject;
}
Esempio n. 6
0
int read_ppi_wsq(int *oppi, FILE *infp)
{
   int ret;
   long savepos;
   int ppi;
   char *value;
   NISTCOM *nistcom;

   /* Save current position in filestream ... */
   if((savepos = ftell(infp)) < 0){
      fprintf(stderr, "ERROR : read_ppi_wsq : ");
      fprintf(stderr, "ftell : couldn't determine current position\n");
      return(-2);
   }
   /* Set file pointer to beginning of filestream ... */
   if(fseek(infp, 0L, SEEK_SET) < 0){
      fprintf(stderr, "ERROR : read_ppi_wsq : ");
      fprintf(stderr, "fseek : couldn't set pointer to start of file\n");
      return(-3);
   }

   /* Get ppi from NISTCOM, if one exists ... */
   if((ret = read_nistcom_wsq(&nistcom, infp))){
      /* Reset file pointer to original position in filestream ... */
      if(fseek(infp, savepos, SEEK_SET) < 0){
         fprintf(stderr, "ERROR : read_ppi_wsq : ");
         fprintf(stderr, "fseek : couldn't reset file pointer\n");
         return(-4);
      }
      return(ret);
   }
   if(nistcom != (NISTCOM *)NULL){
      if((ret = extractfet_ret(&value, NCM_PPI, nistcom))){
         freefet(nistcom);
         /* Reset file pointer to original position in filestream ... */
         if(fseek(infp, savepos, SEEK_SET) < 0){
            fprintf(stderr, "ERROR : read_ppi_wsq : ");
            fprintf(stderr, "fseek : couldn't reset file pointer\n");
            return(-5);
         }
         return(ret);
      }
      if(value != (char *)NULL){
         ppi = atoi(value);
         free(value);
      }
      /* Otherwise, PPI not in NISTCOM, so ppi = -1. */
      else
         ppi = -1;
      freefet(nistcom);
   }
   /* Otherwise, NISTCOM does NOT exist, so ppi = -1. */
   else
      ppi = -1;

   /* Reset file pointer to original position in filestream ... */
   if(fseek(infp, savepos, SEEK_SET) < 0){
      fprintf(stderr, "ERROR : read_ppi_wsq : ");
      fprintf(stderr, "fseek : couldn't reset file pointer\n");
      return(-6);
   }

   *oppi = ppi;

   return(0);
}
Esempio n. 7
0
int main(int argc, char **argv)
{
   int ret, sd_id;
   char *outext, *ifile, ofile[MAXPATHLEN];
   FILE *infp;
   IHEAD *ihead;
   int width, height, depth, ppi;
   unsigned char *tidata, *idata, *odata, *ocdata;
   int i, complen, olen, oclen;
   IMG_DAT *img_dat;
   NISTCOM *nistcom;
   char *comment_text;
   int sampfctr;

   procargs(argc, argv, &sd_id, &outext, &ifile);

   infp = fopen(ifile, "rb");
   if(infp == (FILE *)NULL) {
      fprintf(stderr, "Error opening file %s for reading\n", ifile);
      exit(-2);
   }

   ihead = readihdr(infp);
   if(ihead == (IHEAD *)NULL) {
      fprintf(stderr, "Error reading IHEAD header\n");
      exit(-3);
   }

   sscanf(ihead->height,"%d", &height);
   sscanf(ihead->width,"%d", &width);
   sscanf(ihead->depth,"%d", &depth);
   sscanf(ihead->density,"%d", &ppi);
   sscanf(ihead->complen,"%d", &complen);

   /* Construct NISTCOM */
   if((ret = sd_ihead_to_nistcom(&nistcom, ihead, sd_id))){
      free(ihead);
      exit(ret);
   }
   free(ihead);
   if(sd_id == 14){
      if((ret = combine_wsq_nistcom(&nistcom, width, height, depth, ppi,
                                   1, -1.0 /* unknown bitrate */))){
         freefet(nistcom);
         exit(ret);
      }
   }
   else{
      if((ret = combine_jpegl_nistcom(&nistcom, width, height, depth, ppi,
                                   0, 1, (int *)NULL, (int *)NULL, 0, PRED4))){
         freefet(nistcom);
         exit(ret);
      }
   }

   /* Convert NISTCOM to string. */
   if((ret = fet2string(&comment_text, nistcom))){
      freefet(nistcom);
      exit(ret);
   }
   freefet(nistcom);

   /* Switch on converter, supplying a NISTCOM ... */
   /* If SD14 ... WSQ convert. */
   if(sd_id == 14){
      /* Convert image data to new format in memory. */
      if((ret = wsq14_2_wsq(&odata, &olen, infp))) {
         fclose(infp);
         free(comment_text);
         exit(ret);
      }
      fclose(infp);
      /* Add comment text into new data format stream. */
      if((ret = add_comment_wsq(&ocdata, &oclen, odata, olen,
                                (unsigned char *)comment_text))){
         free(odata);
         free(comment_text);
         exit(ret);
      }
      free(odata);
      odata = ocdata;
      olen = oclen;
   }
   /* Otherwise, SD 4,9,10,18 ... JPEGL convert. */
   else{
      if((tidata = (unsigned char *)malloc(complen)) == (unsigned char *)NULL){
         fprintf(stderr, "ERROR : %s : malloc : tidata\n", argv[0]);
         exit(-4);
      }

      i = fread(tidata, sizeof(unsigned char), complen, infp);
      if(i != complen) {
         fprintf(stderr, "Error reading compressed data from %s\n", ifile);
         free(tidata);
         exit(-5);
      }
      fclose(infp);

      if(debug > 0)
         fprintf(stdout, "File %s read\n", ifile);

      if((idata = (unsigned char *)malloc(width*height))==(unsigned char *)NULL){
         fprintf(stderr, "ERROR : %s : malloc : idata\n", argv[0]);
         free(tidata);
         free(comment_text);
         exit(-6);
      }

      if((ret = jpegl_sd4_decode_mem(tidata, complen,
                                    width, height, depth, idata))){
         free(tidata);
         free(comment_text);
         free(idata);
         exit(ret);
      }
      free(tidata);
      if(debug > 0){
         fprintf(stdout, "Done decode JPEGL SD4 image\n");
         fprintf(stdout, "Starting JPEGL compression\n");
      }

      /* Used to setup integer array of length 1 initialized to 1. */
      sampfctr = 1;
      if((ret = setup_IMG_DAT_nonintrlv_encode(&img_dat, idata,
                                       width, height, depth, ppi,
                                       &sampfctr, &sampfctr, 1,
                                       0, PRED4))) {
         free(comment_text);
         free(idata);
         exit(ret);
      }

      if(debug > 0)
         fprintf(stdout, "Image structure initialized\n");

      if((ret = jpegl_encode_mem(&odata, &olen, img_dat, comment_text))){
         free(comment_text);
         free_IMG_DAT(img_dat, FREE_IMAGE);
         exit(ret);
      }
      free(comment_text);
      free_IMG_DAT(img_dat, FREE_IMAGE);
   }

   if(debug > 0)
      fprintf(stdout, "Image data converted, reformatted byte length = %d\n",
              olen);

   /* Write reformatted file. */

   fileroot(ifile);
   sprintf(ofile, "%s.%s", ifile, outext);

   if((ret = write_raw_from_memsize(ofile, odata, olen))){
      free(odata);
      exit(ret);
   }

   if(debug > 0)
      fprintf(stdout, "Image data written to file %s\n", ofile);

   free(odata);
   exit(0);
}
Esempio n. 8
0
int main(int argc, char *argv[])
{
   int ret, rawflag, img_type;
   unsigned char *idata, *odata;
   int ilen, width, height, depth, ppi, lossyflag;
   IHEAD *ihead;
   FILE *fp;
   char *outext, *ifile, ofile[MAXPATHLEN];
   NISTCOM *nistcom;


   procargs(argc, argv, &outext, &ifile, &rawflag);
   /* Set ppi to unknown */
   ppi = -1;
   nistcom = (NISTCOM *)NULL;

   /* Check image type */
   ret = read_raw_from_filesize(ifile, &idata, &ilen);
   if(ret)
      exit(ret);

   ret = image_type(&img_type, idata, ilen);
   if(ret) {
      free(idata);
      exit(ret);
   }
   free(idata);

   /* Open image file for reading based on image type */
   if((fp = fopen(ifile,"rb")) == NULL) {
      fprintf(stderr, "ERROR: main : fopen : %s\n",ifile);
      exit(-1);
   }
   /* If img_type is ihead ... */
   if(img_type == IHEAD_IMG){
      /* Read ihead header and check for WSQ_SD14 compression */
      ihead = readihdr(fp);
      if(atoi(ihead->compress) != WSQ_SD14){
         fprintf(stderr, "ERROR : main : input image not WSQ_SD14 compressed\n");
         fprintf(stderr, "        compression = %d, WSQ_SD14 = %d\n",
                 atoi(ihead->compress), WSQ_SD14);
         fclose(fp);
         free(ihead);
         exit(-1);
      }

      /* Get ppi from ihead header */
      sscanf(ihead->density,"%d", &ppi);

      /* Create a nistcom for the image attributes */
      ret = sd_ihead_to_nistcom(&nistcom, ihead, 14);
      if(ret) {
         fclose(fp);
         free(ihead);
         exit(ret);
      }
      free(ihead);
   }
   /* If image not WSQ_IMG or IHEAD_IMG, ERROR!!! */
   else if(img_type != WSQ_IMG) {
      fprintf(stderr, "ERROR : main : Invalid image\n");
      fprintf(stderr, "Expected a WSQ_SD14 compressed image in\n");
      fprintf(stderr, "either raw or ihead format.\n");
      fclose(fp);
      exit(-1);
   }

   /* Decode compressed image */
   ret = wsq14_decode_file(&odata, &width, &height, &depth, &lossyflag, fp);
   if(ret){
      fclose(fp);
      if(img_type == IHEAD_IMG)
         freefet(nistcom);
      exit(ret);
   }
   fclose(fp);

   if(debug > 1)
      fprintf(stderr, "Image pixmap constructed\n");

   /* Combine image attributes into current nistcom */
   ret = combine_wsq_nistcom(&nistcom, width, height, depth, ppi, lossyflag, -1.0);
   if(ret){
     if(img_type == IHEAD_IMG)
        freefet(nistcom);
     free(odata);
     exit(ret);
   }
   ret = del_wsq_nistcom(nistcom);
   if(ret){
      free(odata);
      freefet(nistcom);
      exit(ret);
   }

   fileroot(ifile);
   sprintf(ofile, "%s.%s", ifile, NCM_EXT);
   /* Write NISTCOM */
   ret = writefetfile_ret(ofile, nistcom);
   if(ret){
     freefet(nistcom);
     free(odata);
     exit(ret);
   }
   freefet(nistcom);

   /* Write decompressed image */
   sprintf(ofile, "%s.%s", ifile, outext);
   ret = write_raw_or_ihead(!rawflag, ofile, odata, width, height, depth, ppi);
   if(ret){
      free(odata);
      exit(ret);
   }
   free(odata);

   if(debug > 1)
      fprintf(stderr, "Image pixmap written to %s\n", ofile);

   exit(0);
}