summaryrefslogtreecommitdiffstats
path: root/ui/gfx/image.cc
blob: 389935dc8bc2e5c969aff71e993c1a6968ced567 (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
// 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 "ui/gfx/image.h"

#include <algorithm>

#include "base/logging.h"
#include "base/stl_util-inl.h"
#include "third_party/skia/include/core/SkBitmap.h"

#if defined(OS_LINUX)
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib-object.h>
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/gtk_util.h"
#elif defined(OS_MACOSX)
#include "base/mac/mac_util.h"
#include "skia/ext/skia_utils_mac.h"
#endif

namespace gfx {

namespace internal {

#if defined(OS_MACOSX)
// This is a wrapper around gfx::NSImageToSkBitmap() because this cross-platform
// file cannot include the [square brackets] of ObjC.
bool NSImageToSkBitmaps(NSImage* image, std::vector<const SkBitmap*>& bitmaps);
#endif

#if defined(OS_LINUX)
const SkBitmap* GdkPixbufToSkBitmap(GdkPixbuf* pixbuf) {
  gfx::CanvasSkia canvas(gdk_pixbuf_get_width(pixbuf),
                         gdk_pixbuf_get_height(pixbuf),
                         /*is_opaque=*/false);
  canvas.DrawGdkPixbuf(pixbuf, 0, 0);
  return new SkBitmap(canvas.ExtractBitmap());
}
#endif

class SkBitmapRep;
class GdkPixbufRep;
class NSImageRep;

// An ImageRep is the object that holds the backing memory for an Image. Each
// RepresentationType has an ImageRep subclass that is responsible for freeing
// the memory that the ImageRep holds. When an ImageRep is created, it expects
// to take ownership of the image, without having to retain it or increase its
// reference count.
class ImageRep {
 public:
  explicit ImageRep(Image::RepresentationType rep) : type_(rep) {}

  // Deletes the associated pixels of an ImageRep.
  virtual ~ImageRep() {}

  // Cast helpers ("fake RTTI").
  SkBitmapRep* AsSkBitmapRep() {
    CHECK_EQ(type_, Image::kSkBitmapRep);
    return reinterpret_cast<SkBitmapRep*>(this);
  }

#if defined(OS_LINUX)
  GdkPixbufRep* AsGdkPixbufRep() {
    CHECK_EQ(type_, Image::kGdkPixbufRep);
    return reinterpret_cast<GdkPixbufRep*>(this);
  }
#endif

#if defined(OS_MACOSX)
  NSImageRep* AsNSImageRep() {
    CHECK_EQ(type_, Image::kNSImageRep);
    return reinterpret_cast<NSImageRep*>(this);
  }
#endif

  Image::RepresentationType type() const { return type_; }

 private:
  Image::RepresentationType type_;
};

class SkBitmapRep : public ImageRep {
 public:
  explicit SkBitmapRep(const SkBitmap* bitmap)
      : ImageRep(Image::kSkBitmapRep) {
    CHECK(bitmap);
    bitmaps_.push_back(bitmap);
  }

  explicit SkBitmapRep(const std::vector<const SkBitmap*>& bitmaps)
      : ImageRep(Image::kSkBitmapRep),
        bitmaps_(bitmaps) {
    CHECK(!bitmaps_.empty());
  }

  virtual ~SkBitmapRep() {
    STLDeleteElements(&bitmaps_);
  }

  const SkBitmap* bitmap() const { return bitmaps_[0]; }

  const std::vector<const SkBitmap*>& bitmaps() const { return bitmaps_; }

 private:
  std::vector<const SkBitmap*> bitmaps_;

  DISALLOW_COPY_AND_ASSIGN(SkBitmapRep);
};

#if defined(OS_LINUX)
class GdkPixbufRep : public ImageRep {
 public:
  explicit GdkPixbufRep(GdkPixbuf* pixbuf)
      : ImageRep(Image::kGdkPixbufRep),
        pixbuf_(pixbuf) {
    CHECK(pixbuf);
  }

  virtual ~GdkPixbufRep() {
    if (pixbuf_) {
      g_object_unref(pixbuf_);
      pixbuf_ = NULL;
    }
  }

  GdkPixbuf* pixbuf() const { return pixbuf_; }

 private:
  GdkPixbuf* pixbuf_;

  DISALLOW_COPY_AND_ASSIGN(GdkPixbufRep);
};
#endif

#if defined(OS_MACOSX)
class NSImageRep : public ImageRep {
 public:
  explicit NSImageRep(NSImage* image)
      : ImageRep(Image::kNSImageRep),
        image_(image) {
    CHECK(image);
  }

  virtual ~NSImageRep() {
    base::mac::NSObjectRelease(image_);
    image_ = nil;
  }

  NSImage* image() const { return image_; }

 private:
  NSImage* image_;

  DISALLOW_COPY_AND_ASSIGN(NSImageRep);
};
#endif

}  // namespace internal

Image::Image(const SkBitmap* bitmap)
    : default_representation_(Image::kSkBitmapRep) {
  internal::SkBitmapRep* rep = new internal::SkBitmapRep(bitmap);
  AddRepresentation(rep);
}

Image::Image(const std::vector<const SkBitmap*>& bitmaps)
    : default_representation_(Image::kSkBitmapRep) {
  internal::SkBitmapRep* rep = new internal::SkBitmapRep(bitmaps);
  AddRepresentation(rep);
}

#if defined(OS_LINUX)
Image::Image(GdkPixbuf* pixbuf)
    : default_representation_(Image::kGdkPixbufRep) {
  internal::GdkPixbufRep* rep = new internal::GdkPixbufRep(pixbuf);
  AddRepresentation(rep);
}
#endif

#if defined(OS_MACOSX)
Image::Image(NSImage* image) : default_representation_(Image::kNSImageRep) {
  internal::NSImageRep* rep = new internal::NSImageRep(image);
  AddRepresentation(rep);
}
#endif

Image::~Image() {
  for (RepresentationMap::iterator it = representations_.begin();
       it != representations_.end(); ++it) {
    delete it->second;
  }
  representations_.clear();
}

Image::operator const SkBitmap*() {
  internal::ImageRep* rep = GetRepresentation(Image::kSkBitmapRep);
  return rep->AsSkBitmapRep()->bitmap();
}

Image::operator const SkBitmap&() {
  return *(this->operator const SkBitmap*());
}

#if defined(OS_LINUX)
Image::operator GdkPixbuf*() {
  internal::ImageRep* rep = GetRepresentation(Image::kGdkPixbufRep);
  return rep->AsGdkPixbufRep()->pixbuf();
}
#endif

#if defined(OS_MACOSX)
Image::operator NSImage*() {
  internal::ImageRep* rep = GetRepresentation(Image::kNSImageRep);
  return rep->AsNSImageRep()->image();
}
#endif

bool Image::HasRepresentation(RepresentationType type) {
  return representations_.count(type) != 0;
}

void Image::SwapRepresentations(gfx::Image* other) {
  representations_.swap(other->representations_);
  std::swap(default_representation_, other->default_representation_);
}

internal::ImageRep* Image::DefaultRepresentation() {
  RepresentationMap::iterator it =
      representations_.find(default_representation_);
  DCHECK(it != representations_.end());
  return it->second;
}

internal::ImageRep* Image::GetRepresentation(RepresentationType rep_type) {
  // If the requested rep is the default, return it.
  internal::ImageRep* default_rep = DefaultRepresentation();
  if (rep_type == default_representation_)
    return default_rep;

  // Check to see if the representation already exists.
  RepresentationMap::iterator it = representations_.find(rep_type);
  if (it != representations_.end())
    return it->second;

  // At this point, the requested rep does not exist, so it must be converted
  // from the default rep.

  // Handle native-to-Skia conversion.
  if (rep_type == Image::kSkBitmapRep) {
    internal::SkBitmapRep* rep = NULL;
#if defined(OS_LINUX)
    if (default_representation_ == Image::kGdkPixbufRep) {
      internal::GdkPixbufRep* pixbuf_rep = default_rep->AsGdkPixbufRep();
      rep = new internal::SkBitmapRep(
          internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf()));
    }
#elif defined(OS_MACOSX)
    if (default_representation_ == Image::kNSImageRep) {
      internal::NSImageRep* nsimage_rep = default_rep->AsNSImageRep();
      std::vector<const SkBitmap*> bitmaps;
      CHECK(internal::NSImageToSkBitmaps(nsimage_rep->image(), bitmaps));
      rep = new internal::SkBitmapRep(bitmaps);
    }
#endif
    CHECK(rep);
    AddRepresentation(rep);
    return rep;
  }

  // Handle Skia-to-native conversions.
  if (default_rep->type() == Image::kSkBitmapRep) {
    internal::SkBitmapRep* skia_rep = default_rep->AsSkBitmapRep();
    internal::ImageRep* native_rep = NULL;
#if defined(OS_LINUX)
    if (rep_type == Image::kGdkPixbufRep) {
      GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(skia_rep->bitmap());
      native_rep = new internal::GdkPixbufRep(pixbuf);
    }
#elif defined(OS_MACOSX)
    if (rep_type == Image::kNSImageRep) {
      NSImage* image = gfx::SkBitmapsToNSImage(skia_rep->bitmaps());
      base::mac::NSObjectRetain(image);
      native_rep = new internal::NSImageRep(image);
    }
#endif
    CHECK(native_rep);
    AddRepresentation(native_rep);
    return native_rep;
  }

  // Something went seriously wrong...
  return NULL;
}

void Image::AddRepresentation(internal::ImageRep* rep) {
  representations_.insert(std::make_pair(rep->type(), rep));
}

size_t Image::GetNumberOfSkBitmaps() {
  return GetRepresentation(Image::kSkBitmapRep)->AsSkBitmapRep()->
      bitmaps().size();
}

const SkBitmap* Image::GetSkBitmapAtIndex(size_t index) {
  return GetRepresentation(Image::kSkBitmapRep)->AsSkBitmapRep()->
      bitmaps()[index];
}

}  // namespace gfx