CrystalSpace

Public API Reference

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

Generated for Crystal Space by doxygen 1.4.4