# Visualization Library

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

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlCore/ImageTools.hpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
    00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
    00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
    00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
    00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
    00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
    00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
    00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
    00029 /*                                                                                    */
    00030 /**************************************************************************************/
    00031
    00032 #ifndef ImageTools_INCLUDE_ONCE
    00033 #define ImageTools_INCLUDE_ONCE
    00034
    00035 #include <memory.h>
    00036
    00037 namespace vl
    00038 {
    00039 //-----------------------------------------------------------------------------
    00040   typedef unsigned char TPalette3x256[256*3];
    00041   typedef unsigned char TPalette4x256[256*4];
    00042 //-----------------------------------------------------------------------------
    00043   inline void convertRGBToRGBA(void* buf, int w, int h, unsigned char alpha, int bytealign = 1)
    00044   {
    00045     int xbytes = w*3;
    00046     int pitch = (xbytes / bytealign * bytealign) + ((xbytes % bytealign)? bytealign : 0);
    00047
    00048     if(bytealign)
    00049     {
    00050       // compact the image
    00051
    00052       unsigned char *pxl1 = (unsigned char *)buf;
    00053       unsigned char *pxl2 = (unsigned char *)buf;
    00054       int count = 0;
    00055       for(int i=0; i<pitch*h; ++i, count++)
    00056       {
    00057         if (count==pitch)
    00058           count = 0;
    00059
    00060         *pxl1 = *pxl2;
    00061
    00062         if (count < w*3)
    00063           pxl1 ++;
    00064
    00065         pxl2 ++;
    00066       }
    00067     }
    00068
    00069     unsigned char * px32 = (unsigned char*)buf + w * h * 4 - 4;
    00070     unsigned char * px24 = (unsigned char*)buf + w * h * 3 - 3;
    00071     for(int i=0; i<w * h; ++i)
    00072     {
    00073       px32[0] = px24[0];
    00074       px32[1] = px24[1];
    00075       px32[2] = px24[2];
    00076       px32[3] = alpha;
    00077       px24 -= 3;
    00078       px32 -= 4;
    00079     }
    00080   }
    00081 //-----------------------------------------------------------------------------
    00082   inline void convertGrayscaleToRGBA(void* buf, int size, unsigned char alpha)
    00083   {
    00084     unsigned char* px32 = (unsigned char*)buf + size * 4 - 4;
    00085     unsigned char* px8  = (unsigned char*)buf + size * 1 - 1;
    00086     for(int i=0; i<size; ++i)
    00087     {
    00088       px32[0] = *px8;
    00089       px32[1] = *px8;
    00090       px32[2] = *px8;
    00091       px32[3] = alpha;
    00092       px8 -= 1;
    00093       px32 -= 4;
    00094     }
    00095   }
    00096 //-----------------------------------------------------------------------------
    00097   inline void convertA1R5G5B5ToRGBA(void* buf, int size, unsigned char alpha)
    00098   {
    00099     unsigned char* px32 = (unsigned char*)buf + size * 4 - 4;
    00100     unsigned char* px8  = (unsigned char*)buf + size * 2 - 2;
    00101     for(int i=0; i<size; ++i)
    00102     {
    00103       unsigned char r = (px8[1] << 1) & ~0x03;
    00104       unsigned char g = ((px8[1] << 6) | (px8[0] >> 2)) & ~0x03;
    00105       unsigned char b = (px8[0] << 3) & ~0x03;
    00106       // photoshop sets it to zero
    00107       // int a = (px8[1] & 0x80) ? 0xFF : 0;
    00108       px32[0] = r;
    00109       px32[1] = g;
    00110       px32[2] = b;
    00111       px32[3] = alpha;
    00112       px8  -= 2;
    00113       px32 -= 4;
    00114     }
    00115   }
    00116 //-----------------------------------------------------------------------------
    00117   inline void convert8ToRGBA(const TPalette3x256 & palette, void* buf, int w, int h, unsigned char alpha, int bytealign = 1)
    00118   {
    00119
    00120     int xbytes = w;
    00121     int pitch = (xbytes / bytealign * bytealign) + ((xbytes % bytealign)? bytealign : 0);
    00122
    00123     if(bytealign)
    00124     {
    00125       // compact the image
    00126
    00127       unsigned char *pxl1 = (unsigned char *)buf;
    00128       unsigned char *pxl2 = (unsigned char *)buf;
    00129       int count = 0;
    00130       for(int i=0; i<pitch*h; ++i, count++)
    00131       {
    00132         if (count==pitch)
    00133           count = 0;
    00134
    00135         *pxl1 = *pxl2;
    00136
    00137         if (count < w)
    00138           pxl1 ++;
    00139
    00140         pxl2 ++;
    00141       }
    00142     }
    00143
    00144     unsigned char* px32 = (unsigned char*)buf + w * h * 4 - 4;
    00145     unsigned char* px8  = (unsigned char*)buf + w * h * 1 - 1;
    00146     for(int i=0; i<w * h; ++i)
    00147     {
    00148       px32[0] = palette[*px8*3+0];
    00149       px32[1] = palette[*px8*3+1];
    00150       px32[2] = palette[*px8*3+2];
    00151       px32[3] = alpha;
    00152       px8  -= 1;
    00153       px32 -= 4;
    00154     }
    00155   }
    00156 //-----------------------------------------------------------------------------
    00157   inline void convert8ToRGBA(const TPalette4x256 & palette, void* buf, int w, int h, int bytealign = 1)
    00158   {
    00159
    00160     int xbytes = w;
    00161     int pitch = (xbytes / bytealign * bytealign) + ((xbytes % bytealign)? bytealign : 0);
    00162
    00163     if(bytealign)
    00164     {
    00165       // compact the image
    00166
    00167       unsigned char *pxl1 = (unsigned char *)buf;
    00168       unsigned char *pxl2 = (unsigned char *)buf;
    00169       int count = 0;
    00170       for(int i=0; i<pitch*h; ++i, count++)
    00171       {
    00172         if (count==pitch)
    00173           count = 0;
    00174
    00175         *pxl1 = *pxl2;
    00176
    00177         if (count < w)
    00178           pxl1 ++;
    00179
    00180         pxl2 ++;
    00181       }
    00182     }
    00183
    00184     unsigned char * px32 = (unsigned char*)buf + w * h * 4 - 4;
    00185     unsigned char * px8  = (unsigned char*)buf + w * h * 1 - 1;
    00186     for(int i=0; i<w * h; ++i)
    00187     {
    00188       px32[0] = palette[*px8*4+0];
    00189       px32[1] = palette[*px8*4+1];
    00190       px32[2] = palette[*px8*4+2];
    00191       px32[3] = palette[*px8*4+3];
    00192       px8  -= 1;
    00193       px32 -= 4;
    00194     }
    00195   }
    00196 //-----------------------------------------------------------------------------
    00197   inline void swapBytes32(void* buf, int size)
    00198   {
    00199     unsigned char* p = (unsigned char*)buf;
    00200     unsigned char dw[4];
    00201     for(int i=0; i<size; i+=4, p+=4)
    00202     {
    00203       memcpy(dw, p, 4);
    00204       p[0] = dw[3];
    00205       p[3] = dw[0];
    00206       p[1] = dw[2];
    00207       p[2] = dw[1];
    00208     }
    00209   }
    00210 //-----------------------------------------------------------------------------
    00211   inline void swapBytes32_BGRA_RGBA(void* buf, int bytecount)
    00212   {
    00213     unsigned char* p = (unsigned char*)buf;
    00214     unsigned char dw[4];
    00215     for(int i=0; i<bytecount; i+=4, p+=4)
    00216     {
    00217       memcpy(dw, p, 4);
    00218       p[0] = dw[2];
    00219       p[2] = dw[0];
    00220     }
    00221   }
    00222 //-----------------------------------------------------------------------------
    00223   inline void swapBytes24_BGR_RGB(void* buf, int bytecount)
    00224   {
    00225     unsigned char* p = (unsigned char*)buf;
    00226     unsigned char dw[4];
    00227     int pxl = bytecount / 3;
    00228     for(int i=0; i<pxl; ++i, p+=3)
    00229     {
    00230       memcpy(dw, p, 3);
    00231       p[0] = dw[2];
    00232       p[2] = dw[0];
    00233     }
    00234   }
    00235 //-----------------------------------------------------------------------------
    00236   inline void fillRGBA32_Alpha(void* buf, int bytecount, unsigned char alpha)
    00237   {
    00238     unsigned char* pxl = (unsigned char*)buf;
    00239     for(int i=0; i<bytecount; i+=4)
    00240     {
    00241       pxl[i+3] = alpha;
    00242     }
    00243   }
    00244 //-----------------------------------------------------------------------------
    00245   inline void fillGray8Alpha8_Alpha(void* buf, int bytecount, unsigned char alpha)
    00246   {
    00247     unsigned char* pxl = (unsigned char*)buf;
    00248     for(int i=0; i<bytecount; i+=2)
    00249     {
    00250       pxl[i+1] = alpha;
    00251     }
    00252   }
    00253 //-----------------------------------------------------------------------------
    00254 }
    00255
    00256 #endif

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:20.
Permission is granted to use this page to write and publish articles regarding Visualization Library.