Esempio n. 1
0
int
decodeWEBP(Ychannel *channel, Vbitmap *vbitmap,
           YmagineFormatOptions *options)
{
  int nlines = -1;
#if HAVE_WEBP
  WEBPDec  webp;
#endif
  
  if (!YchannelReadable(channel)) {
#if YMAGINE_DEBUG_WEBP
    ALOGD("input channel not readable");
#endif
    return nlines;
  }

#if HAVE_WEBP
  if (WEBPInit(&webp, channel, vbitmap) == YMAGINE_OK) {
    nlines = WEBPDecode(&webp, vbitmap, options);
    WEBPFini(&webp);
  }
#endif

  return nlines;
}
Esempio n. 2
0
int
decodeJPEG(Ychannel *channel, Vbitmap *vbitmap,
           YmagineFormatOptions *options)
{
  struct jpeg_decompress_struct cinfo;
  struct noop_error_mgr jerr;
  int nlines = -1;
  
  if (!YchannelReadable(channel)) {
#if YMAGINE_DEBUG_JPEG
    ALOGD("input channel not readable");
#endif
    return nlines;
  }
  
  memset(&cinfo, 0, sizeof(struct jpeg_decompress_struct));
  cinfo.err = noop_jpeg_std_error(&jerr.pub);
  
  /* Establish the setjmp return context for noop_error_exit to use. */
  if (setjmp(jerr.setjmp_buffer)) {
    /* If we get here, the JPEG code has signaled an error. */
    noop_append_jpeg_message((j_common_ptr) &cinfo);
  } else {
    jpeg_create_decompress(&cinfo);
    if (ymaginejpeg_input(&cinfo, channel) >= 0) {
      nlines = bitmap_decode(&cinfo, vbitmap,
                             options);
    }
  }
  jpeg_destroy_decompress(&cinfo);
  
  return nlines;
}
Esempio n. 3
0
int
matchJPEG(Ychannel *channel)
{
  unsigned char header[8];
  int hlen;

  if (!YchannelReadable(channel)) {
    return YFALSE;
  }

  hlen = YchannelRead(channel, header, sizeof(header));
  if (hlen > 0) {
    YchannelPush(channel, (const char*) header, hlen);
  }

  if (hlen < 3) {
    return YFALSE;
  }

  if ( (header[0] != 0xff) || (header[1] != 0xd8) || (header[2] != 0xff) ) {
    return YFALSE;
  }

  return YTRUE;
}
Esempio n. 4
0
int
matchWEBP(Ychannel *channel)
{
#if HAVE_WEBP
  char header[WEBP_HEADER_SIZE];
  int hlen;

  if (!YchannelReadable(channel)) {
    return YFALSE;
  }

  hlen = YchannelRead(channel, header, sizeof(header));
  if (hlen > 0) {
    YchannelPush(channel, header, hlen);
  }

  if (WebpCheckHeader(header, hlen) > 0) {
    return YTRUE;
  }
#endif

  return YFALSE;
}
Esempio n. 5
0
int
transcodeJPEG(Ychannel *channelin, Ychannel *channelout,
              YmagineFormatOptions *options)
{
  struct jpeg_decompress_struct cinfo;
  struct noop_error_mgr jerr;
  
  struct jpeg_compress_struct cinfoout;
  struct noop_error_mgr jerr2;
  int rc = YMAGINE_ERROR;
  int nlines = 0;
  int quality = YmagineFormatOptions_normalizeQuality(options);
  
  if (!YchannelReadable(channelin) || !YchannelWritable(channelout)) {
    return rc;
  }
  
  memset(&cinfo, 0, sizeof(struct jpeg_decompress_struct));
  cinfo.err = noop_jpeg_std_error(&jerr.pub);
  
  memset(&cinfoout, 0, sizeof(struct jpeg_compress_struct));
  cinfoout.err = noop_jpeg_std_error(&jerr2.pub);
  
  if (setjmp(jerr.setjmp_buffer)) {
    /* If we get here, the JPEG code has signaled an error in decoder */
    noop_append_jpeg_message((j_common_ptr) &cinfo);
  } else if (setjmp(jerr2.setjmp_buffer)) {
    /* If we get here, the JPEG code has signaled an error in encoder */
    noop_append_jpeg_message((j_common_ptr) &cinfoout);
  } else {
    jpeg_create_decompress(&cinfo);
    
    /* Equivalent to:
	   jpeg_CreateCompress(&cinfoout, JPEG_LIB_VERSION,
	   (size_t) sizeof(struct jpeg_compress_struct));
     */
    jpeg_create_compress(&cinfoout);

    if (ymaginejpeg_input(&cinfo, channelin) >= 0 &&
        ymaginejpeg_output(&cinfoout, channelout) >= 0) {
      if (prepareDecompressor(&cinfo, options) == YMAGINE_OK) {
        /* markers copy option (NONE, COMMENTS or ALL) */
        JCOPY_OPTION copyoption;
        int metamode = YMAGINE_METAMODE_DEFAULT;

        if (options != NULL) {
          metamode = options->metamode;
        }
        if (metamode == YMAGINE_METAMODE_NONE) {
          copyoption = JCOPYOPT_NONE;
        } else if (metamode == YMAGINE_METAMODE_COMMENTS) {
          copyoption = JCOPYOPT_COMMENTS;
        } else if (metamode == YMAGINE_METAMODE_ALL) {
          copyoption = JCOPYOPT_ALL;
        } else {
          /* Default to copy all markers */
          copyoption = JCOPYOPT_ALL;
        }

        /* Enable saving of extra markers that we want to copy */
        if (copyoption != JCOPYOPT_NONE) {
          jcopy_markers_setup(&cinfo, copyoption);
        }
        
        /* Force image to be decoded without colorspace conversion if possible */
        if (jpeg_read_header(&cinfo, TRUE) == JPEG_HEADER_OK) {
          YmagineFormatOptions_invokeCallback(options, YMAGINE_IMAGEFORMAT_JPEG,
                                              cinfo.image_width, cinfo.image_height);

          /* Other compression settings */
          int optimize = 0;
          int progressive = 0;
          int grayscale = 0;
          
          if (quality >= 90) {
            optimize = 1;
          }

          if (options != NULL && options->pixelshader != NULL) {
            /* If a shader is enabled, force RGBA mode */
            cinfo.out_color_space = JCS_EXT_RGBA;
            cinfoout.in_color_space = cinfo.out_color_space;
            cinfoout.input_components = 4;
          } else if (options != NULL && options->sharpen > 0.0f) {
            cinfo.out_color_space = JCS_RGB;
            cinfoout.in_color_space = cinfo.out_color_space;
            cinfoout.input_components = 3;
          } else {
            /* Otherwise sse same colorspace than input image (typically YCbCr) */
            cinfo.out_color_space = cinfo.jpeg_color_space;
            cinfoout.in_color_space = cinfo.out_color_space;
            cinfoout.input_components = JpegPixelSize(cinfoout.in_color_space);
          }

          jpeg_set_defaults(&cinfoout);
          cinfoout.optimize_coding = FALSE;
          
          jpeg_set_quality(&cinfoout, quality, FALSE);
          if (grayscale) {
            /* Force a monochrome JPEG file to be generated. */
            jpeg_set_colorspace(&cinfoout, JCS_GRAYSCALE);
          }
          if (optimize) {
            /* Enable entropy parm optimization. */
            cinfoout.optimize_coding = TRUE;
          }

          /* If non-zero, the input image is smoothed; the value should
             be 1 for minimal smoothing to 100 for maximum smoothing. */
          cinfoout.smoothing_factor = 0;

          if (progressive) {
            /* Select simple progressive mode. */
            jpeg_simple_progression(&cinfoout);
          }
          setCompressorOptions(&cinfoout, options);

          cinfo.client_data = (void*) NULL;
          cinfoout.client_data = (void*) NULL;

          nlines = decompress_jpeg(&cinfo, &cinfoout, copyoption, NULL, options);
          if (nlines > 0) {
            rc = YMAGINE_OK;
          }
        }
      }
    }
  }
  
  jpeg_destroy_compress(&cinfoout);
  jpeg_destroy_decompress(&cinfo);
  
  return rc;
}