leocad/common/image.cpp

107 lines
2 KiB
C++
Raw Normal View History

#include "lc_global.h"
2011-09-07 21:06:51 +00:00
#include "image.h"
2013-08-09 04:57:18 +00:00
#include "opengl.h"
2011-09-07 21:06:51 +00:00
Image::Image ()
{
2013-08-09 04:57:18 +00:00
mData = NULL;
mWidth = 0;
mHeight = 0;
mAlpha = false;
2011-09-07 21:06:51 +00:00
}
Image::~Image ()
{
2013-08-09 04:57:18 +00:00
FreeData();
2011-09-07 21:06:51 +00:00
}
void Image::FreeData ()
{
2013-08-09 04:57:18 +00:00
free(mData);
mData = NULL;
mWidth = 0;
mHeight = 0;
mAlpha = false;
2011-09-07 21:06:51 +00:00
}
2013-08-09 04:57:18 +00:00
void Image::Allocate(int Width, int Height, bool Alpha)
2011-09-07 21:06:51 +00:00
{
FreeData ();
2013-08-09 04:57:18 +00:00
mWidth = Width;
mHeight = Height;
mAlpha = Alpha;
2011-09-07 21:06:51 +00:00
2013-08-09 04:57:18 +00:00
if (mAlpha)
mData = (unsigned char*)malloc(mWidth * mHeight * 4);
2011-09-07 21:06:51 +00:00
else
2013-08-09 04:57:18 +00:00
mData = (unsigned char*)malloc(mWidth * mHeight * 3);
2011-09-07 21:06:51 +00:00
}
void Image::ResizePow2 ()
{
int i, shifted_x, shifted_y;
2013-08-09 04:57:18 +00:00
shifted_x = mWidth;
2011-09-07 21:06:51 +00:00
for (i = 0; ((i < 16) && (shifted_x != 0)); i++)
shifted_x = shifted_x >> 1;
shifted_x = (i != 0) ? 1 << (i-1) : 1;
2013-08-09 04:57:18 +00:00
shifted_y = mHeight;
2011-09-07 21:06:51 +00:00
for (i = 0; ((i < 16) && (shifted_y != 0)); i++)
shifted_y = shifted_y >> 1;
shifted_y = (i != 0) ? 1 << (i-1) : 1;
2013-08-09 04:57:18 +00:00
if ((shifted_x != mWidth) || (shifted_y != mHeight))
2011-09-07 21:06:51 +00:00
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;
2013-08-09 04:57:18 +00:00
if (mAlpha)
2011-09-07 21:06:51 +00:00
components = 4;
else
components = 3;
bits = (unsigned char*)malloc (width * height * components);
2013-08-09 04:57:18 +00:00
for (j = 0; j < mHeight; j++)
2011-09-07 21:06:51 +00:00
{
2013-08-09 04:57:18 +00:00
accumy = (float)height*j/(float)mHeight;
2011-09-07 21:06:51 +00:00
sty = (int)floor(accumy);
2013-08-09 04:57:18 +00:00
for (i = 0; i < mWidth; i++)
2011-09-07 21:06:51 +00:00
{
2013-08-09 04:57:18 +00:00
accumx = (float)width*i/(float)mWidth;
2011-09-07 21:06:51 +00:00
stx = (int)floor(accumx);
for (k = 0; k < components; k++)
2013-08-09 04:57:18 +00:00
bits[(stx+sty*width)*components+k] = mData[(i+j*mWidth)*components+k];
2011-09-07 21:06:51 +00:00
}
}
2013-08-09 04:57:18 +00:00
free (mData);
mData = bits;
mWidth = width;
mHeight = height;
2011-09-07 21:06:51 +00:00
}
2013-08-09 04:57:18 +00:00
void Image::FromOpenGL(int Width, int Height)
2011-09-07 21:06:51 +00:00
{
2013-08-09 04:57:18 +00:00
Allocate(Width, Height, true);
2011-09-07 21:06:51 +00:00
2013-08-09 04:57:18 +00:00
lcuint8* Buffer = (lcuint8*)malloc(Width * Height * 4);
2011-09-07 21:06:51 +00:00
glPixelStorei (GL_PACK_ALIGNMENT, 1);
2013-08-09 04:57:18 +00:00
glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, Buffer);
2011-09-07 21:06:51 +00:00
2013-08-09 04:57:18 +00:00
for (int Row = 0; Row < Height; Row++)
memcpy(mData + (Row * Width * 4), Buffer + ((Height - Row - 1) * Width * 4), Width * 4);
2011-09-07 21:06:51 +00:00
2013-08-09 04:57:18 +00:00
free(Buffer);
2011-09-07 21:06:51 +00:00
}