Orb/Doxygen/src/image.cpp
changeset 4 468f4c8d3d5b
parent 0 42188c7ea2d9
equal deleted inserted replaced
3:d8fccb2cd802 4:468f4c8d3d5b
     1 /******************************************************************************
     1 /******************************************************************************
     2  *
     2  *
     3  * 
     3  * 
     4  *
     4  *
     5  *
     5  *
     6  * Copyright (C) 1997-2008 by Dimitri van Heesch.
     6  * Copyright (C) 1997-2010 by Dimitri van Heesch.
     7  *
     7  *
     8  * Permission to use, copy, modify, and distribute this software and its
     8  * Permission to use, copy, modify, and distribute this software and its
     9  * documentation under the terms of the GNU General Public License is hereby 
     9  * documentation under the terms of the GNU General Public License is hereby 
    10  * granted. No representations are made about the suitability of this software 
    10  * granted. No representations are made about the suitability of this software 
    11  * for any purpose. It is provided "as is" without express or implied warranty.
    11  * for any purpose. It is provided "as is" without express or implied warranty.
    18 
    18 
    19 #include "qtbc.h"
    19 #include "qtbc.h"
    20 #include "image.h"
    20 #include "image.h"
    21 //#include "gifenc.h"
    21 //#include "gifenc.h"
    22 #include <qfile.h>
    22 #include <qfile.h>
       
    23 #include <math.h>
    23 #include "lodepng.h"
    24 #include "lodepng.h"
       
    25 #include "config.h"
    24 
    26 
    25 typedef unsigned char  Byte;
    27 typedef unsigned char  Byte;
    26 
    28 
    27 struct Color
    29 struct Color
    28 {
    30 {
   207   { 0x00, 0x00, 0x00, 0xff }
   209   { 0x00, 0x00, 0x00, 0xff }
   208 };
   210 };
   209 
   211 
   210 Image::Image(int w,int h)
   212 Image::Image(int w,int h)
   211 {
   213 {
       
   214   static int hue   = Config_getInt("HTML_COLORSTYLE_HUE");
       
   215   static int sat   = Config_getInt("HTML_COLORSTYLE_SAT");
       
   216   static int gamma = Config_getInt("HTML_COLORSTYLE_GAMMA");
       
   217 
       
   218   double red1,green1,blue1;
       
   219   double red2,green2,blue2;
       
   220 
       
   221   ColoredImage::hsl2rgb(hue/360.0,                  // hue
       
   222                         sat/255.0,                  // saturation
       
   223                         pow(235/255.0,gamma/100.0), // luma (gamma corrected)
       
   224                         &red1,&green1,&blue1
       
   225                        );
       
   226 
       
   227   ColoredImage::hsl2rgb(hue/360.0,                  // hue
       
   228                         sat/255.0,                  // saturation
       
   229                         pow(138/255.0,gamma/100.0), // luma (gamma corrected)
       
   230                         &red2,&green2,&blue2
       
   231                        );
       
   232 
       
   233   palette[2].red   = (int)(red1   * 255.0);
       
   234   palette[2].green = (int)(green1 * 255.0);
       
   235   palette[2].blue  = (int)(blue1  * 255.0);
       
   236 
       
   237   palette[3].red   = (int)(red2   * 255.0);
       
   238   palette[3].green = (int)(green2 * 255.0);
       
   239   palette[3].blue  = (int)(blue2  * 255.0);
       
   240 
   212   data = new uchar[w*h];
   241   data = new uchar[w*h];
   213   memset(data,0,w*h);
   242   memset(data,0,w*h);
   214   width = w;
   243   width = w;
   215   height = h;
   244   height = h;
   216 }
   245 }
   391   free(buffer);
   420   free(buffer);
   392   LodePNG_Encoder_cleanup(&encoder);
   421   LodePNG_Encoder_cleanup(&encoder);
   393   return TRUE;
   422   return TRUE;
   394 }
   423 }
   395 
   424 
       
   425 //----------------------------------------------------------------
       
   426 
       
   427 void ColoredImage::hsl2rgb(double h,double s,double l,
       
   428                          double *pRed,double *pGreen,double *pBlue)
       
   429 {
       
   430   double v;
       
   431   double r,g,b;
       
   432 
       
   433   r = l;   // default to gray
       
   434   g = l;
       
   435   b = l;
       
   436   v = (l <= 0.5) ? (l * (1.0 + s)) : (l + s - l * s);
       
   437   if (v > 0)
       
   438   {
       
   439     double m;
       
   440     double sv;
       
   441     int sextant;
       
   442     double fract, vsf, mid1, mid2;
       
   443 
       
   444     m       = l + l - v;
       
   445     sv      = (v - m ) / v;
       
   446     h      *= 6.0;
       
   447     sextant = (int)h;
       
   448     fract   = h - sextant;
       
   449     vsf     = v * sv * fract;
       
   450     mid1    = m + vsf;
       
   451     mid2    = v - vsf;
       
   452     switch (sextant)
       
   453     {
       
   454       case 0:
       
   455         r = v;
       
   456         g = mid1;
       
   457         b = m;
       
   458         break;
       
   459       case 1:
       
   460         r = mid2;
       
   461         g = v;
       
   462         b = m;
       
   463         break;
       
   464       case 2:
       
   465         r = m;
       
   466         g = v;
       
   467         b = mid1;
       
   468         break;
       
   469       case 3:
       
   470         r = m;
       
   471         g = mid2;
       
   472         b = v;
       
   473         break;
       
   474       case 4:
       
   475         r = mid1;
       
   476         g = m;
       
   477         b = v;
       
   478         break;
       
   479       case 5:
       
   480         r = v;
       
   481         g = m;
       
   482         b = mid2;
       
   483         break;
       
   484     }
       
   485   }
       
   486   *pRed   = r;
       
   487   *pGreen = g;
       
   488   *pBlue  = b;
       
   489 }
       
   490 
       
   491 ColoredImage::ColoredImage(int width,int height,
       
   492            const uchar *greyLevels,const uchar *alphaLevels,
       
   493            int saturation,int hue,int gamma)
       
   494 {
       
   495   m_hasAlpha = alphaLevels!=0;
       
   496   m_width    = width;
       
   497   m_height   = height;
       
   498   m_data     = (uchar*)malloc(width*height*4);
       
   499   int i;
       
   500   for (i=0;i<width*height;i++)
       
   501   {
       
   502     uchar r,g,b,a;
       
   503     double red,green,blue;
       
   504     hsl2rgb(hue/360.0,                            // hue
       
   505             saturation/255.0,                     // saturation
       
   506             pow(greyLevels[i]/255.0,gamma/100.0), // luma (gamma corrected)
       
   507             &red,&green,&blue);
       
   508     r = (int)(red  *255.0);
       
   509     g = (int)(green*255.0);
       
   510     b = (int)(blue *255.0);
       
   511     a = alphaLevels ? alphaLevels[i] : 255;
       
   512     m_data[i*4+0]=r;
       
   513     m_data[i*4+1]=g;
       
   514     m_data[i*4+2]=b;
       
   515     m_data[i*4+3]=a;
       
   516   }
       
   517 }
       
   518 
       
   519 ColoredImage::~ColoredImage()
       
   520 {
       
   521   free(m_data);
       
   522 }
       
   523 
       
   524 bool ColoredImage::save(const char *fileName)
       
   525 {
       
   526   uchar *buffer;
       
   527   size_t bufferSize;
       
   528   LodePNG_Encoder encoder;
       
   529   LodePNG_Encoder_init(&encoder);
       
   530   encoder.infoPng.color.colorType = m_hasAlpha ? 6 : 2; // 2=RGB 24 bit, 6=RGBA 32 bit
       
   531   encoder.infoRaw.color.colorType = 6; // 6=RGBA 32 bit
       
   532   LodePNG_encode(&encoder, &buffer, &bufferSize, m_data, m_width, m_height);
       
   533   LodePNG_saveFile(buffer, bufferSize, fileName);
       
   534   LodePNG_Encoder_cleanup(&encoder);
       
   535   free(buffer);
       
   536   return TRUE;
       
   537 }
       
   538 
       
   539