#if wxUSE_IMAGE
+#include "wx/image.h"
+
#ifndef WX_PRECOMP
#include "wx/log.h"
- #include "wx/app.h"
#include "wx/hash.h"
#include "wx/utils.h"
+ #include "wx/math.h"
+ #include "wx/module.h"
+ #include "wx/palette.h"
+ #include "wx/intl.h"
#endif
-#include "wx/image.h"
-#include "wx/bitmap.h"
-#include "wx/debug.h"
#include "wx/filefn.h"
#include "wx/wfstream.h"
-#include "wx/intl.h"
-#include "wx/module.h"
-#include "wx/math.h"
#if wxUSE_XPM
#include "wx/xpmdecod.h"
// For memcpy
#include <string.h>
+// make the code compile with either wxFile*Stream or wxFFile*Stream:
+#define HAS_FILE_STREAMS (wxUSE_STREAMS && (wxUSE_FILE || wxUSE_FFILE))
+
+#if HAS_FILE_STREAMS
+ #if wxUSE_FILE
+ typedef wxFileInputStream wxImageFileInputStream;
+ typedef wxFileOutputStream wxImageFileOutputStream;
+ #elif wxUSE_FFILE
+ typedef wxFFileInputStream wxImageFileInputStream;
+ typedef wxFFileOutputStream wxImageFileOutputStream;
+ #endif // wxUSE_FILE/wxUSE_FFILE
+#endif // HAS_FILE_STREAMS
+
//-----------------------------------------------------------------------------
// wxImage
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
-#define M_IMGDATA ((wxImageRefData *)m_refData)
+#define M_IMGDATA wx_static_cast(wxImageRefData*, m_refData)
IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject)
UnRef();
}
-wxImage wxImage::Copy() const
+wxObjectRefData* wxImage::CreateRefData() const
{
- wxImage image;
-
- wxCHECK_MSG( Ok(), image, wxT("invalid image") );
-
- image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false );
-
- unsigned char *data = image.GetData();
-
- wxCHECK_MSG( data, image, wxT("unable to create image") );
-
- image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
- image.SetMask( M_IMGDATA->m_hasMask );
-
- memcpy( data, GetData(), M_IMGDATA->m_width*M_IMGDATA->m_height*3 );
+ return new wxImageRefData;
+}
- wxImageRefData *imgData = (wxImageRefData *)image.m_refData;
+wxObjectRefData* wxImage::CloneRefData(const wxObjectRefData* that) const
+{
+ const wxImageRefData* refData = wx_static_cast(const wxImageRefData*, that);
+ wxCHECK_MSG(refData->m_ok, NULL, wxT("invalid image") );
- // also copy the alpha channel
- if (HasAlpha())
+ wxImageRefData* refData_new = new wxImageRefData;
+ refData_new->m_width = refData->m_width;
+ refData_new->m_height = refData->m_height;
+ refData_new->m_maskRed = refData->m_maskRed;
+ refData_new->m_maskGreen = refData->m_maskGreen;
+ refData_new->m_maskBlue = refData->m_maskBlue;
+ refData_new->m_hasMask = refData->m_hasMask;
+ refData_new->m_ok = true;
+ unsigned size = unsigned(refData->m_width) * unsigned(refData->m_height);
+ if (refData->m_alpha != NULL)
{
- image.SetAlpha();
- unsigned char* alpha = image.GetAlpha();
- memcpy( alpha, GetAlpha(), M_IMGDATA->m_width*M_IMGDATA->m_height );
+ refData_new->m_alpha = (unsigned char*)malloc(size);
+ memcpy(refData_new->m_alpha, refData->m_alpha, size);
}
+ size *= 3;
+ refData_new->m_data = (unsigned char*)malloc(size);
+ memcpy(refData_new->m_data, refData->m_data, size);
+#if wxUSE_PALETTE
+ refData_new->m_palette = refData->m_palette;
+#endif
+ refData_new->m_optionNames = refData->m_optionNames;
+ refData_new->m_optionValues = refData->m_optionValues;
+ return refData_new;
+}
- // also copy the image options
- imgData->m_optionNames = M_IMGDATA->m_optionNames;
- imgData->m_optionValues = M_IMGDATA->m_optionValues;
+wxImage wxImage::Copy() const
+{
+ wxImage image;
+
+ wxCHECK_MSG( Ok(), image, wxT("invalid image") );
+
+ image.m_refData = CloneRefData(m_refData);
return image;
}
wxImage wxImage::ShrinkBy( int xFactor , int yFactor ) const
{
if( xFactor == 1 && yFactor == 1 )
- return Copy() ;
+ return *this;
wxImage image;
return image;
}
-wxImage wxImage::Scale( int width, int height ) const
+wxImage wxImage::Scale( int width, int height, int quality ) const
{
wxImage image;
wxCHECK_MSG( (old_height > 0) && (old_width > 0), image,
wxT("invalid old image size") );
- if ( old_width % width == 0 && old_width >= width &&
- old_height % height == 0 && old_height >= height )
+ // If the image's new width and height are the same as the original, no need to waste time or CPU cycles
+ if(old_width == width && old_height == height)
+ return *this;
+
+ // Scale the image (...or more appropriately, resample the image) using either the high-quality or normal method as specified
+ if(quality == wxIMAGE_QUALITY_HIGH)
{
- return ShrinkBy( old_width / width , old_height / height ) ;
+ // We need to check whether we are downsampling or upsampling the image
+ if(width < old_width && height < old_height)
+ {
+ // Downsample the image using the box averaging method for best results
+ image = ResampleBox(width, height);
+ }
+ else
+ {
+ // For upsampling or other random/wierd image dimensions we'll use a bicubic b-spline scaling method
+ image = ResampleBicubic(width, height);
+ }
}
- image.Create( width, height, false );
+ else // Default scaling method == simple pixel replication
+ {
+ if ( old_width % width == 0 && old_width >= width &&
+ old_height % height == 0 && old_height >= height )
+ {
+ return ShrinkBy( old_width / width , old_height / height ) ;
+ }
+ image.Create( width, height, false );
- unsigned char *data = image.GetData();
+ unsigned char *data = image.GetData();
- wxCHECK_MSG( data, image, wxT("unable to create image") );
+ wxCHECK_MSG( data, image, wxT("unable to create image") );
- unsigned char *source_data = M_IMGDATA->m_data;
- unsigned char *target_data = data;
- unsigned char *source_alpha = 0 ;
- unsigned char *target_alpha = 0 ;
+ unsigned char *source_data = M_IMGDATA->m_data;
+ unsigned char *target_data = data;
+ unsigned char *source_alpha = 0 ;
+ unsigned char *target_alpha = 0 ;
- if (M_IMGDATA->m_hasMask)
- {
- image.SetMaskColour( M_IMGDATA->m_maskRed,
- M_IMGDATA->m_maskGreen,
- M_IMGDATA->m_maskBlue );
- }
- else
- {
- source_alpha = M_IMGDATA->m_alpha ;
- if ( source_alpha )
+ if (M_IMGDATA->m_hasMask)
{
- image.SetAlpha() ;
- target_alpha = image.GetAlpha() ;
+ image.SetMaskColour( M_IMGDATA->m_maskRed,
+ M_IMGDATA->m_maskGreen,
+ M_IMGDATA->m_maskBlue );
+ }
+ else
+ {
+ source_alpha = M_IMGDATA->m_alpha ;
+ if ( source_alpha )
+ {
+ image.SetAlpha() ;
+ target_alpha = image.GetAlpha() ;
+ }
}
- }
- long x_delta = (old_width<<16) / width;
- long y_delta = (old_height<<16) / height;
+ long x_delta = (old_width<<16) / width;
+ long y_delta = (old_height<<16) / height;
- unsigned char* dest_pixel = target_data;
+ unsigned char* dest_pixel = target_data;
- long y = 0;
- for ( long j = 0; j < height; j++ )
+ long y = 0;
+ for ( long j = 0; j < height; j++ )
{
- unsigned char* src_line = &source_data[(y>>16)*old_width*3];
- unsigned char* src_alpha_line = source_alpha ? &source_alpha[(y>>16)*old_width] : 0 ;
+ unsigned char* src_line = &source_data[(y>>16)*old_width*3];
+ unsigned char* src_alpha_line = source_alpha ? &source_alpha[(y>>16)*old_width] : 0 ;
- long x = 0;
- for ( long i = 0; i < width; i++ )
- {
- unsigned char* src_pixel = &src_line[(x>>16)*3];
- unsigned char* src_alpha_pixel = source_alpha ? &src_alpha_line[(x>>16)] : 0 ;
- dest_pixel[0] = src_pixel[0];
- dest_pixel[1] = src_pixel[1];
- dest_pixel[2] = src_pixel[2];
- dest_pixel += 3;
- if ( source_alpha )
- *(target_alpha++) = *src_alpha_pixel ;
- x += x_delta;
- }
+ long x = 0;
+ for ( long i = 0; i < width; i++ )
+ {
+ unsigned char* src_pixel = &src_line[(x>>16)*3];
+ unsigned char* src_alpha_pixel = source_alpha ? &src_alpha_line[(x>>16)] : 0 ;
+ dest_pixel[0] = src_pixel[0];
+ dest_pixel[1] = src_pixel[1];
+ dest_pixel[2] = src_pixel[2];
+ dest_pixel += 3;
+ if ( source_alpha )
+ *(target_alpha++) = *src_alpha_pixel ;
+ x += x_delta;
+ }
- y += y_delta;
+ y += y_delta;
+ }
}
// In case this is a cursor, make sure the hotspot is scaled accordingly:
return image;
}
+wxImage wxImage::ResampleBox(int width, int height) const
+{
+ // This function implements a simple pre-blur/box averaging method for downsampling that gives reasonably smooth results
+ // To scale the image down we will need to gather a grid of pixels of the size of the scale factor in each direction
+ // and then do an averaging of the pixels.
+
+ wxImage ret_image(width, height, false);
+
+ double scale_factor_x = double(M_IMGDATA->m_width) / width;
+ double scale_factor_y = double(M_IMGDATA->m_height) / height;
+
+ // If we want good-looking results we need to pre-blur the image a bit first
+ wxImage src_image(*this);
+ src_image = src_image.BlurHorizontal(scale_factor_x / 2);
+ src_image = src_image.BlurVertical(scale_factor_y / 2);
+
+ unsigned char* src_data = src_image.GetData();
+ unsigned char* src_alpha = src_image.GetAlpha();
+ unsigned char* dst_data = ret_image.GetData();
+ unsigned char* dst_alpha = NULL;
+
+ if(src_alpha)
+ {
+ ret_image.SetAlpha();
+ dst_alpha = ret_image.GetAlpha();
+ }
+
+ int x, y, i, j;
+ int averaged_pixels, src_pixel_index, src_x, src_y;
+ double sum_r, sum_g, sum_b, sum_a;
+
+ for(y = 0; y < height; y++) // Destination image - Y direction
+ {
+ // Source pixel in the Y direction
+ src_y = y * scale_factor_y;
+
+ for(x = 0; x < width; x++) // Destination image - X direction
+ {
+ // Source pixel in the X direction
+ src_x = x * scale_factor_x;
+
+ // Box of pixels to average
+ averaged_pixels = 0;
+ sum_r = sum_g = sum_b = sum_a = 0.0;
+
+ for(j = src_y - scale_factor_y / 2 + 1; j <= int(src_y + scale_factor_y / 2); j++) // Y direction
+ {
+ // We don't care to average pixels that don't exist (edges)
+ if(j < 0 || j > M_IMGDATA->m_height)
+ continue;
+
+ for(i = src_x - scale_factor_x / 2 + 1; i <= int(src_x + scale_factor_x / 2); i++) // X direction
+ {
+ // Don't average edge pixels
+ if(i < 0 || i > M_IMGDATA->m_width)
+ continue;
+
+ // Calculate the actual index in our source pixels
+ src_pixel_index = src_y * M_IMGDATA->m_width + src_x;
+
+ sum_r += src_data[src_pixel_index * 3 + 0];
+ sum_g += src_data[src_pixel_index * 3 + 1];
+ sum_b += src_data[src_pixel_index * 3 + 2];
+ if(src_alpha)
+ sum_a += src_alpha[src_pixel_index];
+
+ averaged_pixels++;
+ }
+ }
+
+ // Calculate the average from the sum and number of averaged pixels
+ dst_data[0] = int(sum_r / averaged_pixels);
+ dst_data[1] = int(sum_g / averaged_pixels);
+ dst_data[2] = int(sum_b / averaged_pixels);
+ dst_data += 3;
+ if(src_alpha)
+ *dst_alpha++ = sum_a / averaged_pixels;
+ }
+ }
+
+ return ret_image;
+}
+
+// The following two local functions are for the B-spline weighting of the bicubic sampling algorithm
+static inline double spline_cube(double value)
+{
+ return value <= 0.0 ? 0.0 : value * value * value;
+}
+
+static inline double spline_weight(double value)
+{
+ return (spline_cube(value + 2) - 4 * spline_cube(value + 1) + 6 * spline_cube(value) - 4 * spline_cube(value - 1)) / 6;
+}
+
+// This is the bicubic resampling algorithm
+wxImage wxImage::ResampleBicubic(int width, int height) const
+{
+ // This function implements a Bicubic B-Spline algorithm for resampling. This method is certainly a little slower than wxImage's default
+ // pixel replication method, however for most reasonably sized images not being upsampled too much on a fairly average CPU this
+ // difference is hardly noticeable and the results are far more pleasing to look at.
+ //
+ // This particular bicubic algorithm does pixel weighting according to a B-Spline that basically implements a Gaussian bell-like
+ // weighting kernel. Because of this method the results may appear a bit blurry when upsampling by large factors. This is basically
+ // because a slight gaussian blur is being performed to get the smooth look of the upsampled image.
+
+ // Edge pixels: 3-4 possible solutions
+ // - (Wrap/tile) Wrap the image, take the color value from the opposite side of the image.
+ // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n), where n is nonpositive, will have the value of (2, 1).
+ // - (Ignore) Simply ignore the edge pixels and apply the kernel only to pixels which do have all neighbours.
+ // - (Clamp) Choose the nearest pixel along the border. This takes the border pixels and extends them out to infinity.
+ //
+ // NOTE: below the y_offset and x_offset variables are being set for edge pixels using the "Mirror" method mentioned above
+
+ wxImage ret_image;
+
+ ret_image.Create(width, height, false);
+
+ unsigned char* src_data = M_IMGDATA->m_data;
+ unsigned char* src_alpha = M_IMGDATA->m_alpha;
+ unsigned char* dst_data = ret_image.GetData();
+ unsigned char* dst_alpha = NULL;
+
+ if(src_alpha)
+ {
+ ret_image.SetAlpha();
+ dst_alpha = ret_image.GetAlpha();
+ }
+
+ int k, i;
+ double srcpixx, srcpixy, dx, dy;
+ int dstx, dsty;
+ double sum_r = 0, sum_g = 0, sum_b = 0, sum_a = 0; // Sums for each color channel
+ int x_offset = 0, y_offset = 0;
+ double pixel_weight;
+ long src_pixel_index;
+
+ for(dsty = 0; dsty < height; dsty++)
+ {
+ // We need to calculate the source pixel to interpolate from - Y-axis
+ srcpixy = double(dsty) * M_IMGDATA->m_height / height;
+ dy = srcpixy - (int)srcpixy;
+
+ for(dstx = 0; dstx < width; dstx++)
+ {
+ // X-axis of pixel to interpolate from
+ srcpixx = double(dstx) * M_IMGDATA->m_width / width;
+ dx = srcpixx - (int)srcpixx;
+
+ // Clear all the RGBA sum values
+ sum_r = sum_g = sum_b = sum_a = 0;
+
+ // Here we actually determine the RGBA values for the destination pixel
+ for(k = -1; k <= 2; k++)
+ {
+ // Y offset
+ y_offset = srcpixy + double(k) < 0.0 ? 0 : (srcpixy + double(k) >= M_IMGDATA->m_height ? M_IMGDATA->m_height - 1 : srcpixy + k);
+
+ // Loop across the X axis
+ for(i = -1; i <= 2; i++)
+ {
+ // X offset
+ x_offset = srcpixx + double(i) < 0.0 ? 0 : (srcpixx + double(i) >= M_IMGDATA->m_width ? M_IMGDATA->m_width - 1 : srcpixx + i);
+
+ // Calculate the exact position where the source data should be pulled from based on the x_offset and y_offset
+ src_pixel_index = (y_offset * M_IMGDATA->m_width) + x_offset;
+
+ // Calculate the weight for the specified pixel according to the bicubic b-spline kernel we're using for interpolation
+ pixel_weight = spline_weight(double(i) - dx) * spline_weight(double(k) - dy);
+
+ // Create a sum of all velues for each color channel adjusted for the pixel's calculated weight
+ sum_r += double(src_data[src_pixel_index * 3 + 0]) * pixel_weight;
+ sum_g += double(src_data[src_pixel_index * 3 + 1]) * pixel_weight;
+ sum_b += double(src_data[src_pixel_index * 3 + 2]) * pixel_weight;
+ if(src_alpha)
+ sum_a += double(src_alpha[src_pixel_index]) * pixel_weight;
+ }
+ }
+
+ // Put the data into the destination image. The summed values are of double data type and are rounded here for accuracy
+ dst_data[0] = int(sum_r + 0.5);
+ dst_data[1] = int(sum_g + 0.5);
+ dst_data[2] = int(sum_b + 0.5);
+ dst_data += 3;
+
+ if(src_alpha)
+ *dst_alpha++ = sum_a;
+ }
+ }
+
+ return ret_image;
+}
+
+// Blur in the horizontal direction
+wxImage wxImage::BlurHorizontal(int blurRadius)
+{
+ wxImage ret_image;
+ ret_image.Create(M_IMGDATA->m_width, M_IMGDATA->m_height, false);
+
+ unsigned char* src_data = M_IMGDATA->m_data;
+ unsigned char* dst_data = ret_image.GetData();
+ unsigned char* src_alpha = M_IMGDATA->m_alpha;
+ unsigned char* dst_alpha = NULL;
+
+ // Check for a mask or alpha
+ if(M_IMGDATA->m_hasMask)
+ ret_image.SetMaskColour(M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue);
+ else
+ if(src_alpha)
+ {
+ ret_image.SetAlpha();
+ dst_alpha = ret_image.GetAlpha();
+ }
+
+ // Variables used in the blurring algorithm
+ int x, y;
+ int kernel_x;
+ long sum_r, sum_g, sum_b, sum_a;
+ long pixel_idx;
+
+ // Horizontal blurring algorithm - average all pixels in the specified blur radius in the X or horizontal direction
+ for(y = 0; y < M_IMGDATA->m_height; y++)
+ {
+ sum_r = sum_g = sum_b = sum_a = 0;
+
+ // Calculate the average of all pixels in the blur radius for the first pixel of the row
+ for(kernel_x = -blurRadius; kernel_x <= blurRadius; kernel_x++)
+ {
+ // To deal with the pixels at the start of a row so it's not grabbing GOK values from memory at negative indices of the image's data or grabbing from the previous row
+ if(kernel_x < 0)
+ pixel_idx = y * M_IMGDATA->m_width;
+ else
+ pixel_idx = kernel_x + y * M_IMGDATA->m_width;
+
+ sum_r += src_data[pixel_idx * 3 + 0];
+ sum_g += src_data[pixel_idx * 3 + 1];
+ sum_b += src_data[pixel_idx * 3 + 2];
+ sum_a += src_alpha ? src_alpha[pixel_idx] : 0;
+ }
+ dst_data[y * M_IMGDATA->m_width * 3 + 0] = sum_r / (blurRadius * 2 + 1);
+ dst_data[y * M_IMGDATA->m_width * 3 + 1] = sum_g / (blurRadius * 2 + 1);
+ dst_data[y * M_IMGDATA->m_width * 3 + 2] = sum_b / (blurRadius * 2 + 1);
+ if(src_alpha)
+ dst_alpha[y * M_IMGDATA->m_width] = sum_a / (blurRadius * 2 + 1);
+
+ // Now average the values of the rest of the pixels by just moving the blur radius box along the row
+ for(x = 1; x < M_IMGDATA->m_width; x++)
+ {
+ // Take care of edge pixels on the left edge by essentially duplicating the edge pixel
+ if(x - blurRadius - 1 < 0)
+ pixel_idx = y * M_IMGDATA->m_width;
+ else
+ pixel_idx = (x - blurRadius - 1) + y * M_IMGDATA->m_width;
+
+ // Subtract the value of the pixel at the left side of the blur radius box
+ sum_r -= src_data[pixel_idx * 3 + 0];
+ sum_g -= src_data[pixel_idx * 3 + 1];
+ sum_b -= src_data[pixel_idx * 3 + 2];
+ sum_a -= src_alpha ? src_alpha[pixel_idx] : 0;
+
+ // Take care of edge pixels on the right edge
+ if(x + blurRadius > M_IMGDATA->m_width - 1)
+ pixel_idx = M_IMGDATA->m_width - 1 + y * M_IMGDATA->m_width;
+ else
+ pixel_idx = x + blurRadius + y * M_IMGDATA->m_width;
+
+ // Add the value of the pixel being added to the end of our box
+ sum_r += src_data[pixel_idx * 3 + 0];
+ sum_g += src_data[pixel_idx * 3 + 1];
+ sum_b += src_data[pixel_idx * 3 + 2];
+ sum_a += src_alpha ? src_alpha[pixel_idx] : 0;
+
+ // Save off the averaged data
+ dst_data[x * 3 + y * M_IMGDATA->m_width * 3 + 0] = sum_r / (blurRadius * 2 + 1);
+ dst_data[x * 3 + y * M_IMGDATA->m_width * 3 + 1] = sum_g / (blurRadius * 2 + 1);
+ dst_data[x * 3 + y * M_IMGDATA->m_width * 3 + 2] = sum_b / (blurRadius * 2 + 1);
+ if(src_alpha)
+ dst_alpha[x + y * M_IMGDATA->m_width] = sum_a / (blurRadius * 2 + 1);
+ }
+ }
+
+ return ret_image;
+}
+
+// Blur in the vertical direction
+wxImage wxImage::BlurVertical(int blurRadius)
+{
+ wxImage ret_image;
+ ret_image.Create(M_IMGDATA->m_width, M_IMGDATA->m_height, false);
+
+ unsigned char* src_data = M_IMGDATA->m_data;
+ unsigned char* dst_data = ret_image.GetData();
+ unsigned char* src_alpha = M_IMGDATA->m_alpha;
+ unsigned char* dst_alpha = NULL;
+
+ // Check for a mask or alpha
+ if(M_IMGDATA->m_hasMask)
+ ret_image.SetMaskColour(M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue);
+ else
+ if(src_alpha)
+ {
+ ret_image.SetAlpha();
+ dst_alpha = ret_image.GetAlpha();
+ }
+
+ // Variables used in the blurring algorithm
+ int x, y;
+ int kernel_y;
+ long sum_r, sum_g, sum_b, sum_a;
+ long pixel_idx;
+
+ // Vertical blurring algorithm - same as horizontal but switched the opposite direction
+ for(x = 0; x < M_IMGDATA->m_width; x++)
+ {
+ sum_r = sum_g = sum_b = sum_a = 0;
+
+ // Calculate the average of all pixels in our blur radius box for the first pixel of the column
+ for(kernel_y = -blurRadius; kernel_y <= blurRadius; kernel_y++)
+ {
+ // To deal with the pixels at the start of a column so it's not grabbing GOK values from memory at negative indices of the image's data or grabbing from the previous column
+ if(kernel_y < 0)
+ pixel_idx = x;
+ else
+ pixel_idx = x + kernel_y * M_IMGDATA->m_width;
+
+ sum_r += src_data[pixel_idx * 3 + 0];
+ sum_g += src_data[pixel_idx * 3 + 1];
+ sum_b += src_data[pixel_idx * 3 + 2];
+ sum_a += src_alpha ? src_alpha[pixel_idx] : 0;
+ }
+ dst_data[x * 3 + 0] = sum_r / (blurRadius * 2 + 1);
+ dst_data[x * 3 + 1] = sum_g / (blurRadius * 2 + 1);
+ dst_data[x * 3 + 2] = sum_b / (blurRadius * 2 + 1);
+ if(src_alpha)
+ dst_alpha[x] = sum_a / (blurRadius * 2 + 1);
+
+ // Now average the values of the rest of the pixels by just moving the box along the column from top to bottom
+ for(y = 1; y < M_IMGDATA->m_height; y++)
+ {
+ // Take care of pixels that would be beyond the top edge by duplicating the top edge pixel for the column
+ if(y - blurRadius - 1 < 0)
+ pixel_idx = x;
+ else
+ pixel_idx = x + (y - blurRadius - 1) * M_IMGDATA->m_width;
+
+ // Subtract the value of the pixel at the top of our blur radius box
+ sum_r -= src_data[pixel_idx * 3 + 0];
+ sum_g -= src_data[pixel_idx * 3 + 1];
+ sum_b -= src_data[pixel_idx * 3 + 2];
+ sum_a -= src_alpha ? src_alpha[pixel_idx] : 0;
+
+ // Take care of the pixels that would be beyond the bottom edge of the image similar to the top edge
+ if(y + blurRadius > M_IMGDATA->m_height - 1)
+ pixel_idx = x + (M_IMGDATA->m_height - 1) * M_IMGDATA->m_width;
+ else
+ pixel_idx = x + (blurRadius + y) * M_IMGDATA->m_width;
+
+ // Add the value of the pixel being added to the end of our box
+ sum_r += src_data[pixel_idx * 3 + 0];
+ sum_g += src_data[pixel_idx * 3 + 1];
+ sum_b += src_data[pixel_idx * 3 + 2];
+ sum_a += src_alpha ? src_alpha[pixel_idx] : 0;
+
+ // Save off the averaged data
+ dst_data[(x + y * M_IMGDATA->m_width) * 3 + 0] = sum_r / (blurRadius * 2 + 1);
+ dst_data[(x + y * M_IMGDATA->m_width) * 3 + 1] = sum_g / (blurRadius * 2 + 1);
+ dst_data[(x + y * M_IMGDATA->m_width) * 3 + 2] = sum_b / (blurRadius * 2 + 1);
+ if(src_alpha)
+ dst_alpha[x + y * M_IMGDATA->m_width] = sum_a / (blurRadius * 2 + 1);
+ }
+ }
+
+ return ret_image;
+}
+
+// The new blur function
+wxImage wxImage::Blur(int blurRadius)
+{
+ wxImage ret_image;
+ ret_image.Create(M_IMGDATA->m_width, M_IMGDATA->m_height, false);
+
+ // Blur the image in each direction
+ ret_image = BlurHorizontal(blurRadius);
+ ret_image = ret_image.BlurVertical(blurRadius);
+
+ return ret_image;
+}
+
wxImage wxImage::Rotate90( bool clockwise ) const
{
wxImage image;
wxCHECK_RET( Ok(), wxT("invalid image") );
wxCHECK_RET( image.Ok(), wxT("invalid image") );
+ AllocExclusive();
+
int xx = 0;
int yy = 0;
int width = image.GetWidth();
{
wxCHECK_RET( Ok(), wxT("invalid image") );
+ AllocExclusive();
+
unsigned char *data = GetData();
const int w = GetWidth();
}
}
+ // copy the alpha channel, if any
+ if (HasAlpha())
+ {
+ const size_t alphaSize = GetWidth() * GetHeight();
+ unsigned char *alpha = (unsigned char*)malloc(alphaSize);
+ memcpy(alpha, GetAlpha(), alphaSize);
+ image.InitAlpha();
+ image.SetAlpha(alpha);
+ }
+
return image;
}
long pos = XYToIndex(x, y);
wxCHECK_RET( pos != -1, wxT("invalid image coordinates") );
+ AllocExclusive();
+
pos *= 3;
M_IMGDATA->m_data[ pos ] = r;
{
wxCHECK_RET( Ok(), wxT("invalid image") );
+ AllocExclusive();
+
wxRect rect(rect_);
wxRect imageRect(0, 0, GetWidth(), GetHeight());
if ( rect == wxRect() )
}
else
{
- wxCHECK_RET( imageRect.Inside(rect.GetTopLeft()) &&
- imageRect.Inside(rect.GetBottomRight()),
+ wxCHECK_RET( imageRect.Contains(rect.GetTopLeft()) &&
+ imageRect.Contains(rect.GetBottomRight()),
wxT("invalid bounding rectangle") );
}
long pos = XYToIndex(x, y);
wxCHECK_RET( pos != -1, wxT("invalid image coordinates") );
+ AllocExclusive();
+
M_IMGDATA->m_alpha[pos] = alpha;
}
{
wxCHECK_RET( Ok(), wxT("invalid image") );
+ AllocExclusive();
+
if ( !alpha )
{
alpha = (unsigned char *)malloc(M_IMGDATA->m_width*M_IMGDATA->m_height);
{
wxCHECK_RET( Ok(), wxT("invalid image") );
+ AllocExclusive();
+
M_IMGDATA->m_maskRed = r;
M_IMGDATA->m_maskGreen = g;
M_IMGDATA->m_maskBlue = b;
{
wxCHECK_RET( Ok(), wxT("invalid image") );
+ AllocExclusive();
+
M_IMGDATA->m_hasMask = mask;
}
return false ;
}
+ AllocExclusive();
+
unsigned char *imgdata = GetData();
unsigned char *maskdata = mask.GetData();
return false;
}
+ AllocExclusive();
+
SetMask(true);
SetMaskColour(mr, mg, mb);
{
wxCHECK_RET( Ok(), wxT("invalid image") );
+ AllocExclusive();
+
M_IMGDATA->m_palette = palette;
}
{
wxCHECK_RET( Ok(), wxT("invalid image") );
+ AllocExclusive();
+
int idx = M_IMGDATA->m_optionNames.Index(name, false);
if (idx == wxNOT_FOUND)
{
// image I/O
// ----------------------------------------------------------------------------
-bool wxImage::LoadFile( const wxString& filename, long type, int index )
+bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
+ long WXUNUSED_UNLESS_STREAMS(type),
+ int WXUNUSED_UNLESS_STREAMS(index) )
{
-#if wxUSE_STREAMS
+#if HAS_FILE_STREAMS
if (wxFileExists(filename))
{
- wxFileInputStream stream(filename);
+ wxImageFileInputStream stream(filename);
wxBufferedInputStream bstream( stream );
return LoadFile(bstream, type, index);
}
return false;
}
-#else // !wxUSE_STREAMS
+#else // !HAS_FILE_STREAMS
return false;
-#endif // wxUSE_STREAMS
+#endif // HAS_FILE_STREAMS
}
-bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype, int index )
+bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
+ const wxString& WXUNUSED_UNLESS_STREAMS(mimetype),
+ int WXUNUSED_UNLESS_STREAMS(index) )
{
-#if wxUSE_STREAMS
+#if HAS_FILE_STREAMS
if (wxFileExists(filename))
{
- wxFileInputStream stream(filename);
+ wxImageFileInputStream stream(filename);
wxBufferedInputStream bstream( stream );
return LoadFile(bstream, mimetype, index);
}
return false;
}
-#else // !wxUSE_STREAMS
+#else // !HAS_FILE_STREAMS
return false;
-#endif // wxUSE_STREAMS
+#endif // HAS_FILE_STREAMS
}
return false;
}
-bool wxImage::SaveFile( const wxString& filename, int type ) const
+bool wxImage::SaveFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
+ int WXUNUSED_UNLESS_STREAMS(type) ) const
{
-#if wxUSE_STREAMS
+#if HAS_FILE_STREAMS
wxCHECK_MSG( Ok(), false, wxT("invalid image") );
((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
- wxFileOutputStream stream(filename);
+ wxImageFileOutputStream stream(filename);
if ( stream.IsOk() )
{
wxBufferedOutputStream bstream( stream );
return SaveFile(bstream, type);
}
-#endif // wxUSE_STREAMS
+#endif // HAS_FILE_STREAMS
return false;
}
-bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype ) const
+bool wxImage::SaveFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
+ const wxString& WXUNUSED_UNLESS_STREAMS(mimetype) ) const
{
-#if wxUSE_STREAMS
+#if HAS_FILE_STREAMS
wxCHECK_MSG( Ok(), false, wxT("invalid image") );
((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
- wxFileOutputStream stream(filename);
+ wxImageFileOutputStream stream(filename);
if ( stream.IsOk() )
{
wxBufferedOutputStream bstream( stream );
return SaveFile(bstream, mimetype);
}
-#endif // wxUSE_STREAMS
+#endif // HAS_FILE_STREAMS
return false;
}
-bool wxImage::CanRead( const wxString &name )
+bool wxImage::CanRead( const wxString& WXUNUSED_UNLESS_STREAMS(name) )
{
-#if wxUSE_STREAMS
- wxFileInputStream stream(name);
- return CanRead(stream);
+#if HAS_FILE_STREAMS
+ wxImageFileInputStream stream(name);
+ return CanRead(stream);
#else
- return false;
+ return false;
#endif
}
-int wxImage::GetImageCount( const wxString &name, long type )
+int wxImage::GetImageCount( const wxString& WXUNUSED_UNLESS_STREAMS(name),
+ long WXUNUSED_UNLESS_STREAMS(type) )
{
-#if wxUSE_STREAMS
- wxFileInputStream stream(name);
- if (stream.Ok())
- return GetImageCount(stream, type);
+#if HAS_FILE_STREAMS
+ wxImageFileInputStream stream(name);
+ if (stream.Ok())
+ return GetImageCount(stream, type);
#endif
return 0;
if ( !handler )
{
- wxLogWarning(_("No image handler for type %d defined."), type);
+ wxLogWarning(_("No image handler for type %ld defined."), type);
return false;
}
}
else
{
- wxLogError(_("Image file is not of type %d."), type);
+ wxLogError(_("Image file is not of type %ld."), type);
return 0;
}
}
if (handler == 0)
{
- wxLogWarning( _("No image handler for type %d defined."), type );
+ wxLogWarning( _("No image handler for type %ld defined."), type );
return false;
}
if (stream.IsSeekable() && !handler->CanRead(stream))
{
- wxLogError(_("Image file is not of type %d."), type);
+ wxLogError(_("Image file is not of type %ld."), type);
return false;
}
else
*/
void wxImage::RotateHue(double angle)
{
+ AllocExclusive();
+
unsigned char *srcBytePtr;
unsigned char *dstBytePtr;
unsigned long count;
{
if (wxFileExists(name))
{
- wxFileInputStream stream(name);
+ wxImageFileInputStream stream(name);
return CanRead(stream);
}