ImageTool.cpp
Go to the documentation of this file.
1 /* ========================================================================
2  * Copyright (C) 2004-2005 Graz University of Technology
3  *
4  * This framework is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This framework is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this framework; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * For further information please contact Dieter Schmalstieg under
19  * <schmalstieg@icg.tu-graz.ac.at> or write to Dieter Schmalstieg,
20  * Graz University of Technology, Institut für Maschinelles Sehen und Darstellen,
21  * Inffeldgasse 16a, 8010 Graz, Austria.
22  * ========================================================================
23  * PROJECT: PocketKnife
24  * ======================================================================== */
29 /* ======================================================================== */
30 
31 
32 #include "PocketKnife.h"
33 #include "ImageTool.h"
34 
35 #include <cstdio>
36 #include <string>
37 #include <cstring>
38 
39 #if defined(TARGET_HOST_WIN32) || defined(TARGET_HOST_WINCE)
40 #include <windows.h>
41 #endif
42 
43 namespace PN {
44 
45 
46 namespace ImageTool {
47 
48 
49 void convertPixelDataFrom16BitRGBTo24BitBGR(unsigned char* nDestData, const unsigned short* nPixelData,
50  unsigned int nWidth, unsigned int nHeight)
51 {
52  unsigned int i, size = nWidth * nHeight;
53 
54  for(i=0; i<size; i++)
55  {
56  convertPixel16To24(*nPixelData, nDestData[2], nDestData[1], nDestData[0]);
57 
58  nDestData+=3;
59  nPixelData++;
60  }
61 }
62 
63 
64 
65 void flipImageY(unsigned char* nDstBuffer, unsigned const char* nSrcBuffer, int nWidth, int nHeight, int nPixelSize)
66 {
67  int i, span = nPixelSize*nWidth;
68 
69  for(i=0; i<nHeight; i++)
70  memcpy(nDstBuffer+i*span, nSrcBuffer+(nHeight-1-i)*span, span);
71 }
72 
73 
74 struct TGA_HEADER
75 {
76 #pragma pack( push, 1 )
77  unsigned char identsize; // size of ID field that follows 18 unsigned char header (0 usually)
78  unsigned char colourmaptype; // type of colour map 0=none, 1=has palette
79  unsigned char imagetype; // type of image 0=none,1=indexed,2=rgb,3=grey,+8=rle packed
80 
81  short colourmapstart; // first colour map entry in palette
82  short colourmaplength; // number of colours in palette
83  unsigned char colourmapbits; // number of bits per palette entry 15,16,24,32
84 
85  short xstart; // image x origin
86  short ystart; // image y origin
87  short width; // image width in pixels
88  short height; // image height in pixels
89  unsigned char bits; // image bits per pixel 8,16,24,32
90  unsigned char descriptor; // image descriptor bits (vh flip bits)
91 #pragma pack( pop )
92 };
93 
94 
95 bool
96 saveAsTGA(Image* nImage, const char* nFileName)
97 {
98  TGA_HEADER header;
99  FILE* fp = fopen(nFileName, "wb");
100 
101  if(!fp)
102  return false;
103 
104  memset(&header, 0, sizeof(TGA_HEADER));
105 
106  header.imagetype = 2;
107  header.width = (short)nImage->getWidth();
108  header.height = (short)nImage->getHeight();
109  header.bits = 24;
110  header.descriptor = 0;
111 
112 
113  fwrite(&header, 1, sizeof(TGA_HEADER), fp);
114 
115  int size = nImage->getWidth()*nImage->getHeight()*3;
116  unsigned char *tmpBuf = new unsigned char[size], *tmpBuf2 = new unsigned char[size];
117 
118  convertPixelDataFrom16BitRGBTo24BitBGR(tmpBuf, nImage->getPixels(), nImage->getWidth(), nImage->getHeight());
119  flipImageY(tmpBuf2, tmpBuf, nImage->getWidth(), nImage->getHeight(), 3);
120 
121  fwrite(tmpBuf2, 1, size, fp);
122  fclose(fp);
123 
124  delete tmpBuf;
125  delete tmpBuf2;
126 
127  return true;
128 }
129 
130 
131 }; // namespace ImageTool
132 
133 
134 }; // namespace PN
int getHeight() const
Returns the height of the image.
Definition: Image.h:62
unsigned char descriptor
Definition: ImageTool.cpp:90
unsigned char identsize
Definition: ImageTool.cpp:77
Definition: Image.cpp:46
unsigned char colourmapbits
Definition: ImageTool.cpp:83
bool saveAsTGA(Image *nImage, const char *nFileName)
Save an Image into a TGA file.
Definition: ImageTool.cpp:96
unsigned char imagetype
Definition: ImageTool.cpp:79
void convertPixelDataFrom16BitRGBTo24BitBGR(unsigned char *nDestData, const unsigned short *nPixelData, unsigned int nWidth, unsigned int nHeight)
Definition: ImageTool.cpp:49
unsigned char colourmaptype
Definition: ImageTool.cpp:78
int getWidth() const
Returns the width of the image.
Definition: Image.h:59
void flipImageY(unsigned char *nDstBuffer, unsigned const char *nSrcBuffer, int nWidth, int nHeight, int nPixelSize)
Flips an image in Y-direction.
Definition: ImageTool.cpp:65
The Image class provides basic RGB565 image handing capabilities.
Definition: Image.h:42
unsigned short * getPixels()
Returns the pixel buffer.
Definition: Image.h:133
void convertPixel16To24(unsigned short nPixel, unsigned char &nRed, unsigned char &nGreen, unsigned char &nBlue)
Converts a single pixel from 16-bits (565) RGB to 24-bits RGB.
Definition: ImageTool.h:64


tuw_artoolkitplus
Author(s): Markus Bader
autogenerated on Sun Sep 4 2016 03:24:33