blob: 1fcc4ff18be23eeb51dc843489a32c00dfbbf685 [file] [log] [blame]
robertphillips@google.com7d501ab2012-06-21 21:09:06 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrSurface.h"
9
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000010#include "SkBitmap.h"
reed@google.com473f0aa2013-12-06 20:31:45 +000011#include "SkGr.h"
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000012#include "SkImageEncoder.h"
bungeman@google.comfab44db2013-10-11 18:50:45 +000013#include <stdio.h>
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000014
reed@google.com473f0aa2013-12-06 20:31:45 +000015void GrSurface::asImageInfo(SkImageInfo* info) const {
16 if (!GrPixelConfig2ColorType(this->config(), &info->fColorType)) {
17 sk_throw();
18 }
19 info->fWidth = this->width();
20 info->fHeight = this->height();
21 info->fAlphaType = kPremul_SkAlphaType;
22}
23
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000024bool GrSurface::savePixels(const char* filename) {
25 SkBitmap bm;
26 bm.setConfig(SkBitmap::kARGB_8888_Config, this->width(), this->height());
27 bm.allocPixels();
28
skia.committer@gmail.com1d3bfdc2013-10-01 07:01:46 +000029 bool result = readPixels(0, 0, this->width(), this->height(), kSkia8888_GrPixelConfig,
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000030 bm.getPixels());
31 if (!result) {
32 SkDebugf("------ failed to read pixels for %s\n", filename);
33 return false;
34 }
skia.committer@gmail.com1d3bfdc2013-10-01 07:01:46 +000035
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000036 // remove any previous version of this file
37 remove(filename);
38
39 if (!SkImageEncoder::EncodeFile(filename, bm, SkImageEncoder::kPNG_Type, 100)) {
40 SkDebugf("------ failed to encode %s\n", filename);
41 remove(filename); // remove any partial file
42 return false;
43 }
44
45 return true;
46}