Image.cpp

Go to the documentation of this file.
00001 
00007 #include "Image.h"
00008 #include "PNGHelper.h"
00009 
00010 bool Image::ReadPNG(const char* fileName)
00011 {
00012 
00013     // store here image data
00014     int channels, bit_depth;
00015     int color_type = PNG_COLOR_TYPE_RGB;
00016     unsigned char* buffer = NULL;
00017 
00018     // read png file 
00019     read_png(fileName, buffer, resX, resY, channels, bit_depth, color_type, true);
00020 
00021     // check supported color types
00022     if (!(color_type == PNG_COLOR_TYPE_GRAY
00023         || color_type == PNG_COLOR_TYPE_RGB
00024         || color_type == PNG_COLOR_TYPE_RGBA))
00025     {
00026         std::cout << "Image::ReadPNG(" << fileName << "): not supported color type!" << endl;
00027         if (buffer) free(buffer);
00028         return false;
00029     }
00030 
00031     // currently only support 8 bit 
00032     if (bit_depth != 8)
00033     {
00034         std::cout << "Image::ReadPNG(" << fileName << "): not supported bit depth!" << endl;
00035         if (buffer) free(buffer);
00036         return false;
00037     }
00038 
00039     std::cout << "Read PNG File : " << fileName << std::endl;
00040 
00041     // check whenever we have gray scale png file 
00042     bool grey = color_type == PNG_COLOR_TYPE_GRAY;
00043     bool rgba = color_type == PNG_COLOR_TYPE_RGBA;
00044 
00045     // create pixel to store the data
00046     if (pixel) delete [] pixel;
00047     pixel = new ColorRGBA[resX * resY];
00048 
00049     // copy and convert pixel data
00050     for (int y=0; y < resY; y++)
00051         for (int x=0; x < resX; x++)
00052         {
00053             // convert values
00054             ColorRGBA v = ColorRGBA(float(buffer[(y * resX + x) * channels + (grey ? 0 : 0)]) / 255.0,
00055                             float(buffer[(y * resX + x) * channels + (grey ? 0 : 1)]) / 255.0,
00056                             float(buffer[(y * resX + x) * channels + (grey ? 0 : 2)]) / 255.0,
00057                             rgba ? float(buffer[(y * resX + x) * channels + (grey ? 0 : 3)]) / 255.0 : 1);
00058             // set pixel 
00059             setPixel(v, x, y);
00060         }
00061 
00062     // free up the used data 
00063     if (buffer) free (buffer);
00064 
00065     return true;        
00066 }
00067 
00068 
00069 void Image::WritePNG(const char* fileName)
00070 {
00071     // store here image data
00072     int channels = 3;
00073     int bit_depth = 8;
00074     int color_type = PNG_COLOR_TYPE_RGB;
00075 
00076     // create pixel to store the data
00077     unsigned char* buffer = (unsigned char*)malloc(resX * resY * sizeof(unsigned char) * channels);
00078 
00079     // copy and convert pixel data
00080     for (int y=0; y < resY; y++)
00081         for (int x=0; x < resX; x++)
00082         {
00083             // get pixel 
00084             const ColorRGBA& p = getPixel(x,y);
00085 
00086             // convert values
00087             buffer[(y * resX + x) * channels + 0] = (unsigned char)(255.0 * p.red());
00088             buffer[(y * resX + x) * channels + 1] = (unsigned char)(255.0 * p.green());
00089             buffer[(y * resX + x) * channels + 2] = (unsigned char)(255.0 * p.blue());
00090         }
00091 
00092     // write file 
00093     write_png(fileName, buffer, resX, resY, channels, bit_depth, color_type, true);
00094 
00095     // free up the used data 
00096     if (buffer) free (buffer);
00097 }
00098 

Generated on Thu Jan 31 21:48:48 2008 for RayTracer by  doxygen 1.5.4