Hauptseite   Klassenhierarchie   Auflistung der Dateien  

grayimage.cpp

00001 /**********************************************************************************
00002 * Copyright (c) 2003, Christoph Rueegg <opendev@cdrnet.ch> and Matthias Bader     *
00003 * Partially based on ideas of Tim Weyrich <weyrich@inf.ethz.ch>                   *
00004 * and/or the Swiss Federal Institute of Technology http://www.ethz.ch             *
00005 * All rights reserved.                                                            *
00006 *                                                                                 *
00007 * Project Website: http://www.cdrnet.net/projects/painter/                        *
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 * 1. Redistributions of source code must retain the above copyright notice,       *
00013 * this list of conditions and the following disclaimer.                           *
00014 *                                                                                 *
00015 * 2. Redistributions in binary form must reproduce the above copyright notice,    *
00016 * this list of conditions and the following disclaimer in the documentation       *
00017 * and/or other materials provided with the distribution.                          *
00018 *                                                                                 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"     *
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE       *
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE      *
00022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE        *
00023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR             *
00024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF            *
00025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS        *
00026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN         *
00027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)         *
00028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF          *
00029 * THE POSSIBILITY OF SUCH DAMAGE.                                                 *
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   /* set up OpenGL projection matrix */
00079   glMatrixMode(GL_PROJECTION);
00080   glLoadIdentity();
00081   glOrtho(0, luminancedata->GetWidth(), luminancedata->GetHeight(), 0, -1, 1);
00082   glMatrixMode(GL_MODELVIEW);
00083   /* set up memory alignment */
00084   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00085   glPixelStorei(GL_PACK_ALIGNMENT, 1);
00086   /* clear buffer and draw image */
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 

Erzeugt am Fri Jan 31 15:27:35 2003 für OOP Miniprojekt von doxygen1.3-rc2