Hi Aitor
Aitor Matilla says:
Quote
I haven't done any time test, It's enough quick for me but If it would
be interesting to improve this code.
Something like this, two ways of doing it:
bool __fastcall SetBufferDataCompatibleWithBitmap (int Width_,
int Height_, const Byte* pSource, Byte* &pTarget)
{
if (pSource == NULL) return false;
if (pTarget != NULL) return false;
//Size according to DWORD size
int LineWidth = Width_ * 3;
int FullLineWidth = (LineWidth + 3) &~ 3;
int BitsSize = FullLineWidth * Height_;
//New buffer
pTarget = new Byte [BitsSize];
if (pTarget == NULL) return false;
for(int y = 0; y < Height_; ++y)// Hight must be positive
{
Byte* SourceLine = pSource + y * LineWidth;
Byte* TargetLine = pSource + y * FullLineWidth;
memcpy(TargetLine, SourceLine, LineWidth);
}
return true;
}
//----------------------------------------------------------
or write directly
bool __fastcall WritePixelsToStream(TStream* Strm, int Width_,
int Height_, const Byte* pSource)
{
if (pSource == NULL) return false;
//Size according to DWORD size
int LineWidth = Width_ * 3;
int FullLineWidth = (LineWidth + 3) &~ 3;
int BitsSize = FullLineWidth * Height_;
Byte Pad[3];
PadSize = FullLineWidth - LineWidth;
for(int y = 0; y < Height_; ++y)// Hight must be positive !!!
{
Byte* SourceLine = pSource + y * LineWidth;
Strm->Write( SourceLine, LineWidth);
if(PadSize)
Strm->Write( Pad, PadSize );
}
}
//--------------------------------------------------------
Please note that this code is not tested !!!
but I'm sure You get the picture..
P.s.
Remember to set the size of the stream before You start writing
Strm->SetSize(BmFh.bfSize);
otherwise the stream will reallocate during the writing
Another thing is that You would get a lot more functions to
use if You stored Your data like the bitmap does it.
line-of-data + pad + line-of-data + pad + line-of-data pad... etc.
and if You let windows create the buffer for You with:
CreateDIBSection You can use all kind of API functions on Your
data like: BitBlt, FillRect.....
A struct and a couple of functions thats nice to have when
working with 24 bit Bitmaps
struct TPixel
{
Byte Blue;
Byte Green;
Byte Red;
//===============================================
__fastcall TPixel() : Blue(0), Green(0), Red(0){}
//-----------------------------------------------
__fastcall TPixel(Byte R, Byte G, Byte B)
: Blue(B), Green(G), Red(R){}
//-----------------------------------------------
__fastcall TPixel(const TPixel &t)
{
Blue = t.Blue;
Green = t.Green;
Red = t.Red;
}//----------------------------------------------
__fastcall TPixel(TColor t)
{
int i = (int)t;
Red = i; i = i>>8;
Green = i; i = i>>8;
Blue = i;
}//----------------------------------------------
};
TPixel* __fastcall GetScanLine(int Row)
{
// Check if Row is in bitmap
if(BitmapIsBottomUp)
Row = BitmapHeight - Row - 1;
TPixel* Pixels = (TPixel*)YourPixelBufffer;
return Pixels + Row * FullLineWidth;
}
TPixel __fastcall GetPixel(int X, int Y)
{
// Check if X is in Bitmap
return GetScanLine(Y) + X;
}
void __fastcall SetPixel(int X, int Y, TPixel Color)
{
// Check if X is in Bitmap
TPixel *Pixels = GetScanLine(Y);
*(Pixels + X) = Color;
}
Kind regards
Asger