summaryrefslogtreecommitdiff
path: root/chromium/ui/gfx/image/image_util.cc
blob: a1a30a528724e18dff25cda982c5ef8e1ffcaf52 (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
// Copyright (c) 2012 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 "ui/gfx/image/image_util.h"

#include <stdint.h>

#include <algorithm>
#include <memory>

#include "build/build_config.h"
#include "skia/ext/image_operations.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/jpeg_codec.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/resize_image_dimensions.h"

namespace {

// Returns whether column |x| of |bitmap| has any "visible pixels", where
// "visible" is defined as having an opactiy greater than an arbitrary small
// value.
bool ColumnHasVisiblePixels(const SkBitmap& bitmap, int x) {
  const SkAlpha kMinimumVisibleOpacity = 12;
  for (int y = 0; y < bitmap.height(); ++y) {
    if (SkColorGetA(bitmap.getColor(x, y)) > kMinimumVisibleOpacity)
      return true;
  }
  return false;
}

}  // namespace

namespace gfx {

// The iOS implementations of the JPEG functions are in image_util_ios.mm.
#if !defined(OS_IOS)

Image ImageFrom1xJPEGEncodedData(const unsigned char* input,
                                 size_t input_size) {
  std::unique_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode(input, input_size));
  if (bitmap.get())
    return Image::CreateFrom1xBitmap(*bitmap);

  return Image();
}

Image ResizedImageForSearchByImage(const Image& image) {
  return ResizedImageForSearchByImageSkiaRepresentation(image);
}

// The MacOS implementation of this function is in image_utils_mac.mm.
#if !defined(OS_APPLE)
bool JPEG1xEncodedDataFromImage(const Image& image,
                                int quality,
                                std::vector<unsigned char>* dst) {
  return JPEG1xEncodedDataFromSkiaRepresentation(image, quality, dst);
}
#endif  // !defined(OS_APPLE)

bool JPEG1xEncodedDataFromSkiaRepresentation(const Image& image,
                                             int quality,
                                             std::vector<unsigned char>* dst) {
  const gfx::ImageSkiaRep& image_skia_rep =
      image.AsImageSkia().GetRepresentation(1.0f);
  if (image_skia_rep.scale() != 1.0f)
    return false;

  const SkBitmap& bitmap = image_skia_rep.GetBitmap();
  if (!bitmap.readyToDraw())
    return false;

  return gfx::JPEGCodec::Encode(bitmap, quality, dst);
}

Image ResizedImageForSearchByImageSkiaRepresentation(const Image& image) {
  const gfx::ImageSkiaRep& image_skia_rep =
      image.AsImageSkia().GetRepresentation(1.0f);
  if (image_skia_rep.scale() != 1.0f)
    return image;

  const SkBitmap& bitmap = image_skia_rep.GetBitmap();
  if (bitmap.height() * bitmap.width() > kSearchByImageMaxImageArea &&
      (bitmap.width() > kSearchByImageMaxImageWidth ||
       bitmap.height() > kSearchByImageMaxImageHeight)) {
    double scale = std::min(
        static_cast<double>(kSearchByImageMaxImageWidth) / bitmap.width(),
        static_cast<double>(kSearchByImageMaxImageHeight) / bitmap.height());
    int width = base::ClampToRange<int>(scale * bitmap.width(), 1,
                                        kSearchByImageMaxImageWidth);
    int height = base::ClampToRange<int>(scale * bitmap.height(), 1,
                                         kSearchByImageMaxImageHeight);
    SkBitmap new_bitmap = skia::ImageOperations::Resize(
        bitmap, skia::ImageOperations::RESIZE_GOOD, width, height);
    return Image(ImageSkia(ImageSkiaRep(new_bitmap, 0.0f)));
  }

  return image;
}
#endif  // !defined(OS_IOS)

void GetVisibleMargins(const ImageSkia& image, int* left, int* right) {
  *left = 0;
  *right = 0;
  if (!image.HasRepresentation(1.f))
    return;
  const SkBitmap& bitmap = image.GetRepresentation(1.f).GetBitmap();
  if (bitmap.drawsNothing() || bitmap.isOpaque())
    return;

  int x = 0;
  for (; x < bitmap.width(); ++x) {
    if (ColumnHasVisiblePixels(bitmap, x)) {
      *left = x;
      break;
    }
  }

  if (x == bitmap.width()) {
    // Image is fully transparent.  Divide the width in half, giving the leading
    // region the extra pixel for odd widths.
    *left = (bitmap.width() + 1) / 2;
    *right = bitmap.width() - *left;
    return;
  }

  // Since we already know column *left is non-transparent, we can avoid
  // rechecking that column; hence the '>' here.
  for (x = bitmap.width() - 1; x > *left; --x) {
    if (ColumnHasVisiblePixels(bitmap, x))
      break;
  }
  *right = bitmap.width() - 1 - x;
}

}  // namespace gfx