1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "../../../include/fxge/fx_dib.h"
8 #include "../../../include/fxge/fx_ge.h"
9 #include "dib_int.h"
10 #include <limits.h>
11 extern int SDP_Table[513];
Calc(int dest_len,int dest_min,int dest_max,int src_len,int src_min,int src_max,int flags)12 void CWeightTable::Calc(int dest_len, int dest_min, int dest_max, int src_len, int src_min, int src_max, int flags)
13 {
14     if (m_pWeightTables) {
15         FX_Free(m_pWeightTables);
16         m_pWeightTables = NULL;
17     }
18     double scale, base;
19     scale = FXSYS_Div((FX_FLOAT)(src_len), (FX_FLOAT)(dest_len));
20     if (dest_len < 0) {
21         base = (FX_FLOAT)(src_len);
22     } else {
23         base = 0;
24     }
25     int ext_size = flags & FXDIB_BICUBIC_INTERPOL ? 3 : 1;
26     m_ItemSize = sizeof(int) * 2 + (int)(sizeof(int) * (FXSYS_ceil(FXSYS_fabs((FX_FLOAT)scale)) + ext_size));
27     m_DestMin = dest_min;
28     if ((dest_max - dest_min) > (int)((1U << 30) - 4) / m_ItemSize) {
29         return;
30     }
31     m_pWeightTables = FX_TryAlloc(FX_BYTE, (dest_max - dest_min) * m_ItemSize + 4);
32     if (m_pWeightTables == NULL) {
33         return;
34     }
35     if ((flags & FXDIB_NOSMOOTH) != 0 || FXSYS_fabs((FX_FLOAT)scale) < 1.0f) {
36         for (int dest_pixel = dest_min; dest_pixel < dest_max; dest_pixel ++) {
37             PixelWeight& pixel_weights = *GetPixelWeight(dest_pixel);
38             double src_pos = dest_pixel * scale + scale / 2 + base;
39             if (flags & FXDIB_INTERPOL) {
40                 pixel_weights.m_SrcStart = (int)FXSYS_floor((FX_FLOAT)src_pos - 1.0f / 2);
41                 pixel_weights.m_SrcEnd = (int)FXSYS_floor((FX_FLOAT)src_pos + 1.0f / 2);
42                 if (pixel_weights.m_SrcStart < src_min) {
43                     pixel_weights.m_SrcStart = src_min;
44                 }
45                 if (pixel_weights.m_SrcEnd >= src_max) {
46                     pixel_weights.m_SrcEnd = src_max - 1;
47                 }
48                 if (pixel_weights.m_SrcStart == pixel_weights.m_SrcEnd) {
49                     pixel_weights.m_Weights[0] = 65536;
50                 } else {
51                     pixel_weights.m_Weights[1] = FXSYS_round((FX_FLOAT)(src_pos - pixel_weights.m_SrcStart - 1.0f / 2) * 65536);
52                     pixel_weights.m_Weights[0] = 65536 - pixel_weights.m_Weights[1];
53                 }
54             } else if (flags & FXDIB_BICUBIC_INTERPOL) {
55                 pixel_weights.m_SrcStart = (int)FXSYS_floor((FX_FLOAT)src_pos - 1.0f / 2);
56                 pixel_weights.m_SrcEnd = (int)FXSYS_floor((FX_FLOAT)src_pos + 1.0f / 2);
57                 int start = pixel_weights.m_SrcStart - 1;
58                 int end = pixel_weights.m_SrcEnd + 1;
59                 if (start < src_min) {
60                     start = src_min;
61                 }
62                 if (end >= src_max) {
63                     end = src_max - 1;
64                 }
65                 if (pixel_weights.m_SrcStart < src_min) {
66                     src_pos += src_min - pixel_weights.m_SrcStart;
67                     pixel_weights.m_SrcStart = src_min;
68                 }
69                 if (pixel_weights.m_SrcEnd >= src_max) {
70                     pixel_weights.m_SrcEnd = src_max - 1;
71                 }
72                 int weight;
73                 weight = FXSYS_round((FX_FLOAT)(src_pos - pixel_weights.m_SrcStart - 1.0f / 2) * 256);
74                 if (start == end) {
75                     pixel_weights.m_Weights[0] = (SDP_Table[256 + weight] + SDP_Table[weight] + SDP_Table[256 - weight] + SDP_Table[512 - weight]) << 8;
76                 } else if ((start == pixel_weights.m_SrcStart && (pixel_weights.m_SrcStart == pixel_weights.m_SrcEnd ||
77                             end == pixel_weights.m_SrcEnd) && start < end) || (start < pixel_weights.m_SrcStart && pixel_weights.m_SrcStart == pixel_weights.m_SrcEnd && end == pixel_weights.m_SrcEnd)) {
78                     if (start < pixel_weights.m_SrcStart) {
79                         pixel_weights.m_Weights[0] = SDP_Table[256 + weight] << 8;
80                         pixel_weights.m_Weights[1] = (SDP_Table[weight] + SDP_Table[256 - weight] + SDP_Table[512 - weight]) << 8;
81                     } else {
82                         if (pixel_weights.m_SrcStart == pixel_weights.m_SrcEnd) {
83                             pixel_weights.m_Weights[0] = (SDP_Table[256 + weight] + SDP_Table[weight] + SDP_Table[256 - weight]) << 8;
84                             pixel_weights.m_Weights[1] = SDP_Table[512 - weight] << 8;
85                         } else {
86                             pixel_weights.m_Weights[0] = (SDP_Table[256 + weight] + SDP_Table[weight]) << 8;
87                             pixel_weights.m_Weights[1] = (SDP_Table[256 - weight] + SDP_Table[512 - weight]) << 8;
88                         }
89                     }
90                     if (pixel_weights.m_SrcStart == pixel_weights.m_SrcEnd) {
91                         pixel_weights.m_SrcEnd = end;
92                     }
93                     if (start < pixel_weights.m_SrcStart) {
94                         pixel_weights.m_SrcStart = start;
95                     }
96                 } else if (start == pixel_weights.m_SrcStart &&
97                            start < pixel_weights.m_SrcEnd &&
98                            pixel_weights.m_SrcEnd < end) {
99                     pixel_weights.m_Weights[0] = (SDP_Table[256 + weight] + SDP_Table[weight]) << 8;
100                     pixel_weights.m_Weights[1] = SDP_Table[256 - weight] << 8;
101                     pixel_weights.m_Weights[2] = SDP_Table[512 - weight] << 8;
102                     pixel_weights.m_SrcEnd = end;
103                 } else if (start < pixel_weights.m_SrcStart &&
104                            pixel_weights.m_SrcStart < pixel_weights.m_SrcEnd &&
105                            pixel_weights.m_SrcEnd == end) {
106                     pixel_weights.m_Weights[0] = SDP_Table[256 + weight] << 8;
107                     pixel_weights.m_Weights[1] = SDP_Table[weight] << 8;
108                     pixel_weights.m_Weights[2] = (SDP_Table[256 - weight] + SDP_Table[512 - weight]) << 8;
109                     pixel_weights.m_SrcStart = start;
110                 } else {
111                     pixel_weights.m_Weights[0] = SDP_Table[256 + weight] << 8;
112                     pixel_weights.m_Weights[1] = SDP_Table[weight] << 8;
113                     pixel_weights.m_Weights[2] = SDP_Table[256 - weight] << 8;
114                     pixel_weights.m_Weights[3] = SDP_Table[512 - weight] << 8;
115                     pixel_weights.m_SrcStart = start;
116                     pixel_weights.m_SrcEnd = end;
117                 }
118             } else {
119                 pixel_weights.m_SrcStart = pixel_weights.m_SrcEnd = (int)FXSYS_floor((FX_FLOAT)src_pos);
120                 if (pixel_weights.m_SrcStart < src_min) {
121                     pixel_weights.m_SrcStart = src_min;
122                 }
123                 if (pixel_weights.m_SrcEnd >= src_max) {
124                     pixel_weights.m_SrcEnd = src_max - 1;
125                 }
126                 pixel_weights.m_Weights[0] = 65536;
127             }
128         }
129         return;
130     }
131     for (int dest_pixel = dest_min; dest_pixel < dest_max; dest_pixel ++) {
132         PixelWeight& pixel_weights = *GetPixelWeight(dest_pixel);
133         double src_start = dest_pixel * scale + base;
134         double src_end = src_start + scale;
135         int start_i, end_i;
136         if (src_start < src_end) {
137             start_i = (int)FXSYS_floor((FX_FLOAT)src_start);
138             end_i = (int)FXSYS_ceil((FX_FLOAT)src_end);
139         } else {
140             start_i = (int)FXSYS_floor((FX_FLOAT)src_end);
141             end_i = (int)FXSYS_ceil((FX_FLOAT)src_start);
142         }
143         if (start_i < src_min) {
144             start_i = src_min;
145         }
146         if (end_i >= src_max) {
147             end_i = src_max - 1;
148         }
149         if (start_i > end_i) {
150             if (start_i >= src_max) {
151                 start_i = src_max - 1;
152             }
153             pixel_weights.m_SrcStart = start_i;
154             pixel_weights.m_SrcEnd = start_i;
155             continue;
156         }
157         pixel_weights.m_SrcStart = start_i;
158         pixel_weights.m_SrcEnd = end_i;
159         for (int j = start_i; j <= end_i; j ++) {
160             double dest_start = FXSYS_Div((FX_FLOAT)(j) - base, scale);
161             double dest_end = FXSYS_Div((FX_FLOAT)(j + 1) - base, scale);
162             if (dest_start > dest_end) {
163                 double temp = dest_start;
164                 dest_start = dest_end;
165                 dest_end = temp;
166             }
167             double area_start = dest_start > (FX_FLOAT)(dest_pixel) ? dest_start : (FX_FLOAT)(dest_pixel);
168             double area_end = dest_end > (FX_FLOAT)(dest_pixel + 1) ? (FX_FLOAT)(dest_pixel + 1) : dest_end;
169             double weight = area_start >= area_end ? 0.0f : area_end - area_start;
170             if (weight == 0 && j == end_i) {
171                 pixel_weights.m_SrcEnd --;
172                 break;
173             }
174             pixel_weights.m_Weights[j - start_i] = FXSYS_round((FX_FLOAT)(weight * 65536));
175         }
176     }
177 }
CStretchEngine(IFX_ScanlineComposer * pDestBitmap,FXDIB_Format dest_format,int dest_width,int dest_height,const FX_RECT & clip_rect,const CFX_DIBSource * pSrcBitmap,int flags)178 CStretchEngine::CStretchEngine(IFX_ScanlineComposer* pDestBitmap, FXDIB_Format dest_format,
179                                int dest_width, int dest_height, const FX_RECT& clip_rect,
180                                const CFX_DIBSource* pSrcBitmap, int flags)
181 {
182     m_State = 0;
183     m_DestFormat = dest_format;
184     m_DestBpp = dest_format & 0xff;
185     m_SrcBpp = pSrcBitmap->GetFormat() & 0xff;
186     m_bHasAlpha = pSrcBitmap->GetFormat() & 0x200;
187     m_pSrcPalette = pSrcBitmap->GetPalette();
188     m_pDestBitmap = pDestBitmap;
189     m_DestWidth = dest_width;
190     m_DestHeight = dest_height;
191     m_pInterBuf = NULL;
192     m_pExtraAlphaBuf = NULL;
193     m_pDestMaskScanline = NULL;
194     m_DestClip = clip_rect;
195     FX_DWORD size = clip_rect.Width();
196     if (size && m_DestBpp > (int)(INT_MAX / size)) {
197         return;
198     }
199     size *= m_DestBpp;
200     if (size > INT_MAX - 31) {
201         return;
202     }
203     size += 31;
204     size = size / 32 * 4;
205     m_pDestScanline = FX_TryAlloc(FX_BYTE, size);
206     if (m_pDestScanline == NULL) {
207         return;
208     }
209     if (dest_format == FXDIB_Rgb32) {
210         FXSYS_memset8(m_pDestScanline, 255, size);
211     }
212     m_InterPitch = (m_DestClip.Width() * m_DestBpp + 31) / 32 * 4;
213     m_ExtraMaskPitch = (m_DestClip.Width() * 8 + 31) / 32 * 4;
214     m_pInterBuf = NULL;
215     m_pSource = pSrcBitmap;
216     m_SrcWidth = pSrcBitmap->GetWidth();
217     m_SrcHeight = pSrcBitmap->GetHeight();
218     m_SrcPitch = (m_SrcWidth * m_SrcBpp + 31) / 32 * 4;
219     if ((flags & FXDIB_NOSMOOTH) == 0) {
220         FX_BOOL bInterpol = flags & FXDIB_INTERPOL || flags & FXDIB_BICUBIC_INTERPOL;
221         if (!bInterpol && FXSYS_abs(dest_width) != 0 && FXSYS_abs(dest_height) < m_SrcWidth * m_SrcHeight * 8 / FXSYS_abs(dest_width)) {
222             flags = FXDIB_INTERPOL;
223         }
224         m_Flags = flags;
225     } else {
226         m_Flags = FXDIB_NOSMOOTH;
227         if (flags & FXDIB_DOWNSAMPLE) {
228             m_Flags |= FXDIB_DOWNSAMPLE;
229         }
230     }
231     double scale_x = FXSYS_Div((FX_FLOAT)(m_SrcWidth), (FX_FLOAT)(m_DestWidth));
232     double scale_y = FXSYS_Div((FX_FLOAT)(m_SrcHeight), (FX_FLOAT)(m_DestHeight));
233     double base_x = m_DestWidth > 0 ? 0.0f : (FX_FLOAT)(m_DestWidth);
234     double base_y = m_DestHeight > 0 ? 0.0f : (FX_FLOAT)(m_DestHeight);
235     double src_left = FXSYS_Mul(scale_x, (FX_FLOAT)(clip_rect.left) + base_x);
236     double src_right = FXSYS_Mul(scale_x, (FX_FLOAT)(clip_rect.right) + base_x);
237     double src_top = FXSYS_Mul(scale_y, (FX_FLOAT)(clip_rect.top) + base_y);
238     double src_bottom = FXSYS_Mul(scale_y, (FX_FLOAT)(clip_rect.bottom) + base_y);
239     if (src_left > src_right) {
240         double temp = src_left;
241         src_left = src_right;
242         src_right = temp;
243     }
244     if (src_top > src_bottom) {
245         double temp = src_top;
246         src_top = src_bottom;
247         src_bottom = temp;
248     }
249     m_SrcClip.left = (int)FXSYS_floor((FX_FLOAT)src_left);
250     m_SrcClip.right = (int)FXSYS_ceil((FX_FLOAT)src_right);
251     m_SrcClip.top = (int)FXSYS_floor((FX_FLOAT)src_top);
252     m_SrcClip.bottom = (int)FXSYS_ceil((FX_FLOAT)src_bottom);
253     FX_RECT src_rect(0, 0, m_SrcWidth, m_SrcHeight);
254     m_SrcClip.Intersect(src_rect);
255     if (m_SrcBpp == 1) {
256         if (m_DestBpp == 8) {
257             m_TransMethod = 1;
258         } else {
259             m_TransMethod = 2;
260         }
261     } else if (m_SrcBpp == 8) {
262         if (m_DestBpp == 8) {
263             if (!m_bHasAlpha) {
264                 m_TransMethod = 3;
265             } else {
266                 m_TransMethod = 4;
267             }
268         } else {
269             if (!m_bHasAlpha) {
270                 m_TransMethod = 5;
271             } else {
272                 m_TransMethod = 6;
273             }
274         }
275     } else {
276         if (!m_bHasAlpha) {
277             m_TransMethod = 7;
278         } else {
279             m_TransMethod = 8;
280         }
281     }
282 }
Continue(IFX_Pause * pPause)283 FX_BOOL CStretchEngine::Continue(IFX_Pause* pPause)
284 {
285     while (m_State == 1) {
286         if (ContinueStretchHorz(pPause)) {
287             return TRUE;
288         }
289         m_State = 2;
290         StretchVert();
291     }
292     return FALSE;
293 }
~CStretchEngine()294 CStretchEngine::~CStretchEngine()
295 {
296     if (m_pDestScanline) {
297         FX_Free(m_pDestScanline);
298     }
299     if (m_pInterBuf) {
300         FX_Free(m_pInterBuf);
301     }
302     if (m_pExtraAlphaBuf) {
303         FX_Free(m_pExtraAlphaBuf);
304     }
305     if (m_pDestMaskScanline) {
306         FX_Free(m_pDestMaskScanline);
307     }
308 }
StartStretchHorz()309 FX_BOOL CStretchEngine::StartStretchHorz()
310 {
311     if (m_DestWidth == 0 || m_pDestScanline == NULL || m_SrcClip.Height() > (int)((1U << 29) / m_InterPitch) || m_SrcClip.Height() == 0) {
312         return FALSE;
313     }
314     m_pInterBuf = FX_TryAlloc(unsigned char, m_SrcClip.Height() * m_InterPitch);
315     if (m_pInterBuf == NULL) {
316         return FALSE;
317     }
318     if (m_pSource && m_bHasAlpha && m_pSource->m_pAlphaMask) {
319         m_pExtraAlphaBuf = FX_Alloc2D(unsigned char, m_SrcClip.Height(), m_ExtraMaskPitch);
320         FX_DWORD size = (m_DestClip.Width() * 8 + 31) / 32 * 4;
321         m_pDestMaskScanline = FX_TryAlloc(unsigned char, size);
322         if (!m_pDestMaskScanline) {
323             return FALSE;
324         }
325     }
326     m_WeightTable.Calc(m_DestWidth, m_DestClip.left, m_DestClip.right, m_SrcWidth, m_SrcClip.left, m_SrcClip.right, m_Flags);
327     if (m_WeightTable.m_pWeightTables == NULL) {
328         return FALSE;
329     }
330     m_CurRow = m_SrcClip.top;
331     m_State = 1;
332     return TRUE;
333 }
334 #define FX_STRECH_PAUSE_ROWS	10
ContinueStretchHorz(IFX_Pause * pPause)335 FX_BOOL CStretchEngine::ContinueStretchHorz(IFX_Pause* pPause)
336 {
337     if (!m_DestWidth) {
338         return 0;
339     }
340     if (m_pSource->SkipToScanline(m_CurRow, pPause)) {
341         return TRUE;
342     }
343     int Bpp = m_DestBpp / 8;
344     int rows_to_go = FX_STRECH_PAUSE_ROWS;
345     for (; m_CurRow < m_SrcClip.bottom; m_CurRow ++) {
346         if (rows_to_go == 0) {
347             if (pPause && pPause->NeedToPauseNow()) {
348                 return TRUE;
349             } else {
350                 rows_to_go = FX_STRECH_PAUSE_ROWS;
351             }
352         }
353         FX_LPCBYTE src_scan = m_pSource->GetScanline(m_CurRow);
354         FX_LPBYTE dest_scan = m_pInterBuf + (m_CurRow - m_SrcClip.top) * m_InterPitch;
355         FX_LPCBYTE src_scan_mask = NULL;
356         FX_LPBYTE dest_scan_mask = NULL;
357         if (m_pExtraAlphaBuf) {
358             src_scan_mask = m_pSource->m_pAlphaMask->GetScanline(m_CurRow);
359             dest_scan_mask = m_pExtraAlphaBuf + (m_CurRow - m_SrcClip.top) * m_ExtraMaskPitch;
360         }
361         switch (m_TransMethod) {
362             case 1:
363             case 2: {
364                     for (int col = m_DestClip.left; col < m_DestClip.right; col ++) {
365                         PixelWeight* pPixelWeights = m_WeightTable.GetPixelWeight(col);
366                         int dest_a = 0;
367                         for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; j ++) {
368                             int pixel_weight = pPixelWeights->m_Weights[j - pPixelWeights->m_SrcStart];
369                             if (src_scan[j / 8] & (1 << (7 - j % 8))) {
370                                 dest_a += pixel_weight * 255;
371                             }
372                         }
373                         if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
374                             dest_a = dest_a < 0 ? 0 : dest_a > 16711680 ? 16711680 : dest_a;
375                         }
376                         *dest_scan++ = (FX_BYTE)(dest_a >> 16);
377                     }
378                     break;
379                 }
380             case 3: {
381                     for (int col = m_DestClip.left; col < m_DestClip.right; col ++) {
382                         PixelWeight* pPixelWeights = m_WeightTable.GetPixelWeight(col);
383                         int dest_a = 0;
384                         for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; j ++) {
385                             int pixel_weight = pPixelWeights->m_Weights[j - pPixelWeights->m_SrcStart];
386                             dest_a += pixel_weight * src_scan[j];
387                         }
388                         if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
389                             dest_a = dest_a < 0 ? 0 : dest_a > 16711680 ? 16711680 : dest_a;
390                         }
391                         *dest_scan++ = (FX_BYTE)(dest_a >> 16);
392                     }
393                     break;
394                 }
395             case 4: {
396                     for (int col = m_DestClip.left; col < m_DestClip.right; col ++) {
397                         PixelWeight* pPixelWeights = m_WeightTable.GetPixelWeight(col);
398                         int dest_a = 0, dest_r = 0;
399                         for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; j ++) {
400                             int pixel_weight = pPixelWeights->m_Weights[j - pPixelWeights->m_SrcStart];
401                             pixel_weight = pixel_weight * src_scan_mask[j] / 255;
402                             dest_r += pixel_weight * src_scan[j];
403                             dest_a += pixel_weight;
404                         }
405                         if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
406                             dest_r = dest_r < 0 ? 0 : dest_r > 16711680 ? 16711680 : dest_r;
407                             dest_a = dest_a < 0 ? 0 : dest_a > 65536 ? 65536 : dest_a;
408                         }
409                         *dest_scan++ = (FX_BYTE)(dest_r >> 16);
410                         *dest_scan_mask++ = (FX_BYTE)((dest_a * 255) >> 16);
411                     }
412                     break;
413                 }
414             case 5: {
415                     for (int col = m_DestClip.left; col < m_DestClip.right; col ++) {
416                         PixelWeight* pPixelWeights = m_WeightTable.GetPixelWeight(col);
417                         int dest_r_y = 0, dest_g_m = 0, dest_b_c = 0;
418                         for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; j ++) {
419                             int pixel_weight = pPixelWeights->m_Weights[j - pPixelWeights->m_SrcStart];
420                             unsigned long argb_cmyk = m_pSrcPalette[src_scan[j]];
421                             if (m_DestFormat == FXDIB_Rgb) {
422                                 dest_r_y += pixel_weight * (FX_BYTE)(argb_cmyk >> 16);
423                                 dest_g_m += pixel_weight * (FX_BYTE)(argb_cmyk >> 8);
424                                 dest_b_c += pixel_weight * (FX_BYTE)argb_cmyk;
425                             } else {
426                                 dest_b_c += pixel_weight * (FX_BYTE)(argb_cmyk >> 24);
427                                 dest_g_m += pixel_weight * (FX_BYTE)(argb_cmyk >> 16);
428                                 dest_r_y += pixel_weight * (FX_BYTE)(argb_cmyk >> 8);
429                             }
430                         }
431                         if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
432                             dest_r_y = dest_r_y < 0 ? 0 : dest_r_y > 16711680 ? 16711680 : dest_r_y;
433                             dest_g_m = dest_g_m < 0 ? 0 : dest_g_m > 16711680 ? 16711680 : dest_g_m;
434                             dest_b_c = dest_b_c < 0 ? 0 : dest_b_c > 16711680 ? 16711680 : dest_b_c;
435                         }
436                         *dest_scan++ = (FX_BYTE)(dest_b_c >> 16);
437                         *dest_scan++ = (FX_BYTE)(dest_g_m >> 16);
438                         *dest_scan++ = (FX_BYTE)(dest_r_y >> 16);
439                     }
440                     break;
441                 }
442             case 6: {
443                     for (int col = m_DestClip.left; col < m_DestClip.right; col ++) {
444                         PixelWeight* pPixelWeights = m_WeightTable.GetPixelWeight(col);
445                         int dest_a = 0, dest_r_y = 0, dest_g_m = 0, dest_b_c = 0;
446                         for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; j ++) {
447                             int pixel_weight = pPixelWeights->m_Weights[j - pPixelWeights->m_SrcStart];
448                             pixel_weight = pixel_weight * src_scan_mask[j] / 255;
449                             unsigned long argb_cmyk = m_pSrcPalette[src_scan[j]];
450                             if (m_DestFormat == FXDIB_Rgba) {
451                                 dest_r_y += pixel_weight * (FX_BYTE)(argb_cmyk >> 16);
452                                 dest_g_m += pixel_weight * (FX_BYTE)(argb_cmyk >> 8);
453                                 dest_b_c += pixel_weight * (FX_BYTE)argb_cmyk;
454                             } else {
455                                 dest_b_c += pixel_weight * (FX_BYTE)(argb_cmyk >> 24);
456                                 dest_g_m += pixel_weight * (FX_BYTE)(argb_cmyk >> 16);
457                                 dest_r_y += pixel_weight * (FX_BYTE)(argb_cmyk >> 8);
458                             }
459                             dest_a += pixel_weight;
460                         }
461                         if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
462                             dest_b_c = dest_b_c < 0 ? 0 : dest_b_c > 16711680 ? 16711680 : dest_b_c;
463                             dest_g_m = dest_g_m < 0 ? 0 : dest_g_m > 16711680 ? 16711680 : dest_g_m;
464                             dest_r_y = dest_r_y < 0 ? 0 : dest_r_y > 16711680 ? 16711680 : dest_r_y;
465                             dest_a = dest_a < 0 ? 0 : dest_a > 65536 ? 65536 : dest_a;
466                         }
467                         *dest_scan++ = (FX_BYTE)(dest_b_c >> 16);
468                         *dest_scan++ = (FX_BYTE)(dest_g_m >> 16);
469                         *dest_scan++ = (FX_BYTE)(dest_r_y >> 16);
470                         *dest_scan_mask++ = (FX_BYTE)((dest_a * 255) >> 16);
471                     }
472                     break;
473                 }
474             case 7: {
475                     for (int col = m_DestClip.left; col < m_DestClip.right; col ++) {
476                         PixelWeight* pPixelWeights = m_WeightTable.GetPixelWeight(col);
477                         int dest_r_y = 0, dest_g_m = 0, dest_b_c = 0;
478                         for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; j ++) {
479                             int pixel_weight = pPixelWeights->m_Weights[j - pPixelWeights->m_SrcStart];
480                             FX_LPCBYTE src_pixel = src_scan + j * Bpp;
481                             dest_b_c += pixel_weight * (*src_pixel++);
482                             dest_g_m += pixel_weight * (*src_pixel++);
483                             dest_r_y += pixel_weight * (*src_pixel);
484                         }
485                         if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
486                             dest_b_c = dest_b_c < 0 ? 0 : dest_b_c > 16711680 ? 16711680 : dest_b_c;
487                             dest_g_m = dest_g_m < 0 ? 0 : dest_g_m > 16711680 ? 16711680 : dest_g_m;
488                             dest_r_y = dest_r_y < 0 ? 0 : dest_r_y > 16711680 ? 16711680 : dest_r_y;
489                         }
490                         *dest_scan++ = (FX_BYTE)((dest_b_c) >> 16);
491                         *dest_scan++ = (FX_BYTE)((dest_g_m) >> 16);
492                         *dest_scan++ = (FX_BYTE)((dest_r_y) >> 16);
493                         dest_scan += Bpp - 3;
494                     }
495                     break;
496                 }
497             case 8: {
498                     for (int col = m_DestClip.left; col < m_DestClip.right; col ++) {
499                         PixelWeight* pPixelWeights = m_WeightTable.GetPixelWeight(col);
500                         int dest_a = 0, dest_r_y = 0, dest_g_m = 0, dest_b_c = 0;
501                         for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; j ++) {
502                             int pixel_weight = pPixelWeights->m_Weights[j - pPixelWeights->m_SrcStart];
503                             FX_LPCBYTE src_pixel = src_scan + j * Bpp;
504                             if (m_DestFormat == FXDIB_Argb) {
505                                 pixel_weight = pixel_weight * src_pixel[3] / 255;
506                             } else {
507                                 pixel_weight = pixel_weight * src_scan_mask[j] / 255;
508                             }
509                             dest_b_c += pixel_weight * (*src_pixel++);
510                             dest_g_m += pixel_weight * (*src_pixel++);
511                             dest_r_y += pixel_weight * (*src_pixel);
512                             dest_a += pixel_weight;
513                         }
514                         if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
515                             dest_r_y = dest_r_y < 0 ? 0 : dest_r_y > 16711680 ? 16711680 : dest_r_y;
516                             dest_g_m = dest_g_m < 0 ? 0 : dest_g_m > 16711680 ? 16711680 : dest_g_m;
517                             dest_b_c = dest_b_c < 0 ? 0 : dest_b_c > 16711680 ? 16711680 : dest_b_c;
518                             dest_a = dest_a < 0 ? 0 : dest_a > 65536 ? 65536 : dest_a;
519                         }
520                         *dest_scan++ = (FX_BYTE)((dest_b_c) >> 16);
521                         *dest_scan++ = (FX_BYTE)((dest_g_m) >> 16);
522                         *dest_scan++ = (FX_BYTE)((dest_r_y) >> 16);
523                         if (m_DestFormat == FXDIB_Argb) {
524                             *dest_scan = (FX_BYTE)((dest_a * 255) >> 16);
525                         }
526                         if (dest_scan_mask) {
527                             *dest_scan_mask++ = (FX_BYTE)((dest_a * 255) >> 16);
528                         }
529                         dest_scan += Bpp - 3;
530                     }
531                     break;
532                 }
533         }
534         rows_to_go --;
535     }
536     return FALSE;
537 }
StretchVert()538 void CStretchEngine::StretchVert()
539 {
540     if (m_DestHeight == 0) {
541         return;
542     }
543     CWeightTable table;
544     table.Calc(m_DestHeight, m_DestClip.top, m_DestClip.bottom, m_SrcHeight, m_SrcClip.top, m_SrcClip.bottom, m_Flags);
545     if (table.m_pWeightTables == NULL) {
546         return;
547     }
548     int DestBpp = m_DestBpp / 8;
549     for (int row = m_DestClip.top; row < m_DestClip.bottom; row ++) {
550         unsigned char* dest_scan = m_pDestScanline;
551         unsigned char* dest_sacn_mask = m_pDestMaskScanline;
552         PixelWeight* pPixelWeights = table.GetPixelWeight(row);
553         switch(m_TransMethod) {
554             case 1:
555             case 2:
556             case 3: {
557                     for (int col = m_DestClip.left; col < m_DestClip.right; col ++) {
558                         unsigned char* src_scan = m_pInterBuf + (col - m_DestClip.left) * DestBpp;
559                         int dest_a = 0;
560                         for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; j ++) {
561                             int pixel_weight = pPixelWeights->m_Weights[j - pPixelWeights->m_SrcStart];
562                             dest_a += pixel_weight * src_scan[(j - m_SrcClip.top) * m_InterPitch];
563                         }
564                         if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
565                             dest_a = dest_a < 0 ? 0 : dest_a > 16711680 ? 16711680 : dest_a;
566                         }
567                         *dest_scan = (FX_BYTE)(dest_a >> 16);
568                         dest_scan += DestBpp;
569                     }
570                     break;
571                 }
572             case 4: {
573                     for (int col = m_DestClip.left; col < m_DestClip.right; col ++) {
574                         unsigned char* src_scan = m_pInterBuf + (col - m_DestClip.left) * DestBpp;
575                         unsigned char* src_scan_mask = m_pExtraAlphaBuf + (col - m_DestClip.left);
576                         int dest_a = 0, dest_k = 0;
577                         for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; j ++) {
578                             int pixel_weight = pPixelWeights->m_Weights[j - pPixelWeights->m_SrcStart];
579                             dest_k += pixel_weight * src_scan[(j - m_SrcClip.top) * m_InterPitch];
580                             dest_a += pixel_weight * src_scan_mask[(j - m_SrcClip.top) * m_ExtraMaskPitch];
581                         }
582                         if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
583                             dest_k = dest_k < 0 ? 0 : dest_k > 16711680 ? 16711680 : dest_k;
584                             dest_a = dest_a < 0 ? 0 : dest_a > 16711680 ? 16711680 : dest_a;
585                         }
586                         *dest_scan = (FX_BYTE)(dest_k >> 16);
587                         dest_scan += DestBpp;
588                         *dest_sacn_mask++ = (FX_BYTE)(dest_a >> 16);
589                     }
590                     break;
591                 }
592             case 5:
593             case 7: {
594                     for (int col = m_DestClip.left; col < m_DestClip.right; col ++) {
595                         unsigned char* src_scan = m_pInterBuf + (col - m_DestClip.left) * DestBpp;
596                         int dest_r_y = 0, dest_g_m = 0, dest_b_c = 0;
597                         for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; j ++) {
598                             int pixel_weight = pPixelWeights->m_Weights[j - pPixelWeights->m_SrcStart];
599                             FX_LPCBYTE src_pixel = src_scan + (j - m_SrcClip.top) * m_InterPitch;
600                             dest_b_c += pixel_weight * (*src_pixel++);
601                             dest_g_m += pixel_weight * (*src_pixel++);
602                             dest_r_y += pixel_weight * (*src_pixel);
603                         }
604                         if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
605                             dest_r_y = dest_r_y < 0 ? 0 : dest_r_y > 16711680 ? 16711680 : dest_r_y;
606                             dest_g_m = dest_g_m < 0 ? 0 : dest_g_m > 16711680 ? 16711680 : dest_g_m;
607                             dest_b_c = dest_b_c < 0 ? 0 : dest_b_c > 16711680 ? 16711680 : dest_b_c;
608                         }
609                         dest_scan[0] = (FX_BYTE)((dest_b_c) >> 16);
610                         dest_scan[1] = (FX_BYTE)((dest_g_m) >> 16);
611                         dest_scan[2] = (FX_BYTE)((dest_r_y) >> 16);
612                         dest_scan += DestBpp;
613                     }
614                     break;
615                 }
616             case 6:
617             case 8: {
618                     for (int col = m_DestClip.left; col < m_DestClip.right; col ++) {
619                         unsigned char* src_scan = m_pInterBuf + (col - m_DestClip.left) * DestBpp;
620                         unsigned char* src_scan_mask = NULL;
621                         if (m_DestFormat != FXDIB_Argb) {
622                             src_scan_mask = m_pExtraAlphaBuf + (col - m_DestClip.left);
623                         }
624                         int dest_a = 0, dest_r_y = 0, dest_g_m = 0, dest_b_c = 0;
625                         for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; j ++) {
626                             int pixel_weight = pPixelWeights->m_Weights[j - pPixelWeights->m_SrcStart];
627                             FX_LPCBYTE src_pixel = src_scan + (j - m_SrcClip.top) * m_InterPitch;
628                             int mask_v = 255;
629                             if (src_scan_mask) {
630                                 mask_v = src_scan_mask[(j - m_SrcClip.top) * m_ExtraMaskPitch];
631                             }
632                             dest_b_c += pixel_weight * (*src_pixel++);
633                             dest_g_m += pixel_weight * (*src_pixel++);
634                             dest_r_y += pixel_weight * (*src_pixel);
635                             if (m_DestFormat == FXDIB_Argb) {
636                                 dest_a += pixel_weight * (*(src_pixel + 1));
637                             } else {
638                                 dest_a += pixel_weight * mask_v;
639                             }
640                         }
641                         if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
642                             dest_r_y = dest_r_y < 0 ? 0 : dest_r_y > 16711680 ? 16711680 : dest_r_y;
643                             dest_g_m = dest_g_m < 0 ? 0 : dest_g_m > 16711680 ? 16711680 : dest_g_m;
644                             dest_b_c = dest_b_c < 0 ? 0 : dest_b_c > 16711680 ? 16711680 : dest_b_c;
645                             dest_a = dest_a < 0 ? 0 : dest_a > 16711680 ? 16711680 : dest_a;
646                         }
647                         if (dest_a) {
648                             int r = ((FX_DWORD)dest_r_y) * 255 / dest_a;
649                             int g = ((FX_DWORD)dest_g_m) * 255 / dest_a;
650                             int b = ((FX_DWORD)dest_b_c) * 255 / dest_a;
651                             dest_scan[0] = b > 255 ? 255 : b < 0 ? 0 : b;
652                             dest_scan[1] = g > 255 ? 255 : g < 0 ? 0 : g;
653                             dest_scan[2] = r > 255 ? 255 : r < 0 ? 0 : r;
654                         }
655                         if (m_DestFormat == FXDIB_Argb) {
656                             dest_scan[3] = (FX_BYTE)((dest_a) >> 16);
657                         } else {
658                             *dest_sacn_mask = (FX_BYTE)((dest_a) >> 16);
659                         }
660                         dest_scan += DestBpp;
661                         if (dest_sacn_mask) {
662                             dest_sacn_mask++;
663                         }
664                     }
665                     break;
666                 }
667         }
668         m_pDestBitmap->ComposeScanline(row - m_DestClip.top, m_pDestScanline, m_pDestMaskScanline);
669     }
670 }
CFX_ImageStretcher()671 CFX_ImageStretcher::CFX_ImageStretcher()
672 {
673     m_pScanline = NULL;
674     m_pStretchEngine = NULL;
675     m_pMaskScanline = NULL;
676 }
~CFX_ImageStretcher()677 CFX_ImageStretcher::~CFX_ImageStretcher()
678 {
679     if (m_pScanline) {
680         FX_Free(m_pScanline);
681     }
682     if (m_pStretchEngine) {
683         delete m_pStretchEngine;
684     }
685     if (m_pMaskScanline) {
686         FX_Free(m_pMaskScanline);
687     }
688 }
_GetStretchedFormat(const CFX_DIBSource * pSrc)689 FXDIB_Format _GetStretchedFormat(const CFX_DIBSource* pSrc)
690 {
691     FXDIB_Format format = pSrc->GetFormat();
692     if (format == FXDIB_1bppMask) {
693         format = FXDIB_8bppMask;
694     } else if (format == FXDIB_1bppRgb) {
695         format = FXDIB_8bppRgb;
696     } else if (format == FXDIB_8bppRgb) {
697         if (pSrc->GetPalette()) {
698             format = FXDIB_Rgb;
699         }
700     }
701     return format;
702 }
Start(IFX_ScanlineComposer * pDest,const CFX_DIBSource * pSource,int dest_width,int dest_height,const FX_RECT & rect,FX_DWORD flags)703 FX_BOOL CFX_ImageStretcher::Start(IFX_ScanlineComposer* pDest,
704                                   const CFX_DIBSource* pSource, int dest_width, int dest_height,
705                                   const FX_RECT& rect, FX_DWORD flags)
706 {
707     m_DestFormat = _GetStretchedFormat(pSource);
708     m_DestBPP = m_DestFormat & 0xff;
709     m_pDest = pDest;
710     m_pSource = pSource;
711     m_DestWidth = dest_width;
712     m_DestHeight = dest_height;
713     m_ClipRect = rect;
714     m_Flags = flags;
715     if (pSource->GetFormat() == FXDIB_1bppRgb && pSource->GetPalette()) {
716         FX_ARGB pal[256];
717         int a0, r0, g0, b0, a1, r1, g1, b1;
718         ArgbDecode(pSource->GetPaletteEntry(0), a0, r0, g0, b0);
719         ArgbDecode(pSource->GetPaletteEntry(1), a1, r1, g1, b1);
720         for (int i = 0; i < 256; i ++) {
721             int a = a0 + (a1 - a0) * i / 255;
722             int r = r0 + (r1 - r0) * i / 255;
723             int g = g0 + (g1 - g0) * i / 255;
724             int b = b0 + (b1 - b0) * i / 255;
725             pal[i] = ArgbEncode(a, r, g, b);
726         }
727         if (!pDest->SetInfo(rect.Width(), rect.Height(), m_DestFormat, pal)) {
728             return FALSE;
729         }
730     } else if (pSource->GetFormat() == FXDIB_1bppCmyk && pSource->GetPalette()) {
731         FX_CMYK pal[256];
732         int c0, m0, y0, k0, c1, m1, y1, k1;
733         CmykDecode(pSource->GetPaletteEntry(0), c0, m0, y0, k0);
734         CmykDecode(pSource->GetPaletteEntry(1), c1, m1, y1, k1);
735         for (int i = 0; i < 256; i ++) {
736             int c = c0 + (c1 - c0) * i / 255;
737             int m = m0 + (m1 - m0) * i / 255;
738             int y = y0 + (y1 - y0) * i / 255;
739             int k = k0 + (k1 - k0) * i / 255;
740             pal[i] = CmykEncode(c, m, y, k);
741         }
742         if (!pDest->SetInfo(rect.Width(), rect.Height(), m_DestFormat, pal)) {
743             return FALSE;
744         }
745     } else if (!pDest->SetInfo(rect.Width(), rect.Height(), m_DestFormat, NULL)) {
746         return FALSE;
747     }
748     if (flags & FXDIB_DOWNSAMPLE) {
749         return StartQuickStretch();
750     } else {
751         return StartStretch();
752     }
753 }
Continue(IFX_Pause * pPause)754 FX_BOOL CFX_ImageStretcher::Continue(IFX_Pause* pPause)
755 {
756     if (m_Flags & FXDIB_DOWNSAMPLE) {
757         return ContinueQuickStretch(pPause);
758     } else {
759         return ContinueStretch(pPause);
760     }
761 }
762 #define MAX_PROGRESSIVE_STRETCH_PIXELS	1000000
StartStretch()763 FX_BOOL CFX_ImageStretcher::StartStretch()
764 {
765     m_pStretchEngine = new CStretchEngine(m_pDest, m_DestFormat, m_DestWidth, m_DestHeight, m_ClipRect, m_pSource, m_Flags);
766     m_pStretchEngine->StartStretchHorz();
767     if (m_pSource->GetWidth() * m_pSource->GetHeight() < MAX_PROGRESSIVE_STRETCH_PIXELS) {
768         m_pStretchEngine->Continue(NULL);
769         return FALSE;
770     }
771     return TRUE;
772 }
ContinueStretch(IFX_Pause * pPause)773 FX_BOOL CFX_ImageStretcher::ContinueStretch(IFX_Pause* pPause)
774 {
775     if (m_pStretchEngine == NULL) {
776         return FALSE;
777     }
778     return m_pStretchEngine->Continue(pPause);
779 }
StartQuickStretch()780 FX_BOOL CFX_ImageStretcher::StartQuickStretch()
781 {
782     m_bFlipX = FALSE;
783     m_bFlipY = FALSE;
784     if (m_DestWidth < 0) {
785         m_bFlipX = TRUE;
786         m_DestWidth = -m_DestWidth;
787     }
788     if (m_DestHeight < 0) {
789         m_bFlipY = TRUE;
790         m_DestHeight = -m_DestHeight;
791     }
792     m_LineIndex = 0;
793     FX_DWORD size = m_ClipRect.Width();
794     if (size && m_DestBPP > (int)(INT_MAX / size)) {
795         return FALSE;
796     }
797     size *= m_DestBPP;
798     m_pScanline = FX_Alloc(FX_BYTE, (size / 8 + 3) / 4 * 4);
799     if (m_pSource->m_pAlphaMask) {
800         m_pMaskScanline = FX_Alloc(FX_BYTE, (m_ClipRect.Width() + 3) / 4 * 4);
801     }
802     if (m_pSource->GetWidth() * m_pSource->GetHeight() < MAX_PROGRESSIVE_STRETCH_PIXELS) {
803         ContinueQuickStretch(NULL);
804         return FALSE;
805     }
806     return TRUE;
807 }
ContinueQuickStretch(IFX_Pause * pPause)808 FX_BOOL CFX_ImageStretcher::ContinueQuickStretch(IFX_Pause* pPause)
809 {
810     if (m_pScanline == NULL) {
811         return FALSE;
812     }
813     int result_width = m_ClipRect.Width(), result_height = m_ClipRect.Height();
814     int src_height = m_pSource->GetHeight();
815     for (; m_LineIndex < result_height; m_LineIndex ++) {
816         int dest_y, src_y;
817         if (m_bFlipY) {
818             dest_y = result_height - m_LineIndex - 1;
819             src_y = (m_DestHeight - (dest_y + m_ClipRect.top) - 1) * src_height / m_DestHeight;
820         } else {
821             dest_y = m_LineIndex;
822             src_y = (dest_y + m_ClipRect.top) * src_height / m_DestHeight;
823         }
824         if (src_y >= src_height) {
825             src_y = src_height - 1;
826         }
827         if (src_y < 0) {
828             src_y = 0;
829         }
830         if (m_pSource->SkipToScanline(src_y, pPause)) {
831             return TRUE;
832         }
833         m_pSource->DownSampleScanline(src_y, m_pScanline, m_DestBPP, m_DestWidth, m_bFlipX, m_ClipRect.left, result_width);
834         if (m_pMaskScanline) {
835             m_pSource->m_pAlphaMask->DownSampleScanline(src_y, m_pMaskScanline, 1, m_DestWidth, m_bFlipX, m_ClipRect.left, result_width);
836         }
837         m_pDest->ComposeScanline(dest_y, m_pScanline, m_pMaskScanline);
838     }
839     return FALSE;
840 }
841