Home | History | Annotate | Download | only in images
      1 /*
      2  * Copyright 2009 The Android Open Source Project
      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 "SkImageEncoder.h"
      9 #include "SkBitmap.h"
     10 #include "SkStream.h"
     11 #include "SkTemplates.h"
     12 
     13 SkImageEncoder::~SkImageEncoder() {}
     14 
     15 bool SkImageEncoder::encodeStream(SkWStream* stream, const SkBitmap& bm,
     16                                   int quality) {
     17     quality = SkMin32(100, SkMax32(0, quality));
     18     return this->onEncode(stream, bm, quality);
     19 }
     20 
     21 bool SkImageEncoder::encodeFile(const char file[], const SkBitmap& bm,
     22                                 int quality) {
     23     quality = SkMin32(100, SkMax32(0, quality));
     24     SkFILEWStream   stream(file);
     25     return this->onEncode(&stream, bm, quality);
     26 }
     27 
     28 SkData* SkImageEncoder::encodeData(const SkBitmap& bm, int quality) {
     29     SkDynamicMemoryWStream stream;
     30     quality = SkMin32(100, SkMax32(0, quality));
     31     if (this->onEncode(&stream, bm, quality)) {
     32         return stream.copyToData();
     33     }
     34     return NULL;
     35 }
     36 
     37 bool SkImageEncoder::EncodeFile(const char file[], const SkBitmap& bm, Type t,
     38                                 int quality) {
     39     SkAutoTDelete<SkImageEncoder> enc(SkImageEncoder::Create(t));
     40     return enc.get() && enc.get()->encodeFile(file, bm, quality);
     41 }
     42 
     43 bool SkImageEncoder::EncodeStream(SkWStream* stream, const SkBitmap& bm, Type t,
     44                                   int quality) {
     45     SkAutoTDelete<SkImageEncoder> enc(SkImageEncoder::Create(t));
     46     return enc.get() && enc.get()->encodeStream(stream, bm, quality);
     47 }
     48 
     49 SkData* SkImageEncoder::EncodeData(const SkBitmap& bm, Type t, int quality) {
     50     SkAutoTDelete<SkImageEncoder> enc(SkImageEncoder::Create(t));
     51     return enc.get() ? enc.get()->encodeData(bm, quality) : NULL;
     52 }
     53