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