summaryrefslogtreecommitdiff
path: root/xfa_test/pdf/draw_utils.cc
blob: d38be52aef92da58839ab5c29f64138c7d95d415 (plain)
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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "pdf/draw_utils.h"

#include <algorithm>
#include <math.h>
#include <vector>

#include "base/logging.h"
#include "base/numerics/safe_math.h"

namespace chrome_pdf {

inline uint8 GetBlue(const uint32& pixel) {
  return static_cast<uint8>(pixel & 0xFF);
}

inline uint8 GetGreen(const uint32& pixel) {
  return static_cast<uint8>((pixel >> 8) & 0xFF);
}

inline uint8 GetRed(const uint32& pixel) {
  return static_cast<uint8>((pixel >> 16) & 0xFF);
}

inline uint8 GetAlpha(const uint32& pixel) {
  return static_cast<uint8>((pixel >> 24) & 0xFF);
}

inline uint32_t MakePixel(uint8 red, uint8 green, uint8 blue, uint8 alpha) {
  return (static_cast<uint32_t>(alpha) << 24) |
      (static_cast<uint32_t>(red) << 16) |
      (static_cast<uint32_t>(green) << 8) |
      static_cast<uint32_t>(blue);
}

inline uint8 GradientChannel(uint8 start, uint8 end, double ratio) {
  double new_channel = start - (static_cast<double>(start) - end) * ratio;
  if (new_channel < 0)
    return 0;
  if (new_channel > 255)
    return 255;
  return static_cast<uint8>(new_channel + 0.5);
}

inline uint8 ProcessColor(uint8 src_color, uint8 dest_color, uint8 alpha) {
  uint32 processed = static_cast<uint32>(src_color) * alpha +
      static_cast<uint32>(dest_color) * (0xFF - alpha);
  return static_cast<uint8>((processed / 0xFF) & 0xFF);
}

inline bool ImageDataContainsRect(const pp::ImageData& image_data,
                                  const pp::Rect& rect) {
  return rect.width() >= 0 && rect.height() >= 0 &&
      pp::Rect(image_data.size()).Contains(rect);
}

void AlphaBlend(const pp::ImageData& src, const pp::Rect& src_rc,
                pp::ImageData* dest, const pp::Point& dest_origin,
                uint8 alpha_adjustment) {
  if (src_rc.IsEmpty() || !ImageDataContainsRect(src, src_rc))
    return;

  pp::Rect dest_rc(dest_origin, src_rc.size());
  if (dest_rc.IsEmpty() || !ImageDataContainsRect(*dest, dest_rc))
    return;

  const uint32_t* src_origin_pixel = src.GetAddr32(src_rc.point());
  uint32_t* dest_origin_pixel = dest->GetAddr32(dest_origin);

  int height = src_rc.height();
  int width = src_rc.width();
  for (int y = 0; y < height; y++) {
    const uint32_t* src_pixel = src_origin_pixel;
    uint32_t* dest_pixel = dest_origin_pixel;
    for (int x = 0; x < width; x++) {
      uint8 alpha = static_cast<uint8>(static_cast<uint32_t>(alpha_adjustment) *
          GetAlpha(*src_pixel) / 0xFF);
      uint8 red = ProcessColor(GetRed(*src_pixel), GetRed(*dest_pixel), alpha);
      uint8 green = ProcessColor(GetGreen(*src_pixel),
                                 GetGreen(*dest_pixel), alpha);
      uint8 blue = ProcessColor(GetBlue(*src_pixel),
                                GetBlue(*dest_pixel), alpha);
      *dest_pixel = MakePixel(red, green, blue, GetAlpha(*dest_pixel));

      src_pixel++;
      dest_pixel++;
    }
    src_origin_pixel = reinterpret_cast<const uint32_t*>(
        reinterpret_cast<const char*>(src_origin_pixel) + src.stride());
    dest_origin_pixel = reinterpret_cast<uint32_t*>(
        reinterpret_cast<char*>(dest_origin_pixel) + dest->stride());
  }
}

void GradientFill(pp::ImageData* image, const pp::Rect& rc,
                  uint32 start_color, uint32 end_color, bool horizontal) {
  std::vector<uint32> colors;
  colors.resize(horizontal ? rc.width() : rc.height());
  for (size_t i = 0; i < colors.size(); ++i) {
    double ratio = static_cast<double>(i) / colors.size();
    colors[i] = MakePixel(
        GradientChannel(GetRed(start_color), GetRed(end_color), ratio),
        GradientChannel(GetGreen(start_color), GetGreen(end_color), ratio),
        GradientChannel(GetBlue(start_color), GetBlue(end_color), ratio),
        GradientChannel(GetAlpha(start_color), GetAlpha(end_color), ratio));
  }

  if (horizontal) {
    const void* data = &(colors[0]);
    size_t size = colors.size() * 4;
    uint32_t* origin_pixel = image->GetAddr32(rc.point());
    for (int y = 0; y < rc.height(); y++) {
      memcpy(origin_pixel, data, size);
      origin_pixel = reinterpret_cast<uint32_t*>(
          reinterpret_cast<char*>(origin_pixel) + image->stride());
    }
  } else {
    uint32_t* origin_pixel = image->GetAddr32(rc.point());
    for (int y = 0; y < rc.height(); y++) {
      uint32_t* pixel = origin_pixel;
      for (int x = 0; x < rc.width(); x++) {
        *pixel = colors[y];
        pixel++;
      }
      origin_pixel = reinterpret_cast<uint32_t*>(
          reinterpret_cast<char*>(origin_pixel) + image->stride());
    }
  }
}

void GradientFill(pp::Instance* instance,
                  pp::ImageData* image,
                  const pp::Rect& dirty_rc,
                  const pp::Rect& gradient_rc,
                  uint32 start_color,
                  uint32 end_color,
                  bool horizontal,
                  uint8 transparency) {
  pp::Rect draw_rc = gradient_rc.Intersect(dirty_rc);
  if (draw_rc.IsEmpty())
    return;

  pp::ImageData gradient(instance, PP_IMAGEDATAFORMAT_BGRA_PREMUL,
      gradient_rc.size(), false);

  GradientFill(&gradient, pp::Rect(pp::Point(), gradient_rc.size()),
               start_color, end_color, horizontal);

  pp::Rect copy_rc(draw_rc);
  copy_rc.Offset(-gradient_rc.x(), -gradient_rc.y());
  AlphaBlend(gradient, copy_rc, image, draw_rc.point(), transparency);
}

void CopyImage(const pp::ImageData& src, const pp::Rect& src_rc,
               pp::ImageData* dest, const pp::Rect& dest_rc,
               bool stretch) {
  if (src_rc.IsEmpty() || !ImageDataContainsRect(src, src_rc))
    return;

  pp::Rect stretched_rc(dest_rc.point(),
                        stretch ? dest_rc.size() : src_rc.size());
  if (stretched_rc.IsEmpty() || !ImageDataContainsRect(*dest, stretched_rc))
    return;

  const uint32_t* src_origin_pixel = src.GetAddr32(src_rc.point());
  uint32_t* dest_origin_pixel = dest->GetAddr32(dest_rc.point());
  if (stretch) {
    double x_ratio = static_cast<double>(src_rc.width()) / dest_rc.width();
    double y_ratio = static_cast<double>(src_rc.height()) / dest_rc.height();
    int32_t height = dest_rc.height();
    int32_t width = dest_rc.width();
    for (int32_t y = 0; y < height; ++y) {
      uint32_t* dest_pixel = dest_origin_pixel;
      for (int32_t x = 0; x < width; ++x) {
        uint32 src_x = static_cast<uint32>(x * x_ratio);
        uint32 src_y = static_cast<uint32>(y * y_ratio);
        const uint32_t* src_pixel = src.GetAddr32(
            pp::Point(src_rc.x() + src_x, src_rc.y() + src_y));
        *dest_pixel = *src_pixel;
        dest_pixel++;
      }
      dest_origin_pixel = reinterpret_cast<uint32_t*>(
          reinterpret_cast<char*>(dest_origin_pixel) + dest->stride());
    }
  } else {
    int32_t height = src_rc.height();
    base::CheckedNumeric<int32_t> width_bytes = src_rc.width();
    width_bytes *= 4;
    for (int32_t y = 0; y < height; ++y) {
      memcpy(dest_origin_pixel, src_origin_pixel, width_bytes.ValueOrDie());
      src_origin_pixel = reinterpret_cast<const uint32_t*>(
          reinterpret_cast<const char*>(src_origin_pixel) + src.stride());
      dest_origin_pixel = reinterpret_cast<uint32_t*>(
          reinterpret_cast<char*>(dest_origin_pixel) + dest->stride());
    }
  }
}

void FillRect(pp::ImageData* image, const pp::Rect& rc, uint32 color) {
  int height = rc.height();
  if (height == 0)
    return;

  // Fill in first row.
  uint32_t* top_line = image->GetAddr32(rc.point());
  int width = rc.width();
  for (int x = 0; x < width; x++)
    top_line[x] = color;

  // Fill in the rest of the rectangle.
  int byte_width = width * 4;
  uint32_t* cur_line = reinterpret_cast<uint32_t*>(
          reinterpret_cast<char*>(top_line) + image->stride());
  for (int y = 1; y < height; y++) {
    memcpy(cur_line, top_line, byte_width);
    cur_line = reinterpret_cast<uint32_t*>(
            reinterpret_cast<char*>(cur_line) + image->stride());
  }
}

ShadowMatrix::ShadowMatrix(uint32 depth, double factor, uint32 background)
    : depth_(depth), factor_(factor), background_(background) {
  DCHECK(depth_ > 0);
  matrix_.resize(depth_ * depth_);

  // pv - is a rounding power factor for smoothing corners.
  // pv = 2.0 will make corners completely round.
  const double pv = 4.0;
  // pow_pv - cache to avoid recalculating pow(x, pv) every time.
  std::vector<double> pow_pv(depth_, 0.0);

  double r = static_cast<double>(depth_);
  double coef = 256.0 / pow(r, factor);

  for (uint32 y = 0; y < depth_; y++) {
    // Since matrix is symmetrical, we can reduce the number of calculations
    // by mirroring results.
    for (uint32 x = 0; x <= y; x++) {
      // Fill cache if needed.
      if (pow_pv[x] == 0.0)
        pow_pv[x] =  pow(x, pv);
      if (pow_pv[y] == 0.0)
        pow_pv[y] =  pow(y, pv);

      // v - is a value for the smoothing function.
      // If x == 0 simplify calculations.
      double v = (x == 0) ? y : pow(pow_pv[x] + pow_pv[y], 1 / pv);

      // Smoothing function.
      // If factor == 1, smoothing will be linear from 0 to the end,
      // if 0 < factor < 1, smoothing will drop faster near 0.
      // if factor > 1, smoothing will drop faster near the end (depth).
      double f = 256.0 - coef * pow(v, factor);

      uint8 alpha = 0;
      if (f > kOpaqueAlpha)
        alpha = kOpaqueAlpha;
      else if (f < kTransparentAlpha)
        alpha = kTransparentAlpha;
      else
        alpha = static_cast<uint8>(f);

      uint8 red = ProcessColor(0, GetRed(background), alpha);
      uint8 green = ProcessColor(0, GetGreen(background), alpha);
      uint8 blue = ProcessColor(0, GetBlue(background), alpha);
      uint32 pixel = MakePixel(red, green, blue, GetAlpha(background));

      // Mirror matrix.
      matrix_[y * depth_ + x] = pixel;
      matrix_[x * depth_ + y] = pixel;
    }
  }
}

ShadowMatrix::~ShadowMatrix() {
}

void PaintShadow(pp::ImageData* image,
                 const pp::Rect& clip_rc,
                 const pp::Rect& shadow_rc,
                 const ShadowMatrix& matrix) {
  pp::Rect draw_rc = shadow_rc.Intersect(clip_rc);
  if (draw_rc.IsEmpty())
    return;

  int32 depth = static_cast<int32>(matrix.depth());
  for (int32_t y = draw_rc.y(); y < draw_rc.bottom(); y++) {
    for (int32_t x = draw_rc.x(); x < draw_rc.right(); x++) {
      int32_t matrix_x = std::max(depth + shadow_rc.x() - x - 1,
                                  depth - shadow_rc.right() + x);
      int32_t matrix_y = std::max(depth + shadow_rc.y() - y - 1,
                                  depth - shadow_rc.bottom() + y);
      uint32_t* pixel = image->GetAddr32(pp::Point(x, y));

      if (matrix_x < 0)
        matrix_x = 0;
      else if (matrix_x >= static_cast<int32>(depth))
        matrix_x = depth - 1;

      if (matrix_y < 0)
        matrix_y = 0;
      else if (matrix_y >= static_cast<int32>(depth))
        matrix_y = depth - 1;

      *pixel = matrix.GetValue(matrix_x, matrix_y);
    }
  }
}

void DrawShadow(pp::ImageData* image,
                const pp::Rect& shadow_rc,
                const pp::Rect& object_rc,
                const pp::Rect& clip_rc,
                const ShadowMatrix& matrix) {
  if (shadow_rc == object_rc)
    return;  // Nothing to paint.

  // Fill top part.
  pp::Rect rc(shadow_rc.point(),
              pp::Size(shadow_rc.width(), object_rc.y() - shadow_rc.y()));
  PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix);

  // Fill bottom part.
  rc = pp::Rect(shadow_rc.x(), object_rc.bottom(),
                shadow_rc.width(), shadow_rc.bottom() - object_rc.bottom());
  PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix);

  // Fill left part.
  rc = pp::Rect(shadow_rc.x(), object_rc.y(),
                object_rc.x() - shadow_rc.x(), object_rc.height());
  PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix);

  // Fill right part.
  rc = pp::Rect(object_rc.right(), object_rc.y(),
                shadow_rc.right() - object_rc.right(), object_rc.height());
  PaintShadow(image, rc.Intersect(clip_rc), shadow_rc, matrix);
}

}  // namespace chrome_pdf