leocad/common/image.cpp

122 lines
2.1 KiB
C++
Raw Normal View History

#include "lc_global.h"
2011-09-07 23:06:51 +02:00
#include "image.h"
Image::Image()
2011-09-07 23:06:51 +02:00
{
2013-08-09 06:57:18 +02:00
mData = NULL;
mWidth = 0;
mHeight = 0;
mFormat = LC_PIXEL_FORMAT_INVALID;
2011-09-07 23:06:51 +02:00
}
Image::~Image()
2011-09-07 23:06:51 +02:00
{
2013-08-09 06:57:18 +02:00
FreeData();
2011-09-07 23:06:51 +02:00
}
int Image::GetBPP() const
{
switch (mFormat)
{
case LC_PIXEL_FORMAT_INVALID:
return 0;
case LC_PIXEL_FORMAT_A8:
return 1;
case LC_PIXEL_FORMAT_L8A8:
return 2;
case LC_PIXEL_FORMAT_R8G8B8:
return 3;
case LC_PIXEL_FORMAT_R8G8B8A8:
return 4;
}
return 0;
}
bool Image::HasAlpha() const
{
switch (mFormat)
{
case LC_PIXEL_FORMAT_INVALID:
return false;
case LC_PIXEL_FORMAT_A8:
return true;
case LC_PIXEL_FORMAT_L8A8:
return true;
case LC_PIXEL_FORMAT_R8G8B8:
return false;
case LC_PIXEL_FORMAT_R8G8B8A8:
return true;
}
return 0;
}
void Image::FreeData()
2011-09-07 23:06:51 +02:00
{
2013-08-09 06:57:18 +02:00
free(mData);
mData = NULL;
mWidth = 0;
mHeight = 0;
mFormat = LC_PIXEL_FORMAT_INVALID;
2011-09-07 23:06:51 +02:00
}
void Image::Allocate(int Width, int Height, lcPixelFormat Format)
2011-09-07 23:06:51 +02:00
{
FreeData();
2011-09-07 23:06:51 +02:00
2013-08-09 06:57:18 +02:00
mWidth = Width;
mHeight = Height;
mFormat = Format;
mData = (unsigned char*)malloc(mWidth * mHeight * GetBPP());
2011-09-07 23:06:51 +02:00
}
void Image::ResizePow2()
2011-09-07 23:06:51 +02:00
{
int i, shifted_x, shifted_y;
2011-09-07 23:06:51 +02:00
2013-08-09 06:57:18 +02:00
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;
2011-09-07 23:06:51 +02:00
2013-08-09 06:57:18 +02:00
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;
2011-09-07 23:06:51 +02:00
2013-08-09 06:57:18 +02:00
if ((shifted_x != mWidth) || (shifted_y != mHeight))
Resize (shifted_x, shifted_y);
2011-09-07 23:06:51 +02:00
}
void Image::Resize(int width, int height)
2011-09-07 23:06:51 +02:00
{
int i, j, k, components, stx, sty;
float accumx, accumy;
unsigned char* bits;
2011-09-07 23:06:51 +02:00
components = GetBPP();
2011-09-07 23:06:51 +02:00
bits = (unsigned char*)malloc(width * height * components);
2011-09-07 23:06:51 +02:00
2013-08-09 06:57:18 +02:00
for (j = 0; j < mHeight; j++)
{
2013-08-09 06:57:18 +02:00
accumy = (float)height*j/(float)mHeight;
sty = (int)floor(accumy);
2011-09-07 23:06:51 +02:00
2013-08-09 06:57:18 +02:00
for (i = 0; i < mWidth; i++)
{
2013-08-09 06:57:18 +02:00
accumx = (float)width*i/(float)mWidth;
stx = (int)floor(accumx);
2011-09-07 23:06:51 +02:00
for (k = 0; k < components; k++)
2013-08-09 06:57:18 +02:00
bits[(stx+sty*width)*components+k] = mData[(i+j*mWidth)*components+k];
}
}
2011-09-07 23:06:51 +02:00
2014-10-12 01:26:23 +02:00
free(mData);
2013-08-09 06:57:18 +02:00
mData = bits;
mWidth = width;
mHeight = height;
2011-09-07 23:06:51 +02:00
}