17#if defined(__BORLANDC__)
53 TGdiObject(clipboard.GetClipboardData(CF_PALETTE))
55 WARNX(OwlGDI, !
Handle, 0,
"Cannot create TPalette from the clipboard");
72 LOGPALETTE* logPal =
reinterpret_cast<LOGPALETTE*
>(
new
73 uint8[
sizeof(LOGPALETTE)+(nColors-1)*
sizeof(PALETTEENTRY)]);
75 logPal->palVersion = 0x300;
76 logPal->palNumEntries = nColors;
78 Handle = ::CreatePalette(logPal);
84 WARNX(OwlGDI, !
Handle, 0,
"Cannot create palette from palette " <<
static_cast<void*
>(src.
GetHandle()));
94 Handle = ::CreatePalette(&logPalette);
95 WARNX(OwlGDI, !
Handle, 0,
"Cannot create palette from LOGPALETTE @" <<
static_cast<const void*
>(&logPalette));
100#if defined(OWL5_COMPAT)
109 Handle = ::CreatePalette(logPalette);
110 WARNX(OwlGDI, !
Handle, 0,
"Cannot create palette from LOGPALETTE @" <<
static_cast<const void*
>(logPalette));
122 Init(entries, count);
128void TPalette::Init(
const PALETTEENTRY* entries,
int count)
130 LOGPALETTE* logPal =
reinterpret_cast<LOGPALETTE*
>(
new uint8[
131 sizeof(LOGPALETTE)+(count-1)*
sizeof(PALETTEENTRY) ]);
133 logPal->palVersion = 0x300;
134 logPal->palNumEntries =
static_cast<uint16>(count);
135 memcpy(logPal->palPalEntry, entries, count*
sizeof(PALETTEENTRY));
136 Handle = ::CreatePalette(logPal);
139 WARNX(OwlGDI, !
Handle, 0,
"Cannot create palette from " << count <<
140 "palette entries @" <<
static_cast<const void*
>(entries));
156#if defined(OWL5_COMPAT)
192 WARNX(OwlGDI, !
Handle, 0,
"Cannot create palette from file: " << fileName);
210 const RGBQUAD * rgb = info->bmiColors;
216 info->bmiHeader.biClrUsed :
217 NColors(info->bmiHeader.biBitCount));
220 LOGPALETTE* logPal =
reinterpret_cast<LOGPALETTE*
>(
221 new uint8[
sizeof(LOGPALETTE) + (nColors-1)*
sizeof(PALETTEENTRY)]);
223 logPal->palVersion = 0x300;
224 logPal->palNumEntries = nColors;
225 for (
uint16 n = 0; n < nColors; n++) {
226 logPal->palPalEntry[n].peRed = rgb[n].rgbRed;
227 logPal->palPalEntry[n].peGreen = rgb[n].rgbGreen;
228 logPal->palPalEntry[n].peBlue = rgb[n].rgbBlue;
229 logPal->palPalEntry[n].peFlags =
static_cast<uint8>(flags);
231 Handle = ::CreatePalette(logPal);
237 WARNX(OwlGDI, !
Handle, 0,
"Cannot create palette from bitmapinfo @" <<
static_cast<const void*
>(info));
331 WARNX(OwlGDI, 1, 0,
"TPalette::Read_PAL() Not a RIFF or PAL file");
339 WARNX(OwlGDI, 1, 0,
"TPalette::Read_PAL() No data chunk in file");
344 int iSize =
static_cast<int>(ckChunk.
Size);
346 LOGPALETTE* pLogPal = (LOGPALETTE*)(
char*)pdata;
349 WARNX(OwlGDI, 1, 0,
"TPalette::Read_PAL() Failed to PAL Colors, line" << __LINE__);
354 WARNX(OwlGDI, 1, 0,
"TPalette::Read_PAL() Failed to PAL Colors, line" << __LINE__);
359 if (pLogPal->palVersion != 0x300) {
360 WARNX(OwlGDI, 1, 0,
"TPalette::Read_PAL(): Invalid version number");
365 if (pLogPal->palNumEntries <= 0){
366 WARNX(OwlGDI, 1, 0,
"TPalette::Read_PAL(): No colors in palette");
370 Handle = ::CreatePalette(pLogPal);
385 WARNX(OwlGDI, 1, 0,
"TPalette::Write_PAL() Failed to create RIFF-PAL chunk");
393 WARNX(OwlGDI, 1, 0,
"TPalette::Write_PAL() no colors in palette");
396 int iSize =
sizeof(LOGPALETTE)+(nColors-1)*
sizeof(PALETTEENTRY);
398 LOGPALETTE* logPal =
reinterpret_cast<LOGPALETTE*
>((
char*)pdata);
399 logPal->palVersion = 0x300;
400 logPal->palNumEntries = nColors;
408 WARNX(OwlGDI, 1, 0,
"TPalette::Write_PAL() Failed to create data chunk");
412 if(!file.
Write((
char*)pdata, iSize)) {
413 WARNX(OwlGDI, 1, 0,
"TPalette::Write_PAL() Failed to write data chunk");
427void HSL_to_RGB(
double& h,
double& sl,
double& l,
double* r,
double* g,
double* b)
430 v = (l <= 0.5) ? (l * (1.0 + sl)) : (l + sl - l * sl);
437 double fract, vsf, mid1, mid2;
442 sextant =
static_cast<int>(h);
444 vsf = v * sv * fract;
448 case 0: *r = v; *g = mid1; *b = m;
break;
449 case 1: *r = mid2; *g = v; *b = m;
break;
450 case 2: *r = m; *g = v; *b = mid1;
break;
451 case 3: *r = m; *g = mid2; *b = v;
break;
452 case 4: *r = mid1; *g = m; *b = v;
break;
453 case 5: *r = v; *g = m; *b = mid2;
break;
487 PALETTEENTRY palPalEntry[256];
492#define CMYK(c,m,y,k) (static_cast<COLORREF>((((BYTE)(c)|((WORD)((BYTE)(m))<<8))|(((DWORD)(BYTE)(y))<<16))|(((DWORD)(BYTE)(k))<<24)))
494#define GetCValue(cmyk) ((BYTE)(cmyk))
495#define GetMValue(cmyk) ((BYTE)((cmyk)>> 8))
496#define GetYValue(cmyk) ((BYTE)((cmyk)>>16))
497#define GetKValue(cmyk) ((BYTE)((cmyk)>>24))
510 WARNX(OwlGDI, 1, 0,
"TPalette::Read_ACO() Failed to read ACO header");
513 TRACEX(OwlGDI, 1,
"TPalette::Read_ACO() Data for header: hdr.Version = " << dec << hdr.Version <<
", hdr.Count = " << hdr.Count);
514 if(hdr.Version != 1){
515 WARNX(OwlGDI, 1, 0,
"TPalette::Read_ACO() Invalid version number");
523 for(
int i = 0; i < hdr.Count; i++){
525 WARNX(OwlGDI, 1, 0,
"TPalette::Read_ACO() Failed to ACO Colors, line" << __LINE__);
530 TRACEX(OwlGDI, 1,
"TPalette::Read_ACO() Data for Colors:");
531 for (
int j= 0; j < hdr.Count; j++){
532 TRACEX(OwlGDI, 1,
"TPalette::Read_ACO() index("<< dec << j <<
"), clrSpace("
533 << clr[j].clrSpace <<
"), Color1("<< clr[j].Color1<<
"), Color2(" <<
534 clr[j].Color2<<
"), Color3("<< clr[j].Color3<<
"), Color4("<<
535 clr[j].Color4<<
")");
539 LogPal.palVersion = 0x300;
540 LogPal.palNumEntries = 256;
541 memset(&LogPal.palPalEntry, 0,
sizeof(LogPal.palPalEntry));
551 for (
int k= 0; k < hdr.Count; k++){
552 switch(clr[k].clrSpace){
557 LogPal.palPalEntry[k].peRed =
uint8(clr[k].Color1*255ul/65535u);
558 LogPal.palPalEntry[k].peGreen =
uint8(clr[k].Color2*255ul/65535u);
559 LogPal.palPalEntry[k].peBlue =
uint8(clr[k].Color3*255ul/65535u);
560 LogPal.palPalEntry[k].peFlags = 0;
566 h = clr[k].Color1/65535.0;
567 s = clr[k].Color2/65535.0;
568 l = clr[k].Color3/65535.0;
571 LogPal.palPalEntry[k].peRed =
uint8(r*255u);
572 LogPal.palPalEntry[k].peGreen =
uint8(g*255u);
573 LogPal.palPalEntry[k].peBlue =
uint8(b*255u);
574 LogPal.palPalEntry[k].peFlags = 0;
580 COLORREF clrRef =
CMYK(
uint8(clr[k].Color1*255ul/65535u),
581 uint8(clr[k].Color2*255ul/65535u),
582 uint8(clr[k].Color3*255ul/65535u),
583 uint8(clr[k].Color4*255ul/65535u)
585 LogPal.palPalEntry[k].peRed = GetRValue(clrRef);
586 LogPal.palPalEntry[k].peGreen = GetGValue(clrRef);
587 LogPal.palPalEntry[k].peBlue = GetBValue(clrRef);
588 LogPal.palPalEntry[k].peFlags = 0;
602 LogPal.palPalEntry[k].peRed =
uint8(clr[k].Color1*255ul/10000u);
603 LogPal.palPalEntry[k].peGreen =
uint8(clr[k].Color1*255ul/10000u);
604 LogPal.palPalEntry[k].peBlue =
uint8(clr[k].Color1*255ul/10000u);
605 LogPal.palPalEntry[k].peFlags = 0;
611 WARNX(OwlGDI, 1, 0,
"TPalette::Read_ACO() Error no colors found.");
615 Handle = ::CreatePalette((LOGPALETTE*)&LogPal);
629 WARNX(OwlGDI, 1, 0,
"TPalette::Write_ACO() Error no colors in palette");
642 WARNX(OwlGDI, 1, 0,
"TPalette::Write_ACO() Failed to write ACO header");
647 for (
int i= 0; i < nColors; i++){
649 clr[i].Color1 =
uint16(pColors[i].peRed*65535ul/255);
650 clr[i].Color2 =
uint16(pColors[i].peGreen * 65535ul/255);
651 clr[i].Color3 =
uint16(pColors[i].peBlue * 65535ul/255);
655 for(
int j = 0; j < nColors; j++){
657 WARNX(OwlGDI, 1, 0,
"TPalette::Write_ACO() Failed to write ACO Colors, line" << __LINE__);
677 LogPal.palVersion = 0x300;
678 LogPal.palNumEntries = 256;
679 memset(&LogPal.palPalEntry, 0,
sizeof(LogPal.palPalEntry));
685 WARNX(OwlGDI, 1, 0,
"TPalette::Read_ACT() Failed to read ACT colors");
688 for (
int i= 0; i < 256; i++){
689 LogPal.palPalEntry[i].peRed = clr[i].Red;
690 LogPal.palPalEntry[i].peGreen = clr[i].Green;
691 LogPal.palPalEntry[i].peBlue = clr[i].Blue;
692 LogPal.palPalEntry[i].peFlags = 0;
694 Handle = ::CreatePalette((LOGPALETTE*)&LogPal);
707 WARNX(OwlGDI, 1, 0,
"TPalette::Write_ACT() Error no colors in palette");
715 for (; i < nColors; i++){
716 clr[i].Red = pColors[i].peRed;
717 clr[i].Green = pColors[i].peGreen;
718 clr[i].Blue = pColors[i].peBlue;
720 for (; i < 256; i++){
727 if(!file.
Write(
reinterpret_cast<char*
>(&clr),
sizeof(clr))) {
728 WARNX(OwlGDI, 1, 0,
"TPalette::Write_ACT() Failed to write ACT data");
740 BITMAPFILEHEADER bmf;
743 WARNX(OwlGDI, 1, 0,
"TPalette::Read_BMP() Failed to read BMP header");
747 if(bmf.bfType != bmp_id) {
748 WARNX(OwlGDI, 1, 0,
"TPalette::Read_BMP() Not a Windows 3.x or PM 1.x bitmap file");
756 if (file.
Read(
reinterpret_cast<char*
>(&headerSize),
sizeof headerSize) ==
TFILE_ERROR ||
757 (headerSize !=
sizeof(BITMAPCOREHEADER) && headerSize !=
sizeof(BITMAPINFOHEADER)))
759 WARNX(OwlGDI, 1, 0,
"TPalette::Read_BMP() Not a Windows 3.x or PM 1.x bitmap file");
763 uint16 BitCount, biPlanes;
767 bool isCore = headerSize ==
sizeof(BITMAPCOREHEADER);
770 BITMAPCOREHEADER coreHeader;
771 if (file.
Read(
reinterpret_cast<char*
>(&coreHeader.bcWidth),
773 WARNX(OwlGDI, 1, 0,
"TPalette::Read_BMP() Invalid PM 1.x DIB Header");
777 BitCount = coreHeader.bcBitCount;
778 biPlanes = coreHeader.bcPlanes;
782 BITMAPINFOHEADER infoHeader;
783 infoHeader.biSize =
sizeof(BITMAPINFOHEADER);
785 if (file.
Read((
char*)&infoHeader.biWidth,
787 WARNX(OwlGDI, 1, 0,
"TPalette::Read_BMP() Invalid Win 3.x DIB Header");
791 BitCount = infoHeader.biBitCount;
792 biPlanes = infoHeader.biPlanes;
793 ClrUsed = infoHeader.biClrUsed;
797 WARNX(OwlGDI, 1, 0,
"TPalette::Read_BMP() Invalid number of planes in DIB");
809 int colorRead = isCore ?
810 static_cast<int>(ClrUsed) *
sizeof(RGBTRIPLE) :
811 static_cast<int>(ClrUsed) *
sizeof(RGBQUAD);
816 WARNX(OwlGDI, 1, 0,
"TPalette::Read_BMP() Could not read DIB color table");
820 for (
int i =
int(ClrUsed-1); i >= 0; i--) {
821 Colors[i].rgbRed = ((RGBTRIPLE*)Colors)[i].rgbtRed;
822 Colors[i].rgbGreen = ((RGBTRIPLE*)Colors)[i].rgbtGreen;
823 Colors[i].rgbBlue = ((RGBTRIPLE*)Colors)[i].rgbtBlue;
828 LogPal.palVersion = 0x300;
829 LogPal.palNumEntries = 256;
830 memset(&LogPal.palPalEntry, 0,
sizeof(LogPal.palPalEntry));
832 for(; i < ClrUsed; i++){
833 LogPal.palPalEntry[i].peRed = Colors[i].rgbRed;
834 LogPal.palPalEntry[i].peGreen = Colors[i].rgbGreen;
835 LogPal.palPalEntry[i].peBlue = Colors[i].rgbBlue;
836 LogPal.palPalEntry[i].peFlags = 0;
839 LogPal.palPalEntry[i].peRed = 0;
840 LogPal.palPalEntry[i].peGreen = 0;
841 LogPal.palPalEntry[i].peBlue = 0;
842 LogPal.palPalEntry[i].peFlags = 0;
844 Handle = ::CreatePalette((LOGPALETTE*)&LogPal);
853 BITMAPFILEHEADER bmf;
855 bmf.bfSize =
sizeof(bmf) +
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD)*256 +
sizeof(
uint8);
858 bmf.bfOffBits =
sizeof bmf +
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD)*256;
859 if(!file.
Write((
char*)&bmf,
sizeof bmf)){
860 WARNX(OwlGDI, 1, 0,
"TPalette::Write_BMP() Error write header");
864 BITMAPINFOHEADER infoHeader;
865 infoHeader.biSize =
sizeof(BITMAPINFOHEADER);
866 infoHeader.biWidth = 1;
867 infoHeader.biHeight = 1;
868 infoHeader.biPlanes = 1;
869 infoHeader.biBitCount = 8;
870 infoHeader.biCompression = BI_RGB;
871 infoHeader.biSizeImage = 1;
872 infoHeader.biXPelsPerMeter = 0;
873 infoHeader.biYPelsPerMeter = 0;
874 infoHeader.biClrUsed = 256;
875 infoHeader.biClrImportant = 256;
878 if(!file.
Write((
char*)&infoHeader,
sizeof(infoHeader))){
879 WARNX(OwlGDI, 1, 0,
"TPalette::Write_BMP() Failed to write BMP Info header");
886 WARNX(OwlGDI, 1, 0,
"TPalette::Write_BMP() no colors in palette");
894 for (; i < nColors; i++){
895 clr[i].rgbRed = pColors[i].peRed;
896 clr[i].rgbGreen = pColors[i].peGreen;
897 clr[i].rgbBlue = pColors[i].peBlue;
898 clr[i].rgbReserved = 0;
900 for (; i < 256; i++){
904 clr[i].rgbReserved = 0;
908 if(!file.
Write((
char*)&clr,
sizeof(clr))) {
909 WARNX(OwlGDI, 1, 0,
"TPalette::Write_BMP() Failed to write BMP Colors");
914 if(!file.
Write((
char*)&pixel,
sizeof(pixel))) {
915 WARNX(OwlGDI, 1, 0,
"TPalette::Write_BMP() Failed to write BMP 1x1 bitmap");
#define WARNX(group, condition, level, message)
#define PRECONDITION(condition)
#define TRACEX(group, level, message)
Pointer to an array of type T.
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.
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.
Pseudo-GDI object Device Independent Bitmap (DIB) class.
const BITMAPINFO * GetInfo() const
Returns this DIB's Info field.
bool IsOpen() const
Returns true if the file is open, false otherwise.
@ CreateAlways
Creates a new file. The function overwrites the file if it exists.
uint WriteStruct(void *buffer, TBinType btype, TByteOrderType type)
Writes predefined structures to binary file.
uint ReadStruct(void *buffer, TBinType btype, TByteOrderType type)
Read binary predefined structure from file.
TFileName()
Constructs an empty filename.
static void RefInc(HANDLE handle)
Increments by 1 the reference count of the object associated with the given handle.
void CheckValid(uint resId=IDS_GDIFAILURE)
static void RefRemove(HANDLE handle)
Removes the reference entry to the object with the given handle.
bool ShouldDelete
Should object delete GDI handle in dtor?
static void RefAdd(HANDLE handle, TType type)
RefAdd adds a reference entry for the object with the given handle and type.
TGdiObject()
This default constructor sets Handle to 0 and ShouldDelete to true.
HANDLE Handle
GDI handle of this object.
TPalette(HPALETTE handle, TAutoDelete autoDelete=NoAutoDelete)
Alias an existing palette handle. Assume ownership if autoDelete says so.
bool Write_BMP(TRiffFile &file)
Write palette to BMP 1x1 file.
bool Read_BMP(TRiffFile &file)
Read palette from BMP file.
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.
bool Write_PAL(TRiffFile &file)
Write Microsoft palette to file.
bool GetObject(uint16 &numEntries) const
Finds the number of entries in this logical palette and sets the value in the numEntries argument.
bool Write(const tchar *fileName)
Write this palette into file: .dib,.bmp,.pal,.aco,*.act.
bool Read_ACO(TRiffFile &file)
Read Adobe palette from file.
HPALETTE GetHandle() const
Returns the handle of the palette.
void Create(const BITMAPINFO *info, uint flags)
Accept a pointer to a BITMAPINFO structure and create a GDI logical palette from the color table whic...
bool Read_PAL(TRiffFile &file)
Read Microsoft palette from file.
void ToClipboard(TClipboard &Clipboard)
Moves this palette to the target Clipboard argument.
bool Write_ACT(TRiffFile &file)
Write Adobe palette to file.
bool Read(const tchar *fileName)
Read palette from file.
bool Write_ACO(TRiffFile &file)
Write Adobe palette to file.
bool Read_ACT(TRiffFile &file)
Read Adobe palette from file.
The TRiffFile class is used for reading and writing RIFF files.
bool Ascent(TCkInfo &info)
bool CreateChunk(TCkInfo &info, const TCreateFlags=cfCreateChunk)
Writes a data structure of the specified type to the associated file.
bool Descent(TCkInfo &info, TCkInfo *parent=0, const TFindFlags=ffFindAny)
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.
#define owlFCC(ch0, ch1, ch2, ch3)
@ boLittle_Endian
LSB at lowest address: Intel //.
@ boBig_Endian
MSB at lowest address: Motorola //.
Reliable platform independent header for common memory and string functions.
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.
void HSL_to_RGB(double &h, double &sl, double &l, double *r, double *g, double *b)
convert HSL to RGB
ulong TStaticSync< T >::Count
DIAG_DECLARE_GROUP(OwlThread)
General definitions used by all ObjectWindows programs.
The TBinField struct describes a group of like-typed fields in a structure to be read or written usin...
RIFF chunk information data structure.
uint32 Type
form type or list type