Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00029 
00030 
00031 
00032 #include "PocketKnife.h"
00033 #include "ImageTool.h"
00034 
00035 #include <cstdio>
00036 #include <string>
00037 #include <cstring>
00038 
00039 #if defined(TARGET_HOST_WIN32) || defined(TARGET_HOST_WINCE)
00040 #include <windows.h>
00041 #endif
00042 
00043 namespace PN  {
00044 
00045 
00046 namespace ImageTool  {
00047 
00048 
00049 void convertPixelDataFrom16BitRGBTo24BitBGR(unsigned char* nDestData, const unsigned short* nPixelData,
00050                                                                                         unsigned int nWidth, unsigned int nHeight)
00051 {
00052         unsigned int    i, size = nWidth * nHeight;
00053 
00054         for(i=0; i<size; i++)
00055         {
00056                 convertPixel16To24(*nPixelData, nDestData[2], nDestData[1], nDestData[0]);
00057 
00058                 nDestData+=3;
00059                 nPixelData++;
00060         }
00061 }
00062 
00063 
00064 
00065 void flipImageY(unsigned char* nDstBuffer, unsigned const char* nSrcBuffer, int nWidth, int nHeight, int nPixelSize)
00066 {
00067         int i, span = nPixelSize*nWidth;
00068 
00069         for(i=0; i<nHeight; i++)
00070                 memcpy(nDstBuffer+i*span, nSrcBuffer+(nHeight-1-i)*span, span);
00071 }
00072 
00073 
00074 struct TGA_HEADER
00075 {
00076 #pragma pack( push, 1 )
00077         unsigned char  identsize;       
00078         unsigned char  colourmaptype;   
00079         unsigned char  imagetype;       
00080 
00081         short colourmapstart;                   
00082         short colourmaplength;                  
00083         unsigned char  colourmapbits;   
00084 
00085         short xstart;                                   
00086         short ystart;                                   
00087         short width;                                    
00088         short height;                                   
00089         unsigned char  bits;                    
00090         unsigned char  descriptor;              
00091 #pragma pack( pop )
00092 };
00093 
00094 
00095 bool
00096 saveAsTGA(Image* nImage, const char* nFileName)
00097 {
00098         TGA_HEADER      header;
00099         FILE*           fp = fopen(nFileName, "wb");
00100 
00101         if(!fp)
00102                 return false;
00103 
00104         memset(&header, 0, sizeof(TGA_HEADER));
00105 
00106         header.imagetype = 2;
00107         header.width = (short)nImage->getWidth();
00108         header.height = (short)nImage->getHeight();
00109         header.bits = 24;
00110         header.descriptor = 0;
00111 
00112 
00113         fwrite(&header, 1, sizeof(TGA_HEADER), fp);
00114 
00115         int size = nImage->getWidth()*nImage->getHeight()*3;
00116         unsigned char *tmpBuf = new unsigned char[size], *tmpBuf2 = new unsigned char[size];
00117 
00118         convertPixelDataFrom16BitRGBTo24BitBGR(tmpBuf, nImage->getPixels(), nImage->getWidth(), nImage->getHeight());
00119         flipImageY(tmpBuf2, tmpBuf, nImage->getWidth(), nImage->getHeight(), 3);
00120 
00121         fwrite(tmpBuf2, 1, size, fp);
00122         fclose(fp);
00123 
00124         delete tmpBuf;
00125         delete tmpBuf2;
00126 
00127         return true;
00128 }
00129 
00130 
00131 }; 
00132 
00133 
00134 };