OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
bitmap.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1992, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of GDI Bitmap object class
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/gdiobjec.h>
10#include <owl/metafile.h>
11#include <owl/clipboar.h>
12#include <owl/wsyscls.h>
13
14#if defined(__BORLANDC__)
15# pragma option -w-ccc // Disable "Condition is always true/false"
16#endif
17
18using namespace std;
19
20namespace owl {
21
23DIAG_DECLARE_GROUP(OwlGDI); // General GDI diagnostic group
24DIAG_DECLARE_GROUP(OwlGDIOrphan); // Orphan control tracing group
25
26//
27/// Construct an alias TBitmap for an existing bitmap handle
28//
29/// Creates a TBitmap object and sets the Handle data member to the given borrowed
30/// handle. The ShouldDelete data member defaults to false, ensuring that the
31/// borrowed handle will not be deleted when the C++ object is destroyed.
32//
34:
36{
37 if (ShouldDelete)
39 TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this);
40}
41
42//
43// Create an empty bitmap.
44//
46:
48{
49 TRACEX(OwlGDI, OWL_CDLEVEL, "Default TBitmap constructed @" << (void*)this);
50}
51
52//
53/// Creates a TBitmap object with values from the given Clipboard.
54//
56:
57 TGdiObject(clipboard.GetClipboardData(CF_BITMAP))
58{
61 TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this << " from clipboard.");
62}
63
64//
65/// Creates a bitmap object from bitCount bits in the bits buffer with the given
66/// width, height, and planes argument values.
67//
68TBitmap::TBitmap(int width, int height, uint8 planes, uint8 bitCount, const void * bits)
69{
70 Handle = ::CreateBitmap(width, height, planes, bitCount, bits);
71 WARNX(OwlGDI, !Handle, 0, "Cannot create bitmap (" << width << "x" <<
72 height << "x" << planes << ")");
73 CheckValid();
75 TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this << " using parameters (" <<
76 dec << width << "x" << height << "x" << planes << ")." << hex);
77}
78
79//
80/// Creates a bitmap object with the values found in the given bitmap structure.
81//
83{
85 WARNX(OwlGDI, !Handle, 0, "Cannot create bitmap from BITMAP @" << static_cast<const void*>(&bitmap));
86 CheckValid();
88 TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << static_cast<void*>(this)
89 << " from BITMAP @" << static_cast<const void*>(&bitmap) << ".");
90}
91
92#if defined(OWL5_COMPAT)
93
94//
95/// Creates a bitmap object with the values found in the given bitmap structure.
96/// This overload is deprecated. Use the overload that takes a reference instead.
97//
99{
102 WARNX(OwlGDI, !Handle, 0, "Cannot create bitmap from BITMAP @" <<
103 hex << (void*)bitmap);
104 CheckValid();
106 TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this << " from BITMAP @" <<
107 (void*)bitmap << ".");
108}
109
110#endif
111
112//
113/// Creates a bitmap object for the given device context with the given argument
114/// values.
115//
116TBitmap::TBitmap(const TDC& dc, int width, int height, bool discardable)
117{
118 if (discardable) {
119 Handle = ::CreateDiscardableBitmap(dc, width, height);
120 WARNX(OwlGDI, !Handle, 0, "Cannot create discardable bitmap (" << width <<
121 "x" << height << ") for " << static_cast<void*>(dc.GetHDC()));
122 }
123 else {
124 Handle = ::CreateCompatibleBitmap(dc, width, height);
125 WARNX(OwlGDI, !Handle, 0, "Cannot create compatible bitmap (" << width <<
126 "x" << height << ") for " << static_cast<void*>(dc.GetHDC()));
127 }
128 CheckValid();
130
131 if (discardable) {
132 TRACEX(OwlGDI, OWL_CDLEVEL, "Discardable TBitmap constructed @" << (void*)this << " (" <<
133 dec << width << "x" << height << ") for " << static_cast<void*>(dc.GetHDC()) << ".");
134 }
135 else {
136 TRACEX(OwlGDI, OWL_CDLEVEL, "Compatible TBitmap constructed @" << (void*)this << " (" <<
137 dec << width << "x" << height << ") for " << static_cast<void*>(dc.GetHDC()) << ".");
138 }
139}
140
141//
142/// Creates a bitmap object for the given device context with the given dib and
143/// usage argument values.
144//
146{
148 const_cast<BITMAPINFOHEADER *>(dib.GetInfoHeader()),
149 usage, static_cast<const uint8 *>(dib.GetBits()),
150 const_cast<BITMAPINFO *>(dib.GetInfo()),
151 dib.Usage());
152 // API casts
153 WARNX(OwlGDI, !Handle, 0, "Cannot create bitmap from DIB " << static_cast<void*>(dib.GetHandle())
154 << " for " << static_cast<void*>(dc.GetHDC()));
155 CheckValid();
157 TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this <<
158 " from DIB for " << static_cast<void*>(dc.GetHDC()) << ".");
159}
160
161//
162/// Creates a bitmap object for the given application instance from the given
163/// resource.
164//
166{
167 Handle = ::LoadBitmap(instance, resId);
168 WARNX(OwlGDI, !Handle, 0, "Cannot load bitmap " << resId <<
169 " from " << static_cast<void*>(instance));
170 CheckValid();
172 TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed from resource @" << (void*)this << ".");
173}
174
175//
176/// Creates a copy of the given bitmap object.
177/// Duplicates a bitmap, bits and all
178//
180{
181 Handle = nullptr;
182 Create(src);
183 TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap duplicated @" << (void*)this << " from @" << (void*)&src << ".");
184}
185
186//
187/// Creates a bitmap object from the given metaFile using the given palette and size
188/// arguments.
189//
191{
192 // Adjust size to final metafile size as needed
193 //
195 TSize size = metaFile.CalcPlaySize(memDC, defSize);
196
197 // Create bitmap, either mono or screen compatible
198 //
200 palette.GetObject(nColors);
201 if (nColors > 2) {
202 TScreenDC dc;
203 Handle = ::CreateCompatibleBitmap(dc, size.cx, size.cy);
204 }
205 else
206 Handle = ::CreateBitmap(size.cx, size.cy, 1, 1, nullptr);
207
208 CheckValid();
210
211 // clear bitmap, then play metafile onto it
212 //
213 memDC.SelectObject(*this);
214
215 memDC.SelectStockObject(WHITE_BRUSH);
216 memDC.PatBlt(0, 0, size.cx, size.cy);
217 memDC.SelectObject(palette, false);
218
219 metaFile.PlayOnto(memDC, size);
220
221 TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this <<
222 " from metafile @" << (void*)&metaFile << ".");
223}
224
225//
226/// Creates a bitmap object from the given dib and palette arguments. A working
227/// palette constructed from the DIB's color table is used if no palette is
228/// supplied. The default system palette can also be passed using
229/// &TPalette::GetStock(TPalette::Default);
230//
232{
233 if (palette)
234 Create(dib, *palette);
235 else if (NColors(dib.GetInfo()->bmiHeader.biBitCount) > 0)
237 else
238 {
239 //this is the case when the DIB is full-colored and therefore has no palette
241 const_cast<LPBITMAPINFOHEADER>(dib.GetInfoHeader()),
242 CBM_INIT,
243 static_cast<const uint8*>(dib.GetBits()),
244 const_cast<LPBITMAPINFO>(dib.GetInfo()),
245 dib.Usage()
246 );
247 CheckValid();
249 }
250 TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this << " from DIB @" <<
251 (void*)&dib << " and palette @" << (void*)palette << ".");
252}
253
254//
255// Destroy the C++ object.
256//
258{
259 TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap destructed @" << (void*)this << ".");
260}
261
262//
263// Functional-style overload
264//
265BITMAP
267{
268 BITMAP b;
269 auto r = GetObject(b);
270 if (!r) throw TXOwl(_T("TBitmap::GetObject failed"));
271 return b;
272}
273
274//
275/// Returns the width of this bitmap using GDI's GetObject
276//
277int
279{
280 BITMAP bm;
281 GetObject(bm);
282 return bm.bmWidth;
283}
284
285//
286/// Returns the height of this bitmap using GDI's GetObject
287//
288int
290{
291 BITMAP bm;
292 GetObject(bm);
293 return bm.bmHeight;
294}
295
296//
297/// The width and height of the bitmap in pixels.
298//
299TSize
301{
302 BITMAP bm;
303 GetObject(bm);
304 return TSize(bm.bmWidth, bm.bmHeight);
305}
306
307//
308/// Returns the number of planes in this bitmap.
309//
310int
312{
313 BITMAP bm;
314 GetObject(bm);
315 return bm.bmPlanes;
316}
317
318//
319/// Returns the number of bits per pixel in this bitmap.
320//
321int
323{
324 BITMAP bm;
325 GetObject(bm);
326 return bm.bmBitsPixel;
327}
328
329//
330/// Functional-style overload
331//
332TSize
334{
335 TSize s;
336 const auto ok = GetBitmapDimension(s);
337 if (!ok) throw TXOwl(_T("TBitmap::GetBitmapDimension failed"));
338 return s;
339}
340
341//
342/// Put a device-dependent bitmap on the clipboard as a (DD)BITMAP. Clipboard
343/// assumes ownership of the actual bitmap handle & this TBitmap will not delete
344/// the handle
345//
346void
348{
350 ShouldDelete = false;
352}
353
354//
355/// Create a bitmap & get its handle, given a dib and a palette
356/// Used by ctors here and in derived classes. Assumes Handle member can be
357/// over written, & adds handle to reference container.
358//
359void
361{
362 TScreenDC dc;
363 dc.SelectObject(palette, false);
364 dc.RealizePalette();
365
367 dc,
368 const_cast<LPBITMAPINFOHEADER>(dib.GetInfoHeader()),
369 CBM_INIT,
370 static_cast<const uint8 *>(dib.GetBits()),
371 const_cast<LPBITMAPINFO>(dib.GetInfo()),
372 dib.Usage()
373 ); // API type casts
374
375 CheckValid();
377}
378
379//
380/// Create a bitmap & get its handle, given an other bitmap
381/// Used by ctors here and in derived classes. Assumes Handle can be
382/// written over, & adds handle to reference container.
383//
384void
386{
389
390 BITMAP bm;
391 src.GetObject(bm);
392 if (bm.bmPlanes != 1 || bm.bmBitsPixel != 1) {
393 // create a color bitmap (Assume screen compatible)
394 TScreenDC dc;
395 Handle = ::CreateCompatibleBitmap(dc, bm.bmWidth, bm.bmHeight);
396 }
397 else
398 // create a mono bitmap
399 Handle = ::CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, nullptr);
400
401 CheckValid();
403
404 memDC1.SelectObject(src);
405 memDC2.SelectObject(*this);
406 memDC2.BitBlt(TRect(TPoint(0, 0), TSize(bm.bmWidth, bm.bmHeight)),
407 memDC1, TPoint(0, 0), SRCCOPY);
408}
409
410} // OWL namespace
411
#define WARNX(group, condition, level, message)
Definition checks.h:277
#define PRECONDITION(condition)
Definition checks.h:227
#define DIAG_DECLARE_GROUP(group)
Definition checks.h:404
#define TRACEX(group, level, message)
Definition checks.h:263
TBitmap is the GDI bitmap class derived from TGdiObject.
Definition gdiobjec.h:510
BITMAP GetObject() const
Definition bitmap.cpp:266
void ToClipboard(TClipboard &clipboard)
Put a device-dependent bitmap on the clipboard as a (DD)BITMAP.
Definition bitmap.cpp:347
void Create(const TDib &dib, const TPalette &palette)
Create a bitmap & get its handle, given a dib and a palette Used by ctors here and in derived classes...
Definition bitmap.cpp:360
int Planes() const
Returns the number of planes in this bitmap.
Definition bitmap.cpp:311
TSize Size() const
The width and height of the bitmap in pixels.
Definition bitmap.cpp:300
int Width() const
Returns the width of this bitmap using GDI's GetObject.
Definition bitmap.cpp:278
TSize GetBitmapDimension() const
Functional-style overload.
Definition bitmap.cpp:333
int Height() const
Returns the height of this bitmap using GDI's GetObject.
Definition bitmap.cpp:289
int BitsPixel() const
Returns the number of bits per pixel in this bitmap.
Definition bitmap.cpp:322
The clipboard class encapsulates the methods for the clipboard object of Windows.
Definition clipboar.h:32
HANDLE SetClipboardData(uint format, HANDLE handle)
Copy the data onto the clipboard in the format.
Definition clipboar.h:282
TDC is the root class for GDI DC wrappers.
Definition dc.h:64
void SelectObject(const TBrush &brush)
Selects the given GDI brush object into this DC.
Definition dc.cpp:113
int RealizePalette()
Maps to the system palette the logical palette entries selected into this DC.
Definition dc.h:1054
HDC GetHDC() const
Return the handle of the device context.
Definition dc.h:981
Pseudo-GDI object Device Independent Bitmap (DIB) class.
Definition gdiobjec.h:795
GdiObject is the root, pseudo-abstract base class for ObjectWindows' GDI (Graphics Device Interface) ...
Definition gdiobjec.h:68
static void RefInc(HANDLE handle)
Increments by 1 the reference count of the object associated with the given handle.
Definition gdiobjec.cpp:178
void CheckValid(uint resId=IDS_GDIFAILURE)
Definition gdibase.cpp:49
static void RefRemove(HANDLE handle)
Removes the reference entry to the object with the given handle.
Definition gdiobjec.cpp:163
bool ShouldDelete
Should object delete GDI handle in dtor?
Definition gdibase.h:82
static void RefAdd(HANDLE handle, TType type)
RefAdd adds a reference entry for the object with the given handle and type.
Definition gdiobjec.cpp:137
HANDLE Handle
GDI handle of this object.
Definition gdibase.h:81
A device context (DC) class derived from TCreatedDC, TMemoryDC provides access to a memory DC.
Definition dc.h:784
TMetaFilePict is a support class used with TMetaFileDC to simplify metafile operations,...
Definition metafile.h:80
TPalette is the GDI Palette class derived from TGdiObject.
Definition gdiobjec.h:413
TPoint is a support class, derived from tagPOINT.
Definition geometry.h:87
TRect is a mathematical class derived from tagRect.
Definition geometry.h:308
Derived from TWindowDC, TScreenDC is a DC class that provides direct access to the screen bitmap.
Definition dc.h:610
The tagSIZE struct is defined as.
Definition geometry.h:234
TXOwl is root class of the ObjectWindows exception hierarchy.
Definition except.h:38
Definition of classes for clipboard Encapsulation.
#define _T(x)
Definition cygwin.h:51
Definition of abstract GDI object class and derived classes.
TAutoDelete
Flag for Handle ctors to control Handle deletion in dtor.
Definition gdibase.h:70
Definition of TMetafilePict, a MetaFile wrapper class.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
long NColors(uint16 bitCount)
Functions to convert number of bits to number of palette colors and back, and build an RGB COLORREF.
Definition color.cpp:188
unsigned char uint8
Definition number.h:32
unsigned long uint32
Definition number.h:34
OWL_DIAGINFO
Definition animctrl.cpp:14
#define OWL_CDLEVEL
Definition defs.h:171
Classes for window system structure and type encapsulation.