CrystalSpace

Public API Reference

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   }
00174   static inline uint8* PackRGBpixelToRGB (const csRGBpixel* pixels, 
00175     size_t numPixels)
00176   {
00177     uint8* buf = new uint8[numPixels * 3];
00178     uint8* bufptr = buf;
00179     while (numPixels--)
00180     {
00181       *bufptr++ = pixels->red;
00182       *bufptr++ = pixels->green;
00183       *bufptr++ = pixels->blue;
00184       pixels++; 
00185     }
00186     return buf;
00187   }
00194   static void UnpackRGBtoRGBpixelBuffer (csRGBpixel* buf, uint8* rgb,
00195     size_t numPixels)
00196   {
00197     csRGBpixel* bufptr = buf;
00198     while (numPixels--)
00199     {
00200       bufptr->red = *rgb++;
00201       bufptr->green = *rgb++;
00202       bufptr->blue = *rgb++;
00203       bufptr++; 
00204     }
00205   }
00206 };
00207 
00211 struct csPackRGBA
00212 {
00213   static bool IsRGBpixelSane() { return (sizeof(csRGBpixel) == 4); }
00220   static void PackRGBpixelToRGBA (uint8* buf, const csRGBpixel* pixels, 
00221     size_t numPixels)
00222   {
00223     if (IsRGBpixelSane())
00224       memcpy (buf, pixels, numPixels * 4);
00225     else
00226     {
00227       uint8* bufptr = buf;
00228       while (numPixels--)
00229       {
00230         *bufptr++ = pixels->red;
00231         *bufptr++ = pixels->green;
00232         *bufptr++ = pixels->blue;
00233         *bufptr++ = pixels->alpha;
00234         pixels++; 
00235       }
00236     }
00237   }
00247   static const uint8* PackRGBpixelToRGBA (const csRGBpixel* pixels, 
00248     size_t numPixels)
00249   {
00250     if (IsRGBpixelSane())
00251       return (uint8*)pixels;
00252     else
00253     {
00254       uint8* buf = new uint8[numPixels * 4];
00255       PackRGBpixelToRGBA (buf, pixels, numPixels);
00256       return buf;
00257     }
00258   }
00263   static void DiscardPackedRGBA (const uint8* rgba) 
00264   {
00265     if (!IsRGBpixelSane())
00266     {
00267       delete[] (uint8*)rgba;
00268     }
00269   }
00276   static void UnpackRGBAtoRGBpixel (csRGBpixel* buf, const uint8* rgba, 
00277     size_t numPixels)
00278   {
00279     if (IsRGBpixelSane())
00280       memcpy (buf, rgba, numPixels * 4);
00281     else
00282     {
00283       csRGBpixel* bufptr = buf;
00284       while (numPixels--)
00285       {
00286         bufptr->red = *rgba++;
00287         bufptr->green = *rgba++;
00288         bufptr->blue = *rgba++;
00289         bufptr->alpha = *rgba++;
00290         bufptr++; 
00291       }
00292     }
00293   }
00303   static const csRGBpixel* UnpackRGBAtoRGBpixel (const uint8* rgba, 
00304     size_t numPixels)
00305   {
00306     if (IsRGBpixelSane())
00307       return (csRGBpixel*)rgba;
00308     else
00309     {
00310       csRGBpixel* buf = new csRGBpixel[numPixels];
00311       UnpackRGBAtoRGBpixel (buf, rgba, numPixels);
00312       return buf;
00313     }
00314   }
00324   static csRGBpixel* CopyUnpackRGBAtoRGBpixel (const uint8* rgba, 
00325     size_t numPixels)
00326   {
00327     if (IsRGBpixelSane())
00328     {
00329       csRGBpixel* buf = new csRGBpixel[numPixels];
00330       memcpy (buf, rgba, numPixels * sizeof(csRGBpixel));
00331       return buf;
00332     }
00333     else
00334       return (csRGBpixel*)UnpackRGBAtoRGBpixel (rgba, numPixels);
00335   }
00341   static void csDiscardUnpackedRGBpixel (const csRGBpixel* pixels) 
00342   {
00343     if (!IsRGBpixelSane())
00344       delete[] (csRGBpixel*)pixels;
00345   }
00355   static inline csRGBcolor* UnpackRGBAtoRGBcolor (const uint8* rgba, 
00356     size_t numPixels)
00357   {
00358     csRGBcolor* buf = new csRGBcolor[numPixels];
00359     csRGBcolor* bufptr = buf;
00360     while (numPixels--)
00361     {
00362       bufptr->red = *rgba++;
00363       bufptr->green = *rgba++;
00364       bufptr->blue = *rgba++;
00365       rgba++;
00366       bufptr++; 
00367     }
00368     return buf;
00369   }
00370 };
00371 
00376 #endif // __CSGFX_PACKRGB_H__

Generated for Crystal Space 1.0.2 by doxygen 1.4.7