-
Notifications
You must be signed in to change notification settings - Fork 0
/
DMGSIZE.PAS
459 lines (447 loc) · 14.9 KB
/
DMGSIZE.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
unit Dmgsize;
interface
uses Windows;
{============================================== exported Sizing-Functions}
function mg_TrueColorSizeDown(pBMI: pBitmapInfo; Wid, Hei: longint)
: pBitmapInfo; stdcall;
function mg_TrueColorSizeUp(pBMI: pBitmapInfo; Wid, Hei: longint)
: pBitmapInfo; stdcall;
function mg_ResizePicture(pBMI: pBitmapInfo; Wid, Hei: longint)
: pBitmapInfo; stdcall;
implementation
uses DMGBasic, SysUtils;
{============================================== private Sizing-Functions}
{---------------------------------------------- ResamplePicture}
{ in DMCoSi.hqDIB : the original Bitmap }
{ in DMCoSi.czWid and DMCoSi.czHei : in Width and Height }
{ in DMCoSi.cqWid and DMCoSi.cqHei : out Width and Height }
{ SizeDown : true, SizeUp }
{ Ergebnis-DIB : DMCoSi.pzBMI }
function ResamplePicture(SizeDown: bool): boolean;
var
{-------------- Quelle}
cqBMI : longint;
cqOffs : longint;
{-------------- Ziel}
czBMI : longint;
czDIB : longint;
czOffs : longint;
BPP : longint;
{-------------- HelpVariables}
qPtr, zPtr : pByte;
pQ, pZ : pBGRArr;
y, x, yp, xp : integer;
yStart, yEnde : integer;
xStart, xEnde : integer;
Proz : longint;
SumB, SumG, SumR: longint;
rEHy, rLHy : longint;
rEHx, rLHx : longint;
begin
{------------- StartInitialising}
Result := false;
with DMCoSi
do begin
{---------- input image processing}
with pqBMI^.bmiHeader
do begin
{------- input must be True-Color}
if (biBitCount <= 8)
then begin
ExitCoSiProc(MGERR_NOTRUECOL);
exit;
end {no TrueColor-Image};
{------- Output data}
cqBMI := sizeof(tBitmapInfoHeader);
BPP := biBitCount;
cqLen := mg_GetDIBSize(biWidth, biHeight, biBitCount) DIV biHeight;
end;
czDIB := mg_GetDIBSize(czWid, czHei, BPP);
czLen := czDIB DIV czHei;
czBMI := cqBMI;
pzBMI := mg_SetupDIB(nil, czWid, czHei, czDIB, czBMI, BPP);
if (pzBMI = nil)
then begin
ExitCoSiProc(MGERR_NOMEMORY);
exit;
end;
if not(GetResampleTabelle)
then begin
ExitCoSiProc(mg_LastError);
exit;
end;
if not(GetCoSiBuf)
then begin
ExitCoSiProc(mg_LastError);
exit;
end;
{---------- Startinitialising}
ProzFaktor := 100 / czHei;
dec(czHei);
czOffs := czBMI;
pQ := pBGRArr(pqBuf);
pZ := pBGRArr(pzBuf);
SumB := 0;
SumG := 0;
SumR := 0;
for y := 0 to czHei
do begin
{------- Startinitialising for y}
yStart := pyPix^[y, Erster];
yEnde := pyPix^[y, Letzter];
rEHy := pyRnd^[y, Erster];
rLHy := pyRnd^[y, Letzter];
cqOffs := cqBMI + yStart * cqLen;
fillchar(pZ^, czLen, #0);
for yp := yStart to yEnde
do begin
qPtr := pointer(pChar(pqBMI) + cqOffs);
inc(cqOffs, cqLen);
Move(qPtr^, pqBuf^, cqLen);
for x := 0 to czWid - 1
do begin
{- Startinitialising for x}
xStart := pxPix^[x, Erster];
xEnde := pxPix^[x, Letzter];
rEHx := pxRnd^[x, Erster];
rLHx := pxRnd^[x, Letzter];
for xp := xStart to xEnde
do begin
case SizeDown of
true:
begin
SumB := (pQ^[xp, Bc] SHL MP) DIV rFWF;
SumG := (pQ^[xp, Gc] SHL MP) DIV rFWF;
SumR := (pQ^[xp, Rc] SHL MP) DIV rFWF;
if (xp = xStart)
then begin
SumB := (rEHx * SumB) SHR MP;
SumG := (rEHx * SumG) SHR MP;
SumR := (rEHx * SumR) SHR MP;
end
else if (xp = xEnde)
then begin
SumB := (rLHx * SumB) SHR MP;
SumG := (rLHx * SumG) SHR MP;
SumR := (rLHx * SumR) SHR MP;
end;
if (yp = yStart)
then begin
SumB := (rEHy * SumB) SHR MP;
SumG := (rEHy * SumG) SHR MP;
SumR := (rEHy * SumR) SHR MP;
end
else if (yp = yEnde)
then begin
SumB := (rLHy * SumB) SHR MP;
SumG := (rLHy * SumG) SHR MP;
SumR := (rLHy * SumR) SHR MP;
end;
end;
false:
begin
SumB := pQ^[xp, Bc];
SumG := pQ^[xp, Gc];
SumR := pQ^[xp, Rc];
if (xStart <> xEnde)
then begin
if (xp = xStart)
then begin
SumB := (SumB * rEHx) SHR MP;
SumG := (SumG * rEHx) SHR MP;
SumR := (SumR * rEHx) SHR MP;
end
else if (xp = xEnde)
then begin
SumB := (SumB * rLHx) SHR MP;
SumG := (SumG * rLHx) SHR MP;
SumR := (SumR * rLHx) SHR MP;
end;
end;
if (yStart <> yEnde)
then begin
if (yp = yStart)
then begin
SumB := (SumB * rEHy) SHR MP;
SumG := (SumG * rEHy) SHR MP;
SumR := (SumR * rEHy) SHR MP;
end
else if (yp = yEnde)
then begin
SumB := (SumB * rLHy) SHR MP;
SumG := (SumG * rLHy) SHR MP;
SumR := (SumR * rLHy) SHR MP;
end;
end;
end;
end {case SizeDown};
pZ^[x, Bc] := pZ^[x, Bc] + SumB;
pZ^[x, Gc] := pZ^[x, Gc] + SumG;
pZ^[x, Rc] := pZ^[x, Rc] + SumR;
end;
end;
end;
zPtr := pointer(pChar(pzBMI) + czOffs);
inc(czOffs, czLen);
Move(pzBuf^, zPtr^, czLen);
{------- MultiTasking}
if (MulTa <> nil)
then begin
Proz := round(y * ProzFaktor);
if TMultiTasking(MulTa)(DMG_Resize, Proz)
then begin
ExitCoSiProc(MGERR_CANCEL);
exit;
end {UserAbort};
end {MultiTasking};
end;
end;
ExitCoSiProc(0);
Result := mg_LastError = 0;
end {function ResamplePicture};
{============================================== Exported Sizing-Functions}
{---------------------------------------------- mg_TrueColorSizeDown}
{ in pBMI : the original Bitmap }
{ in Hei, Wid : the Width and Height }
{ DIB : Result }
function mg_TrueColorSizeDown(pBMI: pBitmapInfo; Wid, Hei: longint): pBitmapInfo;
begin
{------------- Startinitialising}
Result := nil;
mg_LastError := 0;
if (pBMI = nil) then exit;
fillchar(DMCoSi, sizeof(tDMCoSi), #0);
with DMCoSi
do begin
pqBMI := pBMI;
czWid := Wid;
czHei := Hei;
cqWid := pBMI^.bmiHeader.biWidth;
cqHei := pBMI^.bmiHeader.biHeight;
if (cqWid <= czWid) OR (cqHei <= czWid)
then mg_LastError := MGERR_NOTLOWER
else if ResamplePicture(true) then Result := pzBMI;
end {with DMCoSi};
end {function mg_TrueColorSizeDown};
{---------------------------------------------- mg_TrueColorSizeUp}
{ in pBMI : the original Bitmap }
{ in Hei, Wid : the Width and Height }
{ DIB : Result }
function mg_TrueColorSizeUp(pBMI: pBitmapInfo; Wid, Hei: longint): pBitmapInfo;
begin
{------------- Startinitialising}
Result := nil;
mg_LastError := 0;
if (pBMI = nil) then exit;
fillchar(DMCoSi, sizeof(tDMCoSi), #0);
with DMCoSi
do begin
pqBMI := pBMI;
czWid := Wid;
czHei := Hei;
cqWid := pBMI^.bmiHeader.biWidth;
cqHei := pBMI^.bmiHeader.biHeight;
if (cqWid >= czWid) OR (cqHei >= czHei)
then mg_LastError := MGERR_NOTHIGHER
else if ResamplePicture(false) then Result := pzBMI;
end {with DMCoSi};
end {function mg_TrueColorSizeUp};
{---------------------------------------------- mg_ResizePicture}
{ in pBMI : the original Bitmap }
{ in Hei, Wid : the Width and -Height }
{ DIB : Result }
function mg_ResizePicture(pBMI: pBitmapInfo; Wid, Hei: longint): pBitmapInfo;
var
{---------------- Quell-DIB}
cqBMI : longint;
cqOffs : longint;
{---------------- Ziel-DIB}
czBMI : longint;
czDIB : longint;
czOffs : longint;
BPP : longint;
qPtr, zPtr : pByte;
y, ySrc : integer;
x, xSrc : integer;
yp, cQ, cZ : integer;
PixWid : word;
Proz : longint;
cqmask, czmask : byte;
Pixel : byte;
Maske : byte;
maskq : byte;
begin
{------------- StartInitialising}
Result := nil;
mg_LastError := 0;
if (pBMI = nil) then exit;
fillchar(DMCoSi, sizeof(tDMCoSi), #0);
with DMCoSi
do begin
pqBMI := pBMI;
czWid := Wid;
czHei := Hei;
with pqBMI^.bmiHeader
do begin
BPP := biBitCount;
cqLen := mg_GetDIBSize(biWidth, biHeight, biBitCount) DIV biHeight;
cqWid := biWidth;
cqHei := biHeight;
end;
if (BPP > 8) AND (BPP < 24) then BPP := 24;
Maske := 0;
PixWid := 0;
case BPP of
1 : Maske := $80;
2 : Maske := $c0;
4 : Maske := $f0;
8 : PixWid := 1;
24 : PixWid := 3;
end;
if not(GetResizeTabelle)
then begin
ExitCoSiProc(mg_LastError);
exit;
end;
y := mg_GetPaletteSize(pqBMI);
cqBMI := sizeof(TBitmapInfoHeader) + y;
czDIB := mg_GetDIBSize(czWid, czHei, BPP);
czLen := czDIB DIV czHei;
czBMI := cqBMI;
pzBMI := mg_SetupDIB(nil, czWid, czHei, czDIB, czBMI, BPP);
if (pzBMI = nil)
then begin
ExitCoSiProc(MGERR_NOMEMORY);
exit;
end;
if (BPP <= 8)
then begin
qPtr := pointer(pChar(pqBMI) + sizeof(TBitmapInfoHeader));
zPtr := pointer(pChar(pzBMI) + sizeof(TBitmapInfoHeader));
Move(qPtr^, zPtr^, y);
end;
if not(GetCoSiBuf)
then begin
ExitCoSiProc(mg_LastError);
exit;
end;
{---------- Startinitialising}
ProzFaktor := 100 / czHei;
dec(czWid);
dec(czHei);
cqOffs := cqBMI;
czOffs := czBMI;
ySrc := yIdx^[0];
case BPP of
{-------- 256 and Colorplanes}
8, 24:
for y := 0 to czHei
do begin
while (ySrc <> yIdx^[y])
do begin
inc(ySrc);
inc(cqOffs, cqLen);
end;
qPtr := pointer(pChar(pqBMI) + cqOffs);
Move(qPtr^, pqBuf^, cqLen);
xSrc := xIdx^[0];
for x := 0 to czWid
do begin
while (xSrc <> xIdx^[x]) do inc(xSrc);
cQ := xSrc * PixWid;
cZ := x * PixWid;
for yp := 0 to PixWid - 1
do begin
Pixel := pByteArray(pqBuf)^[cQ + yp];
pByteArray(pzBuf)^[cZ + yp] := Pixel;
end;
end;
zPtr := pointer(pChar(pzBMI) + czOffs);
inc(czOffs, czLen);
Move(pzBuf^, zPtr^, czLen);
{----- MultiTasking}
if (MulTa <> nil)
then begin
Proz := round(y * ProzFaktor);
if TMultiTasking(MulTa)(DMG_Resize, Proz)
then begin
ExitCoSiProc(MGERR_CANCEL);
exit;
end {UserAbort};
end {MultiTasking};
end;
1, 2, 4:
for y := 0 to czHei
do begin
while (ySrc <> yIdx^[y])
do begin
inc(ySrc);
inc(cqOffs, cqLen);
end;
qPtr := pointer(pChar(pqBMI) + cqOffs);
Move(qPtr^, pqBuf^, cqLen);
fillchar(pzBuf^, czLen, #0);
xSrc := xIdx^[0];
cqmask := Maske;
czmask := Maske;
cQ := 0;
cZ := 0;
for x := 0 to czWid
do begin
while (xSrc <> xIdx^[x])
do begin
inc(xSrc);
cqmask := cqmask SHR BPP;
if (cqmask = 0)
then begin
cqmask := Maske;
inc(cQ);
end;
end;
Pixel := pByteArray(pqBuf)^[cQ] AND cqmask;
if (Pixel <> 0)
then begin
if (cqmask <> czmask)
then begin
maskq := cqmask;
while (maskq <> czmask)
do if (maskq < czmask)
then begin
Pixel := Pixel SHL BPP;
maskq := maskq SHL BPP;
end
else begin
Pixel := Pixel SHR BPP;
maskq := maskq SHR BPP;
end;
end;
Pixel := Pixel OR pByteArray(pzBuf)^[cZ];
pByteArray(pzBuf)^[cZ] := Pixel;
end;
czmask := czmask SHR BPP;
if (czmask = 0)
then begin
czmask := Maske;
inc(cZ);
end;
end;
zPtr := pointer(pChar(pzBMI) + czOffs);
inc(czOffs, czLen);
Move(pzBuf^, zPtr^, czLen);
{----- MultiTasking}
if (MulTa <> nil)
then begin
Proz := round(y * ProzFaktor);
if TMultiTasking(MulTa)(DMG_Resize, Proz)
then begin
ExitCoSiProc(MGERR_CANCEL);
exit;
end {UserAbort};
end {MultiTasking};
end;
end;
end;
ExitCoSiProc(0);
Result := DMCoSi.pzBMI;
end {function mg_ResizePicture};
end.