| 1 |
ghs |
1.1 |
/*
|
| 2 |
|
|
|
| 3 |
|
|
image_ext.cpp: extra functionality for wxImage
|
| 4 |
|
|
Copyright (C) 2006 Gregory Smith
|
| 5 |
|
|
|
| 6 |
|
|
This program is free software; you can redistribute it and/or
|
| 7 |
|
|
modify it under the terms of the GNU General Public License
|
| 8 |
|
|
as published by the Free Software Foundation; either version 2
|
| 9 |
|
|
of the License, or (at your option) any later version.
|
| 10 |
|
|
|
| 11 |
|
|
This program is distributed in the hope that it will be useful,
|
| 12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 14 |
|
|
GNU General Public License for more details.
|
| 15 |
|
|
|
| 16 |
|
|
You should have received a copy of the GNU General Public License
|
| 17 |
|
|
along with this program; if not, write to the Free Software
|
| 18 |
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
| 19 |
|
|
|
| 20 |
|
|
*/
|
| 21 |
|
|
|
| 22 |
|
|
#include "image_ext.h"
|
| 23 |
|
|
|
| 24 |
|
|
void wxImageExt::White()
|
| 25 |
|
|
{
|
| 26 |
|
|
int NumBytes = GetWidth() * GetHeight() * 3;
|
| 27 |
|
|
for (int i = 0; i < NumBytes; i++)
|
| 28 |
|
|
{
|
| 29 |
|
|
GetData()[i] = 0xff;
|
| 30 |
|
|
}
|
| 31 |
|
|
}
|
| 32 |
|
|
|
| 33 |
|
|
void wxImageExt::RemoveAlpha()
|
| 34 |
|
|
{
|
| 35 |
|
|
wxImageExt image;
|
| 36 |
|
|
image.Create(GetWidth(), GetHeight());
|
| 37 |
|
|
memcpy(image.GetData(), GetData(), GetWidth() * GetHeight() * 3);
|
| 38 |
|
|
|
| 39 |
|
|
*this = image;
|
| 40 |
|
|
}
|
| 41 |
|
|
|
| 42 |
|
|
void wxImageExt::MaskToAlpha()
|
| 43 |
|
|
{
|
| 44 |
|
|
wxImageExt image;
|
| 45 |
|
|
image.Create(GetWidth(), GetHeight());
|
| 46 |
|
|
image.InitAlpha();
|
| 47 |
|
|
image.SetMask(false);
|
| 48 |
|
|
|
| 49 |
|
|
memcpy(image.GetData(), GetData(), GetWidth() * GetHeight() * 3);
|
| 50 |
|
|
|
| 51 |
|
|
for (int x = 0; x < GetWidth(); x++)
|
| 52 |
|
|
{
|
| 53 |
|
|
for (int y = 0; y < GetHeight(); y++)
|
| 54 |
|
|
{
|
| 55 |
|
|
if (IsTransparent(x, y))
|
| 56 |
|
|
{
|
| 57 |
|
|
image.GetAlpha()[x + y * GetWidth()] = 0x00;
|
| 58 |
|
|
}
|
| 59 |
|
|
else
|
| 60 |
|
|
{
|
| 61 |
|
|
image.GetAlpha()[x + y * GetWidth()] = 0xff;
|
| 62 |
|
|
}
|
| 63 |
|
|
}
|
| 64 |
|
|
}
|
| 65 |
|
|
|
| 66 |
|
|
*this = image;
|
| 67 |
|
|
}
|
| 68 |
|
|
|
| 69 |
|
|
void wxImageExt::PropRescale(int width, int height)
|
| 70 |
|
|
{
|
| 71 |
|
|
float scale = (float) MAX(width, height) / MAX(GetWidth(), GetHeight());
|
| 72 |
|
|
Rescale(GetWidth() * scale, GetHeight() * scale);
|
| 73 |
|
|
Resize(wxSize(width, height), wxPoint((width - GetWidth()) / 2, (height - GetHeight()) / 2));
|
| 74 |
|
|
}
|
| 75 |
|
|
|
| 76 |
|
|
void wxImageExt::FromAlpha(const wxImageExt& source)
|
| 77 |
|
|
{
|
| 78 |
|
|
Create(source.GetWidth(), source.GetHeight());
|
| 79 |
|
|
int NumPixels = GetWidth() * GetHeight();
|
| 80 |
|
|
for (int i = 0; i < NumPixels; i++)
|
| 81 |
|
|
{
|
| 82 |
|
|
GetData()[i * 3 + 0] = GetData()[i * 3 + 1] = GetData()[i * 3 + 2] = source.GetAlpha()[i];
|
| 83 |
|
|
}
|
| 84 |
|
|
}
|
| 85 |
|
|
|
| 86 |
|
|
void wxImageExt::ToAlpha(wxImageExt& dest) const
|
| 87 |
|
|
{
|
| 88 |
|
|
if (!dest.HasAlpha())
|
| 89 |
|
|
{
|
| 90 |
|
|
dest.InitAlpha();
|
| 91 |
|
|
}
|
| 92 |
|
|
|
| 93 |
|
|
int NumPixels = GetWidth() * GetHeight();
|
| 94 |
|
|
for (int i = 0; i < NumPixels; i++)
|
| 95 |
|
|
{
|
| 96 |
|
|
unsigned int Average = (GetData()[i * 3 + 0] + GetData()[i * 3 + 1] + GetData()[i * 3 + 2]) / 3;
|
| 97 |
|
|
dest.GetAlpha()[i] = Average & 0xff;
|
| 98 |
|
|
}
|
| 99 |
|
|
}
|
| 100 |
|
|
|
| 101 |
|
|
void wxImageExt::MakeOpacTypeTwo()
|
| 102 |
|
|
{
|
| 103 |
|
|
wxImageExt image;
|
| 104 |
|
|
image.Create(GetWidth(), GetHeight());
|
| 105 |
|
|
int NumPixels = GetWidth() * GetHeight();
|
| 106 |
|
|
for (int i = 0; i < NumPixels; i++)
|
| 107 |
|
|
{
|
| 108 |
|
|
unsigned int Average = (GetData()[i * 3 + 0] + GetData()[i * 3 + 1] + GetData()[i * 3 + 2]) / 3;
|
| 109 |
|
|
image.GetData()[i * 3 + 0] = image.GetData()[i * 3 + 1] = image.GetData()[i * 3 + 2] = Average & 0xff;
|
| 110 |
|
|
}
|
| 111 |
|
|
|
| 112 |
|
|
*this = image;
|
| 113 |
|
|
}
|
| 114 |
|
|
|
| 115 |
|
|
void wxImageExt::MakeOpacTypeThree()
|
| 116 |
|
|
{
|
| 117 |
|
|
wxImageExt image;
|
| 118 |
|
|
image.Create(GetWidth(), GetHeight());
|
| 119 |
|
|
int NumPixels = GetWidth() * GetHeight();
|
| 120 |
|
|
for (int i = 0; i < NumPixels; i++)
|
| 121 |
|
|
{
|
| 122 |
|
|
unsigned char Max = MAX(GetData()[i * 3 + 0], MAX(GetData()[i * 3 + 1], GetData()[i * 3 + 2]));
|
| 123 |
|
|
image.GetData()[i * 3 + 0] = image.GetData()[i * 3 + 1] = image.GetData()[i * 3 + 2] = Max;
|
| 124 |
|
|
}
|
| 125 |
|
|
|
| 126 |
|
|
*this = image;
|
| 127 |
|
|
}
|
| 128 |
|
|
|