Don't crash when SimpleCache index is corrupt.
[chromium-blink-merge.git] / ui / gfx / image / image_util.cc
blobe230a0b12643f637de72c055bde15f54f059a310
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/image/image_util.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "third_party/skia/include/core/SkBitmap.h"
9 #include "ui/gfx/codec/jpeg_codec.h"
10 #include "ui/gfx/image/image.h"
11 #include "ui/gfx/image/image_skia.h"
13 namespace gfx {
15 // The iOS implementations of the JPEG functions are in image_util_ios.mm.
16 #if !defined(OS_IOS)
17 Image ImageFrom1xJPEGEncodedData(const unsigned char* input,
18 size_t input_size) {
19 scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode(input, input_size));
20 if (bitmap.get())
21 return Image::CreateFrom1xBitmap(*bitmap);
23 return Image();
26 bool JPEG1xEncodedDataFromImage(const Image& image, int quality,
27 std::vector<unsigned char>* dst) {
28 const gfx::ImageSkiaRep& image_skia_rep =
29 image.AsImageSkia().GetRepresentation(ui::SCALE_FACTOR_100P);
30 if (image_skia_rep.scale_factor() != ui::SCALE_FACTOR_100P)
31 return false;
33 const SkBitmap& bitmap = image_skia_rep.sk_bitmap();
34 SkAutoLockPixels bitmap_lock(bitmap);
36 if (!bitmap.readyToDraw())
37 return false;
39 return gfx::JPEGCodec::Encode(
40 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
41 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(),
42 bitmap.height(),
43 static_cast<int>(bitmap.rowBytes()), quality,
44 dst);
46 #endif // !defined(OS_IOS)