ImageLuminance.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D 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 Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <string.h>
00022 #include <stdlib.h>
00023 #include <image/ImageLuminance.h>
00024 #include <common/Defines.h>
00025 
00026 ImageLuminance::ImageLuminance(const std::string &filename) : base_(NULL)
00027 {
00028         loadFromFile(filename);
00029 }
00030 
00031 ImageLuminance::~ImageLuminance()
00032 {
00033         free(base_);
00034 }
00035 
00036 void ImageLuminance::removeOwnership()
00037 {
00038 }
00039 
00040 unsigned char *ImageLuminance::getBits()
00041 {
00042         return base_;
00043 }
00044 
00045 int ImageLuminance::getWidth()
00046 {
00047         return image.xsize;
00048 }
00049 
00050 int ImageLuminance::getHeight()
00051 {
00052         return image.ysize;
00053 }
00054 
00055 int ImageLuminance::getComponents()
00056 {
00057         return image.zsize;
00058 }
00059 
00060 int ImageLuminance::getAlignment()
00061 {
00062         return image.zsize;
00063 }
00064 
00065 void ImageLuminance::convertShort(unsigned short *array, unsigned int length)
00066 {
00067         unsigned short b1, b2;
00068         unsigned char *ptr;
00069 
00070         ptr = (unsigned char *) array;
00071         while (length--) 
00072         {
00073                 b1 = *ptr++;
00074                 b2 = *ptr++;
00075                 *array++ = (unsigned short) ((b1 << 8) | (b2));
00076         }
00077 }
00078 
00079 void ImageLuminance::convertUint(unsigned *array, unsigned int length)
00080 {
00081         unsigned int b1, b2, b3, b4;
00082         unsigned char *ptr;
00083 
00084         ptr = (unsigned char *) array;
00085         while (length--) 
00086         {
00087                 b1 = *ptr++;
00088                 b2 = *ptr++;
00089                 b3 = *ptr++;
00090                 b4 = *ptr++;
00091                 *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
00092         }
00093 }
00094 
00095 void ImageLuminance::imageGetRow(FILE *file, unsigned char *buf, int y, int z, unsigned int *rowStart, int *rowSize)
00096 {
00097         if ((image.type & 0xFF00) == 0x0100) 
00098         {
00099                 unsigned char *tmp = (unsigned char *) malloc(image.xsize * 256);
00100 
00101                 fseek(file, (long) rowStart[y + z * image.ysize], SEEK_SET);
00102                 fread(tmp, 1, (unsigned int) rowSize[y + z * image.ysize], file);
00103 
00104                 unsigned char *iPtr = tmp;
00105                 unsigned char *oPtr = buf;
00106                 for (;;) 
00107                 {
00108                         unsigned char pixel = *iPtr++;
00109                         int count = (int) (pixel & 0x7F);
00110                         if (!count) break;
00111 
00112                         if (pixel & 0x80) 
00113                         {
00114                                 while (count--) *oPtr++ = *iPtr++;
00115                         } 
00116                         else 
00117                         {
00118                                 pixel = *iPtr++;
00119                                 while (count--) *oPtr++ = pixel;
00120                         }
00121                 }
00122 
00123                 free(tmp);
00124         } 
00125         else 
00126         {
00127                 fseek(file, 512 + (y * image.xsize) + (z * image.xsize * image.ysize), SEEK_SET);
00128                 fread(buf, 1, image.xsize, file);
00129         }
00130 }
00131 
00132 bool ImageLuminance::loadFromFile(const std::string &fileName)
00133 {
00134         union 
00135         {
00136                 int testWord;
00137                 char testByte[4];
00138         } endianTest;
00139 
00140         endianTest.testWord = 1;
00141 
00142         int swapFlag = 0;
00143         if (endianTest.testByte[0] == 1) swapFlag = 1;
00144 
00145         FILE *file = fopen(fileName.c_str(), "rb");
00146         if (file)
00147         {
00148                 memset(&image, 0, sizeof(image));
00149                 fread(&image, 1, 12, file);
00150 
00151                 if (swapFlag) convertShort(&image.imagic, 6);
00152 
00153                 base_ = (unsigned char *)
00154                         malloc(image.xsize * image.ysize * sizeof(unsigned char));
00155 
00156                 if (base_)
00157                 {
00158                         if ((image.type & 0xFF00) == 0x0100) 
00159                         {
00160                                 int x = image.ysize * image.zsize * (int) sizeof(unsigned);
00161                                 unsigned int *rowStart = (unsigned int *) malloc(x);
00162                                 int *rowSize = (int *) malloc(x);
00163 
00164                                 if (rowStart && rowSize)
00165                                 {                       
00166                                         fseek(file, 512, SEEK_SET);
00167                                         fread(rowStart, 1, x, file);
00168                                         fread(rowSize, 1, x, file);
00169 
00170                                         if (swapFlag) 
00171                                         {
00172                                                 convertUint(rowStart, x / (int) sizeof(unsigned));
00173                                                 convertUint((unsigned *) rowSize, x / (int) sizeof(int));
00174                                         }
00175 
00176                                         unsigned char *lptr = base_;
00177                                         for (int y = 0; y < image.ysize; y++) 
00178                                         {
00179                                                 imageGetRow(file, lptr, y, 0, rowStart, rowSize);
00180                                                 lptr += image.xsize;
00181                                         }
00182 
00183                                         free(rowStart);
00184                                         free(rowSize);
00185                                 }
00186                         }
00187                         else
00188                         {
00189                                 unsigned char *lptr = base_;
00190                                 for (int y = 0; y < image.ysize; y++) 
00191                                 {
00192                                         imageGetRow(file, lptr, y, 0, 0, 0);
00193                                         lptr += image.xsize;
00194                                 }
00195                         }
00196                 }
00197 
00198                 fclose(file);
00199         }
00200 
00201         return true;
00202 }

Generated on Mon Feb 16 15:14:50 2009 for Scorched3D by  doxygen 1.5.3