-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUIcoFiles.pas
737 lines (675 loc) · 23.7 KB
/
UIcoFiles.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
// ??? ???, ??? ???????? ?????????? ?????? ? ????
// ??? ? ????? ??????, ??????? ?? ????? ???? ????? ????? ???? ? ?? ??????? ?????.
// ??? ???????? ??? ????????? "?????????" ?????? ?? ?????, ??? ???????? ??????????????? ?
// ????? ????? ??????, ??? ???????? ?????? ????? ?????? ?? ??????, ??? ???????? ??????
// ?????? ? ????, ??? ??????????????? ?????? ? png, ??? ?????????, ???? ?? ? ?????? alpha-?????,
// ? ??? ??????????????? ?????? ?? 32 ???? ? true color ??? ???????????.
//
// ???? ???????? ?????, ? ??? ????? ?????? ?????????, ?? ? ????? ??????? ???????????
// ? ?????? ??? ?? ???
// ???? ????????? ?????? ?????? ????? ??, ?? http://janych.selfip.com/examples/delphi/icons/
//
unit UIcoFiles;
interface
uses Classes,Graphics,windows;
type
TBits=array of Byte;
TIconData= // ?????? ?? ????????? ??????
packed record
Info:TIconRec; // ????????? ?? ????? ??? ????????? ?? ??????
BitmapInfo, // ????????? ??????, ?????????????? ? ??? ?????????
MaskBitmapInfo:PBitmapInfo; // ????????? ????? - ? ???? ?? ???????????, ????????? needMaskBitmapInfo
ImageLineWidth, // ???????????? ? IsValidAlpha ? ConvertToPNG
MaskLineWidth:Integer; // ???????????? ? ConvertToPNG
ImageBits:TBits; // ???? ???????????
MaskBits:TBits; // ???? ?????
iRgbTable:integer; // ?????? ???????, ?????????? ? BitmapInfo
end;
TIcons=array of TIconData; // ????? "?????????" ??????? ?? ?????? ?????
type
TIcoFile=
class(TComponent)
private
FIcons:TIcons;
public
destructor Destroy;override;
procedure loadFromStream(Stream:TStream);
procedure loadFromHandle(h:hicon);
procedure saveTrueColorFrom32(icoNo:integer;var IconData:TIconData);
procedure saveToStream(Stream:TStream);
procedure check;
procedure draw(icoNo,x,y:integer;dest:hdc;drawMask,drawImage,drawAlpha:boolean);overload;
procedure draw(IconData:TIconData;x,y:integer;dest:hdc;drawMask,drawImage,drawAlpha:boolean);overload;
function IsValidAlpha(icoNo:integer):boolean; // ?????????, ???? ?? ?????-?????
property Icons:TIcons read FIcons write FIcons;
procedure ConvertToPNG(icoNo:integer;Stream:TStream);
procedure saveAsPng(icoNo:integer;fn:string);
procedure Add(IconData:TIconData);
procedure AddCopy(IconData:TIconData);
end;
function getIconHandleForFile(fn:string;large:boolean):hicon;
implementation
uses sysUtils,
ShellAPI, // ???????????? ?????? ??? getIconHandleForFile
pngimage; // ???????????? ?????? ??? ConvertToPNG/saveAsPng, ????? ??????, ???? ?? ?????????
type
TRGBQuadArray=array[byte] of TRGBQuad;
PRGBQuadArray=^TRGBQuadArray;
{ TIcoFile }
destructor TIcoFile.Destroy;
var
i:integer;
begin
for i:=low(FIcons) to high(FIcons) do
with FIcons[i] do
begin
if BitmapInfo<>nil
then
FreeMem(BitmapInfo,sizeof(BITMAPINFOHEADER)+iRgbTable);
if MaskBitmapInfo<>nil
then
FreeMem(MaskBitmapInfo,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*2);
BitmapInfo:=nil;
MaskBitmapInfo:=nil;
end;
inherited;
end;
procedure TIcoFile.loadFromStream(Stream:TStream);
var
FileHeader:TCursorOrIcon;
i,Size:integer;
begin
Stream.ReadBuffer(FileHeader,SizeOf(FileHeader)); // ?????? ?????????
if (FileHeader.Reserved<>0) or not (FileHeader.wType in [RC3_ICON,RC3_CURSOR])
then
raise Exception.Create('Invalid icon');
SetLength(FIcons,FileHeader.Count); // ??????? ?????? ??????. ??? ?????? ?? ?????? Stream ?????? 1 ???????
for i:=0 to FileHeader.Count-1 do // ?????? ????????? ????????? ?????? ????? ?? ??????? ????-???? ?? ?????
with FIcons[i] do
Stream.ReadBuffer(Info,SizeOf(TIconRec));
for i:=0 to FileHeader.Count-1 do
begin
with FIcons[i] do
begin
Stream.Position:=Info.DIBOffset;
getMem(BitmapInfo,SizeOf(BitmapInfo^.bmiHeader));
Stream.ReadBuffer(BitmapInfo^.bmiHeader,SizeOf(BitmapInfo^.bmiHeader)); // ?????? ????????? ???????
with BitmapInfo^.bmiHeader do
begin
if biBitCount>16
then
iRgbTable:=0
else
if (biBitCount<16)
then
iRgbTable:=(1 shl biBitCount)*sizeof(RGBQUAD)
else
Assert(false); // ? ?????? ??????? ???????? ?? ????????
end;
BitmapInfo:=ReallocMemory(BitmapInfo,sizeof(BITMAPINFOHEADER)+iRgbTable);
if iRgbTable<>0
then
Stream.ReadBuffer(BitmapInfo^.bmiColors,iRgbTable);
with BitmapInfo^.bmiHeader do
begin
ImageLineWidth:=BytesPerScanline(biWidth,biBitCount,32); // ?? ???? ?????????? ?????? ?????
Assert((biWidth*biBitCount+31) div 32*4=ImageLineWidth);
Size:=(biHeight div 2)*ImageLineWidth; // ? ?????? ????? ???????
SetLength(ImageBits,Size); // ???? ???????????
Stream.ReadBuffer(ImageBits[0],Size); // ??????
MaskLineWidth:=BytesPerScanline(biWidth,1,32);
Size:=(biHeight div 2)*MaskLineWidth; // ?????? ????? (1-??????)
Assert((biWidth+31) div 32*4*(biHeight div 2)=Size);
end;
SetLength(MaskBits,Size);
Stream.ReadBuffer(MaskBits[0],Size); // ??????
end;
end;
end;
function InternalGetDIB(Bitmap:HBITMAP;var iRgbTable:integer;var BitmapInfo:PBitmapInfo;var Bits:TBits):Boolean;
var
DC:HDC;
DS:TDIBSection;
Bytes:Integer;
begin
iRgbTable:=0;
getMem(BitmapInfo,SizeOf(BitmapInfo^.bmiHeader));
try
FillChar(BitmapInfo^.bmiHeader,sizeof(BitmapInfo^.bmiHeader),0);
with BitmapInfo^ do
begin
DS.dsbmih.biSize:=0;
Bytes:=GetObject(Bitmap,SizeOf(DS),@DS);
Assert(Bytes<>0);
if (Bytes>=(sizeof(DS.dsbm)+sizeof(DS.dsbmih))) and
(DS.dsbmih.biSize>=DWORD(sizeof(DS.dsbmih)))
then
bmiHeader:=DS.dsbmih
else
begin
bmiHeader.biSize:=SizeOf(bmiHeader);
bmiHeader.biWidth:=DS.dsbm.bmWidth;
bmiHeader.biHeight:=DS.dsbm.bmHeight;
bmiHeader.biBitCount:=DS.dsbm.bmBitsPixel;
bmiHeader.biPlanes:=DS.dsbm.bmPlanes;
end;
if bmiHeader.biClrImportant>bmiHeader.biClrUsed
then
bmiHeader.biClrImportant:=bmiHeader.biClrUsed;
if bmiHeader.biSizeImage=0
then
bmiHeader.biSizeImage:=BytesPerScanLine(bmiHeader.biWidth,bmiHeader.biBitCount,32)*Abs(bmiHeader.biHeight);
Assert(bmiHeader.biBitCount<>16);
Assert(bmiHeader.biCompression=0);
if bmiHeader.biBitCount>16
then
iRgbTable:=0
else
if bmiHeader.biClrUsed=0
then
iRgbTable:=SizeOf(TRGBQuad)*(1 shl bmiHeader.biBitCount)
else
iRgbTable:=SizeOf(TRGBQuad)*bmiHeader.biClrUsed;
if iRgbTable>0
then
BitmapInfo:=ReallocMemory(BitmapInfo,SizeOf(TBitmapInfoHeader)+iRgbTable);
setLength(Bits,bmiHeader.biSizeImage);
end;
DC:=CreateCompatibleDC(0);
try
Result:=GetDIBits(DC,Bitmap,0,BitmapInfo^.bmiHeader.biHeight,@Bits[0],BitmapInfo^,DIB_RGB_COLORS)<>0;
finally
DeleteDC(DC);
end;
except
freeMem(BitmapInfo,SizeOf(BitmapInfo^.bmiHeader)+iRgbTable);
raise;
end;
end;
procedure TIcoFile.loadFromHandle(h:hicon);
var
IconInfo:TIconInfo;
Size:Cardinal;
i:integer;
begin
try
setLength(FIcons,length(FIcons)+1);
with FIcons[high(FIcons)] do
begin
Assert(GetIconInfo(h,IconInfo));
InternalGetDIB(IconInfo.hbmColor,iRgbTable,BitmapInfo,ImageBits);
InternalGetDIB(IconInfo.hbmMask,i,MaskBitmapInfo,MaskBits);
// MaskBitmapInfo ????? ???????????? ?????? ??? ?????????, ??? ?????????? ? ???? ??? ?? ?????}
with Info do
begin
Colors:=0;
Width:=BitmapInfo^.bmiHeader.biWidth;
Height:=BitmapInfo^.bmiHeader.biHeight;
Reserved1:=MaskBitmapInfo^.bmiHeader.biBitCount;
Reserved2:=BitmapInfo^.bmiHeader.biBitCount;
DIBSize:=MaskBitmapInfo^.bmiHeader.biSizeImage+DWORD(iRgbTable)+BitmapInfo^.bmiHeader.biSize+BitmapInfo^.bmiHeader.biSizeImage;
DIBOffset:=-1; // ???? ?????????? ??? ??????????.
end;
with BitmapInfo^.bmiHeader do
begin
ImageLineWidth:=BytesPerScanline(biWidth,biBitCount,32); // ?? ???? ?????????? ?????? ?????
Assert((biWidth*biBitCount+31) div 32*4=ImageLineWidth);
Size:=biHeight*ImageLineWidth; // ? ?????? ?????????? ?????? ????? ???????
Assert(Size=biSizeImage);
biHeight:=biHeight*2; // ??? ?????? ???? ????? ??-?? ??????? ?????
end;
with MaskBitmapInfo^.bmiHeader do
begin
MaskLineWidth:=BytesPerScanline(biWidth,biBitCount,32);
Assert((biWidth+31) div 32*4=MaskLineWidth); // ????????
Size:=biHeight*MaskLineWidth; // ?????? ????? (1-??????)
Assert(Size=biSizeImage);
end;
end;
except
setLength(FIcons,length(FIcons)-1);
end;
end;
procedure TIcoFile.saveTrueColorFrom32(icoNo:integer;var IconData:TIconData);
var
useAlpha:boolean;
Bitmap1,Bitmap2:Graphics.TBitmap;
Size:integer;
procedure setNewBitmap(h:HBITMAP);
begin
with IconData do
begin
if BitmapInfo<>nil
then
freeMem(BitmapInfo,sizeOf(BitmapInfo^.bmiHeader)+iRgbTable);
InternalGetDIB(h,iRgbTable,BitmapInfo,ImageBits);
with Info do
begin
Colors:=0;
Width:=BitmapInfo^.bmiHeader.biWidth;
Height:=BitmapInfo^.bmiHeader.biHeight;
Reserved1:=MaskBitmapInfo^.bmiHeader.biBitCount;
Reserved2:=BitmapInfo^.bmiHeader.biBitCount;
DIBSize:=MaskBitmapInfo^.bmiHeader.biSizeImage+DWORD(iRgbTable)+BitmapInfo^.bmiHeader.biSize+BitmapInfo^.bmiHeader.biSizeImage;
DIBOffset:=-1; // ???? ?????????? ??? ??????????.
end;
with BitmapInfo^.bmiHeader do
begin
ImageLineWidth:=BytesPerScanline(biWidth,biBitCount,32); // ?? ???? ?????????? ?????? ?????
Assert((biWidth*biBitCount+31) div 32*4=ImageLineWidth);
Size:=biHeight*ImageLineWidth; // ????????? ?????? ????? ???????
Assert(Size=biSizeImage);
biHeight:=biHeight*2;
end;
end;
end;
begin
useAlpha:=FIcons[icoNo].BitmapInfo.bmiHeader.biBitCount=32;
if useAlpha
then
useAlpha:=IsValidAlpha(icoNo);
try
IconData:=FIcons[high(FIcons)];
Bitmap1:=Graphics.TBitmap.Create;
Bitmap2:=Graphics.TBitmap.Create;
try
with IconData do
begin
MaskBits:=copy(FIcons[high(FIcons)].MaskBits);
getMem(MaskBitmapInfo,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*2);
move(FIcons[high(FIcons)].MaskBitmapInfo^,MaskBitmapInfo^,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*2);
with MaskBitmapInfo^.bmiHeader do
begin
Assert(MaskLineWidth=BytesPerScanline(biWidth,biBitCount,32));
MaskLineWidth:=BytesPerScanline(biWidth,biBitCount,32);
Assert((biWidth+31) div 32*4=MaskLineWidth);
Size:=biHeight*MaskLineWidth;
Assert(Size=biSizeImage);
end;
BitmapInfo:=nil; // ??? ????? ???? ????? ??????, ? ?? ????? ?????
with FIcons[high(FIcons)] do
begin
Bitmap2.Width:=Info.Width;
Bitmap2.Height:=Info.Height;
Bitmap2.PixelFormat:=pf24bit;
if useAlpha
then
begin
Bitmap1.Width:=Info.Width;
Bitmap1.Height:=Info.Height;
Bitmap1.PixelFormat:=pf24bit;
Bitmap1.Canvas.Brush.Color:=clWhite;
Bitmap1.Canvas.FillRect(Rect(0,0,Bitmap1.Width,Bitmap1.Height));
// ?????? ? Bitmap1 ?????? ??? ?? ????? ?????
setNewBitmap(Bitmap1.Handle);
Bitmap2.Canvas.Brush.Color:=clWhite;
Bitmap2.Canvas.FillRect(Rect(0,0,Bitmap2.Width,Bitmap2.Height));
draw(IconData,0,0,Bitmap2.Canvas.Handle,true,true,false);
// ?????? ?? ?????? ??????? - ????? ??? ?? ????? ?????
draw(IcoNo,0,0,Bitmap2.Canvas.Handle,true,true,useAlpha);
end
else
begin
// ???? ??? ???????????, ?? ?????? ??????? ?? ?????? ????
Bitmap2.Canvas.Brush.Color:=clBlack;
Bitmap2.Canvas.FillRect(Rect(0,0,Bitmap2.Width,Bitmap2.Height));
draw(IcoNo,0,0,Bitmap2.Canvas.Handle,true,true,useAlpha);
end;
setNewBitmap(Bitmap2.Handle);
end;
end;
finally
Bitmap1.Free;
Bitmap2.Free;
end;
except
setLength(FIcons,length(FIcons)-1);
end;
end;
procedure TIcoFile.saveToStream(Stream:TStream);
var
FileHeader:TCursorOrIcon;
i:integer;
offset:integer;
begin
FileHeader.Reserved:=0;
FileHeader.wType:=RC3_ICON;
FileHeader.Count:=length(FIcons);
Stream.WriteBuffer(FileHeader,SizeOf(FileHeader));
offset:=Stream.Position+length(FIcons)*SizeOf(TIconRec); // ??????? ???????? ?????
for i:=low(FIcons) to high(FIcons) do
with FIcons[i] do
begin
Info.DIBOffset:=offset;
Stream.WriteBuffer(Info,SizeOf(TIconRec));
offset:=offset+SizeOf(BitmapInfo^.bmiHeader)+iRgbTable+length(ImageBits)+length(MaskBits);
end;
for i:=low(FIcons) to high(FIcons) do
with FIcons[i] do
begin
Stream.WriteBuffer(BitmapInfo^.bmiHeader,SizeOf(BitmapInfo^.bmiHeader)+iRgbTable);
Stream.WriteBuffer(ImageBits[0],length(ImageBits));
Stream.WriteBuffer(MaskBits[0],length(MaskBits));
end;
end;
procedure TIcoFile.check;
var
i:integer;
begin
// ??? ????? ?????????, ??? ? ????????? ???????, ??? ? ????.
// ????? ????? ?????????, ????? ????????? ???????????? ???????? ? ?????? ?? ?????? ??????
// ????? ???????? ?????.
// "????????? - ??? ???????? ? ????."
for i:=low(FIcons) to high(FIcons) do
with FIcons[i] do
begin
Assert((Info.Reserved1=0)=(Info.Reserved2=0)); // ????? ???? ?????? ????????????
Assert((Info.Colors<>0) or (Info.Reserved1<>0));
Assert(Info.Reserved1 in [0,1]);
with BitmapInfo^.bmiHeader do
begin
Assert(biSize=sizeOf(BitmapInfo^.bmiHeader));
Assert(Info.Width=biWidth);
Assert(Info.Height*2=biHeight);
Assert(biPlanes=1);
Assert(Info.Reserved2 in [0,biBitCount]);
Assert(biBitCount in [1,4,8,16,24,32]);
Assert(biCompression=BI_RGB{=0});
Assert(biXPelsPerMeter=0);
Assert(biYPelsPerMeter=0);
end;
end;
end;
procedure TIcoFile.draw(icoNo,x,y:integer;dest:hdc;drawMask,drawImage,drawAlpha:boolean);
begin
draw(Icons[icoNo],x,y,dest,drawMask,drawImage,drawAlpha);
end;
procedure TIcoFile.draw(IconData:TIconData;x,y:integer;dest:hdc;drawMask,drawImage,drawAlpha:boolean);
var
h,hdcColor,hdcMask:hdc;
pcolorBits:pointer;
colorBitmap,hOldC,maskBitmap,hOldM:HBITMAP;
pmaskBits:pointer;
blend:BLENDFUNCTION;
procedure needMaskBitmapInfo(var IconData:TIconData);
begin
with IconData do
begin
if MaskBitmapInfo<>nil
then
exit;
// ??? ??? ?????????? ? ???? ?? ????? ?????????
getMem(MaskBitmapInfo,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*2);
FillChar(MaskBitmapInfo^,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*2,0);
with MaskBitmapInfo^ do
begin
bmiHeader.biSize:=sizeof(BITMAPINFOHEADER);
bmiHeader.biWidth:=BitmapInfo^.bmiHeader.biWidth;
bmiHeader.biHeight:=BitmapInfo^.bmiHeader.biHeight;
bmiHeader.biPlanes:=1;
bmiHeader.biBitCount:=1;
bmiHeader.biSizeImage:=length(MaskBits);
bmiColors[0].rgbReserved:=0;
bmiColors[0].rgbRed:=0;
bmiColors[0].rgbBlue:=0;
bmiColors[0].rgbGreen:=0;
with PRGBQuadArray(@bmiColors)^[1] do
begin
rgbReserved:=0;
rgbRed:=255;
rgbBlue:=255;
rgbGreen:=255;
end;
end;
end;
end;
begin
h:=GetDC(0);
pcolorBits:=nil;
colorBitmap:=CreateDIBSection(h,IconData.BitmapInfo^,DIB_RGB_COLORS,pcolorBits,0,0);
hdcColor:=CreateCompatibleDC(h);
ReleaseDC(0,h);
hOldC:=SelectObject(hdcColor,colorBitmap);
with IconData do
begin
if not drawAlpha
then
begin
needMaskBitmapInfo(IconData);
pmaskBits:=nil;
maskBitmap:=CreateDIBSection(0,MaskBitmapInfo^,DIB_RGB_COLORS,pmaskBits,0,0);
hdcMask:=CreateCompatibleDC(0);
hOldM:=SelectObject(hdcMask,maskBitmap);
SetDIBitsToDevice(hdcMask,0,0,Info.Width,Info.Height,0,0,0,Info.Height,MaskBits,MaskBitmapInfo^,DIB_RGB_COLORS);
if drawMask
then
BitBlt(dest,0,0,Info.Width,Info.Height,hdcMask,0,0,SRCAND);
end
else
begin // ????????? ????? ???????????
hdcMask:=0;
maskBitmap:=0;
hOldM:=0;
end;
SetDIBitsToDevice(hdcColor,0,0,Info.Width,Info.Height,0,0,0,Info.Height,ImageBits,BitmapInfo^,DIB_RGB_COLORS);
if drawImage
then
if not drawAlpha
then
BitBlt(dest,0,0,Info.Width,Info.Height,hdcColor,0,0,SRCINVERT)
else
begin
// ?????????????? ????????? ??? ????? ?????-?????? ???????
blend.BlendOp:=AC_SRC_OVER;
blend.BlendFlags:=0;
blend.SourceConstantAlpha:=255;
blend.AlphaFormat:=AC_SRC_ALPHA;
windows.AlphaBlend(dest,0,0,Info.Width,Info.Height,hdcColor,0,0,Info.Width,Info.Height,blend);
end;
end;
//??????????? ??????????????? ??????? GDI ? ?????????? ???????????? ???????, ????? ???????? ?????? ????????, ?????????? ???????? ?????? ??????????.
SelectObject(hdcColor,hOldC);
if not drawAlpha
then
begin
SelectObject(hdcMask,hOldM);
DeleteObject(hdcMask);
DeleteObject(maskBitmap);
end;
DeleteObject(hdcColor);
DeleteObject(colorBitmap);
end;
function TIcoFile.IsValidAlpha(icoNo:integer):boolean;
var
X,Y:integer;
Line32:PRGBQuadArray;
begin
Result := False;
with Icons[icoNo] do
begin
if BitmapInfo.bmiHeader.biBitCount<=24 then
exit;
for Y:=0 to Info.Height-1 do
begin
Line32:=@ImageBits[Y*ImageLineWidth];
for X:=0 to Info.Width-1 do // ?????? shGetFileInfo ?????????? ?????? ? ?????? ?????-???????
begin
if not (Line32[X].rgbReserved=0) then
begin
result:=true;
break;
end;
end;
if (Result) then
break;
end;
end;
end;
function getIconHandleForFile(fn:string;large:boolean):hicon;
var
shfi:TShFileInfo;
flag:integer;
begin
fillChar(shfi,SizeOf(TShFileInfo),0);
if large
then
flag:=SHGFI_LARGEICON
else
flag:=SHGFI_SMALLICON;
shGetFileInfo(pchar(fn),0,shfi,SizeOf(shfi),SHGFI_ICON or flag or SHGFI_SYSICONINDEX);
result:=shfi.hIcon;
end;
type
TChunkIHDRHack=
class(TChunkIHDR);
procedure Convert(const PNG:TPNGObject;const IconData:TIconData);
var
X,Y,Y2,BitCount:Integer;
BitBuf:Byte;
Line32:PRGBQuadArray;
PNGLine:pRGBLine;
Alpha:PByteArray;
Mask:PByte;
OnlyAlpha:boolean;
begin
with
IconData do
begin
PNG.Header.Width:=Info.Width;
PNG.Header.Height:=Info.Height;
PNG.Header.BitDepth:=8;
PNG.Header.ColorType:=COLOR_RGBALPHA;
TChunkIHDRHack(PNG.Header).PrepareImageData;
BitBuf:=0;
onlyAlpha:=true;
for Y:=0 to Info.Height-1 do
begin
Line32:=@ImageBits[Y*ImageLineWidth];
for X:=0 to Info.Width-1 do
if Line32[X].rgbReserved=0
then
begin
onlyAlpha:=false;
break;
end;
if not onlyAlpha
then
break;
end;
for Y:=0 to Info.Height-1 do
begin
Line32:=@ImageBits[Y*ImageLineWidth];
Mask:=@MaskBits[Y*MaskLineWidth];
Y2:=Info.Height-Y-1;
Alpha:=PNG.AlphaScanline[Y2];
PNGLine:=PNG.ScanLine[Y2];
BitCount:=0;
for X:=0 to Info.Width-1 do
begin
if BitCount=0
then
begin
BitCount:=8;
BitBuf:=Mask^;
Inc(Mask);
end;
if BitBuf and $80=0
then
with PNGLine[X],Line32[X] do
begin
rgbtRed:=rgbRed;
rgbtGreen:=rgbGreen;
rgbtBlue:=rgbBlue;
if not onlyAlpha
then
Alpha[X]:=rgbReserved
else
Alpha[X]:=$FF;
end
else
with PNGLine[X] do
begin
rgbtRed:=0;
rgbtGreen:=0;
rgbtBlue:=0;
Alpha[X]:=0;
end;
BitBuf:=BitBuf shl 1;
Dec(BitCount);
end;
end;
end;
end;
procedure TIcoFile.ConvertToPNG(icoNo:integer;Stream:TStream);
var
PNG:TPNGObject;
C:TChunkIHDR;
begin
with
Icons[icoNo] do
begin
Assert(BitmapInfo.bmiHeader.biBitCount>=24);
if IsValidAlpha(icoNo)
then
begin //mrcavalcanti - 10/11/2008
PNG:=TPNGObject.Create; //CreateBlank(COLOR_RGBALPHA,8,Info.Width,Info.Height)
{Important chunks}
RegisterChunk(TChunkIEND);
RegisterChunk(TChunkIHDR);
RegisterChunk(TChunkIDAT);
RegisterChunk(TChunkPLTE);
RegisterChunk(TChunkgAMA);
RegisterChunk(TChunktRNS);
{Not so important chunks}
RegisterChunk(TChunktIME);
RegisterChunk(TChunktEXt);
RegisterChunk(TChunkzTXt);
end
else
PNG:=TPNGObject.Create;// CreateBlank(COLOR_RGB,8,Info.Width,Info.Height);
C := TChunkIHDR.Create(PNG);
PNG.Chunks.Add(TChunkClass(C));
//PNG.Header.ColorType := COLOR_RGB;
//PNG.Header.BitDepth := 8;
//PNG.Width := Info.Width;
//PNG.Height := Info.Height;
try
Convert(PNG,Icons[icoNo]);
PNG.CompressionLevel:=9;
PNG.SaveToStream(Stream);
finally
PNG.Free;
end;
end;
end;
procedure TIcoFile.saveAsPng(icoNo:integer;fn:string);
var
Stream:TStream;
begin
Stream:=TFileStream.Create(fn,fmCreate);
try
ConvertToPNG(icoNo,Stream);
finally
Stream.Free;
end;
end;
procedure TIcoFile.Add(IconData:TIconData);
begin
setLength(FIcons,length(FIcons)+1);
Icons[high(FIcons)]:=IconData;
end;
procedure TIcoFile.AddCopy(IconData:TIconData);
begin
setLength(FIcons,length(FIcons)+1);
Icons[high(FIcons)]:=IconData;
with Icons[high(FIcons)] do
begin
ImageBits:=copy(IconData.ImageBits);
MaskBits:=copy(IconData.MaskBits);
getMem(BitmapInfo,sizeof(BITMAPINFOHEADER)+iRgbTable);
getMem(MaskBitmapInfo,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*2);
move(IconData.BitmapInfo^,BitmapInfo^,sizeof(BITMAPINFOHEADER)+iRgbTable);
move(IconData.MaskBitmapInfo^,MaskBitmapInfo^,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*2);
end;
end;
end.