home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / win32 / include / cximage / ximajpg.h < prev    next >
C/C++ Source or Header  |  2004-02-29  |  10KB  |  322 lines

  1. /*
  2.  * File:    ximajpg.h
  3.  * Purpose:    JPG Image Class Loader and Writer
  4.  */
  5. /* === C R E D I T S  &  D I S C L A I M E R S ==============
  6.  * CxImageJPG (c) 07/Aug/2001 Davide Pizzolato - www.xdp.it
  7.  * Permission is given by the author to freely redistribute and include
  8.  * this code in any program as long as this credit is given where due.
  9.  *
  10.  * CxImage version 5.99a 08/Feb/2004
  11.  * See the file history.htm for the complete bugfix and news report.
  12.  *
  13.  * Special thanks to Troels Knakkergaard for new features, enhancements and bugfixes
  14.  *
  15.  * Special thanks to Chris Shearer Cooper for CxFileJpg tips & code
  16.  *
  17.  * EXIF support based on jhead-1.8 by Matthias Wandel <mwandel(at)rim(dot)net>
  18.  *
  19.  * original CImageJPG  and CImageIterator implementation are:
  20.  * Copyright:    (c) 1995, Alejandro Aguilar Sierra <asierra(at)servidor(dot)unam(dot)mx>
  21.  *
  22.  * This software is based in part on the work of the Independent JPEG Group.
  23.  * Copyright (C) 1991-1998, Thomas G. Lane.
  24.  *
  25.  * COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
  26.  * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
  27.  * THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
  28.  * OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
  29.  * CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
  30.  * THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
  31.  * SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
  32.  * PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  33.  * THIS DISCLAIMER.
  34.  *
  35.  * Use at your own risk!
  36.  * ==========================================================
  37.  */
  38. #if !defined(__ximaJPEG_h)
  39. #define __ixmaJPEG_h
  40.  
  41. #include "ximage.h"
  42.  
  43. #if CXIMAGE_SUPPORT_JPG
  44.  
  45. #define CXIMAGEJPG_SUPPORT_EXIF 1
  46.  
  47. extern "C" {
  48.  #include "../jpeg/jpeglib.h"
  49.  #include "../jpeg/jerror.h"
  50. }
  51.  
  52. class DLL_EXP CxImageJPG: public CxImage
  53. {
  54. public:
  55.     CxImageJPG();
  56.     ~CxImageJPG() {};
  57.  
  58. //    bool Load(const char * imageFileName){ return CxImage::Load(imageFileName,CXIMAGE_FORMAT_JPG);}
  59. //    bool Save(const char * imageFileName){ return CxImage::Save(imageFileName,CXIMAGE_FORMAT_JPG);}
  60.     bool Decode(CxFile * hFile);
  61.     bool Decode(FILE *hFile) { CxIOFile file(hFile); return Decode(&file); }
  62.  
  63. #if CXIMAGE_SUPPORT_ENCODE
  64.     bool Encode(CxFile * hFile);
  65.     bool Encode(FILE *hFile) { CxIOFile file(hFile); return Encode(&file); }
  66. #endif // CXIMAGE_SUPPORT_ENCODE
  67.  
  68. /*
  69.  * EXIF support based on jhead-1.8 by Matthias Wandel <mwandel(at)rim(dot)net>
  70.  */
  71.  
  72. #if CXIMAGEJPG_SUPPORT_EXIF
  73.  
  74. #define MAX_COMMENT 1000
  75. #define MAX_SECTIONS 20
  76.  
  77. typedef struct tag_ExifInfo {
  78.     char  Version      [5];
  79.     char  CameraMake   [32];
  80.     char  CameraModel  [40];
  81.     char  DateTime     [20];
  82.     int   Height, Width;
  83.     int   Orientation;
  84.     int   IsColor;
  85.     int   Process;
  86.     int   FlashUsed;
  87.     float FocalLength;
  88.     float ExposureTime;
  89.     float ApertureFNumber;
  90.     float Distance;
  91.     float CCDWidth;
  92.     float ExposureBias;
  93.     int   Whitebalance;
  94.     int   MeteringMode;
  95.     int   ExposureProgram;
  96.     int   ISOequivalent;
  97.     int   CompressionLevel;
  98.     float FocalplaneXRes;
  99.     float FocalplaneYRes;
  100.     float FocalplaneUnits;
  101.     float Xresolution;
  102.     float Yresolution;
  103.     float ResolutionUnit;
  104.     float Brightness;
  105.     char  Comments[MAX_COMMENT];
  106.  
  107.     unsigned char * ThumbnailPointer;  /* Pointer at the thumbnail */
  108.     unsigned ThumbnailSize;     /* Size of thumbnail. */
  109.  
  110.     bool  IsExif;
  111. } EXIFINFO;
  112.  
  113. //--------------------------------------------------------------------------
  114. // JPEG markers consist of one or more 0xFF bytes, followed by a marker
  115. // code byte (which is not an FF).  Here are the marker codes of interest
  116. // in this program.  (See jdmarker.c for a more complete list.)
  117. //--------------------------------------------------------------------------
  118.  
  119. #define M_SOF0  0xC0            // Start Of Frame N
  120. #define M_SOF1  0xC1            // N indicates which compression process
  121. #define M_SOF2  0xC2            // Only SOF0-SOF2 are now in common use
  122. #define M_SOF3  0xC3
  123. #define M_SOF5  0xC5            // NB: codes C4 and CC are NOT SOF markers
  124. #define M_SOF6  0xC6
  125. #define M_SOF7  0xC7
  126. #define M_SOF9  0xC9
  127. #define M_SOF10 0xCA
  128. #define M_SOF11 0xCB
  129. #define M_SOF13 0xCD
  130. #define M_SOF14 0xCE
  131. #define M_SOF15 0xCF
  132. #define M_SOI   0xD8            // Start Of Image (beginning of datastream)
  133. #define M_EOI   0xD9            // End Of Image (end of datastream)
  134. #define M_SOS   0xDA            // Start Of Scan (begins compressed data)
  135. #define M_JFIF  0xE0            // Jfif marker
  136. #define M_EXIF  0xE1            // Exif marker
  137. #define M_COM   0xFE            // COMment 
  138.  
  139. class CxExifInfo
  140. {
  141.  
  142. typedef struct tag_Section_t{
  143.     BYTE*    Data;
  144.     int      Type;
  145.     unsigned Size;
  146. } Section_t;
  147.  
  148. public:
  149.     EXIFINFO* m_exifinfo;
  150.     char m_szLastError[256];
  151.     CxExifInfo(EXIFINFO* info = NULL);
  152.     ~CxExifInfo();
  153.     bool DecodeExif(CxFile * hFile);
  154. protected:
  155.     bool process_EXIF(unsigned char * CharBuf, unsigned int length);
  156.     void process_COM (const BYTE * Data, int length);
  157.     void process_SOFn (const BYTE * Data, int marker);
  158.     int Get16u(void * Short);
  159.     int Get16m(void * Short);
  160.     long Get32s(void * Long);
  161.     unsigned long Get32u(void * Long);
  162.     double ConvertAnyFormat(void * ValuePtr, int Format);
  163.     bool ProcessExifDir(unsigned char * DirStart, unsigned char * OffsetBase, unsigned ExifLength,
  164.                            EXIFINFO * const pInfo, unsigned char ** const LastExifRefdP);
  165.     int ExifImageWidth;
  166.     int MotorolaOrder;
  167.     Section_t Sections[MAX_SECTIONS];
  168.     int SectionsRead;
  169.     bool freeinfo;
  170. };
  171.  
  172.     EXIFINFO m_exifinfo;
  173.     bool DecodeExif(CxFile * hFile);
  174.     bool DecodeExif(FILE * hFile) { CxIOFile file(hFile); return DecodeExif(&file); }
  175.  
  176. #endif //CXIMAGEJPG_SUPPORT_EXIF
  177.  
  178. ////////////////////////////////////////////////////////////////////////////////////////
  179. //////////////////////        C x F i l e J p g         ////////////////////////////////
  180. ////////////////////////////////////////////////////////////////////////////////////////
  181.  
  182. // thanks to Chris Shearer Cooper <cscooper(at)frii(dot)com>
  183. class CxFileJpg : public jpeg_destination_mgr, public jpeg_source_mgr
  184.     {
  185. public:
  186.     enum { eBufSize = 4096 };
  187.  
  188.     CxFileJpg(CxFile* pFile)
  189.     {
  190.         m_pFile = pFile;
  191.  
  192.         init_destination = InitDestination;
  193.         empty_output_buffer = EmptyOutputBuffer;
  194.         term_destination = TermDestination;
  195.  
  196.         init_source = InitSource;
  197.         fill_input_buffer = FillInputBuffer;
  198.         skip_input_data = SkipInputData;
  199.         resync_to_restart = jpeg_resync_to_restart; // use default method
  200.         term_source = TermSource;
  201.         next_input_byte = NULL; //* => next byte to read from buffer 
  202.         bytes_in_buffer = 0;    //* # of bytes remaining in buffer 
  203.  
  204.         m_pBuffer = new unsigned char[eBufSize];
  205.     }
  206.     ~CxFileJpg()
  207.     {
  208.         delete [] m_pBuffer;
  209.     }
  210.  
  211.     static void InitDestination(j_compress_ptr cinfo)
  212.     {
  213.         CxFileJpg* pDest = (CxFileJpg*)cinfo->dest;
  214.         pDest->next_output_byte = pDest->m_pBuffer;
  215.         pDest->free_in_buffer = eBufSize;
  216.     }
  217.  
  218.     static boolean EmptyOutputBuffer(j_compress_ptr cinfo)
  219.     {
  220.         CxFileJpg* pDest = (CxFileJpg*)cinfo->dest;
  221.         if (pDest->m_pFile->Write(pDest->m_pBuffer,1,eBufSize)!=(size_t)eBufSize)
  222.             ERREXIT(cinfo, JERR_FILE_WRITE);
  223.         pDest->next_output_byte = pDest->m_pBuffer;
  224.         pDest->free_in_buffer = eBufSize;
  225.         return TRUE;
  226.     }
  227.  
  228.     static void TermDestination(j_compress_ptr cinfo)
  229.     {
  230.         CxFileJpg* pDest = (CxFileJpg*)cinfo->dest;
  231.         size_t datacount = eBufSize - pDest->free_in_buffer;
  232.         /* Write any data remaining in the buffer */
  233.         if (datacount > 0) {
  234.             if (!pDest->m_pFile->Write(pDest->m_pBuffer,1,datacount))
  235.                 ERREXIT(cinfo, JERR_FILE_WRITE);
  236.         }
  237.         pDest->m_pFile->Flush();
  238.         /* Make sure we wrote the output file OK */
  239.         if (pDest->m_pFile->Error()) ERREXIT(cinfo, JERR_FILE_WRITE);
  240.         return;
  241.     }
  242.  
  243.     static void InitSource(j_decompress_ptr cinfo)
  244.     {
  245.         CxFileJpg* pSource = (CxFileJpg*)cinfo->src;
  246.         pSource->m_bStartOfFile = TRUE;
  247.     }
  248.  
  249.     static boolean FillInputBuffer(j_decompress_ptr cinfo)
  250.     {
  251.         size_t nbytes;
  252.         CxFileJpg* pSource = (CxFileJpg*)cinfo->src;
  253.         nbytes = pSource->m_pFile->Read(pSource->m_pBuffer,1,eBufSize);
  254.         if (nbytes <= 0){
  255.             if (pSource->m_bStartOfFile)    //* Treat empty input file as fatal error 
  256.                 ERREXIT(cinfo, JERR_INPUT_EMPTY);
  257.             WARNMS(cinfo, JWRN_JPEG_EOF);
  258.             // Insert a fake EOI marker 
  259.             pSource->m_pBuffer[0] = (JOCTET) 0xFF;
  260.             pSource->m_pBuffer[1] = (JOCTET) JPEG_EOI;
  261.             nbytes = 2;
  262.         }
  263.         pSource->next_input_byte = pSource->m_pBuffer;
  264.         pSource->bytes_in_buffer = nbytes;
  265.         pSource->m_bStartOfFile = FALSE;
  266.         return TRUE;
  267.     }
  268.  
  269.     static void SkipInputData(j_decompress_ptr cinfo, long num_bytes)
  270.     {
  271.         CxFileJpg* pSource = (CxFileJpg*)cinfo->src;
  272.         if (num_bytes > 0){
  273.             while (num_bytes > (long)pSource->bytes_in_buffer){
  274.                 num_bytes -= (long)pSource->bytes_in_buffer;
  275.                 FillInputBuffer(cinfo);
  276.                 // note we assume that fill_input_buffer will never return FALSE,
  277.                 // so suspension need not be handled.
  278.             }
  279.             pSource->next_input_byte += (size_t) num_bytes;
  280.             pSource->bytes_in_buffer -= (size_t) num_bytes;
  281.         }
  282.     }
  283.  
  284.     static void TermSource(j_decompress_ptr cinfo)
  285.     {
  286.         return;
  287.     }
  288. protected:
  289.     CxFile  *m_pFile;
  290.     unsigned char *m_pBuffer;
  291.     bool m_bStartOfFile;
  292. };
  293.  
  294. public:
  295.     enum CODEC_OPTION
  296.     {
  297.         ENCODE_BASELINE = 0x1,
  298.         ENCODE_ARITHMETIC = 0x2,
  299.         ENCODE_GRAYSCALE = 0x4,
  300.         ENCODE_OPTIMIZE = 0x8,
  301.         ENCODE_PROGRESSIVE = 0x10,
  302.         ENCODE_LOSSLESS = 0x20,
  303.         ENCODE_SMOOTHING = 0x40,
  304.         DECODE_GRAYSCALE = 0x80,
  305.         DECODE_QUANTIZE = 0x100,
  306.         DECODE_DITHER = 0x200,
  307.         DECODE_ONEPASS = 0x400,
  308.         DECODE_NOSMOOTH = 0x800
  309.     }; 
  310.  
  311.     int m_nPredictor;
  312.     int m_nPointTransform;
  313.     int m_nSmoothing;
  314.     int m_nQuantize;
  315.     J_DITHER_MODE m_nDither;
  316.  
  317. };
  318.  
  319. #endif
  320.  
  321. #endif
  322.