home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2008 January / Mac_easy_01_08.iso / Software / Online / Chat / macam.0.9.1.dmg / macam source / utilities / JpgDecompress.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-06  |  6.1 KB  |  273 lines

  1. /*
  2.  *  JpgDecompress.c
  3.  *  macam
  4.  *
  5.  *  Created by Vincenzo Mantova on 15/05/06.
  6.  *  Copyright 2006 __MyCompanyName__. All rights reserved.
  7.  *
  8.  */
  9.  
  10.  
  11. #include "JpgDecompress.h"
  12.  
  13.  
  14. // JPEG decompression using spca5xx code
  15.  
  16. #if 1
  17.  
  18.  
  19. typedef unsigned int uint;
  20.  
  21. #include "spcadecoder.h"
  22.  
  23.  
  24. int JpgDecompress(unsigned char * pIn, unsigned char * pOut, int size, int width, int height)
  25. {
  26.     int i;
  27.     struct usb_spca50x  spca50x;
  28.     struct spca50x_frame  myframe;
  29.     
  30.     spca50x.qindex = 5;
  31.     
  32.     init_jpeg_decoder(&spca50x);
  33.     
  34.     /*  I think these get set in the jpeg-decoding routine
  35.         spca50x->frame->dcts ?
  36.         spca50x->frame->out ?
  37.         spca50x->frame->max ?
  38.     */
  39.     
  40.     myframe.hdrwidth = width;
  41.     myframe.hdrheight = height;
  42.     myframe.width = width;
  43.     myframe.height = height;
  44.     
  45.     // before jpeg_decode422() is called:
  46.     //   copy data to tmpbuffer, possibly skippin some header info
  47.     //   scanlength is the length of data
  48.     
  49.     // when jpeg_decode422() is called:
  50.     //   frame.data - points to output buffer
  51.     //   frame.scanlength -length of data (tmpbuffer on input, data on output)
  52.     //   frame.tmpbuffer - points to input buffer
  53.     
  54.     myframe.data = pOut;  // output
  55.     myframe.tmpbuffer = pIn;  // definitely input data
  56.     myframe.scanlength = size;  // current length of data
  57.     
  58.     myframe.decoder = &spca50x.maindecode;  // has the code table, are red, green, blue set up?
  59.     
  60.     for (i = 0; i < 256; i++) 
  61.     {
  62.         myframe.decoder->Red[i] = i;
  63.         myframe.decoder->Green[i] = i;
  64.         myframe.decoder->Blue[i] = i;
  65.     }
  66.     
  67.     myframe.cameratype = JPEG;
  68.     
  69.     myframe.format = VIDEO_PALETTE_RGB24;
  70.     
  71.     myframe.cropx1 = 0;
  72.     myframe.cropx2 = 0;
  73.     myframe.cropy1 = 0;
  74.     myframe.cropy2 = 0;
  75.     
  76.     myframe.decoder->info.dri = 0;
  77.     
  78.     jpeg_decode422(&myframe, 1);
  79.     
  80.     return 1; 
  81. }
  82.  
  83. #endif
  84.  
  85.  
  86. // JPEG decompression using libjpeg
  87.  
  88. #if 0
  89.  
  90. #include "setjmp.h"
  91. #include <sys/types.h>
  92. #include <stdio.h>
  93.  
  94. #include "libjpeg-6b/jpeglib.h"
  95.  
  96. jmp_buf BadErrorJmp;
  97.  
  98. unsigned char * InputData;
  99. unsigned int SizeData;
  100.  
  101. void ErrorHandlerErrorExit(j_decompress_ptr cinfo)
  102. {
  103.     // cinfo->err->output_message(cinfo);
  104.     longjmp(BadErrorJmp, -1);
  105. }
  106.  
  107. void DataSourceInitSource (j_decompress_ptr cinfo)
  108. {
  109.     cinfo->src->next_input_byte = InputData;
  110.     cinfo->src->bytes_in_buffer = SizeData;
  111.     return;
  112. }
  113.  
  114. int DataSourceFillInputBuffer(j_decompress_ptr cinfo)
  115. {
  116.     return 1;
  117. }
  118.  
  119. void DataSourceSkipInputData(j_decompress_ptr cinfo, long num_bytes)
  120. {
  121.     return;
  122. }
  123.  
  124. void DataSourceTermSource(j_decompress_ptr cinfo)
  125. {
  126.     return;
  127. }
  128.  
  129. int JpgDecompress(unsigned char * pIn, unsigned char * pOut, int size, int width, int height)
  130. {
  131.     struct jpeg_decompress_struct cinfo;
  132.     struct jpeg_error_mgr jerr;
  133.     struct jpeg_source_mgr src;
  134.     JSAMPROW row;
  135.     int i = 0;
  136.     int scanlines = 1;
  137.  
  138.     if (size < 0) return -1;
  139.  
  140.     InputData = pIn;
  141.     SizeData = size;
  142.  
  143.     src.init_source = DataSourceInitSource;
  144.     src.fill_input_buffer = DataSourceFillInputBuffer;
  145.     src.skip_input_data = DataSourceSkipInputData;
  146.     src.resync_to_restart = jpeg_resync_to_restart;
  147.     src.term_source = DataSourceTermSource;
  148.     cinfo.err = jpeg_std_error(&jerr);
  149.     jerr.error_exit = ErrorHandlerErrorExit;
  150.  
  151.     jpeg_create_decompress(&cinfo);
  152.     
  153.     cinfo.src = &src;
  154.     
  155.     jpeg_read_header(&cinfo, TRUE);
  156.     if (cinfo.image_width != width && cinfo.image_height != height) return 0;
  157.     
  158.     jpeg_start_decompress(&cinfo);
  159.     
  160.     while (i < cinfo.image_height && scanlines > 0) {
  161.         row = pOut + cinfo.image_width * 3 * i;
  162.         i+= (scanlines=jpeg_read_scanlines(&cinfo,&row,1));
  163.     }
  164.     
  165.     if (i < cinfo.image_height) printf("Premature end.\n");
  166.  
  167.     if (setjmp(BadErrorJmp)) {
  168.         jpeg_abort_decompress(&cinfo);
  169.         jpeg_destroy_decompress(&cinfo);
  170.         return 0;
  171.     }
  172.  
  173.     jpeg_finish_decompress(&cinfo);
  174.  
  175.     jpeg_destroy_decompress(&cinfo);
  176.     
  177.     return 1;
  178. }
  179.  
  180. #endif
  181.  
  182.  
  183. // some ideas for other JPEG decompression (Cocoa)
  184.  
  185.  
  186. /*
  187.  
  188.  {
  189.     CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, size, MyReleaseProc);
  190.     
  191.     CGImageRef image = CGImageCreateWithJPEGDataProvider(provider, NULL, true, kCGRenderingIntentDefault);
  192.     
  193.     if (provider != NULL) 
  194.          CFRelease(provider);
  195.  }
  196.  
  197.  void MyReleaseProc(void * info, const void * data, size_t size)
  198.  {
  199.      if (info != NULL) 
  200.      {
  201.          // release private information here 
  202.      }
  203.      
  204.      if (data != NULL) 
  205.      {
  206.          // release picture data here 
  207.      }
  208.  }
  209.  
  210.  
  211.  
  212.  use CGImageCreateWithJPEGDataProvider
  213.  
  214.  use CGDataProviderCreateWithData
  215.  
  216.  
  217.  
  218.  void MyCreateAndDrawBitmapImage (CGContextRef myContext, // 1
  219.                                   CGRect myContextRect, 
  220.                                   const char *filename);
  221.  {
  222.      CGImageRef image;
  223.      CGDataProviderRef provider;
  224.      CFStringRef path;
  225.      CFURLRef url;
  226.      
  227.      path = CFStringCreateWithCString (NULL, filename, 
  228.                                        kCFStringEncodingUTF8); 
  229.      url = CFURLCreateWithFileSystemPath (NULL, path, // 2
  230.                                           kCFURLPOSIXPathStyle, NULL);
  231.      CFRelease(path);    
  232.      provider = CGDataProviderCreateWithURL (url);// 3
  233.          CFRelease (url);
  234.          image = CGImageCreateWithJPEGDataProvider (provider,// 4
  235.                                                     NULL,
  236.                                                     true,
  237.                                                     kCGRenderingIntentDefault);
  238.          CGDataProviderRelease (provider);// 5
  239.              CGContextDrawImage (myContext, myContextRect, image);// 6
  240.                  CGImageRelease (image);// 7
  241.  }
  242.  
  243.  
  244.  QDPictRef MyCreateQDPictWithData (void *data, size_t size)
  245.  {
  246.      QDPictRef picture = NULL;
  247.      
  248.      CGDataProviderRef provider = 
  249.          CGDataProviderCreateWithData (NULL, data, size, MyReleaseProc);// 1
  250.          
  251.          if (provider != NULL)
  252.          {
  253.              picture = QDPictCreateWithProvider (provider);// 2
  254.              CFRelease (provider);
  255.          }
  256.          
  257.          return picture;
  258.  }
  259.  
  260.  void MyReleaseProc(void * info, const void * data, size_t size)
  261.  {
  262.      if (info != NULL) { 
  263.          // release private information here 
  264.      }
  265.      
  266.      if (data != NULL) {
  267.          // release picture data here 
  268.      }
  269.  }
  270.  
  271.  */
  272.  
  273.