Blender  V2.59
Image.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: Image.cpp 36546 2011-05-08 09:05:52Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * Contributors: Amorilia (amorilia@users.sourceforge.net)
00021  *
00022  * ***** END GPL LICENSE BLOCK *****
00023  */
00024 
00030 /*
00031  * This file is based on a similar file from the NVIDIA texture tools
00032  * (http://nvidia-texture-tools.googlecode.com/)
00033  *
00034  * Original license from NVIDIA follows.
00035  */
00036 
00037 // This code is in the public domain -- castanyo@yahoo.es
00038 
00039 #include <Color.h>
00040 #include <Image.h>
00041 
00042 #include <stdio.h> // printf
00043 
00044 Image::Image() : m_width(0), m_height(0), m_format(Format_RGB), m_data(NULL)
00045 {
00046 }
00047 
00048 Image::~Image()
00049 {
00050         free();
00051 }
00052 
00053 void Image::allocate(uint w, uint h)
00054 {
00055         free();
00056         m_width = w;
00057         m_height = h;
00058         m_data = new Color32[w * h];
00059 }
00060 
00061 void Image::free()
00062 {
00063         if (m_data) delete [] m_data;
00064         m_data = NULL;
00065 }
00066 
00067 
00068 uint Image::width() const
00069 {
00070         return m_width;
00071 }
00072 
00073 uint Image::height() const
00074 {
00075         return m_height;
00076 }
00077 
00078 const Color32 * Image::scanline(uint h) const
00079 {
00080         if (h >= m_height) {
00081                 printf("DDS: scanline beyond dimensions of image");
00082                 return m_data;
00083         }
00084         return m_data + h * m_width;
00085 }
00086 
00087 Color32 * Image::scanline(uint h)
00088 {
00089         if (h >= m_height) {
00090                 printf("DDS: scanline beyond dimensions of image");
00091                 return m_data;
00092         }
00093         return m_data + h * m_width;
00094 }
00095 
00096 const Color32 * Image::pixels() const
00097 {
00098         return m_data;
00099 }
00100 
00101 Color32 * Image::pixels()
00102 {
00103         return m_data;
00104 }
00105 
00106 const Color32 & Image::pixel(uint idx) const
00107 {
00108         if (idx >= m_width * m_height) {
00109                 printf("DDS: pixel beyond dimensions of image");
00110                 return m_data[0];
00111         }
00112         return m_data[idx];
00113 }
00114 
00115 Color32 & Image::pixel(uint idx)
00116 {
00117         if (idx >= m_width * m_height) {
00118                 printf("DDS: pixel beyond dimensions of image");
00119                 return m_data[0];
00120         }
00121         return m_data[idx];
00122 }
00123 
00124 
00125 Image::Format Image::format() const
00126 {
00127         return m_format;
00128 }
00129 
00130 void Image::setFormat(Image::Format f)
00131 {
00132         m_format = f;
00133 }
00134 
00135