csgfx/packrgb.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 1998-2003 by Jorrit Tyberghein 00003 2003 by Frank Richter 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00025 #ifndef __CSGFX_PACKRGB_H__ 00026 #define __CSGFX_PACKRGB_H__ 00027 00028 #include "csextern.h" 00029 00030 #include "cstypes.h" 00031 #include "csgfx/rgbpixel.h" 00032 00033 00055 struct csPackRGB 00056 { 00057 static bool IsRGBcolorSane () { return (sizeof(csRGBcolor) == 3); } 00064 static void PackRGBcolorToRGBBuffer (uint8* buf, 00065 const csRGBcolor* pixels, size_t numPixels) 00066 { 00067 if (IsRGBcolorSane()) 00068 memcpy (buf, pixels, numPixels * 3); 00069 else 00070 { 00071 uint8* bufptr = buf; 00072 while (numPixels--) 00073 { 00074 *bufptr++ = pixels->red; 00075 *bufptr++ = pixels->green; 00076 *bufptr++ = pixels->blue; 00077 pixels++; 00078 } 00079 } 00080 } 00090 static const uint8* PackRGBcolorToRGB (const csRGBcolor* pixels, 00091 size_t numPixels) 00092 { 00093 if (IsRGBcolorSane ()) 00094 return (uint8*)pixels; 00095 else 00096 { 00097 uint8* buf = new uint8[numPixels * 3]; 00098 PackRGBcolorToRGBBuffer (buf, pixels, numPixels); 00099 return buf; 00100 } 00101 } 00106 static void DiscardPackedRGB (const uint8* rgb) 00107 { 00108 if (!IsRGBcolorSane ()) 00109 delete[] (uint8*)rgb; 00110 } 00117 static void UnpackRGBtoRGBcolor (csRGBcolor* buf, const uint8* rgb, 00118 size_t numPixels) 00119 { 00120 if (IsRGBcolorSane ()) 00121 memcpy (buf, rgb, numPixels * 3); 00122 else 00123 { 00124 csRGBcolor* bufptr = buf; 00125 while (numPixels--) 00126 { 00127 bufptr->red = *rgb++; 00128 bufptr->green = *rgb++; 00129 bufptr->blue = *rgb++; 00130 bufptr++; 00131 } 00132 } 00133 } 00143 static const csRGBcolor* UnpackRGBtoRGBcolor (const uint8* rgb, 00144 size_t numPixels) 00145 { 00146 if (IsRGBcolorSane ()) 00147 return (const csRGBcolor*)rgb; 00148 else 00149 { 00150 csRGBcolor* buf = new csRGBcolor[numPixels]; 00151 UnpackRGBtoRGBcolor (buf, rgb, numPixels); 00152 return buf; 00153 } 00154 } 00160 static void DiscardUnpackedRGBcolor (const csRGBcolor* pixels) 00161 { 00162 if (!IsRGBcolorSane ()) 00163 delete[] (csRGBcolor*)pixels; 00164 } 00169 static inline void PackRGBpixelToRGB (const csRGBpixel* pixels, 00170 uint8* packTo, size_t numPixels) 00171 { 00172 uint8* bufptr = packTo; 00173 while (numPixels--) 00174 { 00175 *bufptr++ = pixels->red; 00176 *bufptr++ = pixels->green; 00177 *bufptr++ = pixels->blue; 00178 pixels++; 00179 } 00180 } 00190 static inline uint8* PackRGBpixelToRGB (const csRGBpixel* pixels, 00191 size_t numPixels) 00192 { 00193 uint8* buf = new uint8[numPixels * 3]; 00194 PackRGBpixelToRGB (pixels, buf, numPixels); 00195 return buf; 00196 } 00203 static void UnpackRGBtoRGBpixelBuffer (csRGBpixel* buf, uint8* rgb, 00204 size_t numPixels) 00205 { 00206 csRGBpixel* bufptr = buf; 00207 while (numPixels--) 00208 { 00209 bufptr->red = *rgb++; 00210 bufptr->green = *rgb++; 00211 bufptr->blue = *rgb++; 00212 bufptr++; 00213 } 00214 } 00215 }; 00216 00220 struct csPackRGBA 00221 { 00222 static bool IsRGBpixelSane () { return (sizeof(csRGBpixel) == 4); } 00229 static void PackRGBpixelToRGBA (uint8* buf, const csRGBpixel* pixels, 00230 size_t numPixels) 00231 { 00232 if (IsRGBpixelSane ()) 00233 memcpy (buf, pixels, numPixels * 4); 00234 else 00235 { 00236 uint8* bufptr = buf; 00237 while (numPixels--) 00238 { 00239 *bufptr++ = pixels->red; 00240 *bufptr++ = pixels->green; 00241 *bufptr++ = pixels->blue; 00242 *bufptr++ = pixels->alpha; 00243 pixels++; 00244 } 00245 } 00246 } 00256 static const uint8* PackRGBpixelToRGBA (const csRGBpixel* pixels, 00257 size_t numPixels) 00258 { 00259 if (IsRGBpixelSane ()) 00260 return (uint8*)pixels; 00261 else 00262 { 00263 uint8* buf = new uint8[numPixels * 4]; 00264 PackRGBpixelToRGBA (buf, pixels, numPixels); 00265 return buf; 00266 } 00267 } 00272 static void DiscardPackedRGBA (const uint8* rgba) 00273 { 00274 if (!IsRGBpixelSane ()) 00275 { 00276 delete[] (uint8*)rgba; 00277 } 00278 } 00285 static void UnpackRGBAtoRGBpixel (csRGBpixel* buf, const uint8* rgba, 00286 size_t numPixels) 00287 { 00288 if (IsRGBpixelSane ()) 00289 memcpy (buf, rgba, numPixels * 4); 00290 else 00291 { 00292 csRGBpixel* bufptr = buf; 00293 while (numPixels--) 00294 { 00295 bufptr->red = *rgba++; 00296 bufptr->green = *rgba++; 00297 bufptr->blue = *rgba++; 00298 bufptr->alpha = *rgba++; 00299 bufptr++; 00300 } 00301 } 00302 } 00312 static const csRGBpixel* UnpackRGBAtoRGBpixel (const uint8* rgba, 00313 size_t numPixels) 00314 { 00315 if (IsRGBpixelSane ()) 00316 return (csRGBpixel*)rgba; 00317 else 00318 { 00319 csRGBpixel* buf = new csRGBpixel[numPixels]; 00320 UnpackRGBAtoRGBpixel (buf, rgba, numPixels); 00321 return buf; 00322 } 00323 } 00333 static csRGBpixel* CopyUnpackRGBAtoRGBpixel (const uint8* rgba, 00334 size_t numPixels) 00335 { 00336 if (IsRGBpixelSane ()) 00337 { 00338 csRGBpixel* buf = new csRGBpixel[numPixels]; 00339 memcpy (buf, rgba, numPixels * sizeof(csRGBpixel)); 00340 return buf; 00341 } 00342 else 00343 return (csRGBpixel*)UnpackRGBAtoRGBpixel (rgba, numPixels); 00344 } 00350 static void csDiscardUnpackedRGBpixel (const csRGBpixel* pixels) 00351 { 00352 if (!IsRGBpixelSane ()) 00353 delete[] (csRGBpixel*)pixels; 00354 } 00364 static inline csRGBcolor* UnpackRGBAtoRGBcolor (const uint8* rgba, 00365 size_t numPixels) 00366 { 00367 csRGBcolor* buf = new csRGBcolor[numPixels]; 00368 csRGBcolor* bufptr = buf; 00369 while (numPixels--) 00370 { 00371 bufptr->red = *rgba++; 00372 bufptr->green = *rgba++; 00373 bufptr->blue = *rgba++; 00374 rgba++; 00375 bufptr++; 00376 } 00377 return buf; 00378 } 00379 }; 00380 00385 #endif // __CSGFX_PACKRGB_H__
Generated for Crystal Space 2.0 by doxygen 1.6.1