On 07.12.2010, at 23:39, Slick Dealer wrote:

>> On 07.12.2010, at 19:26, Slick Dealer wrote:
>> 
>>> If I could easily resize PNG images using FLTK, I wouldn't be using 
>>> Fl_Help_View.
>> 
>> You can, but it's a low quality resizing algorithm. The key is
>> Fl_Image::copy(int w, int h). You should load the original image
>> and keep it in the background, then use copy() from the original
>> image whenever you need to resize the image to a different size.
>> Don't forget to delete the previous image copy.
>> 
>> This works well as long as the quality is high enough for your
>> use case.
>> 
>> Albrecht
> 
> Albrecht,
> I think I tested  Fl_Image resizing but realized that image rendering is much 
> better if I use Fl_Help_View to display/resize the image. Has anyone had a 
> similar experience?

Image scaling - not perfect but fast and better than just copy(w, h) because it 
does at least a little bit of filtering:

/**
* Scale horizontally by 50%.
*/
void Flmp_Image::half_width()
{
 if (!w() || !h() || !d() || !array) return;
 uncache();
 int dw = w()/2, dh = h(), dd = d(), i, j, k, stride = ld();
 if (stride==0) stride = w()*d();
 uchar *new_array = new uchar[dw*dh*dd];

 uchar *dst = new_array;
 for (i=0; i<dh; i++) {
   const uchar *src = array + i*stride;
   for (j=0; j<dw; j++) {
     for (k=0; k<dd; k++) {
       unsigned short a = src[0];
       unsigned short b = src[dd];
       *dst++ = (a+b)>>1;
       src++;
     }
     src+=dd;
   }
 }

 if (alloc_array) delete[] (uchar *)array;  
 array = new_array;
 alloc_array = 1;
 ld(0);
 d(dd);
 w(dw);
 h(dh);
}

/**
* Scale horizontally by 50%.
*/
void Flmp_Image::half_height()
{
 if (!w() || !h() || !d() || !array) return;
 uncache();
 int dw = w(), dh = h()/2, dd = d(), i, j, k, stride = ld();
 if (stride==0) stride = w()*d();
 uchar *new_array = new uchar[dw*dh*dd];

 uchar *dst = new_array;
 for (i=0; i<dh; i++) {
   const uchar *src = array + 2*i*stride;
   for (j=0; j<dw; j++) {
     for (k=0; k<dd; k++) {
       unsigned short a = src[0];
       unsigned short b = src[stride];
       *dst++ = (a+b)>>1;
       src++;
     }
   }
 }

 if (alloc_array) delete[] (uchar *)array;  
 array = new_array;
 alloc_array = 1;
 ld(0);
 d(dd);
 w(dw);
 h(dh);
}

Fl_Image *Flmp_Image::scale(int W, int H) {
 if (2*W<w() || 2*H<h()) {
   Flmp_Image *img = (Flmp_Image*)copy();
   while (2*W<img->w())
     img->half_width();
   while (2*H<img->h())
     img->half_height();
   Fl_Image *ret = img->copy(W, H);
   delete img;
   return ret;
 }
 return copy(W, H);
}

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to