1
// Copyright (C) 2002-2011 Nikolaus Gebhardt / Thomas Alten
2
// This file is part of the "Irrlicht Engine".
3
// For conditions of distribution and use, see copyright notice in irrlicht.h
7
#include "CColorConverter.h"
15
//! Constructor of empty image
16
CImage::CImage(ECOLOR_FORMAT format, const core::dimension2d<u32>& size)
17
:Data(0), Size(size), Format(format), DeleteMemory(true)
23
//! Constructor from raw data
24
CImage::CImage(ECOLOR_FORMAT format, const core::dimension2d<u32>& size, void* data,
25
bool ownForeignMemory, bool deleteForeignMemory)
26
: Data(0), Size(size), Format(format), DeleteMemory(deleteForeignMemory)
30
Data = (u8*)0xbadf00d;
38
memcpy(Data, data, Size.Height * Pitch);
43
//! assumes format and size has been set and creates the rest
44
void CImage::initData()
47
setDebugName("CImage");
49
BytesPerPixel = getBitsPerPixelFromFormat(Format) / 8;
51
// Pitch should be aligned...
52
Pitch = BytesPerPixel * Size.Width;
57
Data = new u8[Size.Height * Pitch];
70
//! Returns width and height of image data.
71
const core::dimension2d<u32>& CImage::getDimension() const
77
//! Returns bits per pixel.
78
u32 CImage::getBitsPerPixel() const
80
return getBitsPerPixelFromFormat(Format);
84
//! Returns bytes per pixel
85
u32 CImage::getBytesPerPixel() const
91
//! Returns image data size in bytes
92
u32 CImage::getImageDataSizeInBytes() const
94
return Pitch * Size.Height;
98
//! Returns image data size in pixels
99
u32 CImage::getImageDataSizeInPixels() const
101
return Size.Width * Size.Height;
105
//! returns mask for red value of a pixel
106
u32 CImage::getRedMask() const
124
//! returns mask for green value of a pixel
125
u32 CImage::getGreenMask() const
143
//! returns mask for blue value of a pixel
144
u32 CImage::getBlueMask() const
162
//! returns mask for alpha value of a pixel
163
u32 CImage::getAlphaMask() const
182
void CImage::setPixel(u32 x, u32 y, const SColor &color, bool blend)
184
if (x >= Size.Width || y >= Size.Height)
191
u16 * dest = (u16*) (Data + ( y * Pitch ) + ( x << 1 ));
192
*dest = video::A8R8G8B8toA1R5G5B5( color.color );
197
u16 * dest = (u16*) (Data + ( y * Pitch ) + ( x << 1 ));
198
*dest = video::A8R8G8B8toR5G6B5( color.color );
203
u8* dest = Data + ( y * Pitch ) + ( x * 3 );
204
dest[0] = (u8)color.getRed();
205
dest[1] = (u8)color.getGreen();
206
dest[2] = (u8)color.getBlue();
211
u32 * dest = (u32*) (Data + ( y * Pitch ) + ( x << 2 ));
212
*dest = blend ? PixelBlend32 ( *dest, color.color ) : color.color;
219
SColor CImage::getPixel(u32 x, u32 y) const
221
if (x >= Size.Width || y >= Size.Height)
227
return A1R5G5B5toA8R8G8B8(((u16*)Data)[y*Size.Width + x]);
229
return R5G6B5toA8R8G8B8(((u16*)Data)[y*Size.Width + x]);
231
return ((u32*)Data)[y*Size.Width + x];
234
u8* p = Data+(y*3)*Size.Width + (x*3);
235
return SColor(255,p[0],p[1],p[2]);
243
//! returns the color format
244
ECOLOR_FORMAT CImage::getColorFormat() const
250
//! copies this surface into another at given position
251
void CImage::copyTo(IImage* target, const core::position2d<s32>& pos)
253
Blit(BLITTER_TEXTURE, target, 0, &pos, this, 0, 0);
257
//! copies this surface partially into another at given position
258
void CImage::copyTo(IImage* target, const core::position2d<s32>& pos, const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect)
260
Blit(BLITTER_TEXTURE, target, clipRect, &pos, this, &sourceRect, 0);
264
//! copies this surface into another, using the alpha mask, a cliprect and a color to add with
265
void CImage::copyToWithAlpha(IImage* target, const core::position2d<s32>& pos, const core::rect<s32>& sourceRect, const SColor &color, const core::rect<s32>* clipRect)
267
// color blend only necessary on not full spectrum aka. color.color != 0xFFFFFFFF
268
Blit(color.color == 0xFFFFFFFF ? BLITTER_TEXTURE_ALPHA_BLEND: BLITTER_TEXTURE_ALPHA_COLOR_BLEND,
269
target, clipRect, &pos, this, &sourceRect, color.color);
273
//! copies this surface into another, scaling it to the target image size
274
// note: this is very very slow.
275
void CImage::copyToScaling(void* target, u32 width, u32 height, ECOLOR_FORMAT format, u32 pitch)
277
if (!target || !width || !height)
280
const u32 bpp=getBitsPerPixelFromFormat(format)/8;
284
if (Format==format && Size.Width==width && Size.Height==height)
288
memcpy(target, Data, height*pitch);
293
u8* tgtpos = (u8*) target;
295
const u32 bwidth = width*bpp;
296
const u32 rest = pitch-bwidth;
297
for (u32 y=0; y<height; ++y)
300
memcpy(tgtpos, srcpos, bwidth);
302
memset(tgtpos+bwidth, 0, rest);
310
const f32 sourceXStep = (f32)Size.Width / (f32)width;
311
const f32 sourceYStep = (f32)Size.Height / (f32)height;
314
for (u32 y=0; y<height; ++y)
317
for (u32 x=0; x<width; ++x)
319
CColorConverter::convert_viaFormat(Data+ syval + ((s32)sx)*BytesPerPixel, Format, 1, ((u8*)target)+ yval + (x*bpp), format);
323
syval=((s32)sy)*Pitch;
329
//! copies this surface into another, scaling it to the target image size
330
// note: this is very very slow.
331
void CImage::copyToScaling(IImage* target)
336
const core::dimension2d<u32>& targetSize = target->getDimension();
338
if (targetSize==Size)
344
copyToScaling(target->lock(), targetSize.Width, targetSize.Height, target->getColorFormat());
349
//! copies this surface into another, scaling it to fit it.
350
void CImage::copyToScalingBoxFilter(IImage* target, s32 bias, bool blend)
352
const core::dimension2d<u32> destSize = target->getDimension();
354
const f32 sourceXStep = (f32) Size.Width / (f32) destSize.Width;
355
const f32 sourceYStep = (f32) Size.Height / (f32) destSize.Height;
359
s32 fx = core::ceil32( sourceXStep );
360
s32 fy = core::ceil32( sourceYStep );
365
for ( u32 y = 0; y != destSize.Height; ++y )
368
for ( u32 x = 0; x != destSize.Width; ++x )
370
target->setPixel( x, y,
371
getPixelBox( core::floor32(sx), core::floor32(sy), fx, fy, bias ), blend );
381
//! fills the surface with given color
382
void CImage::fill(const SColor &color)
389
c = color.toA1R5G5B5();
393
c = video::A8R8G8B8toR5G6B5( color.color );
402
CColorConverter::convert_A8R8G8B8toR8G8B8(&color, 1, rgb);
403
const u32 size = getImageDataSizeInBytes();
404
for (u32 i=0; i<size; i+=3)
406
memcpy(Data+i, rgb, 3);
412
if (Format != ECF_A1R5G5B5 && Format != ECF_R5G6B5 &&
413
Format != ECF_A8R8G8B8)
416
memset32( Data, c, getImageDataSizeInBytes() );
420
//! get a filtered pixel
421
inline SColor CImage::getPixelBox( s32 x, s32 y, s32 fx, s32 fy, s32 bias ) const
424
s32 a = 0, r = 0, g = 0, b = 0;
426
for ( s32 dx = 0; dx != fx; ++dx )
428
for ( s32 dy = 0; dy != fy; ++dy )
430
c = getPixel( core::s32_min ( x + dx, Size.Width - 1 ) ,
431
core::s32_min ( y + dy, Size.Height - 1 )
442
s32 sdiv = s32_log2_s32(fx * fy);
444
a = core::s32_clamp( ( a >> sdiv ) + bias, 0, 255 );
445
r = core::s32_clamp( ( r >> sdiv ) + bias, 0, 255 );
446
g = core::s32_clamp( ( g >> sdiv ) + bias, 0, 255 );
447
b = core::s32_clamp( ( b >> sdiv ) + bias, 0, 255 );
454
} // end namespace video
455
} // end namespace irr