style/CornerImage.C

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Koen Deforche, Kessel-Lo, Belgium.
00003  *
00004  * See the LICENSE file for terms of use.
00005  */
00006 #include <stdio.h>
00007 #include "gd.h"
00008 
00009 #include <WMemoryResource>
00010 
00011 #include "CornerImage.h"
00012 
00013 CornerImage::CornerImage(Corner c, WColor fg, WColor bg,
00014                          int radius, WContainerWidget *parent)
00015   : WImage(parent),
00016     corner_(c),
00017     fg_(fg),
00018     bg_(bg),
00019     radius_(radius),
00020     resource_(0)
00021 {
00022   compute();
00023 }
00024 
00025 CornerImage::~CornerImage()
00026 {
00027   if (resource_) {
00028     delete resource_;
00029   }
00030 }
00031 
00032 void CornerImage::setRadius(int radius)
00033 {
00034   if (radius != radius_) {
00035     radius_ = radius;
00036     compute();
00037   }
00038 }
00039 
00040 void CornerImage::setForeground(WColor color)
00041 {
00042   if (fg_ != color) {
00043     fg_ = color;
00044     compute();
00045   }
00046 }
00047 
00048 void CornerImage::compute()
00049 {
00050   /* We want an anti-aliased image: oversample twice */
00051   int AA = 2;
00052 
00053   gdImagePtr imBig = gdImageCreate(radius_ * AA, radius_ * AA);
00054 
00055   /* automatically becomes the background color -- gd documentation */
00056   gdImageColorAllocate(imBig, bg_.red(), bg_.green(), bg_.blue());
00057 
00058   int fgColor
00059     = gdImageColorAllocate(imBig, fg_.red(), fg_.green(), fg_.blue());
00060 
00061   int cx, cy;
00062 
00063   if (corner_ & Top)
00064     cy = radius_ * AA - 1;
00065   else
00066     cy = 0;
00067 
00068   if (corner_ & Left)
00069     cx = radius_ * AA - 1;
00070   else
00071     cx = 0;
00072 
00073   gdImageFilledArc(imBig, cx, cy, (radius_*2 - 1) * AA, (radius_*2 - 1) * AA,
00074                    0, 360, fgColor, gdArc);
00075 
00076   /* now create the real image, downsampled by a factor of 2 */
00077   gdImagePtr im = gdImageCreateTrueColor(radius_, radius_);
00078   gdImageCopyResampled(im, imBig, 0, 0, 0, 0, im->sx, im->sy,
00079                        imBig->sx, imBig->sy);
00080 
00081   /* and generate an in-memory png file */
00082   int size;
00083   char *data;
00084   data = (char *) gdImagePngPtr(im, &size);
00085   if (!data) {
00086     return;
00087     /* Error */
00088   }
00089 
00090   std::vector<char> vdata(data, data + size);
00091   if (resource_) {
00092     resource_->setData(vdata);
00093   } else {
00094     /* create and set the memory resource that contains the image */
00095     resource_ = new WMemoryResource("image/png", vdata);
00096     setResource(resource_);
00097   }
00098 
00099   gdFree(data);  
00100 
00101   gdImageDestroy(im);
00102   gdImageDestroy(imBig);
00103 }
00104 

Generated on Sun Jul 1 19:37:23 2007 for Wt by doxygen 1.4.7