Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
ImageTools.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef ImageTools_INCLUDE_ONCE
33 #define ImageTools_INCLUDE_ONCE
34 
35 #include <memory.h>
36 
37 namespace vl
38 {
39 //-----------------------------------------------------------------------------
40  typedef unsigned char TPalette3x256[256*3];
41  typedef unsigned char TPalette4x256[256*4];
42 //-----------------------------------------------------------------------------
43  inline void convertRGBToRGBA(void* buf, int w, int h, unsigned char alpha, int bytealign = 1)
44  {
45  int xbytes = w*3;
46  int pitch = (xbytes / bytealign * bytealign) + ((xbytes % bytealign)? bytealign : 0);
47 
48  if(bytealign)
49  {
50  // compact the image
51 
52  unsigned char *pxl1 = (unsigned char *)buf;
53  unsigned char *pxl2 = (unsigned char *)buf;
54  int count = 0;
55  for(int i=0; i<pitch*h; ++i, count++)
56  {
57  if (count==pitch)
58  count = 0;
59 
60  *pxl1 = *pxl2;
61 
62  if (count < w*3)
63  pxl1 ++;
64 
65  pxl2 ++;
66  }
67  }
68 
69  unsigned char * px32 = (unsigned char*)buf + w * h * 4 - 4;
70  unsigned char * px24 = (unsigned char*)buf + w * h * 3 - 3;
71  for(int i=0; i<w * h; ++i)
72  {
73  px32[0] = px24[0];
74  px32[1] = px24[1];
75  px32[2] = px24[2];
76  px32[3] = alpha;
77  px24 -= 3;
78  px32 -= 4;
79  }
80  }
81 //-----------------------------------------------------------------------------
82  inline void convertGrayscaleToRGBA(void* buf, int size, unsigned char alpha)
83  {
84  unsigned char* px32 = (unsigned char*)buf + size * 4 - 4;
85  unsigned char* px8 = (unsigned char*)buf + size * 1 - 1;
86  for(int i=0; i<size; ++i)
87  {
88  px32[0] = *px8;
89  px32[1] = *px8;
90  px32[2] = *px8;
91  px32[3] = alpha;
92  px8 -= 1;
93  px32 -= 4;
94  }
95  }
96 //-----------------------------------------------------------------------------
97  inline void convertA1R5G5B5ToRGBA(void* buf, int size, unsigned char alpha)
98  {
99  unsigned char* px32 = (unsigned char*)buf + size * 4 - 4;
100  unsigned char* px8 = (unsigned char*)buf + size * 2 - 2;
101  for(int i=0; i<size; ++i)
102  {
103  unsigned char r = (px8[1] << 1) & ~0x03;
104  unsigned char g = ((px8[1] << 6) | (px8[0] >> 2)) & ~0x03;
105  unsigned char b = (px8[0] << 3) & ~0x03;
106  // photoshop sets it to zero
107  // int a = (px8[1] & 0x80) ? 0xFF : 0;
108  px32[0] = r;
109  px32[1] = g;
110  px32[2] = b;
111  px32[3] = alpha;
112  px8 -= 2;
113  px32 -= 4;
114  }
115  }
116 //-----------------------------------------------------------------------------
117  inline void convert8ToRGBA(const TPalette3x256 & palette, void* buf, int w, int h, unsigned char alpha, int bytealign = 1)
118  {
119 
120  int xbytes = w;
121  int pitch = (xbytes / bytealign * bytealign) + ((xbytes % bytealign)? bytealign : 0);
122 
123  if(bytealign)
124  {
125  // compact the image
126 
127  unsigned char *pxl1 = (unsigned char *)buf;
128  unsigned char *pxl2 = (unsigned char *)buf;
129  int count = 0;
130  for(int i=0; i<pitch*h; ++i, count++)
131  {
132  if (count==pitch)
133  count = 0;
134 
135  *pxl1 = *pxl2;
136 
137  if (count < w)
138  pxl1 ++;
139 
140  pxl2 ++;
141  }
142  }
143 
144  unsigned char* px32 = (unsigned char*)buf + w * h * 4 - 4;
145  unsigned char* px8 = (unsigned char*)buf + w * h * 1 - 1;
146  for(int i=0; i<w * h; ++i)
147  {
148  px32[0] = palette[*px8*3+0];
149  px32[1] = palette[*px8*3+1];
150  px32[2] = palette[*px8*3+2];
151  px32[3] = alpha;
152  px8 -= 1;
153  px32 -= 4;
154  }
155  }
156 //-----------------------------------------------------------------------------
157  inline void convert8ToRGBA(const TPalette4x256 & palette, void* buf, int w, int h, int bytealign = 1)
158  {
159 
160  int xbytes = w;
161  int pitch = (xbytes / bytealign * bytealign) + ((xbytes % bytealign)? bytealign : 0);
162 
163  if(bytealign)
164  {
165  // compact the image
166 
167  unsigned char *pxl1 = (unsigned char *)buf;
168  unsigned char *pxl2 = (unsigned char *)buf;
169  int count = 0;
170  for(int i=0; i<pitch*h; ++i, count++)
171  {
172  if (count==pitch)
173  count = 0;
174 
175  *pxl1 = *pxl2;
176 
177  if (count < w)
178  pxl1 ++;
179 
180  pxl2 ++;
181  }
182  }
183 
184  unsigned char * px32 = (unsigned char*)buf + w * h * 4 - 4;
185  unsigned char * px8 = (unsigned char*)buf + w * h * 1 - 1;
186  for(int i=0; i<w * h; ++i)
187  {
188  px32[0] = palette[*px8*4+0];
189  px32[1] = palette[*px8*4+1];
190  px32[2] = palette[*px8*4+2];
191  px32[3] = palette[*px8*4+3];
192  px8 -= 1;
193  px32 -= 4;
194  }
195  }
196 //-----------------------------------------------------------------------------
197  inline void swapBytes32(void* buf, int size)
198  {
199  unsigned char* p = (unsigned char*)buf;
200  unsigned char dw[4];
201  for(int i=0; i<size; i+=4, p+=4)
202  {
203  memcpy(dw, p, 4);
204  p[0] = dw[3];
205  p[3] = dw[0];
206  p[1] = dw[2];
207  p[2] = dw[1];
208  }
209  }
210 //-----------------------------------------------------------------------------
211  inline void swapBytes32_BGRA_RGBA(void* buf, int bytecount)
212  {
213  unsigned char* p = (unsigned char*)buf;
214  unsigned char dw[4];
215  for(int i=0; i<bytecount; i+=4, p+=4)
216  {
217  memcpy(dw, p, 4);
218  p[0] = dw[2];
219  p[2] = dw[0];
220  }
221  }
222 //-----------------------------------------------------------------------------
223  inline void swapBytes24_BGR_RGB(void* buf, int bytecount)
224  {
225  unsigned char* p = (unsigned char*)buf;
226  unsigned char dw[4];
227  int pxl = bytecount / 3;
228  for(int i=0; i<pxl; ++i, p+=3)
229  {
230  memcpy(dw, p, 3);
231  p[0] = dw[2];
232  p[2] = dw[0];
233  }
234  }
235 //-----------------------------------------------------------------------------
236  inline void fillRGBA32_Alpha(void* buf, int bytecount, unsigned char alpha)
237  {
238  unsigned char* pxl = (unsigned char*)buf;
239  for(int i=0; i<bytecount; i+=4)
240  {
241  pxl[i+3] = alpha;
242  }
243  }
244 //-----------------------------------------------------------------------------
245  inline void fillGray8Alpha8_Alpha(void* buf, int bytecount, unsigned char alpha)
246  {
247  unsigned char* pxl = (unsigned char*)buf;
248  for(int i=0; i<bytecount; i+=2)
249  {
250  pxl[i+1] = alpha;
251  }
252  }
253 //-----------------------------------------------------------------------------
254 }
255 
256 #endif
void swapBytes32(void *buf, int size)
Definition: ImageTools.hpp:197
void convert8ToRGBA(const TPalette3x256 &palette, void *buf, int w, int h, unsigned char alpha, int bytealign=1)
Definition: ImageTools.hpp:117
void swapBytes32_BGRA_RGBA(void *buf, int bytecount)
Definition: ImageTools.hpp:211
void fillRGBA32_Alpha(void *buf, int bytecount, unsigned char alpha)
Definition: ImageTools.hpp:236
void convertA1R5G5B5ToRGBA(void *buf, int size, unsigned char alpha)
Definition: ImageTools.hpp:97
Visualization Library main namespace.
void fillGray8Alpha8_Alpha(void *buf, int bytecount, unsigned char alpha)
Definition: ImageTools.hpp:245
void convertRGBToRGBA(void *buf, int w, int h, unsigned char alpha, int bytealign=1)
Definition: ImageTools.hpp:43
void convertGrayscaleToRGBA(void *buf, int size, unsigned char alpha)
Definition: ImageTools.hpp:82
unsigned char TPalette3x256[256 *3]
Definition: ImageTools.hpp:40
void swapBytes24_BGR_RGB(void *buf, int bytecount)
Definition: ImageTools.hpp:223
unsigned char TPalette4x256[256 *4]
Definition: ImageTools.hpp:41