00001 
00002 #ifndef  __JBXL_CPP_JPEG_TOOl_H_
00003 #define  __JBXL_CPP_JPEG_TOOl_H_
00004 
00017 #include "Gdata.h"
00018 #include "xtools.h"
00019 
00020 
00021 #ifndef HAVE_JPEGLIB_H
00022 #ifndef DISABLE_JPEGLIB
00023 #define DISABLE_JPEGLIB
00024 #endif
00025 #endif
00026 
00027 #ifdef DISABLE_JPEGLIB
00028 #undef ENABLE_JPEGLIB
00029 #endif
00030 
00031 
00032 #ifdef ENABLE_JPEGLIB
00033 
00034 #undef HAVE_STDLIB_H
00035 #include <jpeglib.h>
00036 
00037 #ifdef WIN32
00038 
00039 #pragma  comment(lib, "jpeg.lib")
00040 #endif
00041 
00042 
00043 
00044 namespace jbxl {
00045 
00047 
00048 class JPEGImage 
00049 {
00050 public:
00051     int     xs;
00052     int     ys;
00053     int     col;
00054     int     state;
00055 
00056     JSAMPLE*    gp;
00057     JSAMPARRAY  image;
00058 
00059 public:
00060     JPEGImage(void)  { init();}
00061     virtual ~JPEGImage(void) {}
00062 
00063     void    init(void);                                     
00064     bool    isNull(void);                                   
00065     void    clear(void);                                    
00066     void    fill(JSAMPLE v=(JSAMPLE)0);                     
00067     void    free(void);                                     
00068 
00069     JSAMPLE& point(int x, int y, int c) { return image[y][x*col + c];}
00070     void    getm(int x, int y, int c);
00071     void    set (int x, int y, int c);
00072 };
00073 
00074 
00076 
00077 JPEGImage   readJPEGFile (const char* fname);
00078 JPEGImage   readJPEGData (FILE* fp);
00079 int         writeJPEGFile(const char* fname, JPEGImage jp, int qulty);
00080 int         writeJPEGData(FILE* fp, JPEGImage jp, int qulty);
00081 
00082 int         isJPEGHeader(Buffer buf);
00083 
00084 CmnHead     JPEGImage2CmnHead(JPEGImage jp);
00085 JPEGImage   CmnHead2JPEGImage(CmnHead hd);
00086 
00087 
00088 
00089 
00090 
00091 
00102 template <typename T>  MSGraph<T> JPEGImage2MSGraph(JPEGImage jp)
00103 {
00104     MSGraph<T> vp;
00105 
00106     if (jp.isNull()) {
00107         vp.state = JBXL_GRAPH_NODATA_ERROR;
00108         return vp;
00109     }
00110 
00111     vp.set(jp.xs, jp.ys, jp.col);
00112     if (vp.isNull()) return vp;
00113     
00114     if (jp.col==3) vp.color = GRAPH_COLOR_RGB;
00115 
00116     for (int k=0; k<jp.col; k++) {
00117         int zp = k*jp.xs*jp.ys;
00118         for (int j=0; j<jp.ys; j++) {
00119             int yp = zp + j*jp.xs;
00120             for (int i=0; i<jp.xs; i++) {
00121                 vp.gp[yp + i] = (T)jp.point(i, j, k);
00122             }
00123         }
00124     }
00125 
00126     return vp;
00127 }
00128 
00129 
00140 template <typename T>  JPEGImage  MSGraph2JPEGImage(MSGraph<T> vp)
00141 {
00142     JPEGImage jp;
00143 
00144     if (vp.isNull()) {
00145         jp.state = JBXL_GRAPH_NODATA_ERROR;
00146         return jp;
00147     }
00148 
00149     jp.set(vp.xs, vp.ys, vp.zs);
00150     if (jp.isNull()) return jp;
00151 
00152     for (int k=0; k<jp.col; k++) {
00153         int zp = k*jp.xs*jp.ys;
00154         for (int j=0; j<jp.ys; j++) {
00155             int yp = zp + j*jp.xs;
00156             for (int i=0; i<jp.xs; i++) {
00157                 jp.point(i, j, k) = (JSAMPLE)vp.gp[yp + i];
00158             }
00159         }
00160     }
00161 
00162     return jp;
00163 }
00164 
00165 
00166 }       
00167 
00168 
00169 
00170 #endif  // ENABLE_JPEGLIB
00171 
00172 #endif  // __JBXL_CPP_JPEG_TOOl_H_
00173 
00174 
00175