00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "grayimage.h"
00033 #include "alphaimage.h"
00034
00035 GrayImage::GrayImage(const int height, const int width): AlphaImage(height,width)
00036 {
00037 luminancedata = new Channel<unsigned char>(height,width);
00038 luminancefilter = luminancedata;
00039 }
00040
00041 GrayImage::~GrayImage()
00042 {
00043 delete luminancedata;
00044 }
00045
00046 inline unsigned char GrayImage::GetLuminance(const int x, const int y)
00047 {
00048 return(luminancefilter->Get(x,y));
00049 }
00050
00051 inline void GrayImage::SetLuminance(const int x, const int y, const unsigned char val)
00052 {
00053 luminancefilter->Set(x,y,val);
00054 }
00055
00056 void GrayImage::AttachLuminanceFilter(ChannelFilter<unsigned char>* filter)
00057 {
00058 filter->SetSubChannel(luminancefilter);
00059 luminancefilter = filter;
00060 }
00061
00062 void GrayImage::RemoveLuminanceFilters()
00063 {
00064 luminancefilter->ResetChannel();
00065 luminancefilter = luminancedata;
00066 }
00067
00068 double GrayImage::Draw()
00069 {
00070 GLdouble xFract, yFract, zoom;
00071 GLint vp[4];
00072 glGetIntegerv(GL_VIEWPORT, vp);
00073 xFract = (GLdouble)vp[2] / luminancedata->GetWidth();
00074 yFract = (GLdouble)vp[3] / luminancedata->GetHeight();
00075 zoom = (xFract < yFract) ? xFract : yFract;
00076 if(zoom > 1.0)
00077 zoom = (int)zoom;
00078
00079 glMatrixMode(GL_PROJECTION);
00080 glLoadIdentity();
00081 glOrtho(0, luminancedata->GetWidth(), luminancedata->GetHeight(), 0, -1, 1);
00082 glMatrixMode(GL_MODELVIEW);
00083
00084 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00085 glPixelStorei(GL_PACK_ALIGNMENT, 1);
00086
00087 glClearColor(0.3,0.4,0.4,1.0);
00088 glClear(GL_COLOR_BUFFER_BIT);
00089 glRasterPos2d(0, 0);
00090 glPixelZoom(zoom, -zoom);
00091 glDrawPixels(luminancedata->GetWidth(), luminancedata->GetHeight(),GL_LUMINANCE,GL_UNSIGNED_BYTE,*(luminancedata->GetRawData()));
00092 glutSwapBuffers();
00093 return((double)zoom);
00094 }
00095
00096 GrayImage* GrayImage::CreateFromTiff(const char* filename)
00097 {
00098 TIFF *tif;
00099 if ((tif = TIFFOpen(filename, "r")) != NULL)
00100 {
00101 uint32 width, height;
00102 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
00103 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
00104 GrayImage* result = new GrayImage(height,width);
00105 uint32 *buf = new uint32[width*height];
00106 if (TIFFReadRGBAImage(tif, width, height, buf, 0))
00107 {
00108 uint32 *p;
00109 int q,c;
00110 for (p=buf+width*(height-1),q=0;p >= buf;p-=width, q++)
00111 for (c=width-1; c>=0; c--)
00112 result->SetLuminance(c,q,(unsigned char)(((65536*30/100)*TIFFGetR(p[c])+(65536*59/100)*TIFFGetG(p[c])
00113 +(65536*11/100)*TIFFGetB(p[c]))/65536));
00114 }
00115 else
00116 {
00117 delete result;
00118 result = 0;
00119 }
00120 delete [] buf;
00121 TIFFClose(tif);
00122 return result;
00123 }
00124 return 0;
00125 }
00126
00127 void GrayImage::WriteToTiff(const char* filename)
00128 {
00129 TIFF *tif;
00130 if ((tif = TIFFOpen(filename, "w")) != NULL)
00131 {
00132 int y;
00133
00134 TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 0);
00135 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, GetWidth());
00136 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, GetHeight());
00137 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
00138 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
00139 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
00140 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
00141 TIFFSetField(tif, TIFFTAG_DOCUMENTNAME, filename);
00142 TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, "Created With My Personal Painter Application :-)");
00143 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
00144 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 64);
00145 TIFFSetField(tif, TIFFTAG_XRESOLUTION, 75.0);
00146 TIFFSetField(tif, TIFFTAG_YRESOLUTION, 75.0);
00147 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
00148 TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
00149 unsigned char *data = *(luminancedata->GetRawData());
00150 for (y=0; y<GetHeight(); y++)
00151 if (TIFFWriteScanline(tif, data+y*GetWidth(), y, 0) < 0)
00152 break;
00153 if (y < GetHeight())
00154 cerr << "Error while writing to TIFF file." << endl;
00155 TIFFClose(tif);
00156 }
00157 else
00158 cerr << "Couldn't open `" << filename << "' for writing." << endl;
00159 }
00160