CrystalSpace

Public API Reference

Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

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 "iutil/databuff.h"
00032 #include "csutil/databuf.h"
00033 #include "rgbpixel.h"
00034 
00056 struct csPackRGB
00057 {
00058   static bool IsRGBcolorSane() { return (sizeof(csRGBcolor) == 3); }
00065   static void PackRGBcolorToRGBBuffer (uint8* buf, 
00066     const csRGBcolor* pixels, size_t numPixels)
00067   {
00068     if (IsRGBcolorSane())
00069       memcpy (buf, pixels, numPixels * 3);
00070     else
00071     {
00072       uint8* bufptr = buf;
00073       while (numPixels--)
00074       {
00075         *bufptr++ = pixels->red;
00076         *bufptr++ = pixels->green;
00077         *bufptr++ = pixels->blue;
00078         pixels++; 
00079       }
00080     }
00081   }
00091   static const uint8* PackRGBcolorToRGB (const csRGBcolor* pixels, 
00092     size_t numPixels)
00093   {
00094     if (IsRGBcolorSane())
00095       return (uint8*)pixels;
00096     else
00097     {
00098       uint8* buf = new uint8[numPixels * 3];
00099       PackRGBcolorToRGBBuffer (buf, pixels, numPixels);
00100       return buf;
00101     }
00102   }
00107   static void DiscardPackedRGB (const uint8* rgb) 
00108   {
00109     if (!IsRGBcolorSane())
00110       delete[] (uint8*)rgb;
00111   }
00118   static void UnpackRGBtoRGBcolor (csRGBcolor* buf, const uint8* rgb, 
00119     size_t numPixels)
00120   {
00121     if (IsRGBcolorSane())
00122       memcpy (buf, rgb, numPixels * 3);
00123     else
00124     {
00125       csRGBcolor* bufptr = buf;
00126       while (numPixels--)
00127       {
00128         bufptr->red = *rgb++;
00129         bufptr->green = *rgb++;
00130         bufptr->blue = *rgb++;
00131         bufptr++; 
00132       }
00133     }
00134   }
00144   static const csRGBcolor* UnpackRGBtoRGBcolor (const uint8* rgb, 
00145     size_t numPixels)
00146   {
00147     if (IsRGBcolorSane())
00148       return (const csRGBcolor*)rgb;
00149     else
00150     {
00151       csRGBcolor* buf = new csRGBcolor[numPixels];
00152       UnpackRGBtoRGBcolor (buf, rgb, numPixels);
00153       return buf;
00154     }
00155   }
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 by doxygen 1.3.9.1