Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: third_party/agg23/agg_rasterizer_scanline_aa.cpp

Issue 2347603002: Use safe math when rendering line segments in AGG. (Closed)
Patch Set: Add patch file Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/agg23/README.pdfium ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 //---------------------------------------------------------------------------- 2 //----------------------------------------------------------------------------
3 // XYQ: 2006-01-22 Copied from AGG project. 3 // XYQ: 2006-01-22 Copied from AGG project.
4 // This file uses only integer data, so it's suitable for all platforms. 4 // This file uses only integer data, so it's suitable for all platforms.
5 //---------------------------------------------------------------------------- 5 //----------------------------------------------------------------------------
6 //---------------------------------------------------------------------------- 6 //----------------------------------------------------------------------------
7 // Anti-Grain Geometry - Version 2.3 7 // Anti-Grain Geometry - Version 2.3
8 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com) 8 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
9 // 9 //
10 // Permission to copy, use, modify, sell and distribute this software 10 // Permission to copy, use, modify, sell and distribute this software
(...skipping 30 matching lines...) Expand all
41 // ideas. Two functions - render_line and render_hline are the core of 41 // ideas. Two functions - render_line and render_hline are the core of
42 // the algorithm - they calculate the exact coverage of each pixel cell 42 // the algorithm - they calculate the exact coverage of each pixel cell
43 // of the polygon. I left these functions almost as is, because there's 43 // of the polygon. I left these functions almost as is, because there's
44 // no way to improve the perfection - hats off to David and his group! 44 // no way to improve the perfection - hats off to David and his group!
45 // 45 //
46 // All other code is very different from the original. 46 // All other code is very different from the original.
47 // 47 //
48 //---------------------------------------------------------------------------- 48 //----------------------------------------------------------------------------
49 #include <limits.h> 49 #include <limits.h>
50 #include "agg_rasterizer_scanline_aa.h" 50 #include "agg_rasterizer_scanline_aa.h"
51 #include "third_party/base/numerics/safe_math.h"
51 namespace agg 52 namespace agg
52 { 53 {
53 AGG_INLINE void cell_aa::set_cover(int c, int a) 54 AGG_INLINE void cell_aa::set_cover(int c, int a)
54 { 55 {
55 cover = c; 56 cover = c;
56 area = a; 57 area = a;
57 } 58 }
58 AGG_INLINE void cell_aa::add_cover(int c, int a) 59 AGG_INLINE void cell_aa::add_cover(int c, int a)
59 { 60 {
60 cover += c; 61 cover += c;
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 int cy = (y1 + y2) >> 1; 231 int cy = (y1 + y2) >> 1;
231 render_line(x1, y1, cx, cy); 232 render_line(x1, y1, cx, cy);
232 render_line(cx, cy, x2, y2); 233 render_line(cx, cy, x2, y2);
233 } 234 }
234 int dy = y2 - y1; 235 int dy = y2 - y1;
235 int ey1 = y1 >> poly_base_shift; 236 int ey1 = y1 >> poly_base_shift;
236 int ey2 = y2 >> poly_base_shift; 237 int ey2 = y2 >> poly_base_shift;
237 int fy1 = y1 & poly_base_mask; 238 int fy1 = y1 & poly_base_mask;
238 int fy2 = y2 & poly_base_mask; 239 int fy2 = y2 & poly_base_mask;
239 int x_from, x_to; 240 int x_from, x_to;
240 int p, rem, mod, lift, delta, first, incr; 241 int rem, mod, lift, delta, first, incr;
241 if(ey1 == ey2) { 242 if(ey1 == ey2) {
242 render_hline(ey1, x1, fy1, x2, fy2); 243 render_hline(ey1, x1, fy1, x2, fy2);
243 return; 244 return;
244 } 245 }
245 incr = 1; 246 incr = 1;
246 if(dx == 0) { 247 if(dx == 0) {
247 int ex = x1 >> poly_base_shift; 248 int ex = x1 >> poly_base_shift;
248 int two_fx = (x1 - (ex << poly_base_shift)) << 1; 249 int two_fx = (x1 - (ex << poly_base_shift)) << 1;
249 int area; 250 int area;
250 first = poly_base_size; 251 first = poly_base_size;
(...skipping 10 matching lines...) Expand all
261 area = two_fx * delta; 262 area = two_fx * delta;
262 while(ey1 != ey2) { 263 while(ey1 != ey2) {
263 m_cur_cell.set_cover(delta, area); 264 m_cur_cell.set_cover(delta, area);
264 ey1 += incr; 265 ey1 += incr;
265 set_cur_cell(ex, ey1); 266 set_cur_cell(ex, ey1);
266 } 267 }
267 delta = fy2 - poly_base_size + first; 268 delta = fy2 - poly_base_size + first;
268 m_cur_cell.add_cover(delta, two_fx * delta); 269 m_cur_cell.add_cover(delta, two_fx * delta);
269 return; 270 return;
270 } 271 }
271 p = (poly_base_size - fy1) * dx; 272 pdfium::base::CheckedNumeric<int> safeP = poly_base_size - fy1;
273 safeP *= dx;
274 if (!safeP.IsValid())
275 return;
272 first = poly_base_size; 276 first = poly_base_size;
273 if(dy < 0) { 277 if(dy < 0) {
274 p = fy1 * dx; 278 safeP = fy1;
275 first = 0; 279 safeP *= dx;
276 incr = -1; 280 if (!safeP.IsValid())
277 dy = -dy; 281 return;
282 first = 0;
283 incr = -1;
284 dy = -dy;
278 } 285 }
279 delta = p / dy; 286 delta = safeP.ValueOrDie() / dy;
280 mod = p % dy; 287 mod = safeP.ValueOrDie() % dy;
281 if(mod < 0) { 288 if(mod < 0) {
282 delta--; 289 delta--;
283 mod += dy; 290 mod += dy;
284 } 291 }
285 x_from = x1 + delta; 292 x_from = x1 + delta;
286 render_hline(ey1, x1, fy1, x_from, first); 293 render_hline(ey1, x1, fy1, x_from, first);
287 ey1 += incr; 294 ey1 += incr;
288 set_cur_cell(x_from >> poly_base_shift, ey1); 295 set_cur_cell(x_from >> poly_base_shift, ey1);
289 if(ey1 != ey2) { 296 if(ey1 != ey2) {
290 p = poly_base_size * dx; 297 safeP = static_cast<int>(poly_base_size);
291 lift = p / dy; 298 safeP *= dx;
292 rem = p % dy; 299 if (!safeP.IsValid())
293 if(rem < 0) { 300 return;
294 lift--; 301 lift = safeP.ValueOrDie() / dy;
295 rem += dy; 302 rem = safeP.ValueOrDie() % dy;
303 if (rem < 0) {
304 lift--;
305 rem += dy;
296 } 306 }
297 mod -= dy; 307 mod -= dy;
298 while(ey1 != ey2) { 308 while(ey1 != ey2) {
299 delta = lift; 309 delta = lift;
300 mod += rem; 310 mod += rem;
301 if (mod >= 0) { 311 if (mod >= 0) {
302 mod -= dy; 312 mod -= dy;
303 delta++; 313 delta++;
304 } 314 }
305 x_to = x_from + delta; 315 x_to = x_from + delta;
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 } 489 }
480 for(i = 0; i < m_sorted_y.size(); i++) { 490 for(i = 0; i < m_sorted_y.size(); i++) {
481 const sorted_y& cur_y = m_sorted_y[i]; 491 const sorted_y& cur_y = m_sorted_y[i];
482 if(cur_y.num) { 492 if(cur_y.num) {
483 qsort_cells(m_sorted_cells.data() + cur_y.start, cur_y.num); 493 qsort_cells(m_sorted_cells.data() + cur_y.start, cur_y.num);
484 } 494 }
485 } 495 }
486 m_sorted = true; 496 m_sorted = true;
487 } 497 }
488 } 498 }
OLDNEW
« no previous file with comments | « third_party/agg23/README.pdfium ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698