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. 4 5 #include "ui/gfx/image/image_png_rep.h" 6 7 #include "third_party/skia/include/core/SkBitmap.h" 8 #include "ui/gfx/codec/png_codec.h" 9 #include "ui/gfx/size.h" 10 11 namespace gfx { 12 13 ImagePNGRep::ImagePNGRep() 14 : raw_data(NULL), 15 scale_factor(ui::SCALE_FACTOR_NONE) { 16 } 17 18 ImagePNGRep::ImagePNGRep(const scoped_refptr<base::RefCountedMemory>& data, 19 ui::ScaleFactor data_scale_factor) 20 : raw_data(data), 21 scale_factor(data_scale_factor) { 22 } 23 24 ImagePNGRep::~ImagePNGRep() { 25 } 26 27 gfx::Size ImagePNGRep::Size() const { 28 // The only way to get the width and height of a raw PNG stream, at least 29 // using the gfx::PNGCodec API, is to decode the whole thing. 30 CHECK(raw_data.get()); 31 SkBitmap bitmap; 32 if (!gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(), 33 &bitmap)) { 34 LOG(ERROR) << "Unable to decode PNG."; 35 return gfx::Size(0, 0); 36 } 37 return gfx::Size(bitmap.width(), bitmap.height()); 38 } 39 40 } // namespace gfx 41