1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
#include "lc_global.h"
#include "image.h"
#include "lc_file.h"
static void CopyFromQImage(const QImage& Src, Image& Dest)
{
const bool Alpha = Src.hasAlphaChannel();
Dest.Allocate(Src.width(), Src.height(), Alpha ? lcPixelFormat::R8G8B8A8 : lcPixelFormat::R8G8B8);
quint8* Bytes = (quint8*)Dest.mData;
for (int y = 0; y < Dest.mHeight; y++)
{
for (int x = 0; x < Dest.mWidth; x++)
{
QRgb Pixel = Src.pixel(x, y);
*Bytes++ = qRed(Pixel);
*Bytes++ = qGreen(Pixel);
*Bytes++ = qBlue(Pixel);
if (Alpha)
*Bytes++ = qAlpha(Pixel);
}
}
}
Image::Image()
{
mData = nullptr;
mWidth = 0;
mHeight = 0;
mFormat = lcPixelFormat::Invalid;
}
Image::Image(Image&& Other)
{
mData = Other.mData;
mWidth = Other.mWidth;
mHeight = Other.mHeight;
mFormat = Other.mFormat;
Other.mData = nullptr;
Other.mWidth = 0;
Other.mHeight = 0;
Other.mFormat = lcPixelFormat::Invalid;
}
Image::~Image()
{
FreeData();
}
int Image::GetBPP() const
{
switch (mFormat)
{
case lcPixelFormat::Invalid:
return 0;
case lcPixelFormat::A8:
return 1;
case lcPixelFormat::L8A8:
return 2;
case lcPixelFormat::R8G8B8:
return 3;
case lcPixelFormat::R8G8B8A8:
return 4;
}
return 0;
}
bool Image::HasAlpha() const
{
switch (mFormat)
{
case lcPixelFormat::Invalid:
return false;
case lcPixelFormat::A8:
return true;
case lcPixelFormat::L8A8:
return true;
case lcPixelFormat::R8G8B8:
return false;
case lcPixelFormat::R8G8B8A8:
return true;
}
return 0;
}
void Image::FreeData()
{
free(mData);
mData = nullptr;
mWidth = 0;
mHeight = 0;
mFormat = lcPixelFormat::Invalid;
}
void Image::Allocate(int Width, int Height, lcPixelFormat Format)
{
FreeData();
mWidth = Width;
mHeight = Height;
mFormat = Format;
mData = (unsigned char*)malloc(mWidth * mHeight * GetBPP());
}
void Image::ResizePow2()
{
int i, shifted_x, shifted_y;
shifted_x = mWidth;
for (i = 0; ((i < 16) && (shifted_x != 0)); i++)
shifted_x = shifted_x >> 1;
shifted_x = (i != 0) ? 1 << (i-1) : 1;
shifted_y = mHeight;
for (i = 0; ((i < 16) && (shifted_y != 0)); i++)
shifted_y = shifted_y >> 1;
shifted_y = (i != 0) ? 1 << (i-1) : 1;
if ((shifted_x != mWidth) || (shifted_y != mHeight))
Resize (shifted_x, shifted_y);
}
void Image::Resize(int width, int height)
{
int i, j, k, components, stx, sty;
float accumx, accumy;
unsigned char* bits = nullptr;
components = GetBPP();
const int BufferSize = width * height * components;
if (BufferSize)
{
bits = (unsigned char*)malloc(BufferSize);
if (bits)
{
for (j = 0; j < mHeight; j++)
{
accumy = (float)height*j / (float)mHeight;
sty = (int)floor(accumy);
for (i = 0; i < mWidth; i++)
{
accumx = (float)width*i / (float)mWidth;
stx = (int)floor(accumx);
for (k = 0; k < components; k++)
bits[(stx + sty*width)*components + k] = mData[(i + j * mWidth) * components + k];
}
}
}
}
free(mData);
mData = bits;
mWidth = width;
mHeight = height;
}
bool Image::FileLoad(lcMemFile& File)
{
QImage Image;
const unsigned char* Buffer = File.mBuffer + File.mPosition;
const size_t BufferLength = File.mFileSize - File.mPosition;
if (!Image.loadFromData(Buffer, (int)BufferLength))
return false;
CopyFromQImage(Image, *this);
return true;
}
bool Image::FileLoad(const QString& FileName)
{
QImage Image;
if (!Image.load(FileName))
return false;
CopyFromQImage(Image, *this);
return true;
}
|