示例#1
0
DistillerStatus gjpg_passthrough(DistillerInput *din, DistillerOutput *dout)
{
  strcpy(dout->mimeType, din->mimeType);
  SetDataLength(dout,DataLength(din));
  SetData(dout, DataPtr(din));
  DataNeedsFree(dout,gm_False);
  return distOk;
}
示例#2
0
//============================================================================
// Asyncload will queue up a buffer for loading in our loading list the first time it is called.
// It will load in "length" number of bytes, if length is non zero. If length is zero the entire file will be loaded
// When called subsequent times it will check to see if the data has been loaded.
// Returns kPending while still loading the file. Returns kSuccess when the data has been loaded.
// While a file is loading(fLoading == true, and fLoaded == false) a buffer, no paremeters of the buffer should be modified.
plSoundBuffer::ELoadReturnVal plSoundBuffer::AsyncLoad(plAudioFileReader::StreamType type, unsigned length /* = 0 */ )
{
    if(!gLoaderThread.IsRunning())
        return kError;  // we cannot load the data since the load thread is no longer running
    if(!fLoading && !fLoaded)
    {
        fAsyncLoadLength = length;
        fStreamType = type;
        if(fData == nil )
        {
            fData = new uint8_t[ fAsyncLoadLength ? fAsyncLoadLength : fDataLength ];
            if( fData == nil )
                return kError;
        }

        gLoaderThread.AddBuffer(this);
        fLoading = true;
    }
    if(fLoaded) 
    {   
        if(fLoading)    // ensures we only do this stuff one time
        {
            ELoadReturnVal retVal = kSuccess;
            if(fError)
            {
                retVal = kError;
                fError = false;
            }
            if(fReader)
            {
                fHeader = fReader->GetHeader();
                SetDataLength(fReader->GetDataSize());
            }

            fFlags &= ~kIsExternal;
            fLoading = false;
            return retVal;
        }
        return kSuccess;
    }
    
    return kPending;
}
示例#3
0
int
main(int argc, char *argv[])
{
  DistillerStatus st;
  UINT32 len;
  int i;
  char nextfile[MAX_FILENAME];
  char logFile[MAX_FILENAME];
  char *timeStr;
  TestRun runs[MAX_TESTRUNS];
  FILE *f;
  time_t t;
  
  int numRuns;
  int repeatCount;
  DistillerInput in;
  DistillerOutput out;
  C_DistillerType distType;


  sprintf(distType.string, "test " INPUT_MIME_TYPE);
  if (argc < 3) {
    usage();
    exit(1);
  }
  
  if (!(numRuns = readInputFile(argv[1],runs))) {
    fprintf(stderr, "Error reading input file %s.\n", argv[1]);
    exit(1);
  }
  repeatCount = atoi(argv[2]);
  if (repeatCount <=0) {
    fprintf(stderr, "Invaild count value.\n");
    exit(1);
  }

  if ((st = DistillerInit(distType, 0, NULL)) != distOk) {
    fprintf(stderr, "DistillerInit failed: error %d\n", (int)st);
    exit(1);
  }

  sprintf(logFile,"harness.log");

  t = time(NULL);
  timeStr = ctime(&t);

  printf("%s\n",logFile);
  if ((f = fopen(logFile, "a")) == NULL) {
    fprintf(stderr, "Can't open log file %s\n", logFile);
    return 0;
  }

  SetMimeType(&in, INPUT_MIME_TYPE);
  
  for (i=0;i<numRuns;i++) {
    int fd;
    int count;
    int ii;

    fprintf(stdout,"Test run for case %s:\n",runs[i].inputFileName);
    fprintf(f,"Test run for case %s:\n",runs[i].inputFileName);
    printArguments(f,*(runs[i].argList));

    fd = open(runs[i].inputFileName, O_RDONLY);


    if (fd == -1) {
      fprintf(stderr, "Can't read %s, skipping\n", runs[i].inputFileName);
      fprintf(f, "Can't read %s, skipping\n", runs[i].inputFileName);
      continue;
    }
    for (len = 0;
         (count = read(fd, (void*)(buf+len), (sizeof(buf)-len))) > 0;
         len += count)
      ;

          
    fprintf(stderr, "Read %lu bytes from %s\n", len, runs[i].inputFileName);
    fprintf(f, "Read %lu bytes from %s\n", len, runs[i].inputFileName);
    SetData(&in, (void *)buf);
    SetDataLength(&in, len);
    SetMetadata(&in, NULL);
    SetMetadataLength(&in, 0);


    for (ii= 0; ii<repeatCount; ii++) {

      fprintf(stderr,"Calling distiller main\n");
      fprintf(f,"Calling distiller main\n");
     /* Distiller status */
      st = DistillerMain(runs[i].argList->arg,runs[i].argList->nargs,&in,&out); 
      if (st != distOk) {
        fprintf(stderr, "DistillerMain failed: error %d\n", (int)st);
        fprintf(f, "DistillerMain failed: error %d\n", (int)st);
      }
      close(fd);
      strcpy(nextfile,runs[i].inputFileName);
      strcat(nextfile, ".OUT");
      fd = open(nextfile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
      if (fd == -1) {
        fprintf(stderr, "Can't write %s, using stdout\n", nextfile);
        fprintf(f, "Can't write %s, using stdout\n", nextfile);
        fd = fileno(stdout);
      }
      len = write(fd, (const void *)DataPtr(&out), (size_t)(DataLength(&out)));
      if (fd != fileno(stdout))
        close(fd);
      fprintf(stderr, "Wrote %lu of %lu bytes to %s\n", len, DataLength(&out), nextfile);
      fprintf(f, "Wrote %lu of %lu bytes to %s\n", len, DataLength(&out), nextfile);
      if (out.data.freeMe == gm_True) 
        DistillerFree(DataPtr(&out));
      if (out.metadata.freeMe == gm_True)
        DistillerFree(MetadataPtr(&out));
    }
  }
  fclose(f);
  return(1);
}
示例#4
0
int
main(int argc, char *argv[])
{
  DistillerStatus st;
  UINT32 len;
  Argument args[12];
  int nargs = 0;
  int i;
  char *k;
  FILE *f;
  char nextfile[255];
  DistillerInput in;
  DistillerOutput out;
  C_DistillerType distType;

  sprintf(distType.string, "test " INPUT_MIME_TYPE);
  
  if ((argc < 2) || (argc >= 2  &&  strncmp(argv[1], "-h", 2) == 0)) {
    usage();
    exit(1);
  }

  if ((f = fopen(argv[1], "r")) == NULL) {
    fprintf(stderr, "Can't open input file %s\n", argv[1]);
    exit(1);
  }


  for (i=2; i<argc-1; i += 2, nargs++) {
    SET_ARG_ID(args[nargs], strtoul(&argv[i][1], (char**)NULL, 0));
    switch (argv[i][0]) {
    case 'i':
      SET_ARG_INT(args[nargs], strtol(argv[i+1], (char**)NULL, 0));
      fprintf(stderr, "Arg id %lu is %ld\n", ARG_ID(args[nargs]),
              ARG_INT(args[nargs]));
      break;
    case 'f':
      SET_ARG_DOUBLE(args[nargs], strtod(argv[i+1], (char**)NULL));
      fprintf(stderr, "Arg id %lu is %f\n", ARG_ID(args[nargs]),
              (float)ARG_DOUBLE(args[nargs]));
      break;
    case 's':
    default:
      SET_ARG_STRING(args[nargs], argv[i+1]);
      fprintf(stderr, "Arg id %lu is \"%s\"\n", ARG_ID(args[nargs]),
              ARG_STRING(args[nargs]));
    }
  }

  if ((st = DistillerInit(distType, 0, NULL)) != distOk) {
    fprintf(stderr, "DistillerInit failed: error %d\n", (int)st);
    exit(1);
  }

  SetMimeType(&in, INPUT_MIME_TYPE);
  while (fgets(nextfile, 254, f) != NULL) {
    char nextfile2[255];
    int fd;
    int count;
    int ii;
      
    nextfile[strlen(nextfile)-1] = 0;
    fd = open(nextfile, O_RDONLY);
    if (fd == -1) {
      fprintf(stderr, "Can't read %s, skipping\n", nextfile);
      continue;
    }
    for (len = 0;
         (count = read(fd, (void*)(buf+len), (sizeof(buf)-len))) > 0;
         len += count)
      ;
          
    fprintf(stderr, "Read %lu bytes from %s\n", len, nextfile);
    SetData(&in, (void *)buf);
    SetDataLength(&in, len);
    SetMetadata(&in, NULL);
    SetMetadataLength(&in, 0);

    for (ii= 0; ii<REPEAT_COUNT; ii++) {

      fprintf(stderr,"Calling distiller main\n");
      st = DistillerMain(args,nargs,&in,&out);
      if (st != distOk) {
        fprintf(stderr, "DistillerMain failed: error %d\n", (int)st);
      }
      close(fd);
      strcpy(nextfile2, argv[argc-1]);
      if (nextfile2[strlen(nextfile2)-1] != '/')
        strcat(nextfile2,"/");
      k = strrchr(nextfile, '/');
      if (k)
        strcat(nextfile2, k+1);
      else
        strcat(nextfile2, nextfile);
      strcat(nextfile2, ".OUT");
      fd = open(nextfile2, O_CREAT | O_WRONLY | O_TRUNC, 0666);
      if (fd == -1) {
        fprintf(stderr, "Can't write %s, using stdout\n", nextfile2);
        fd = fileno(stdout);
      }
      len = write(fd, (const void *)DataPtr(&out), (size_t)(DataLength(&out)));
      if (fd != fileno(stdout))
        close(fd);
      fprintf(stderr, "Wrote %lu of %lu bytes to %s\n", len, DataLength(&out), nextfile2);
      if (out.data.freeMe == gm_True) 
        DistillerFree(DataPtr(&out));
      if (out.metadata.freeMe == gm_True)
        DistillerFree(MetadataPtr(&out));
    }
  }
  return(1);
}
示例#5
0
DistillerStatus
DistillerMain(Argument *args, int nargs,
	      DistillerInput *din,
	      DistillerOutput *dout)
{
  JSAMPROW handoff = NULL;
  JDIMENSION handoff_height, handoff_width, num_scanlines;
  int        max_x=-1, max_y=-1, min_x=-1, min_y=-1, qual=55, i;
  int        expert=0, resize=0, quality=3, nodistill=0, ismap=0;
  double     scale = 0.5;
  cjpeg_source_ptr src_mgr;
  void       *phase1_data;
  INT32       phase1_length;
  int        fin_denom, fin_qual;
  DistillerStatus result = distBadInput;

  SetData(dout, NULL);
  bailout_now = 0;

  if ( (setjmp(jumpbuffer) != 0) ) {
    /*
     *  fatal error occurred, so return immediately.
     */
    MonitorClientSend(monID, "Distiller Errors", 
		      "Resetting distiller...\n", "Log");
    DistillerExit();
    DistillerInit(dType, 0, NULL);

    if (DataPtr(dout) != NULL)
      DataNeedsFree(dout,gm_True);
    else
      DataNeedsFree(dout,gm_False);
    return distFatalError;
  }

  /*
   *  parse distillation arguments.  set default values for some
   *  things, then override them if args are specified.  Default will
   *  be to scale each axis by 0.5, turn quality down to 55%.
   */

  for (i=0; i<nargs; i++) {
    INT32 argval = ARG_INT(args[i]);

    switch(ARG_ID(args[i])) {
    case GJPG_MAX_X:
      max_x = (int) argval;
      break;
    case GJPG_MAX_Y:
      max_y = (int) argval;
      break;
    case GJPG_MIN_X:
      min_x = (int) argval;
      break;
    case GJPG_MIN_Y:
      min_y = (int) argval;
      break;
    case GJPG_SCALE:
      scale = (double) ARG_DOUBLE(args[i]);
      break;
    case GJPG_QUAL:
      qual = (int) argval;
      break;
    case FRONT_RESIZE:
      resize = (int) argval;
      break;
    case FRONT_NO_DISTILL:
      nodistill = (int) argval;
      break;
    case FRONT_QUALITY:
      if (argval >= 1 && argval <= 5)
        quality = (int) argval;
      break;
    case FRONT_EXPERT:
      expert = (int) argval;
      break;
    case FRONT_ISMAP:
      ismap = (int) argval;
      break;
    default:
      break;
    }
  }

  if (nodistill) {
    return gjpg_passthrough(din, dout);
  }

  /* First pass through, we're just going to convert the GIF to JPEG */
  phase1_data = NULL;
  phase1_length = 0;
  dstinfo.in_color_space = JCS_RGB;
  jpeg_set_defaults(&dstinfo);
  src_mgr = jinit_read_gif(&dstinfo, (JOCTET *) DataPtr(din),
			   (INT32) DataLength(din));
  (*src_mgr->start_input)(&dstinfo, src_mgr);
  jpeg_default_colorspace(&dstinfo);
  jpeg_mem_dest(&dstinfo, (void **) &phase1_data, (UINT32 *) &phase1_length);
  jpeg_start_compress(&dstinfo, TRUE);

  while (dstinfo.next_scanline < dstinfo.image_height) {
    num_scanlines = (*src_mgr->get_pixel_rows)(&dstinfo, src_mgr);
    jpeg_write_scanlines(&dstinfo, src_mgr->buffer, num_scanlines);
  }

  (*src_mgr->finish_input)(&dstinfo, src_mgr);
  jpeg_finish_compress(&dstinfo);

  /* early bailout because of animated or transparent gif? */
  if (bailout_now
      &&  DataLength(din) <= bailout_thresh[quality]) {
    result = distBadInput;
    goto JPGMUNGE_RETURN;
  }
    

  /* Now we're into the second pass.  Let's do our JPEG->JPEG
   * distillation. We need to compute the denominator and the quality
   * knob setting. */

  if (expert) {
    /* Do expert-like things here.  Need to work out still. */
    fin_qual = qual;
    fin_denom = compute_scale_denom(max_x, max_y, min_x, min_y,
				    srcinfo.image_width,
				    srcinfo.image_height, scale);
  } else {
    /* We're in beginner mode.  Life is easier. */
    if (ismap) {
      fin_qual = ismap_qual[quality-1];
      fin_denom = ismap_denom[quality-1];
    } else if (resize) {
      fin_qual = noscale_qual[quality-1];
      fin_denom = noscale_denom[quality-1];
    } else {
      fin_qual = norm_qual[quality-1];
      fin_denom = norm_denom[quality-1];
    }
  }

  /* Prep the input decompressor */
  jpeg_mem_src(&srcinfo, phase1_data, phase1_length);
  jpeg_read_header(&srcinfo, TRUE);

  srcinfo.scale_num = 1;
  srcinfo.scale_denom = fin_denom;
  srcinfo.dither_mode = JDITHER_ORDERED;  
  srcinfo.dct_method = JDCT_FASTEST;      
  jpeg_start_decompress(&srcinfo);

  /* Prep the output compressor */
  SetDataLength(dout,0);
  SetData(dout, NULL);
  sprintf(dout->mimeType, "image/jpeg");
  jpeg_mem_dest(&dstinfo, (void **) &(DataPtr(dout)),
                (UINT32 *) &(DataLength(dout)));
  dstinfo.image_width = srcinfo.output_width;
  dstinfo.image_height = srcinfo.output_height;
  dstinfo.input_components = srcinfo.output_components;
  dstinfo.in_color_space = srcinfo.out_color_space;
  jpeg_set_defaults(&dstinfo);
  jpeg_set_quality(&dstinfo, fin_qual, TRUE);
  jpeg_start_compress(&dstinfo, TRUE);

  handoff_height = (JDIMENSION) 1;
  handoff_width = 
    srcinfo.output_width*sizeof(JSAMPLE)*srcinfo.output_components;
  handoff = (JSAMPROW) malloc(handoff_width);

  /* We're going to need some buffer space to hand off data
     from the decompressor to the compressor. */
  while (srcinfo.output_scanline < srcinfo.output_height) {
    num_scanlines = jpeg_read_scanlines(&srcinfo, &handoff, handoff_height);
    jpeg_write_scanlines(&dstinfo, &handoff, num_scanlines);
  }

  jpeg_finish_decompress(&srcinfo);
  jpeg_finish_compress(&dstinfo);

  result = distOk;

JPGMUNGE_RETURN:
  if (handoff)
    free(handoff);
  DataNeedsFree(dout,gm_True);
  if (phase1_data)
    free(phase1_data);
  if (DataLength(dout) > DataLength(din)) {
    SetDataLength(dout, DataLength(din));
    memcpy(DataPtr(dout), DataPtr(din), DataLength(din));
    sprintf(dout->mimeType, "image/gif");
  }
  DEBUG("finished processing\n");
  return result;
}