-
Notifications
You must be signed in to change notification settings - Fork 8
/
BigBmpClass.cpp
1501 lines (1426 loc) · 61.7 KB
/
BigBmpClass.cpp
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//---------------------------------------------------------------------------
/*
Copyright (c) 2018 Victor Sheinmann, Vicshann@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma hdrstop
#include "BigBmpClass.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//===========================================================================
//
// P U B L I C
//
//===========================================================================
_stdcall CBIGBITMAP::CBIGBITMAP(void)
{
SYSTEM_INFO sinf;
GetSystemInfo(&sinf);
MapPageSize = sinf.dwAllocationGranularity;
DefHeap = GetProcessHeap();
hFileBmp = INVALID_HANDLE_VALUE;
BmpFExtended = false;
CallBackProc = NULL;
RowIndexBuff = NULL;
ResAverBufPtr = NULL;
MemBmpBuffer = NULL;
DecRowBuffer = NULL;
ReadedRowsBuf = NULL;
MappedSection = NULL;
BmpInfoProcs = NULL;
BmpInfoOrig = NULL;
IndexBuffer = NULL;
BitmapInfo = NULL;
hMappedBmp = NULL;
DstWidth = 0;
DstHeight = 0;
ImageType = 0;
InitializeCriticalSection(&CSecRowsMap);
}
//===========================================================================
_stdcall CBIGBITMAP::~CBIGBITMAP()
{
FreeBitmap();
DeleteCriticalSection(&CSecRowsMap);
}
//===========================================================================
void _stdcall CBIGBITMAP::SetCallBack(PROGRESSCALLBACK CallBack)
{
__GUARDNEEDED(ProcGuardEnter());
CallBackProc = CallBack;
__GUARDNEEDED(ProcGuardExit());
}
//===========================================================================
DWORD _stdcall CBIGBITMAP::SetView(HDC TargetDC, RECT *DstRect, RECT *SrcRect)
{
DWORD BmpBufSize;
__GUARDNEEDED(ProcGuardEnter());
if(TargetDC){DstWndDC = TargetDC;if(!ImageType){__GUARDNEEDED(ProcGuardExit());return 0;}}
if(!ImageType){__GUARDNEEDED(ProcGuardExit());return 1;}
if(SrcRect)
{ // Safe RECT change
ViewRect.top = (SrcRect->top > BmpHeight)?(0):SrcRect->top;
ViewRect.left = (SrcRect->left > BmpWidth)?(0):SrcRect->left;
ViewRect.right = ((ViewRect.left+SrcRect->right) > BmpWidth)?(BmpWidth-ViewRect.left):SrcRect->right; // This is right ? (Need only for resized view)
ViewRect.bottom = ((ViewRect.top+SrcRect->bottom) > BmpHeight)?(BmpHeight-ViewRect.top):SrcRect->bottom; // This is right ? (Need only for resized view)
}
if(DstRect && !EqualRect(&TargetRect,DstRect))
{
// Recreate View Buffer
CopyRect(&TargetRect,DstRect);
if(MemBmpBuffer)VirtualFree(MemBmpBuffer,NULL,MEM_RELEASE);
// Get width and height of destination rect
DstWidth = (TargetRect.right-TargetRect.left);
DstHeight = (TargetRect.bottom-TargetRect.top);
BmpBufSize = (DstWidth*sizeof(DWORD)); // Assume as used 32bpp - Max
BmpBufSize = _ALIGN_FRWRD(BmpBufSize,sizeof(DWORD))*DstHeight; // Size of window in bytes
MemBmpBuffer = VirtualAlloc(NULL,BmpBufSize,MEM_COMMIT,PAGE_READWRITE);
RowsToBuffer = (DstHeight/ROWBUFDIV); // This is Fine ????
SrcWidth = ((DstWidth > BmpWidth)&&!ResizePixels)?BmpWidth:DstWidth;
SrcHeight = ((DstHeight > BmpHeight)&&!ResizePixels)?BmpHeight:DstHeight;
DRowWidth = ((ViewRect.left+SrcWidth) > BmpWidth)?(BmpWidth-ViewRect.left):SrcWidth;
DstRowSize = (ResizePixels)?(SrcWidth):DRowWidth;
DstRowSize = (DstBytesPP*DstRowSize);
ADstRowSize = _ALIGN_FRWRD(DstRowSize,sizeof(DWORD));
BmpViewInfoHdr->bmiHeader.biWidth = SrcWidth; // For currently selected header
BmpViewInfoHdr->bmiHeader.biHeight = SrcHeight*(-1);
}
BmpBufSize = UpdateBitmapBuffer();
__GUARDNEEDED(ProcGuardExit());
return BmpBufSize; // Error code from "UpdateBitmapBuffer"
}
//===========================================================================
DWORD _stdcall CBIGBITMAP::SetProcessing(bool Resizing, bool Grayscale, bool Sharpen, bool Blur, int ResQuality, int Contrast, int Brightness)
{
if(!ImageType)return 1;
__GUARDNEEDED(ProcGuardEnter());
ResizeQuality = ResQuality;
ProcessPixels = (Blur||Sharpen||Grayscale||Contrast||Brightness);
ResizePixels = Resizing;
FGrayscale = Grayscale;
FBrightness = Brightness;
FContrast = Contrast;
FSharpen = Sharpen;
FBlur = Blur;
if(Resizing||(ProcessPixels&&(BytesInPixel<3)))
{
BmpInfoProcs->bmiHeader.biWidth = BmpViewInfoHdr->bmiHeader.biWidth;
BmpInfoProcs->bmiHeader.biHeight = BmpViewInfoHdr->bmiHeader.biHeight;
BmpViewInfoHdr = BmpInfoProcs;
DstBytesPP = 4;
}
else
{
BmpInfoOrig->bmiHeader.biWidth = BmpViewInfoHdr->bmiHeader.biWidth;
BmpInfoOrig->bmiHeader.biHeight = BmpViewInfoHdr->bmiHeader.biHeight;
BmpViewInfoHdr = BmpInfoOrig;
DstBytesPP = BytesInPixel;
}
if(ResizePixels && !ResAverBufPtr){if(!(ResAverBufPtr = (RESIZEAVERBUF*)VirtualAlloc(NULL,(BaseBmpWidth*sizeof(RESIZEAVERBUF)),MEM_COMMIT,PAGE_READWRITE))){__GUARDNEEDED(ProcGuardExit());return 1;}} // Allocate color averaging buffer (used for fast averaging row by row)
SrcWidth = ((DstWidth > BmpWidth)&&!ResizePixels)?BmpWidth:DstWidth;
SrcHeight = ((DstHeight > BmpHeight)&&!ResizePixels)?BmpHeight:DstHeight;
DRowWidth = ((ViewRect.left+SrcWidth) > BmpWidth)?(BmpWidth-ViewRect.left):SrcWidth;
DstRowSize = (ResizePixels)?(SrcWidth):DRowWidth;
DstRowSize = (DstBytesPP*DstRowSize);
ADstRowSize = _ALIGN_FRWRD(DstRowSize,sizeof(DWORD));
BmpViewInfoHdr->bmiHeader.biWidth = SrcWidth; // For currently selected header
BmpViewInfoHdr->bmiHeader.biHeight = SrcHeight*(-1);
__GUARDNEEDED(ProcGuardExit());
return 0;
}
//===========================================================================
DWORD _stdcall CBIGBITMAP::SelectImageIndex(int Index)
{
int Width;
int Height;
int Offset;
__GUARDNEEDED(ProcGuardEnter());
if((!ImageType)||(Index >= BmpAHdr.LevelsCount)||(Index == BAGImageIndex)){__GUARDNEEDED(ProcGuardExit());return 1;}
Width = BmpAHdr.BaseWidth;
Height = BmpAHdr.BaseHeight;
Offset = 0;
for(int ctr=0;ctr<Index;ctr++){Offset+=Height;Height=(Height/2);Width=(Width/2);}
RowIndexBuff = &((DWORD*)IndexBuffer)[Offset];
UsedRowSize = (Width*BytesInPixel); // Number of used bytes in row
AlignedRowSize = _ALIGN_FRWRD(UsedRowSize,sizeof(DWORD));
BmpHeight = Height;
BmpWidth = Width;
PrvRowIndex = -1;
PrvPixCount = -1;
PrvPixOffset = -1;
FirstRowLoaded = -1;
TotalRowsLoaded = -1;
SrcWidth = ((DstWidth > BmpWidth)&&!ResizePixels)?BmpWidth:DstWidth;
SrcHeight = ((DstHeight > BmpHeight)&&!ResizePixels)?BmpHeight:DstHeight;
if(Index > BAGImageIndex)
{ // Keep position in smaller image (Safe shrink)
Offset = (1 << (Index - BAGImageIndex)); // 2 pow (Index - BAGImageIndex)
ViewRect.top = (SrcHeight < BmpHeight)?(ViewRect.top / Offset):0;
ViewRect.left = (SrcWidth < BmpWidth)?(ViewRect.left / Offset):0;
if((ViewRect.top+SrcHeight) > BmpHeight)ViewRect.top = (BmpHeight-SrcHeight);
if((ViewRect.left+SrcWidth) > BmpWidth)ViewRect.left = (BmpWidth-SrcWidth);
}
else
{ // Keep position in bigger image
Offset = (Index - BAGImageIndex)*2;
ViewRect.top = (ViewRect.top * Offset);
ViewRect.left = (ViewRect.left * Offset);
}
DRowWidth = ((ViewRect.left+SrcWidth) > BmpWidth)?(BmpWidth-ViewRect.left):SrcWidth;
DstRowSize = (ResizePixels)?(SrcWidth):DRowWidth;
DstRowSize = (DstBytesPP*DstRowSize);
ADstRowSize = _ALIGN_FRWRD(DstRowSize,sizeof(DWORD));
BAGImageIndex = Index;
BmpViewInfoHdr->bmiHeader.biWidth = SrcWidth; // For currently selected header
BmpViewInfoHdr->bmiHeader.biHeight = SrcHeight*(-1);
__GUARDNEEDED(ProcGuardExit());
return 0;
}
//===========================================================================
DWORD _stdcall CBIGBITMAP::SetDrawExParams(int PosX, int PosY, int DestWidth, int DestHeight, DWORD RasterOp, DWORD TranspClr, DWORD TranspMode)
{
if(!ImageType)return 1;
__GUARDNEEDED(ProcGuardEnter());
DExPosX = PosX;
DExPosY = PosY;
DExWidth = DestWidth;
DExHeight = DestHeight;
DExRasterOp = RasterOp;
DExTranspMode = TranspMode;
DExTranspClr = (TranspMode > 1)?(TrpModeTwoClr):(TranspClr);
__GUARDNEEDED(ProcGuardExit());
return 0;
}
//===========================================================================
DWORD _stdcall CBIGBITMAP::GetBitmapInfo(BIGBMPINFO *BigInfo)
{
if(!ImageType){ZeroMemory(BigInfo,sizeof(BIGBMPINFO));return 1;}
__GUARDNEEDED(ProcGuardEnter());
CopyRect(&BigInfo->ImageRect,&ViewRect);
CopyRect(&BigInfo->DcRect,&TargetRect);
BigInfo->ExTranspMode = DExTranspMode;
BigInfo->ExTranspClr = DExTranspClr;
BigInfo->ExRop = DExRasterOp;
BigInfo->ExPosX = DExPosX;
BigInfo->ExPosY = DExPosY;
BigInfo->ExWidth = DExWidth;
BigInfo->ExHeight = DExHeight;
BigInfo->Width = BmpWidth;
BigInfo->Height = BmpHeight;
BigInfo->BitsPP = (BytesInPixel*8);
BigInfo->Images = ImagesNumber;
BigInfo->IType = ImageType;
BigInfo->CurImage = BAGImageIndex;
BigInfo->Contrast = FContrast;
BigInfo->Brightness = FBrightness;
BigInfo->ResQuality = ResizeQuality;
BigInfo->Resizing = ResizePixels;
BigInfo->Grayscale = FGrayscale;
BigInfo->Sharpen = FSharpen;
BigInfo->Blur = FBlur;
__GUARDNEEDED(ProcGuardExit());
return 0;
}
//===========================================================================
DWORD _stdcall CBIGBITMAP::ReDraw(void)
{
// SetDIBitsToDevice flips rows of BOTTOMTOP bitmaps to normal TOPBOTTOM
__GUARDNEEDED(ProcGuardEnter());
if(!MemBmpBuffer || !ImageType){__GUARDNEEDED(ProcGuardExit());return 1;}
if(!SetDIBitsToDevice(DstWndDC,
TargetRect.left, // X pos of SrcLeftCorner in Target DC
TargetRect.top, // Y pos of SrcLeftCorner in Target DC
SrcWidth, // Trim to target DC width, width may be greater - it is safe
SrcHeight, // (For correct target DC pos, must be equal real number of used scanlines)if greater real image height, difference offsets from top of target window
0,
0, // Y offset controlled by scanline parameters
0,
SrcHeight, // Draws only specified number of lines(TOPDOWN trims down,BOTTOMUP trims up)
MemBmpBuffer,
BmpViewInfoHdr,
DIB_RGB_COLORS)){__GUARDNEEDED(ProcGuardExit());return 2;}
__GUARDNEEDED(ProcGuardExit());
return 0;
}
//===========================================================================
DWORD _stdcall CBIGBITMAP::ReDrawEx(void)
{
// SetDIBitsToDevice flips rows of BOTTOMTOP bitmaps to normal TOPBOTTOM
__GUARDNEEDED(ProcGuardEnter());
if(!MemBmpBuffer || !ImageType){__GUARDNEEDED(ProcGuardExit());return 1;}
if(DExTranspMode)
{
if(!TranspStrDIBits(DstWndDC,
DExPosX, // X pos of SrcLeftCorner in Target DC
DExPosY, // Y pos of SrcLeftCorner in Target DC
DExWidth,
DExHeight,
0,
0,
SrcWidth,
SrcHeight,
MemBmpBuffer,
BmpViewInfoHdr,
((DExTranspMode > 1)?(TrpModeTwoClr):(DExTranspClr)))){__GUARDNEEDED(ProcGuardExit());return GetLastError();}
}
else
{
if(!StretchDIBits(DstWndDC,
DExPosX, // X pos of SrcLeftCorner in Target DC
DExPosY, // Y pos of SrcLeftCorner in Target DC
DExWidth,
DExHeight,
0,
0,
SrcWidth,
SrcHeight,
MemBmpBuffer,
BmpViewInfoHdr,
DIB_RGB_COLORS,
DExRasterOp)){__GUARDNEEDED(ProcGuardExit());return GetLastError();}
}
__GUARDNEEDED(ProcGuardExit());
return 0;
}
//===========================================================================
DWORD _stdcall CBIGBITMAP::FreeBitmap(void)
{
__GUARDNEEDED(ProcGuardEnter());
if(MappedSection){UnmapViewOfFile(MappedSection);MappedSection=NULL;ReadedRowsBuf=NULL;}
if(hMappedBmp){CloseHandle(hMappedBmp);hMappedBmp=NULL;}
if(hFileBmp != INVALID_HANDLE_VALUE){if(BmpFExtended){SetFilePointer(hFileBmp,-((int)(sizeof(DWORD))),NULL,FILE_END);SetEndOfFile(hFileBmp);BmpFExtended=false;}CloseHandle(hFileBmp);hFileBmp = INVALID_HANDLE_VALUE;} // Remove if needed exess bytes from file
if(BitmapInfo){HeapFree(DefHeap, NULL, BitmapInfo);BitmapInfo=NULL;}
if(BmpInfoProcs){HeapFree(DefHeap, NULL, BmpInfoProcs);BmpInfoProcs=NULL;}
if(BmpInfoOrig){HeapFree(DefHeap, NULL, BmpInfoOrig);BmpInfoOrig=NULL;}
if(ResAverBufPtr){VirtualFree(ResAverBufPtr,NULL,MEM_RELEASE);ResAverBufPtr=NULL;}
if(MemBmpBuffer){VirtualFree(MemBmpBuffer,NULL,MEM_RELEASE);MemBmpBuffer=NULL;}
if(DecRowBuffer){VirtualFree(DecRowBuffer,NULL,MEM_RELEASE);DecRowBuffer=NULL;}
if(IndexBuffer){VirtualFree(IndexBuffer,NULL,MEM_RELEASE);IndexBuffer=NULL;RowIndexBuff=NULL;}
ZeroMemory(&ViewRect,sizeof(RECT));
ZeroMemory(&TargetRect,sizeof(RECT));
ZeroMemory(&BitmapInfo,sizeof(BITMAPINFO));
ZeroMemory(&BmpAHdr,sizeof(BMPARCHIVEHDR));
FirstRowLoaded = -1;
TotalRowsLoaded = -1;
BAGImageIndex = -1;
PrvRowIndex = -1;
PrvPixCount = -1;
PrvPixOffset = -1;
DExWidth = 0;
DExHeight = 0;
DExTranspClr = 0;
DExTranspMode = 0;
ImagesNumber = 0;
RowsToBuffer = 0;
RowsBufSize = 0;
ImageType = 0;
FContrast = 0;
FBrightness = 0;
FGrayscale = false;
FSharpen = false;
FBlur = false;
BmpFExtended = false;
DExRasterOp = SRCCOPY; // Default ROP
__GUARDNEEDED(ProcGuardExit());
return 0;
}
//===========================================================================
// Set better file reading and mapping attrebutes
//
DWORD _stdcall CBIGBITMAP::LoadBitmap(LPSTR FileName)
{
BITMAPFILEHEADER BmpFHdr;
BMSFHEADER *BmsFHdr;
DWORD FileBytesUsed = 0;
DWORD HdrSize;
DWORD ErrCode = 0;
DWORD Result;
FreeBitmap();
__GUARDNEEDED(ProcGuardEnter());
if((hFileBmp = CreateFile(FileName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL))==INVALID_HANDLE_VALUE){ErrCode=1;goto RExit;}
SetFilePointer(hFileBmp,0,NULL,FILE_BEGIN);
ReadFile(hFileBmp,&BmpFHdr,sizeof(BITMAPFILEHEADER),&Result,NULL);
if(Result != sizeof(BITMAPFILEHEADER)){ErrCode=2;goto RExit;}
switch(BmpFHdr.bfType)
{
case FLAG_BM: // Ordinary BMP file
{
HdrSize = BmpFHdr.bfOffBits; // Do not exclude size of BITMAPFILEHEADER for safe
if(!(BitmapInfo = (BITMAPINFO*)HeapAlloc(DefHeap, HEAP_ZERO_MEMORY, HdrSize))){ErrCode=3;goto RExit;}
ReadFile(hFileBmp,BitmapInfo,(HdrSize-sizeof(BITMAPFILEHEADER)),&Result,NULL); // Read bitmap parameters
if(Result != (HdrSize-sizeof(BITMAPFILEHEADER))){ErrCode=4;goto RExit;}
if((BitmapInfo->bmiHeader.biCompression != BI_RGB)&&(BitmapInfo->bmiHeader.biCompression != BI_BITFIELDS)||(BitmapInfo->bmiHeader.biBitCount < 8)){ErrCode=5;goto RExit;} // Format not supported ( Rle, Jpeg, ...)
FileBytesUsed = (BitmapInfo->bmiHeader.biWidth*(BitmapInfo->bmiHeader.biBitCount/8));
FileBytesUsed = _ALIGN_FRWRD(FileBytesUsed,sizeof(DWORD)); // For safe buffer size detection
FileBytesUsed = ((int)BitmapInfo->bmiHeader.biHeight < 0)?(FileBytesUsed * (-((int)BitmapInfo->bmiHeader.biHeight))):(FileBytesUsed * BitmapInfo->bmiHeader.biHeight);
FileBytesUsed = (FileBytesUsed + HdrSize);
ImagesNumber = 1;
RowsOffset = BmpFHdr.bfOffBits;
ImageType = FLAG_BM;
break;
}
case FLAG_BS: // My Modified RLE with indexed rows file
{
BmsFHdr = ((BMSFHEADER*)&BmpFHdr);
if(!BmsFHdr->IndexOffset){ErrCode=6;goto RExit;} // Supported only indexed RLE
HdrSize = BmsFHdr->IndexOffset; // Indexes always after header; Do not exclude size of BMSFHEADER for safe
if(!(BitmapInfo = (BITMAPINFO*)HeapAlloc(DefHeap, HEAP_ZERO_MEMORY, HdrSize))){ErrCode=7;goto RExit;}
ReadFile(hFileBmp,BitmapInfo,(HdrSize-sizeof(BMSFHEADER)),&Result,NULL); // Read bitmap parameters
if(Result != (HdrSize-sizeof(BMSFHEADER))){ErrCode=8;goto RExit;}
if((BitmapInfo->bmiHeader.biCompression != BI_IRC)||(BitmapInfo->bmiHeader.biBitCount < 8)){ErrCode=9;goto RExit;} // Format not supported ( Not indexed rows, Pixel < 1Byte)
RowsOffset = (BmsFHdr->PixelsOffset-BmsFHdr->IndexOffset); // Use difference, because do not easy get a bitmap height (may be nagative)
if(!(IndexBuffer = VirtualAlloc(NULL,(RowsOffset+sizeof(DWORD)),MEM_COMMIT,PAGE_READWRITE))){ErrCode=10;goto RExit;} // Buffer for row indexes
RowIndexBuff = IndexBuffer;
SetFilePointer(hFileBmp,BmsFHdr->IndexOffset,NULL,FILE_BEGIN);
ReadFile(hFileBmp,RowIndexBuff,RowsOffset,&Result,NULL); // Read row indexes into buffer
if(Result != RowsOffset){ErrCode=11;goto RExit;}
FileBytesUsed = GetComprRowWidthF(hFileBmp,((DWORD*)RowIndexBuff)[(RowsOffset/sizeof(DWORD))-1],BmsFHdr->RleFlag,BitmapInfo->bmiHeader.biWidth,(BitmapInfo->bmiHeader.biBitCount/8));
((DWORD*)RowIndexBuff)[(RowsOffset/sizeof(DWORD))] = (((DWORD*)RowIndexBuff)[(RowsOffset/sizeof(DWORD))-1] + FileBytesUsed); // For last row size detection
FileBytesUsed = ((DWORD*)RowIndexBuff)[(RowsOffset/sizeof(DWORD))];
RowsOffset = (BitmapInfo->bmiHeader.biWidth*(BitmapInfo->bmiHeader.biBitCount/8));
RowsOffset = _ALIGN_FRWRD(RowsOffset,sizeof(DWORD));
if(!(DecRowBuffer = VirtualAlloc(NULL,RowsOffset,MEM_COMMIT,PAGE_READWRITE))){ErrCode=12;goto RExit;} // Buffer for row decompression
BitmapInfo->bmiHeader.biCompression = BI_RGB; // Normal RGB format - For WinAPI
ImagesNumber = 1;
RleFlagCode = BmsFHdr->RleFlag;
RowsOffset = BmsFHdr->PixelsOffset;
ImageType = FLAG_BS;
break;
}
case FLAG_BG: // My Modified RLE with indexed rows pow2 images group file
{
if(((BMPARCHIVEHDR*)&BmpFHdr)->FlagBGAR != FLAG_BAGR){ErrCode=13;goto RExit;} // Unknow file type
SetFilePointer(hFileBmp,0,NULL,FILE_BEGIN);
ReadFile(hFileBmp,&BmpAHdr,sizeof(BMPARCHIVEHDR),&Result,NULL); // Read bitmap parameters
if(Result != sizeof(BMPARCHIVEHDR)){ErrCode=14;goto RExit;}
HdrSize = (BmpAHdr.PaletteSize+sizeof(BITMAPINFO));
if(!(BitmapInfo = (BITMAPINFO*)HeapAlloc(DefHeap, HEAP_ZERO_MEMORY, HdrSize))){ErrCode=15;goto RExit;}
if(BmpAHdr.PaletteSize)
{
ReadFile(hFileBmp,&BitmapInfo->bmiColors,BmpAHdr.PaletteSize,&Result,NULL); // Read bitmap pallette
if(Result != BmpAHdr.PaletteSize){ErrCode=16;goto RExit;}
}
if(!(IndexBuffer = VirtualAlloc(NULL,(BmpAHdr.IndexesSize+sizeof(DWORD)),MEM_COMMIT,PAGE_READWRITE))){ErrCode=17;goto RExit;} // Buffer for row indexes
RowIndexBuff = IndexBuffer;
ReadFile(hFileBmp,RowIndexBuff,BmpAHdr.IndexesSize,&Result,NULL); // Read row indexes into buffer
if(Result != BmpAHdr.IndexesSize){ErrCode=18;goto RExit;}
FileBytesUsed = (BmpAHdr.BaseWidth / (1 << BmpAHdr.LevelsCount));
FileBytesUsed = GetComprRowWidthF(hFileBmp,((DWORD*)RowIndexBuff)[(BmpAHdr.IndexesSize/sizeof(DWORD))-1],BmpAHdr.RlePFlag,FileBytesUsed,BmpAHdr.BytesPerPixel);
((DWORD*)RowIndexBuff)[(BmpAHdr.IndexesSize/sizeof(DWORD))] = (((DWORD*)RowIndexBuff)[(BmpAHdr.IndexesSize/sizeof(DWORD))-1] + FileBytesUsed); // For last row size detection
FileBytesUsed = ((DWORD*)RowIndexBuff)[(BmpAHdr.IndexesSize/sizeof(DWORD))];
RowsOffset = (BmpAHdr.BaseWidth*BmpAHdr.BytesPerPixel);
RowsOffset = _ALIGN_FRWRD(RowsOffset,sizeof(DWORD));
if(!(DecRowBuffer = VirtualAlloc(NULL,RowsOffset,MEM_COMMIT,PAGE_READWRITE))){ErrCode=19;goto RExit;} // Buffer for row decompression
BitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo->bmiHeader.biWidth = BmpAHdr.BaseWidth;
BitmapInfo->bmiHeader.biHeight = BmpAHdr.BaseHeight*(-1); // Always TOPDOWN
BitmapInfo->bmiHeader.biPlanes = 1;
BitmapInfo->bmiHeader.biBitCount = (BmpAHdr.BytesPerPixel*8);
BitmapInfo->bmiHeader.biCompression = BI_RGB;
BitmapInfo->bmiHeader.biSizeImage = (RowsOffset*BmpAHdr.BaseHeight);
BitmapInfo->bmiHeader.biXPelsPerMeter = 0;
BitmapInfo->bmiHeader.biYPelsPerMeter = 0;
BitmapInfo->bmiHeader.biClrUsed = 0;
BitmapInfo->bmiHeader.biClrImportant = 0;
ImagesNumber = BmpAHdr.LevelsCount;
RleFlagCode = BmpAHdr.RlePFlag;
RowsOffset = (sizeof(BMPARCHIVEHDR)+BmpAHdr.PaletteSize+BmpAHdr.IndexesSize);
ImageType = FLAG_BG;
break;
}
default : {ErrCode=20;goto RExit;} // Unknow file type
}
// Determine rows order and true bitmap height
if((int)BitmapInfo->bmiHeader.biHeight < 0){BmpHeight = -((int)BitmapInfo->bmiHeader.biHeight);BmpTopDown=true;}
else {BmpHeight = BitmapInfo->bmiHeader.biHeight;BmpTopDown=false;}
BmpWidth = BitmapInfo->bmiHeader.biWidth;
BaseBmpWidth = BmpWidth;
BytesInPixel = (BitmapInfo->bmiHeader.biBitCount/8);
UsedRowSize = (BmpWidth*BytesInPixel); // Number of used bytes in row
SizeMask = (0xFFFFFFFF >> ((sizeof(DWORD)-BytesInPixel)*8));
FlagCode = (RleFlagCode & SizeMask);
AlignedRowSize = _ALIGN_FRWRD(UsedRowSize,sizeof(DWORD));
if(!(BmpInfoProcs = (BITMAPINFO*)HeapAlloc(DefHeap, HEAP_ZERO_MEMORY, sizeof(BITMAPINFO)))){ErrCode=21;goto RExit;} // ********** SIZE ???
if(!(BmpInfoOrig = (BITMAPINFO*)HeapAlloc(DefHeap, HEAP_ZERO_MEMORY, HdrSize))){ErrCode=22;goto RExit;}
BmpViewInfoHdr = BmpInfoOrig;
ProcessPixels = false;
ResizePixels = false;
ResizeQuality = 0;
BAGImageIndex = 0;
DExWidth = 100; // ???? Use Predefined ??
DExHeight = 100; // ???? Use Predefined ??
DstBytesPP = BytesInPixel;
CopyMemory(BmpInfoOrig,BitmapInfo,HdrSize); // Original header with palette
BmpInfoOrig->bmiHeader.biSizeImage = 0; // Do Not used in BI_RGB
BmpInfoOrig->bmiHeader.biCompression = BI_RGB;
CopyMemory(BmpInfoProcs,BmpInfoOrig,sizeof(BITMAPINFO)); // ************ SIZE ???
BmpInfoProcs->bmiHeader.biClrUsed = 0;
BmpInfoProcs->bmiHeader.biClrImportant = 0;
BmpInfoProcs->bmiHeader.biBitCount = 32; // Processing uses 32 Bpp color - faster
if(GetFileSize(hFileBmp,NULL) < (FileBytesUsed+sizeof(DWORD)))
{
SetFilePointer(hFileBmp,sizeof(DWORD),NULL,FILE_END); // Add mapping buffer to end of file
BmpFExtended = SetEndOfFile(hFileBmp); // Some functions reas mapped section by DWORDs - Results AV if read DWORD in middle of section and unallocated address scpace
}
if(!(hMappedBmp = CreateFileMapping(hFileBmp,NULL,PAGE_READWRITE,0,0,NULL)))ErrCode=23;
TrpModeTwoClr = GetBitmapPixel(0,0); // Most TopLeft pixel as transparent color for mode 2
RExit:
__GUARDNEEDED(ProcGuardExit());
if(ErrCode)FreeBitmap();
return ErrCode;
}
//===========================================================================
// For now, DstRect used only for width and height aquring
//
DWORD _stdcall CBIGBITMAP::SaveBitmap(LPSTR FileName, RECT *DstRect, RECT *SrcRect, DWORD BitPerPix, DWORD Format, DWORD ResQuality, bool Resize, bool Grayscale, bool Sharpen, bool Blur, int Contrast, int Brightness)
{
int DestWidth;
int DestHeight;
int PalIndex;
bool UseFilter;
RECT RSource;
RECT RDestin;
DWORD Result;
DWORD ErrCode;
DWORD FlagRLE;
DWORD HdrSize;
DWORD DataSize;
DWORD ComprSize;
DWORD ImagesNum;
DWORD IndexCount;
DWORD DstBytesPP;
DWORD SrcBytesPP;
DWORD CBackPrvPos;
DWORD CallbackMax;
DWORD SaveRowSize;
PVOID PalettePtr = NULL;
PVOID SrcRowPtr = NULL;
PVOID DstRowPtr = NULL;
PVOID FmtRowPtr = NULL;
PVOID RowToSave = NULL;
PVOID IndexBlock = NULL;
PVOID RowCompBuf = NULL;
PVOID ClrAverBuf = NULL;
HANDLE hSaveFile;
RGBQUAD Palette[256];
BMSFHEADER BSFHdr;
BMPARCHIVEHDR BGFHdr;
BITMAPFILEHEADER BmpFHdr;
BITMAPINFOHEADER BmpIHdr;
OCTREEQUANTIZER *Quantizer = NULL;
if((BitPerPix != 0)&&(BitPerPix != 8)&&(BitPerPix != 16)&&(BitPerPix != 24)&&(BitPerPix != 32))return 1;
if((Format != FLAG_BM)&&(Format != FLAG_BS)&&(Format != FLAG_BG))return 2;
__GUARDNEEDED(ProcGuardEnter());
DeleteFile(FileName);
if((hSaveFile = CreateFile(FileName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL))==INVALID_HANDLE_VALUE){ErrCode=3;goto Exit;}
if(!SrcRect)SetRect(&RSource,0,0,BmpWidth,BmpHeight);
else CopyRect(&RSource,SrcRect);
if(!DstRect){CopyRect(&RDestin,&RSource);Resize=false;}
else CopyRect(&RDestin,DstRect);
DestWidth = (RDestin.right-RDestin.left);
DestHeight = (RDestin.bottom-RDestin.top);
DstBytesPP = (BitPerPix)?(BitPerPix/8):BytesInPixel;
IndexCount = 0;
ImagesNum = 0;
DataSize = 0;
PalIndex = 0;
ErrCode = 5;
// This is instead 'SetView' - very important!
RowsToBuffer = ((MEGABYTE*RBUFSAVEMB)/(DestWidth*4));
if(RowsToBuffer > (DestHeight/2))RowsToBuffer = (DestHeight/2); // Prebuffering of rows
DstWidth = DestWidth;
DstHeight = DestHeight;
UseFilter = (Blur||Sharpen||Grayscale||Contrast||Brightness);
SrcBytesPP = (Resize || (UseFilter && (BytesInPixel < 2)))?(4):BytesInPixel;
PalettePtr = (DstBytesPP != SrcBytesPP)?(&Palette):((PVOID)&BitmapInfo->bmiColors);
SaveRowSize = _ALIGN_FRWRD((DestWidth*DstBytesPP),sizeof(DWORD));
DstRowPtr = HeapAlloc(DefHeap, HEAP_ZERO_MEMORY, (DestWidth*4)+16); // For safe use maximum Bpp
if(!ResAverBufPtr)
{
ClrAverBuf = HeapAlloc(DefHeap, HEAP_ZERO_MEMORY, ((DestWidth+1)*sizeof(RESIZEAVERBUF))); // Temporary allocate the color averaging buffer
ResAverBufPtr = (RESIZEAVERBUF*)ClrAverBuf;
if(!ClrAverBuf){ErrCode=4;goto Exit;}
}
if((DstBytesPP != SrcBytesPP)||(Format == FLAG_BG)){if(!(FmtRowPtr = HeapAlloc(DefHeap, HEAP_ZERO_MEMORY, (DstBytesPP*(DestWidth+4))))){ErrCode=5;goto Exit;}} // 4-For heap safe
if(Format == FLAG_BM)IndexCount = DestHeight;
if((Format == FLAG_BS)||(Format == FLAG_BG))
{
if(Format == FLAG_BS)IndexCount = DestHeight;
if(Format == FLAG_BG)
{
int Hght=DestHeight;
do
{
ImagesNum++;
IndexCount += Hght;
Hght = (Hght / 2);
}
while(Hght >= IMGMINSIZE);
}
if(!(IndexBlock = HeapAlloc(DefHeap, HEAP_ZERO_MEMORY, ((IndexCount+4)*sizeof(DWORD))))){ErrCode=5;goto Exit;} // 4-For heap safe
if(!(RowCompBuf = HeapAlloc(DefHeap, HEAP_ZERO_MEMORY, (SaveRowSize+sizeof(DWORD))))){ErrCode=5;goto Exit;} // 4-For heap safe
FlagRLE = ((DstBytesPP==1)?(FLAG_RGB08):((DstBytesPP==2)?(FLAG_RGB16):((DstBytesPP==3)?(FLAG_RGB24):FLAG_RGB32)));
}
switch(Format) // Calculate headers size
{
case FLAG_BM:
HdrSize = (((DstBytesPP==1)?(sizeof(Palette)):0)+(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)));
break;
case FLAG_BS:
HdrSize = (((DstBytesPP==1)?(sizeof(Palette)):0)+(sizeof(BMSFHEADER)+sizeof(BITMAPINFOHEADER))+(IndexCount*sizeof(DWORD)));
break;
case FLAG_BG:
HdrSize = (((DstBytesPP==1)?(sizeof(Palette)):0)+(sizeof(BMPARCHIVEHDR))+(IndexCount*sizeof(DWORD)));
BGFHdr.FlagBGAR = FLAG_BAGR;
BGFHdr.RlePFlag = FlagRLE;
BGFHdr.BaseWidth = DestWidth;
BGFHdr.BaseHeight = DestHeight;
BGFHdr.LevelsCount = ImagesNum;
BGFHdr.PaletteSize = ((DstBytesPP==1)?(sizeof(Palette)):0);
BGFHdr.IndexesSize = (IndexCount*sizeof(DWORD));
BGFHdr.BytesPerPixel = DstBytesPP;
break;
default : {ErrCode=5;goto Exit;}
}
// Process bitmap rows
Quantizer = new OCTREEQUANTIZER; // Create always for safe !!!
SetFilePointer(hSaveFile,HdrSize,NULL,FILE_BEGIN); // Enlarge file
if(((DstBytesPP != SrcBytesPP)||UseFilter)&&(DstBytesPP == 1)) // For filter need recreate 8 bit palette
{
CBackPrvPos = DestWidth+30; // 30 - for GetPalette delay
CallbackMax = (IndexCount+CBackPrvPos);
ZeroMemory(&Palette,sizeof(Palette));
if(CallBackProc){if(CallBackProc(-1,CallbackMax)){ErrCode=10;goto Exit;}}
if(ResQuality > 9)Quantizer->SetOctreeDepth(5+(ResQuality-9)); // More smooth, but colors differents
for(int CurRow=0,RowBPP=(UseFilter)?(4):SrcBytesPP;CurRow < DestHeight;CurRow++)
{
SrcRowPtr = GetBitmapRow(CurRow,RSource.left,DestWidth);
if(UseFilter){FilterRow(DstRowPtr,SrcRowPtr,RowBPP,BytesInPixel,DestWidth,Brightness,Contrast,Grayscale,Sharpen,Blur);SrcRowPtr = DstRowPtr;}
for(int CurPix=0;CurPix < DestWidth;CurPix++)
{
Result = ExtractRGB(&((BYTE*)SrcRowPtr)[RowBPP*CurPix], RowBPP);
Quantizer->AppendColor(((BYTE*)&Result)[2],((BYTE*)&Result)[1],((BYTE*)&Result)[0]);
}
if(CallBackProc){if(CallBackProc(CurRow,CallbackMax)){ErrCode=10;goto Exit;}}
}
Quantizer->GetPalette((RGBQUAD*)((PVOID)&Palette),256); // Always 256-color Palette
if(CallBackProc){if(CallBackProc((CBackPrvPos-30),CallbackMax)){ErrCode=10;goto Exit;}}
// Preserve basic colors
if(Quantizer->ColorsInTree >= 256) // If resulting colors is wrong - check here
{
Result = Quantizer->FindNearestColor(0x00,0x00,0x00); // BLACK
Palette[Result].rgbRed = 0x00;
Palette[Result].rgbBlue = 0x00;
Palette[Result].rgbGreen = 0x00;
Result = Quantizer->FindNearestColor(0xFF,0xFF,0xFF); // WHITE
Palette[Result].rgbRed = 0xFF;
Palette[Result].rgbBlue = 0xFF;
Palette[Result].rgbGreen = 0xFF;
Result = Quantizer->FindNearestColor(0xFF,0x00,0x00); // RED
Palette[Result].rgbRed = 0xFF;
Palette[Result].rgbBlue = 0x00;
Palette[Result].rgbGreen = 0x00;
Result = Quantizer->FindNearestColor(0x00,0xFF,0x00); // GREEN
Palette[Result].rgbRed = 0x00;
Palette[Result].rgbBlue = 0x00;
Palette[Result].rgbGreen = 0xFF;
Result = Quantizer->FindNearestColor(0x00,0x00,0xFF); // BLUE
Palette[Result].rgbRed = 0x00;
Palette[Result].rgbBlue = 0xFF;
Palette[Result].rgbGreen = 0x00; // ???
Result = Quantizer->FindNearestColor(0xFF,0x00,0xFF);
Palette[Result].rgbRed = 0xFF;
Palette[Result].rgbBlue = 0xFF;
Palette[Result].rgbGreen = 0x00;
Result = Quantizer->FindNearestColor(0xFF,0xFF,0x00); // ???
Palette[Result].rgbRed = 0xFF;
Palette[Result].rgbBlue = 0x00;
Palette[Result].rgbGreen = 0xFF;
Result = Quantizer->FindNearestColor(0x00,0xFF,0xFF); // ???
Palette[Result].rgbRed = 0x00;
Palette[Result].rgbBlue = 0xFF;
Palette[Result].rgbGreen = 0xFF;
}
if(CallBackProc){if(CallBackProc(CBackPrvPos,CallbackMax)){ErrCode=10;goto Exit;}}
}
else
{
CBackPrvPos = 0;
CallbackMax = IndexCount;
Quantizer->CurPalette = (RGBQUAD*)PalettePtr;
Quantizer->ColorsInTree = 256;
if(CallBackProc){if(CallBackProc(-1,CallbackMax)){ErrCode=10;goto Exit;}}
}
for(int CurRow=0,Index=0;CurRow < DestHeight;CurRow++,Index++)
{
if(CallBackProc){if(CallBackProc((CBackPrvPos+Index),CallbackMax)){ErrCode=10;goto Exit;}}
if(Resize)
{ // Use smooth resizing algorythm
ResizePixelsRow(DstRowPtr,&RSource,DestWidth,DestHeight,CurRow,ResQuality); // Standart sharpen after resizing
FilterRow(DstRowPtr,DstRowPtr,4,4,DestWidth,Brightness,Contrast,Grayscale,true,Blur); // Always sharpen after resizing
if(Sharpen)FilterRow(DstRowPtr,DstRowPtr,4,4,DestWidth,0,0,false,Sharpen,false); // More Sharpen
}
else
{ // Use direct pixels copy (No resizing)
SrcRowPtr = GetBitmapRow(CurRow,RSource.left,DestWidth);
if(UseFilter)FilterRow(DstRowPtr,SrcRowPtr,SrcBytesPP,BytesInPixel,DestWidth,Brightness,Contrast,Grayscale,Sharpen,Blur);
else CopyMemory(DstRowPtr,SrcRowPtr,(DestWidth*SrcBytesPP));
}
if(DstBytesPP != SrcBytesPP){PalIndex += ChangeColorFormat(FmtRowPtr,DstRowPtr,DstBytesPP,SrcBytesPP,DestWidth,Quantizer);RowToSave = FmtRowPtr;}
else RowToSave = DstRowPtr;
switch(Format) // Write Rows
{
case FLAG_BM:
{
WriteFile(hSaveFile,RowToSave,SaveRowSize,&Result,NULL);
if(Result != SaveRowSize){ErrCode=8;goto Exit;}
break;
}
case FLAG_BS:
{
((DWORD*)IndexBlock)[Index] = (HdrSize+DataSize);
ComprSize = CompressRow(RowCompBuf,RowToSave,SaveRowSize,DstBytesPP,FlagRLE);
DataSize += ComprSize;
WriteFile(hSaveFile,RowCompBuf,ComprSize,&Result,NULL);
if(Result != ComprSize){ErrCode=8;goto Exit;}
break;
}
case FLAG_BG:
{
((DWORD*)IndexBlock)[Index] = (HdrSize+DataSize);
ComprSize = CompressRow(RowCompBuf,RowToSave,SaveRowSize,DstBytesPP,FlagRLE);
DataSize += ComprSize;
WriteFile(hSaveFile,RowCompBuf,ComprSize,&Result,NULL);
if(Result != ComprSize){ErrCode=8;goto Exit;}
if(((CurRow+1) >= DestHeight)&&(ImagesNum > 1))
{
Resize = true;
DestWidth = (DestWidth/2);
DestHeight = (DestHeight/2);
SrcBytesPP = 4;
SaveRowSize = _ALIGN_FRWRD((DestWidth*DstBytesPP),sizeof(DWORD));
if((DestWidth >= IMGMINSIZE)&&(DestHeight >= IMGMINSIZE))CurRow = -1;
}
break;
}
default : {ErrCode=5;goto Exit;}
}
}
// Set Safe DWORD Mapped sec read buffer
SetFilePointer(hSaveFile,sizeof(DWORD),NULL,FILE_CURRENT);
BmpFExtended = SetEndOfFile(hSaveFile);
// Write Headers
SetFilePointer(hSaveFile,0,NULL,FILE_BEGIN);
switch(Format)
{
case FLAG_BM:
{
BmpFHdr.bfType = FLAG_BM;
BmpFHdr.bfSize = (HdrSize+(SaveRowSize*DestHeight));
BmpFHdr.bfReserved1 = 0;
BmpFHdr.bfReserved2 = 0;
BmpFHdr.bfOffBits = HdrSize;
BmpIHdr.biSize = sizeof(BITMAPINFOHEADER);
BmpIHdr.biWidth = DestWidth;
BmpIHdr.biHeight = DestHeight*(-1); // Always saved as TOPDOWN
BmpIHdr.biPlanes = 1;
BmpIHdr.biBitCount = (DstBytesPP*8);
BmpIHdr.biCompression = BI_RGB;
BmpIHdr.biSizeImage = (SaveRowSize*DestHeight);
BmpIHdr.biXPelsPerMeter = 0;
BmpIHdr.biYPelsPerMeter = 0;
BmpIHdr.biClrUsed = PalIndex;
BmpIHdr.biClrImportant = PalIndex;
WriteFile(hSaveFile,&BmpFHdr,sizeof(BITMAPFILEHEADER),&Result,NULL);
if(Result != sizeof(BITMAPFILEHEADER)){ErrCode=6;goto Exit;}
WriteFile(hSaveFile,&BmpIHdr,sizeof(BITMAPINFOHEADER),&Result,NULL);
if(Result != sizeof(BITMAPINFOHEADER)){ErrCode=6;goto Exit;}
if(DstBytesPP == 1){WriteFile(hSaveFile,PalettePtr,sizeof(Palette),&Result,NULL);if(Result != sizeof(Palette)){ErrCode=6;goto Exit;}}
break;
}
case FLAG_BS:
{
BSFHdr.FlagBS = FLAG_BS;
BSFHdr.RleFlag = FlagRLE;
BSFHdr.IndexOffset = HdrSize-(DestHeight*sizeof(DWORD));
BSFHdr.PixelsOffset = HdrSize;
BmpIHdr.biSize = sizeof(BITMAPINFOHEADER);
BmpIHdr.biWidth = DestWidth;
BmpIHdr.biHeight = DestHeight*(-1); // Always saved as TOPDOWN
BmpIHdr.biPlanes = 1;
BmpIHdr.biBitCount = (DstBytesPP*8);
BmpIHdr.biCompression = BI_IRC;
BmpIHdr.biSizeImage = (SaveRowSize*DestHeight);
BmpIHdr.biXPelsPerMeter = 0;
BmpIHdr.biYPelsPerMeter = 0;
BmpIHdr.biClrUsed = PalIndex;
BmpIHdr.biClrImportant = PalIndex;
WriteFile(hSaveFile,&BSFHdr,sizeof(BMSFHEADER),&Result,NULL);
if(Result != sizeof(BMSFHEADER)){ErrCode=6;goto Exit;}
WriteFile(hSaveFile,&BmpIHdr,sizeof(BITMAPINFOHEADER),&Result,NULL);
if(Result != sizeof(BITMAPINFOHEADER)){ErrCode=6;goto Exit;}
if(DstBytesPP == 1){WriteFile(hSaveFile,PalettePtr,sizeof(Palette),&Result,NULL);if(Result != sizeof(Palette)){ErrCode=6;goto Exit;}}
WriteFile(hSaveFile,IndexBlock,(IndexCount*sizeof(DWORD)),&Result,NULL);
if(Result != (IndexCount*sizeof(DWORD))){ErrCode=6;goto Exit;}
break;
}
case FLAG_BG:
{
WriteFile(hSaveFile,&BGFHdr,sizeof(BMPARCHIVEHDR),&Result,NULL);
if(Result != sizeof(BMPARCHIVEHDR)){ErrCode=6;goto Exit;}
if(DstBytesPP == 1){WriteFile(hSaveFile,PalettePtr,sizeof(Palette),&Result,NULL);if(Result != sizeof(Palette)){ErrCode=6;goto Exit;}}
WriteFile(hSaveFile,IndexBlock,(IndexCount*sizeof(DWORD)),&Result,NULL);
if(Result != (IndexCount*sizeof(DWORD))){ErrCode=6;goto Exit;}
break;
}
default : {ErrCode=10;goto Exit;}
}
ErrCode = 0;
Exit:
if(CallBackProc)CallBackProc(CallbackMax,CallbackMax);
if(DstRowPtr) HeapFree(DefHeap, NULL, DstRowPtr);
if(FmtRowPtr) HeapFree(DefHeap, NULL, FmtRowPtr);
if(IndexBlock)HeapFree(DefHeap, NULL, IndexBlock);
if(RowCompBuf)HeapFree(DefHeap, NULL, RowCompBuf);
if(ClrAverBuf){HeapFree(DefHeap, NULL, ClrAverBuf);ResAverBufPtr=NULL;} // Set to zero as prev
if(hSaveFile != INVALID_HANDLE_VALUE)CloseHandle(hSaveFile);
if(ErrCode)DeleteFile(FileName); // Do not keep broken file
if(Quantizer)delete(Quantizer);
__GUARDNEEDED(ProcGuardExit());
return ErrCode;
}
//===========================================================================
//
// P U B L I C
//
DWORD _stdcall CBIGBITMAP::LoadBitmapRows(int StartRow, int RowsNumber)
{
int UpperRow;
int LowerRow;
int ExtraRows;
DWORD DataOffset;
QWORD FileOffset;
QWORD AlignedFOff;
DWORD MapDataSize;
FirstRowLoaded = ((StartRow > RowsToBuffer)?(StartRow-RowsToBuffer):0);
if(BmpTopDown)
{
UpperRow = FirstRowLoaded;
LowerRow = (StartRow+RowsNumber+RowsToBuffer)-1;
}
else
{
UpperRow = (BmpHeight-StartRow-RowsNumber-RowsToBuffer-1);
LowerRow = (BmpHeight-StartRow-1)+RowsToBuffer;
}
if(UpperRow < 0)UpperRow = 0;
if(LowerRow >= BmpHeight)LowerRow = (BmpHeight-1);
switch(ImageType)
{
case FLAG_BM:
{
FileOffset = (RowsOffset+(AlignedRowSize*UpperRow)); // Get offset of LoadedUpperRow in file for TOPBOTTOM
AlignedFOff = _ALIGN_BKWRD(FileOffset,MapPageSize); // Align Offset to full Page size for mapping
DataOffset = (FileOffset - AlignedFOff); // Extra bytes of alignment
ExtraRows = (DataOffset / AlignedRowSize); // Additional rows, resulting by alignment
DataOffset = (DataOffset % AlignedRowSize); // Additional bytes, not enough for row
UpperRow -= ExtraRows; // Expand upper border
TotalRowsLoaded = (LowerRow-UpperRow)+1; // Total number of rows to map (DstRectHeight+UpBorder+DownBorder)
MapDataSize = (DataOffset+(TotalRowsLoaded*AlignedRowSize)); // All bytes to mapping
break;
}
case FLAG_BS:
case FLAG_BG:
{
FileOffset = ((DWORD*)RowIndexBuff)[UpperRow]; // Upper row offset
AlignedFOff = _ALIGN_BKWRD(FileOffset,MapPageSize); // Align Offset to full Page size for mapping
ExtraRows = 0;
for(int Index=(UpperRow-1);(Index>=0)&&(((DWORD*)RowIndexBuff)[Index]>=AlignedFOff);Index--)ExtraRows++;
UpperRow -= ExtraRows;
TotalRowsLoaded = (LowerRow-UpperRow)+1; // Total number of rows to map (DstRectHeight+UpBorder+DownBorder)
MapDataSize = (((DWORD*)RowIndexBuff)[LowerRow+1]-AlignedFOff);
DataOffset = (((DWORD*)RowIndexBuff)[UpperRow]-AlignedFOff); // Extra bytes of alignment
break;
}
default : {__GUARDNEEDED(ProcGuardExit());return 3;}
}
if(BmpTopDown)FirstRowLoaded -= ExtraRows;
if(MappedSection)UnmapViewOfFile(MappedSection);
if(!(MappedSection = MapViewOfFile(hMappedBmp,FILE_MAP_READ|FILE_MAP_WRITE,HIDWORD(AlignedFOff),LODWORD(AlignedFOff),(MapDataSize+3)))){ReadedRowsBuf=NULL;return 3;} // Map rows for screen and top-bottom buffers ;+3 works together with temporary bitmap file enlarging
ReadedRowsBuf = &((BYTE*)MappedSection)[DataOffset];
return 0;
}
//===========================================================================
//
// NEED MAXIMUM PERFOMANCE - THE BASE CALCULATIONS ARE MAKED HERE !!!
//
//
DWORD _stdcall CBIGBITMAP::UpdateBitmapBuffer(void) // CallBack Here is makes it is a bit slow
{
PVOID SrcRowPtr;
PVOID DstRowPtr;
// if(CallBackProc)CallBackProc(-1,SrcHeight); // !!!!!! SLOWER !!!!!!
if(ResizePixels)
{
// Use smooth resizing algorythm
if(ProcessPixels)
{
for(int BRow=0;BRow < SrcHeight;BRow++)
{
// if(CallBackProc)CallBackProc(BRow,SrcHeight); // !!!!!! SLOWER !!!!!!
DstRowPtr = &((BYTE*)MemBmpBuffer)[(ADstRowSize*BRow)];
ResizePixelsRow(DstRowPtr,&ViewRect,DstWidth,DstHeight,(BRow+ViewRect.top),ResizeQuality); // *** Dst... OR Src... ??? *****
FilterRow(DstRowPtr,DstRowPtr,4,4,DstWidth,FBrightness,FContrast,FGrayscale,FSharpen,FBlur);
}
}
else
{
for(int BRow=0;BRow < SrcHeight;BRow++)
{
// if(CallBackProc)CallBackProc(BRow,SrcHeight); // !!!!!! SLOWER !!!!!!
DstRowPtr = &((BYTE*)MemBmpBuffer)[(ADstRowSize*BRow)];
ResizePixelsRow(DstRowPtr,&ViewRect,DstWidth,DstHeight,(BRow+ViewRect.top),ResizeQuality);
}
}
}
else
{
// Use direct pixels copy (No resizing)
if(ProcessPixels)
{
for(int BRow=0;BRow < SrcHeight;BRow++)
{
// if(CallBackProc)CallBackProc(BRow,SrcHeight); // !!!!!! SLOWER !!!!!!
SrcRowPtr = GetBitmapRow((BRow+ViewRect.top),ViewRect.left,DRowWidth);
DstRowPtr = &((BYTE*)MemBmpBuffer)[(ADstRowSize*BRow)];
FilterRow(DstRowPtr,SrcRowPtr,DstBytesPP,BytesInPixel,DRowWidth,FBrightness,FContrast,FGrayscale,FSharpen,FBlur);
}
}
else
{
for(int BRow=0;BRow < SrcHeight;BRow++)
{
// if(CallBackProc)CallBackProc(BRow,SrcHeight); // !!!!!! SLOWER !!!!!!
SrcRowPtr = GetBitmapRow((BRow+ViewRect.top),ViewRect.left,DRowWidth);
DstRowPtr = &((BYTE*)MemBmpBuffer)[(ADstRowSize*BRow)];
CopyMemory(DstRowPtr,SrcRowPtr,DstRowSize); // Copy image row
}
}
}
// if(CallBackProc)CallBackProc(SrcHeight,SrcHeight); // !!!!!! SLOWER !!!!!!
return 0;
}
//===========================================================================
void _stdcall CBIGBITMAP::SetPixelRGB(PVOID DataPtr, DWORD BytesPP, BYTE Red, BYTE Green, BYTE Blue)
{
DWORD PixColor;
switch(BytesPP)
{
case 4:
{
((BYTE*)DataPtr)[0] = Blue;
((BYTE*)DataPtr)[1] = Green;
((BYTE*)DataPtr)[2] = Red;
((BYTE*)DataPtr)[3] = 0;
break;
}
case 3:
{
((BYTE*)DataPtr)[0] = Blue;
((BYTE*)DataPtr)[1] = Green;
((BYTE*)DataPtr)[2] = Red;
break;
}
case 2:
{
Red = (Red>>3);
Green = (Green>>3);
Blue = (Blue>>3);
PixColor = Red;
PixColor = ((PixColor<<5)|Green);
PixColor = ((PixColor<<5)|Blue);
((BYTE*)DataPtr)[0] = ((BYTE*)&PixColor)[0];