18#if defined(__BORLANDC__)
22#define BFT_BITMAP 0x4d42
34class TFileIO :
public TDib::IFileIn,
public TDib::IFileOut {
36 TFileIO(TFile& file) : File(file) {}
38 virtual long Read(
void * buffer,
long size)
40 return File.
Read(buffer, size);
42 virtual void Skip(
long size)
46 virtual bool Write(
void * buffer,
long size)
48 return File.
Write(buffer, size);
64 int maskAlloc = infoHeader.biCompression==BI_BITFIELDS ? 3*
sizeof(
uint32) : 0;
65 int colorAlloc = int(infoHeader.biClrUsed *
sizeof(RGBQUAD));
67 uint32 size =
sizeof(BITMAPINFOHEADER) + maskAlloc + colorAlloc + infoHeader.biSizeImage;
68 Handle = ::GlobalAlloc(GMEM_MOVEABLE, size);
75 Info =
reinterpret_cast<LPBITMAPINFO
>(::GlobalLock(
Handle));
76 Info->bmiHeader = infoHeader;
94 Info =
reinterpret_cast<LPBITMAPINFO
>(::LockResource(
Handle));
96 Info =
reinterpret_cast<LPBITMAPINFO
>(::GlobalLock(
Handle));
103 if (Info->bmiHeader.biSize !=
sizeof(BITMAPINFOHEADER)) {
112 if (Info->bmiHeader.biCompression == BI_BITFIELDS) {
113 Mask =
reinterpret_cast<uint32 *
>(Info->bmiColors);
114 Colors =
reinterpret_cast<TRgbQuad *
>(
reinterpret_cast<char *
>(Info->bmiColors) + 3 *
sizeof(
uint32));
118 Colors =
static_cast<TRgbQuad *
>(Info->bmiColors);
124 NumClrs = Info->bmiHeader.biClrUsed
125 ? Info->bmiHeader.biClrUsed
126 :
NColors(Info->bmiHeader.biBitCount);
128 Bits =
reinterpret_cast<char *
>(Colors) +
static_cast<int>(NumClrs) *
sizeof(RGBQUAD);
135 Mode = DIB_RGB_COLORS;
145 const char* begin =
reinterpret_cast<const char*
>(Info);
146 const char* end =
static_cast<const char*
>(Bits) +
SizeImage();
147 size_t size = end - begin;
148 HANDLE tempHandle = ::GlobalAlloc(GMEM_MOVEABLE, size);
152 void* tempInfo = ::GlobalLock(tempHandle);
153 memcpy(tempInfo, Info, size);
159 Info =
reinterpret_cast<LPBITMAPINFO
>(tempInfo);
173 Info(nullptr), IsResHandle(false)
184 TGdiBase(clipboard.GetClipboardData(CF_DIB)),
185 Info(nullptr), IsResHandle(false)
206 Handle = ::GlobalAlloc(GMEM_MOVEABLE, size);
210 Info =
reinterpret_cast<LPBITMAPINFO
>(::GlobalLock(
Handle));
211 memcpy(Info, src.Info, size);
213 Mask =
reinterpret_cast<uint32 *
>(src.Mask ?
214 reinterpret_cast<char *
>(Info) + (
reinterpret_cast<char *
>(src.Mask) -
reinterpret_cast<char *
>(src.Info)) :
nullptr);
215 Bits =
reinterpret_cast<char *
>(Info) + (
reinterpret_cast<char *
>(src.Bits) -
reinterpret_cast<char *
>(src.Info));
217 NumClrs = src.NumClrs;
224 Colors =
reinterpret_cast<TRgbQuad *
>(
reinterpret_cast<char *
>(Info) + (
reinterpret_cast<char *
>(src.Colors) -
reinterpret_cast<char *
>(src.Info)));
235 if (!
Handle || IsResHandle)
return;
252 if (Mode != DIB_RGB_COLORS)
257 for (
int c = 0; c < NumClrs; c++) {
259 for (
uint16 i = 0; i < nEntries; i++) {
262 if (pe.peRed == Colors[c].rgbRed &&
263 pe.peGreen == Colors[c].rgbGreen &&
264 pe.peBlue == Colors[c].rgbBlue) {
269 (
reinterpret_cast<uint16*
>(Colors))[c] = index;
271 Mode = DIB_PAL_COLORS;
286 if (Mode != DIB_PAL_COLORS ||
287 static_cast<uint>(
reinterpret_cast<char*
>(Bits) -
reinterpret_cast<char*
>(Info)) < NumClrs*
sizeof(RGBQUAD))
292 for (
int c =
static_cast<int>(NumClrs)-1; c >= 0; c--) {
298 Colors[c].rgbRed = pe.peRed;
299 Colors[c].rgbGreen = pe.peGreen;
300 Colors[c].rgbBlue = pe.peBlue;
302 Mode = DIB_RGB_COLORS;
316 if (entry >= 0 && entry < NumClrs)
317 if (Mode == DIB_RGB_COLORS)
330 if (entry >= 0 && entry < NumClrs)
331 if (Mode == DIB_RGB_COLORS)
342 for (
int entry = 0; entry < NumClrs; entry++)
358 for (
int entry = 0; entry < NumClrs; entry++)
375 if (entry >= 0 && entry < NumClrs)
376 if (Mode == DIB_PAL_COLORS)
389 if (entry >= 0 && entry < NumClrs)
390 if (Mode == DIB_PAL_COLORS)
401 for (
int entry = 0; entry < NumClrs; entry++)
416 for (
int entry = 0; entry < NumClrs; entry++)
515 if (!
Read(file, readFileHeader))
524 if (!
Read(is, readFileHeader))
534 Info(nullptr), Mask(nullptr), Colors(nullptr), Bits(nullptr),
553 TRACEX(OwlGDI, 0,
"Cannot open bitmap file '" << name <<
"' to read");
559 bool ok =
Read(file,
true);
574 return Read(in, readFileHeader);
587 TStreamIn(istream& is) : Is(is) {}
589 virtual long Read(
void* buffer,
long size)
592 streamsize n = Is.read(
static_cast<char*
>(buffer), size).gcount();
597 return static_cast<long>(n);
600 virtual void Skip(
long size)
602 Is.seekg(size, ios::cur);
622 return Read(in, readFileHeader);
644 if (readFileHeader) {
645 BITMAPFILEHEADER bmf;
646 if (
static_cast<size_t>(in.
Read(&bmf,
sizeof(bmf))) !=
sizeof(bmf) || bmf.bfType !=
BFT_BITMAP) {
647 TRACEX(OwlGDI, 0,
"Not a Windows 3.x or PM 1.x bitmap file");
652 offBits = bmf.bfOffBits -
sizeof(BITMAPFILEHEADER);
660 size_t n =
static_cast<size_t>(in.
Read(&headerSize,
sizeof(headerSize)));
661 if (n !=
sizeof(headerSize) ||
662 (headerSize !=
sizeof(BITMAPCOREHEADER) && headerSize !=
sizeof(BITMAPINFOHEADER)))
664 TRACEX(OwlGDI, 0,
"Not a Windows 3.x or PM 1.x bitmap file");
671 BITMAPINFOHEADER infoHeader;
672 infoHeader.biSize =
sizeof(BITMAPINFOHEADER);
676 bool isCore = headerSize ==
sizeof(BITMAPCOREHEADER);
680 BITMAPCOREHEADER coreHeader;
681 int size =
static_cast<int>(headerSize)-
sizeof(
uint32);
682 if (in.
Read(&coreHeader.bcWidth, size) != size) {
683 TRACEX(OwlGDI, 0,
"Invalid PM 1.x DIB Header");
690 infoHeader.biWidth = coreHeader.bcWidth;
691 infoHeader.biHeight = coreHeader.bcHeight;
692 infoHeader.biPlanes = coreHeader.bcPlanes;
693 infoHeader.biBitCount = coreHeader.bcBitCount;
694 infoHeader.biCompression = BI_RGB;
695 infoHeader.biSizeImage = 0;
696 infoHeader.biXPelsPerMeter = 0;
697 infoHeader.biYPelsPerMeter = 0;
698 infoHeader.biClrUsed = 0;
699 infoHeader.biClrImportant = 0;
706 int size =
static_cast<int>(headerSize)-
sizeof(
uint32);
707 if (in.
Read(&infoHeader.biWidth, size) != size) {
708 TRACEX(OwlGDI, 0,
"Invalid Win 3.x DIB Header");
716 if (infoHeader.biPlanes != 1) {
717 TRACEX(OwlGDI, 0,
"Invalid number of planes in DIB");
725 if (!infoHeader.biClrUsed)
726 infoHeader.biClrUsed =
NColors(infoHeader.biBitCount);
735 if (infoHeader.biCompression == BI_RGB ||
736 infoHeader.biCompression == BI_BITFIELDS)
737 infoHeader.biSizeImage =
739 infoHeader.biBitCount) * std::abs(infoHeader.biHeight);
748 int size = 3*
sizeof(
uint32);
749 if (in.
Read(
reinterpret_cast<char *
>(Mask), size) != size) {
750 TRACEX(OwlGDI, 0,
"Could not read DIB color mask");
759 int colorRead = isCore ?
760 static_cast<int>(NumClrs) *
sizeof(RGBTRIPLE) :
761 static_cast<int>(NumClrs) *
sizeof(RGBQUAD);
763 if (in.
Read(
reinterpret_cast<char *
>(Colors), colorRead) != colorRead) {
764 TRACEX(OwlGDI, 0,
"Could not read DIB color table");
769 for (
int i =
int(NumClrs-1); i >= 0; i--) {
770 Colors[i].rgbRed = (
reinterpret_cast<RGBTRIPLE*
>(Colors))[i].rgbtRed;
771 Colors[i].rgbGreen = (
reinterpret_cast<RGBTRIPLE*
>(Colors))[i].rgbtGreen;
772 Colors[i].rgbBlue = (
reinterpret_cast<RGBTRIPLE*
>(Colors))[i].rgbtBlue;
773 Colors[i].rgbReserved = 0;
782 if (offBits && offBits -
static_cast<long>(headerSize+colorRead) > 0)
783 in.
Skip(offBits - (headerSize+colorRead));
784 if (!in.
Read(Bits, infoHeader.biSizeImage)) {
785 TRACEX(OwlGDI, 0,
"Could not read DIB bits");
798 Info(nullptr), Mask(nullptr), Colors(nullptr), Bits(nullptr),
817 HRSRC resHandle = ::FindResource(instance, resId, RT_BITMAP);
820 TRACEX(OwlGDI, 0,
"Cannot access bitmap resource");
851 TRACEX(OwlGDI, 0,
"Cannot open bitmap file '" << name <<
"' to write");
855 bool ok =
Write(file,
true);
859 TRACEX(OwlGDI, 0,
"Disk error writing file '" << name <<
"'");
873 return Write(out, writeFileHeader);
887 TStreamOut( ostream& os) : Os(os) {}
889 virtual bool Write(
void * buffer,
long size)
891 return Os.write(
reinterpret_cast<char*
>(buffer), size).good();
904 return Write(out, writeFileHeader);
914 size_t size = ::GlobalSize(
Handle);
920 size_t bfSize = size +
sizeof(BITMAPFILEHEADER);
924 const char* begin =
reinterpret_cast<const char*
>(Info);
925 const char* end =
static_cast<const char*
>(Bits) +
sizeof(BITMAPFILEHEADER);
926 size_t bfOffBits = end - begin;
930 BITMAPFILEHEADER bmf;
932 bmf.bfSize =
static_cast<DWORD
>(bfSize);
935 bmf.bfOffBits =
static_cast<DWORD
>(bfOffBits);
936 if (!out.
Write(&bmf,
sizeof(bmf)))
944 if (!out.
Write(Info,
static_cast<long>(size)))
959 BITMAPINFOHEADER infoHeader;
960 infoHeader.biSize =
sizeof(BITMAPINFOHEADER);
961 infoHeader.biWidth = width;
962 infoHeader.biHeight = height;
963 infoHeader.biPlanes = 1;
964 infoHeader.biBitCount =
NBits(nColors);
965 infoHeader.biCompression = BI_RGB;
967 infoHeader.biSizeImage = bytes * std::abs(height);
968 infoHeader.biXPelsPerMeter = 0;
969 infoHeader.biYPelsPerMeter = 0;
970 infoHeader.biClrUsed = infoHeader.biBitCount > 8 ? 0 : nColors;
971 infoHeader.biClrImportant = 0;
982 const uint paletteSize = infoHeader.biClrUsed;
983 Mode = (paletteSize > 0) ? mode :
static_cast<uint16>(DIB_RGB_COLORS);
984 if (Mode == DIB_PAL_COLORS) {
988 for (
uint i = 0; i < paletteSize; i++)
989 (
reinterpret_cast<uint16*
>(Colors))[i] =
static_cast<uint16>(i);
991 else if (paletteSize > 0) {
996 uint clrCnt = ::GetSystemPaletteEntries(dc, 0, paletteSize,
reinterpret_cast<LPPALETTEENTRY
>(Colors));
997 for (
uint32 i = 0; i < clrCnt; i++) {
998 Swap(Colors[i].rgbRed, Colors[i].rgbBlue);
999 Colors[i].rgbReserved = 0;
1015 BITMAPINFOHEADER infoHeader;
1016 infoHeader.biSize =
sizeof(BITMAPINFOHEADER);
1017 infoHeader.biWidth = bm.bmWidth;
1018 infoHeader.biHeight = bm.bmHeight;
1019 infoHeader.biPlanes = 1;
1023 infoHeader.biBitCount =
NBits(nColors);
1026 infoHeader.biBitCount =
uint16(bm.bmBitsPixel*bm.bmPlanes);
1027 nColors =
static_cast<uint16>(
NColors(infoHeader.biBitCount));
1029 infoHeader.biCompression = BI_RGB;
1030 infoHeader.biSizeImage =
ScanBytes(infoHeader.biWidth,
1031 infoHeader.biBitCount) * std::abs(infoHeader.biHeight);
1032 infoHeader.biXPelsPerMeter = 0;
1033 infoHeader.biYPelsPerMeter = 0;
1034 infoHeader.biClrUsed = nColors;
1035 infoHeader.biClrImportant = 0;
1062 for (
int i = 0; i < 256; i++)
1077 PALETTEENTRY pe[256];
1080 for (
int j = 0; j < 256; j++) {
1081 colorTab->rgbRed = pe[j].peRed;
1082 colorTab->rgbGreen = pe[j].peGreen;
1083 colorTab->rgbBlue = pe[j].peBlue;
#define PRECONDITION(condition)
#define DIAG_DECLARE_GROUP(group)
#define TRACEX(group, level, message)
TBitmap is the GDI bitmap class derived from TGdiObject.
bool GetObject(BITMAP &bitmap) const
Retrieves data (width, height, and color format) for this bitmap and sets it in the given BITMAP stru...
The clipboard class encapsulates the methods for the clipboard object of Windows.
HANDLE SetClipboardData(uint format, HANDLE handle)
Copy the data onto the clipboard in the format.
Class wrapper for management of color values.
static const TColor LtMagenta
Static TColor object with fixed Value set by RGB(255, 0, 255).
static const TColor SysBtnText
The symbolic system color value for the text on buttons.
static const TColor Sys3dHilight
The symbolic system color value for highlighted 3-dimensional display elements (for edges facing the ...
static const TColor SysWindowFrame
The symbolic system color value for the frame around each window.
TColor()
Construct a null color.
static const TColor LtYellow
Static TColor object with fixed Value set by RGB(255, 255, 0).
static const TColor LtGray
Static TColor object with fixed Value set by RGB(192, 192, 192).
static const TColor Sys3dShadow
The symbolic system color value for the shadow regions of 3-dimensional display elements (for edges f...
static const TColor Sys3dFace
The symbolic system color value for the face color of 3-dimensional display elements.
static const TColor Gray
Static TColor object with fixed Value set by RGB(128, 128, 128).
static const TColor White
Static TColor object with fixed Value set by RGB(255, 255, 255).
static const TColor SysWindow
The symbolic system color value for the background of each window.
static const TColor Black
Static TColor object with fixed Value set by RGB(0, 0, 0).
virtual int GetDeviceCaps(int index) const
Used under WIN3.1 or later, GetDeviceCaps returns capability information about this DC.
void SelectObject(const TBrush &brush)
Selects the given GDI brush object into this DC.
int GetDIBits(const TBitmap &bitmap, uint startScan, uint numScans, void *bits, const BITMAPINFO &info, uint16 usage)
The first version retrieves some or all of the bits from the given bitmap on this DC and copies them ...
Internal DIB file Reda/Write functions talk to these interfaces.
virtual void Skip(long size)=0
virtual long Read(void *buffer, long size)=0
virtual bool Write(void *buffer, long size)=0
uint NumScans() const
Returns the number of scans in this DIB.
TColor GetColor(int entry) const
Returns the color for the given palette entry.
uint16 Usage() const
Returns the Mode for this DIB.
uint32 SizeDib() const
Size of dib is measured as the end of the bits minus the start of the block.
const uint16 * GetIndices() const
Returns the bmiColors indexes of this DIB.
uint16 GetIndex(int entry) const
int FindIndex(uint16 index)
Returns the palette entry corresponding to the given index.
bool Write(TFile &file, bool writeFileHeader=false)
Write this DIB to a file object.
void MapUIColors(uint mapColors, const TColor *bkColor=nullptr)
Maps the UI colors to the value specified in the parameter, mapColors, which is one of the values of ...
bool Read(TFile &file, bool readFileHeader=false)
Read a Windows 3.x or PM 1.x device independent bitmap from a TFile Return true iff Dib was read OK.
void ResToMemHandle()
Under Win32, resources are read-only.
int FindColor(const TColor &color)
Returns the palette entry for the given color.
bool WriteFile(const tstring &filename)
Writes the DIB into an external file.
void SetColor(int entry, const TColor &color)
Sets the given color for the given palette entry.
bool LoadResource(HINSTANCE, TResId)
Loads this DIB from the given resource and returns true if successful.
int MapColor(const TColor &fromColor, const TColor &toColor, bool doAll=false)
Maps the first or all occurances of fromColor to the toColor in the current palette of this DIB.
int MapIndex(uint16 fromIndex, uint16 toIndex, bool doAll=false)
Maps the fromIndex to the toIndex in the current palette of this DIB.
bool ReadFile(const tstring &name)
Tests if the passed file is a Windows 3.x (or PM 1.x) DIB and if so read it.
int Pitch() const
Size of scan in bytes = Pixel Width * bits per pixel rounded up to a uint32 boundary.
TDib()
Protected ctor- derived classes need to fill in handle & get info.
void MapToPalette(const TPalette &pal)
Modifies this DIB so that the pixels and color table coincide with a given palette.
void ToClipboard(TClipboard &clipboard)
Moves this dib to the clipboard.
static int ScanBytes(long w, int bpp)
Width+bpp to dword aligned bytes.
TDib(HGLOBAL handle, TAutoDelete autoDelete=NoAutoDelete)
Creates a TDib object and sets the Handle data member to the given borrowed handle.
bool ChangeModeToRGB(const TPalette &pal)
Converts the existing color table in place to use absolute RGB values.
int Height() const
Returns the height of the DIB.
void InfoFromHeader(const BITMAPINFOHEADER &infoHeader)
Given a BITMAPINFOHEADER, allocate a global buffer for Handle & init Info.
void SetIndex(int entry, uint16 index)
Sets the given index for the given entry.
bool ShouldDelete
Should object delete GDI handle in dtor?
virtual ~TDib()
Destruct a TDib by unlocking & freeing its global memory handle as required.
uint StartScan() const
Returns the starting scan line.
HANDLE Handle
GDI handle of this object.
void InfoFromHandle()
Locks the global/res handle if needed and extracts the pointers and cached info maintained as member ...
uint32 SizeImage() const
Returns number of bytes used to store the image.
bool ChangeModeToPal(const TPalette &pal)
Converts the existing color table in place to use palette relative values.
const void * GetBits() const
Returns the Bits data member for this DIB.
const TRgbQuad * GetColors() const
Returns the bmiColors value of this DIB.
The TFile class encapsulates standard file characteristics and operations.
bool IsOpen() const
Returns true if the file is open, false otherwise.
virtual uint64 Seek(int64 offset, TSeekDir origin=beg)
Repositions the file pointer to offset bytes from the specified origin.
@ OpenExisting
Opens the file. The function fails if the file does not exist.
@ CreateAlways
Creates a new file. The function overwrites the file if it exists.
virtual uint32 Read(void *buffer, uint32 numBytes)
Reads numBytes from the file into buffer. The number of bytes read is returned.
virtual bool Write(const void *buffer, uint32 numBytes)
Writes numbytes of buffer to the file.
@ cur
Seek from the current position in the file.
virtual bool Close()
Closes the file. Returns true if successful, false otherwise.
TGdiBase()
This default constructor sets Handle to 0 and ShouldDelete to true.
TPalette is the GDI Palette class derived from TGdiObject.
uint GetPaletteEntries(uint16 start, uint16 count, PALETTEENTRY *entries) const
Retrieves a range of entries in this logical palette and places them in the entries array.
uint GetNearestPaletteIndex(const TColor &color) const
Returns the index of the color entry that represents the closest color in this palette to the given c...
uint GetPaletteEntry(uint16 index, PALETTEENTRY &entry) const
Retrieves the entry in this logical palette at index and places it in the entries array.
bool GetObject(uint16 &numEntries) const
Finds the number of entries in this logical palette and sets the value in the numEntries argument.
uint16 GetNumEntries() const
Returns the number of entries in this palette, or 0 if the call fails.
Wrapper for Windows' RBGQUAD type.
Derived from TWindowDC, TScreenDC is a DC class that provides direct access to the screen bitmap.
static void Raise(uint resId=IDS_GDIFAILURE, HANDLE handle=0)
Throws the exception.
Definition of classes for clipboard Encapsulation.
Definition of abstract GDI object class and derived classes.
TAutoDelete
Flag for Handle ctors to control Handle deletion in dtor.
static void Raise()
Construct a TXOutOfMemory exception from scratch, and throw it.
Object Windows Library (OWLNext Core).
long NColors(uint16 bitCount)
Functions to convert number of bits to number of palette colors and back, and build an RGB COLORREF.
uint16 NBits(long colors)
Return the number of bits required to represent a given number of colors.
bool IsRepresentable(U v)
Helper function for testing for integer truncations.
ObjectWindows exception class & function definitions.
Classes for window system structure and type encapsulation.