poppler-0.66.0-PSOutputDev-rgb.patch 12.7 KB
Newer Older
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
From 64aa150a92ccb082db6a3383fa734a6ac91cf1bf Mon Sep 17 00:00:00 2001
From: Marek Kasik <mkasik@redhat.com>
Date: Tue, 30 Apr 2019 18:47:44 +0200
Subject: [PATCH] PSOutputDev: Don't read outside of image buffer

Check whether input image is RGB or BGR to not treat
it as CMYK in those cases in PSOutputDev::checkPageSlice().

Fixes #751
---
 poppler/PSOutputDev.cc | 248 ++++++++++++++++++++++++++++++++---------
 1 file changed, 196 insertions(+), 52 deletions(-)

diff --git a/poppler/PSOutputDev.cc b/poppler/PSOutputDev.cc
index 0d201835..155a8cbe 100644
--- a/poppler/PSOutputDev.cc
+++ b/poppler/PSOutputDev.cc
@@ -3385,13 +3385,21 @@ GBool PSOutputDev::checkPageSlice(Page *
       }
       break;
     case psLevel1Sep:
+      GfxColor inputColor;
+      GfxCMYK cmyk;
+      unsigned char cmykColor[4];
+      GfxDeviceRGBColorSpace *rgbCS;
+      SplashColorMode colorMode;
+
+      colorMode = bitmap->getMode();
+
       p = bitmap->getDataPtr();
       // Check for an all gray image
       if (getOptimizeColorSpace()) {
         isGray = gTrue;
         for (y = 0; y < h; ++y) {
 	  for (x = 0; x < w; ++x) {
-	    if (p[4*x] != p[4*x + 1] || p[4*x] != p[4*x + 2]) {
+	    if (p[numComps*x] != p[numComps*x + 1] || p[numComps*x] != p[numComps*x + 2]) {
 	      isGray = gFalse;
 	      y = h;
 	      break;
@@ -3411,7 +3419,9 @@ GBool PSOutputDev::checkPageSlice(Page *
       col[0] = col[1] = col[2] = col[3] = 0;
       if (isGray) {
         int g;
-        if ((psProcessBlack & processColors) == 0) {
+        if ((psProcessBlack & processColors) == 0 &&
+            colorMode != splashModeRGB8 &&
+            colorMode != splashModeBGR8) {
 	  // Check if the image uses black
 	  for (y = 0; y < h; ++y) {
 	    for (x = 0; x < w; ++x) {
@@ -3425,59 +3435,23 @@ GBool PSOutputDev::checkPageSlice(Page *
 	  }
           p = bitmap->getDataPtr() + (h - 1) * bitmap->getRowSize();
         }
-        for (y = 0; y < h; ++y) {
-	  if (useBinary) {
-	    // Binary gray image
-	    for (x = 0; x < w; ++x) {
-	      g = p[4*x] + p[4*x + 3];
-	      g = 255 - g;
-	      if (g < 0) g = 0;
-	      hexBuf[i++] = (Guchar) g;
-	      if (i >= 64) {
-	        writePSBuf(hexBuf, i);
-	        i = 0;
-	      }
-	    }
-	  } else {
-	    // Hex gray image
-	    for (x = 0; x < w; ++x) {
-	      g = p[4*x] + p[4*x + 3];
-	      g = 255 - g;
-	      if (g < 0) g = 0;
-	      digit = g / 16;
-	      hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
-	      digit = g % 16;
-	      hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
-	      if (i >= 64) {
-	        hexBuf[i++] = '\n';
-	        writePSBuf(hexBuf, i);
-	        i = 0;
-	      }
-	    }
-          }
-          p -= bitmap->getRowSize();
-        }
-      } else if (((psProcessCyan | psProcessMagenta | psProcessYellow | psProcessBlack) & ~processColors) != 0) {
-	// Color image, need to check color flags for each dot
-        for (y = 0; y < h; ++y) {
-          for (comp = 0; comp < 4; ++comp) {
+        if (colorMode == splashModeRGB8 || colorMode != splashModeBGR8) {
+          for (y = 0; y < h; ++y) {
 	    if (useBinary) {
-	      // Binary color image
+	      // Binary gray image
 	      for (x = 0; x < w; ++x) {
-	        col[comp] |= p[4*x + comp];
-	        hexBuf[i++] = p[4*x + comp];
+	        hexBuf[i++] = (Guchar) p[3*x];
 	        if (i >= 64) {
 	          writePSBuf(hexBuf, i);
 	          i = 0;
 	        }
 	      }
 	    } else {
-	      // Gray color image
+	      // Hex gray image
 	      for (x = 0; x < w; ++x) {
-	        col[comp] |= p[4*x + comp];
-	        digit = p[4*x + comp] / 16;
+	        digit = p[3*x] / 16;
 	        hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
-	        digit = p[4*x + comp] % 16;
+	        digit = p[3*x] % 16;
 	        hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
 	        if (i >= 64) {
 	          hexBuf[i++] = '\n';
@@ -3485,29 +3459,31 @@ GBool PSOutputDev::checkPageSlice(Page *
 	          i = 0;
 	        }
 	      }
-	    }
+            }
           }
-          p -= bitmap->getRowSize();
-        }
-      } else {
-	// Color image, do not need to check color flags
-        for (y = 0; y < h; ++y) {
-          for (comp = 0; comp < 4; ++comp) {
+        } else {
+          for (y = 0; y < h; ++y) {
 	    if (useBinary) {
-	      // Binary color image
+	      // Binary gray image
 	      for (x = 0; x < w; ++x) {
-	        hexBuf[i++] = p[4*x + comp];
+	        g = p[4*x] + p[4*x + 3];
+	        g = 255 - g;
+	        if (g < 0) g = 0;
+	        hexBuf[i++] = (Guchar) g;
 	        if (i >= 64) {
 	          writePSBuf(hexBuf, i);
 	          i = 0;
 	        }
 	      }
 	    } else {
-	      // Hex color image
+	      // Hex gray image
 	      for (x = 0; x < w; ++x) {
-	        digit = p[4*x + comp] / 16;
+	        g = p[4*x] + p[4*x + 3];
+	        g = 255 - g;
+	        if (g < 0) g = 0;
+	        digit = g / 16;
 	        hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
-	        digit = p[4*x + comp] % 16;
+	        digit = g % 16;
 	        hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
 	        if (i >= 64) {
 	          hexBuf[i++] = '\n';
@@ -3515,9 +3491,207 @@ GBool PSOutputDev::checkPageSlice(Page *
 	          i = 0;
 	        }
 	      }
-	    }
+            }
           }
-          p -= bitmap->getRowSize();
+        }
+        p -= bitmap->getRowSize();
+      } else if (((psProcessCyan | psProcessMagenta | psProcessYellow | psProcessBlack) & ~processColors) != 0) {
+	// Color image, need to check color flags for each dot
+        switch (colorMode) {
+          case splashModeRGB8:
+          case splashModeBGR8:
+            rgbCS = new GfxDeviceRGBColorSpace();
+            for (y = 0; y < h; ++y) {
+              for (comp = 0; comp < 4; ++comp) {
+	        if (useBinary) {
+	          // Binary color image
+	          for (x = 0; x < w; ++x) {
+	            if (likely(colorMode == splashModeRGB8)) {
+                      inputColor.c[0] = byteToCol(p[3*x + 0]);
+                      inputColor.c[1] = byteToCol(p[3*x + 1]);
+                      inputColor.c[2] = byteToCol(p[3*x + 2]);
+                    } else {
+                      inputColor.c[0] = byteToCol(p[3*x + 2]);
+                      inputColor.c[1] = byteToCol(p[3*x + 1]);
+                      inputColor.c[2] = byteToCol(p[3*x + 0]);
+                    }
+                    rgbCS->getCMYK(&inputColor, &cmyk);
+                    cmykColor[0] = colToByte(cmyk.c);
+                    cmykColor[1] = colToByte(cmyk.m);
+                    cmykColor[2] = colToByte(cmyk.y);
+                    cmykColor[3] = colToByte(cmyk.k);
+
+	            col[comp] |= cmykColor[comp];
+	            hexBuf[i++] = cmykColor[comp];
+	            if (i >= 64) {
+	              writePSBuf(hexBuf, i);
+	              i = 0;
+	            }
+	          }
+	        } else {
+	          // Gray color image
+	          for (x = 0; x < w; ++x) {
+	            if (likely(colorMode == splashModeRGB8)) {
+                      inputColor.c[0] = byteToCol(p[3*x + 0]);
+                      inputColor.c[1] = byteToCol(p[3*x + 1]);
+                      inputColor.c[2] = byteToCol(p[3*x + 2]);
+                    } else {
+                      inputColor.c[0] = byteToCol(p[3*x + 2]);
+                      inputColor.c[1] = byteToCol(p[3*x + 1]);
+                      inputColor.c[2] = byteToCol(p[3*x + 0]);
+                    }
+                    rgbCS->getCMYK(&inputColor, &cmyk);
+                    cmykColor[0] = colToByte(cmyk.c);
+                    cmykColor[1] = colToByte(cmyk.m);
+                    cmykColor[2] = colToByte(cmyk.y);
+                    cmykColor[3] = colToByte(cmyk.k);
+
+	            col[comp] |= cmykColor[comp];
+	            digit = cmykColor[comp] / 16;
+	            hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
+	            digit = cmykColor[comp] % 16;
+	            hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
+	            if (i >= 64) {
+	              hexBuf[i++] = '\n';
+	              writePSBuf(hexBuf, i);
+	              i = 0;
+	            }
+	          }
+	        }
+              }
+              p -= bitmap->getRowSize();
+            }
+            delete rgbCS;
+            break;
+          default:
+            for (y = 0; y < h; ++y) {
+              for (comp = 0; comp < 4; ++comp) {
+	        if (useBinary) {
+	          // Binary color image
+	          for (x = 0; x < w; ++x) {
+	            col[comp] |= p[4*x + comp];
+	            hexBuf[i++] = p[4*x + comp];
+	            if (i >= 64) {
+	              writePSBuf(hexBuf, i);
+	              i = 0;
+	            }
+	          }
+	        } else {
+	          // Gray color image
+	          for (x = 0; x < w; ++x) {
+	            col[comp] |= p[4*x + comp];
+	            digit = p[4*x + comp] / 16;
+	            hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
+	            digit = p[4*x + comp] % 16;
+	            hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
+	            if (i >= 64) {
+	              hexBuf[i++] = '\n';
+	              writePSBuf(hexBuf, i);
+	              i = 0;
+	            }
+	          }
+	        }
+              }
+              p -= bitmap->getRowSize();
+            }
+          break;
+        }
+      } else {
+	// Color image, do not need to check color flags
+        switch (colorMode) {
+          case splashModeRGB8:
+          case splashModeBGR8:
+            rgbCS = new GfxDeviceRGBColorSpace();
+            for (y = 0; y < h; ++y) {
+              for (comp = 0; comp < 4; ++comp) {
+	        if (useBinary) {
+	          // Binary color image
+	          for (x = 0; x < w; ++x) {
+	            if (likely(colorMode == splashModeRGB8)) {
+                      inputColor.c[0] = byteToCol(p[3*x + 0]);
+                      inputColor.c[1] = byteToCol(p[3*x + 1]);
+                      inputColor.c[2] = byteToCol(p[3*x + 2]);
+                    } else {
+                      inputColor.c[0] = byteToCol(p[3*x + 2]);
+                      inputColor.c[1] = byteToCol(p[3*x + 1]);
+                      inputColor.c[2] = byteToCol(p[3*x + 0]);
+                    }
+                    rgbCS->getCMYK(&inputColor, &cmyk);
+                    cmykColor[0] = colToByte(cmyk.c);
+                    cmykColor[1] = colToByte(cmyk.m);
+                    cmykColor[2] = colToByte(cmyk.y);
+                    cmykColor[3] = colToByte(cmyk.k);
+
+	            hexBuf[i++] = cmykColor[comp];
+	            if (i >= 64) {
+	              writePSBuf(hexBuf, i);
+	              i = 0;
+	            }
+	          }
+	        } else {
+	          // Hex color image
+	          for (x = 0; x < w; ++x) {
+	            if (likely(colorMode == splashModeRGB8)) {
+                      inputColor.c[0] = byteToCol(p[3*x + 0]);
+                      inputColor.c[1] = byteToCol(p[3*x + 1]);
+                      inputColor.c[2] = byteToCol(p[3*x + 2]);
+                    } else {
+                      inputColor.c[0] = byteToCol(p[3*x + 2]);
+                      inputColor.c[1] = byteToCol(p[3*x + 1]);
+                      inputColor.c[2] = byteToCol(p[3*x + 0]);
+                    }
+                    rgbCS->getCMYK(&inputColor, &cmyk);
+                    cmykColor[0] = colToByte(cmyk.c);
+                    cmykColor[1] = colToByte(cmyk.m);
+                    cmykColor[2] = colToByte(cmyk.y);
+                    cmykColor[3] = colToByte(cmyk.k);
+
+	            digit = cmykColor[comp] / 16;
+	            hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
+	            digit = cmykColor[comp] % 16;
+	            hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
+	            if (i >= 64) {
+	              hexBuf[i++] = '\n';
+	              writePSBuf(hexBuf, i);
+	              i = 0;
+	            }
+	          }
+	        }
+              }
+              p -= bitmap->getRowSize();
+            }
+            delete rgbCS;
+            break;
+          default:
+            for (y = 0; y < h; ++y) {
+              for (comp = 0; comp < 4; ++comp) {
+	        if (useBinary) {
+	          // Binary color image
+	          for (x = 0; x < w; ++x) {
+	            hexBuf[i++] = p[4*x + comp];
+	            if (i >= 64) {
+	              writePSBuf(hexBuf, i);
+	              i = 0;
+	            }
+	          }
+	        } else {
+	          // Hex color image
+	          for (x = 0; x < w; ++x) {
+	            digit = p[4*x + comp] / 16;
+	            hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
+	            digit = p[4*x + comp] % 16;
+	            hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
+	            if (i >= 64) {
+	              hexBuf[i++] = '\n';
+	              writePSBuf(hexBuf, i);
+	              i = 0;
+	            }
+	          }
+	        }
+              }
+              p -= bitmap->getRowSize();
+            }
+          break;
         }
       }
       if (i != 0) {