ImageTool.cpp
Go to the documentation of this file.
00001 /* ========================================================================
00002  * Copyright (C) 2004-2005  Graz University of Technology
00003  *
00004  * This framework is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This framework is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this framework; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  * For further information please contact Dieter Schmalstieg under
00019  * <schmalstieg@icg.tu-graz.ac.at> or write to Dieter Schmalstieg,
00020  * Graz University of Technology, Institut für Maschinelles Sehen und Darstellen,
00021  * Inffeldgasse 16a, 8010 Graz, Austria.
00022  * ========================================================================
00023  * PROJECT: PocketKnife
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;       // size of ID field that follows 18 unsigned char header (0 usually)
00078         unsigned char  colourmaptype;   // type of colour map 0=none, 1=has palette
00079         unsigned char  imagetype;       // type of image 0=none,1=indexed,2=rgb,3=grey,+8=rle packed
00080 
00081         short colourmapstart;                   // first colour map entry in palette
00082         short colourmaplength;                  // number of colours in palette
00083         unsigned char  colourmapbits;   // number of bits per palette entry 15,16,24,32
00084 
00085         short xstart;                                   // image x origin
00086         short ystart;                                   // image y origin
00087         short width;                                    // image width in pixels
00088         short height;                                   // image height in pixels
00089         unsigned char  bits;                    // image bits per pixel 8,16,24,32
00090         unsigned char  descriptor;              // image descriptor bits (vh flip bits)
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 }; // namespace ImageTool
00132 
00133 
00134 }; // namespace PN


v4r_artoolkitplus
Author(s): Markus Bader
autogenerated on Wed Aug 26 2015 16:41:52