int parse (size_t size, unsigned char *data) {
  int writtendata = 0;
  MagickWand *input = NewMagickWand();
  MagickWand *output = input;

  convert_t *opts = (convert_t*) data;
  convert_info_t info_data;

  size -= sizeof(convert_t);
  data += sizeof(convert_t);

  int status = convert(input, &output, opts, data, size);

  if (status < 0) {
    destroy(input, output);
    return status;
  } 

  if (opts->format == INFO) {
    to_convert_info(output, &info_data);
    data = (unsigned char*) &info_data;
    size = sizeof(convert_info_t);
    writtendata = io_write(size, data);
  } else {
    if (opts->split) {
      // If we split the file then we will make a tarball containing all the pages
      char* filename;
      filename = tmpnam (NULL);

      size = write_archive_from_mem(filename, output);
      writtendata = io_write_file_to_stdout(filename);
      unlink(filename);
    } else {
 
    char *format = MagickGetImageFormat(output);   

      if(strcmp(format,"PNG") == 0 && opts->quality != 0){
        struct bufferAndSize d = pngQuant(output);
        
        writtendata = io_write(d.size, d.data);
        free(d.data);
        free(format);

      }else{

        data = MagickWriteImageBlob(output, &size);
        writtendata = io_write(size, data);
        free(data);
      }

      destroy(input, output);
    }
  }
  return writtendata;
}
Ejemplo n.º 2
0
int parse (size_t size, unsigned char *data) {
  int writtendata = 0;
  MagickWand *input = NewMagickWand();
  MagickWand *output = input;

  convert_t *opts = (convert_t*) data;
  convert_info_t info_data;

  size -= sizeof(convert_t);
  data += sizeof(convert_t);

  int status = convert(input, &output, opts, data, size);

  if (status < 0) {
    destroy(input, output);
    return status;
  } 

  if (opts->format == INFO) {
    to_convert_info(output, &info_data);
    data = (unsigned char*) &info_data;
    size = sizeof(convert_info_t);
    writtendata = io_write(size, data);
  } else {
    if (opts->split) {
      // If we split the file then we will make a tarball containing all the pages
      char* filename;
      filename = "/tmp/cmguiXXXXXX";
      size = write_archive_from_mem(filename, output);
      writtendata = io_write_file_to_stdout(filename);
      unlink(filename);
    } else {
      data = MagickWriteImageBlob(output, &size);
      writtendata = io_write(size, data);
    }
  }
  //destroy(input, output);
  return writtendata;
}