1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/images/SkImageEncoder_argb.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,119 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#include "SkImageEncoder.h" 1.12 +#include "SkBitmap.h" 1.13 +#include "SkColorPriv.h" 1.14 +#include "SkStream.h" 1.15 +#include "SkTemplates.h" 1.16 + 1.17 +class SkARGBImageEncoder : public SkImageEncoder { 1.18 +protected: 1.19 + virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK_OVERRIDE; 1.20 + 1.21 +private: 1.22 + typedef SkImageEncoder INHERITED; 1.23 +}; 1.24 + 1.25 +typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* argb, int width, 1.26 + const SkPMColor* SK_RESTRICT ctable); 1.27 + 1.28 +static void ARGB_8888_To_ARGB(const uint8_t* in, uint8_t* argb, int width, const SkPMColor*) { 1.29 + const uint32_t* SK_RESTRICT src = (const uint32_t*)in; 1.30 + for (int i = 0; i < width; ++i) { 1.31 + const uint32_t c = *src++; 1.32 + argb[0] = SkGetPackedA32(c); 1.33 + argb[1] = SkGetPackedR32(c); 1.34 + argb[2] = SkGetPackedG32(c); 1.35 + argb[3] = SkGetPackedB32(c); 1.36 + argb += 4; 1.37 + } 1.38 +} 1.39 + 1.40 +static void RGB_565_To_ARGB(const uint8_t* in, uint8_t* argb, int width, const SkPMColor*) { 1.41 + const uint16_t* SK_RESTRICT src = (const uint16_t*)in; 1.42 + for (int i = 0; i < width; ++i) { 1.43 + const uint16_t c = *src++; 1.44 + argb[0] = 0xFF; 1.45 + argb[1] = SkPacked16ToR32(c); 1.46 + argb[2] = SkPacked16ToG32(c); 1.47 + argb[3] = SkPacked16ToB32(c); 1.48 + argb += 4; 1.49 + } 1.50 +} 1.51 + 1.52 +static void ARGB_4444_To_ARGB(const uint8_t* in, uint8_t* argb, int width, const SkPMColor*) { 1.53 + const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in; 1.54 + for (int i = 0; i < width; ++i) { 1.55 + const SkPMColor16 c = *src++; 1.56 + argb[0] = SkPacked4444ToA32(c); 1.57 + argb[1] = SkPacked4444ToR32(c); 1.58 + argb[2] = SkPacked4444ToG32(c); 1.59 + argb[3] = SkPacked4444ToB32(c); 1.60 + argb += 4; 1.61 + } 1.62 +} 1.63 + 1.64 +static void Index8_To_ARGB(const uint8_t* in, uint8_t* argb, int width, 1.65 + const SkPMColor* SK_RESTRICT ctable) { 1.66 + const uint8_t* SK_RESTRICT src = (const uint8_t*)in; 1.67 + for (int i = 0; i < width; ++i) { 1.68 + const uint32_t c = ctable[*src++]; 1.69 + argb[0] = SkGetPackedA32(c); 1.70 + argb[1] = SkGetPackedR32(c); 1.71 + argb[2] = SkGetPackedG32(c); 1.72 + argb[3] = SkGetPackedB32(c); 1.73 + argb += 4; 1.74 + } 1.75 +} 1.76 + 1.77 +static ScanlineImporter ChooseImporter(const SkBitmap::Config& config) { 1.78 + switch (config) { 1.79 + case SkBitmap::kARGB_8888_Config: 1.80 + return ARGB_8888_To_ARGB; 1.81 + case SkBitmap::kRGB_565_Config: 1.82 + return RGB_565_To_ARGB; 1.83 + case SkBitmap::kARGB_4444_Config: 1.84 + return ARGB_4444_To_ARGB; 1.85 + case SkBitmap::kIndex8_Config: 1.86 + return Index8_To_ARGB; 1.87 + default: 1.88 + return NULL; 1.89 + } 1.90 +} 1.91 + 1.92 +bool SkARGBImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bitmap, int) { 1.93 + const SkBitmap::Config config = bitmap.config(); 1.94 + const ScanlineImporter scanline_import = ChooseImporter(config); 1.95 + if (NULL == scanline_import) { 1.96 + return false; 1.97 + } 1.98 + 1.99 + SkAutoLockPixels alp(bitmap); 1.100 + const uint8_t* src = (uint8_t*)bitmap.getPixels(); 1.101 + if (NULL == bitmap.getPixels()) { 1.102 + return false; 1.103 + } 1.104 + 1.105 + SkAutoLockColors ctLocker; 1.106 + const SkPMColor* colors = ctLocker.lockColors(bitmap); 1.107 + 1.108 + const int argbStride = bitmap.width() * 4; 1.109 + SkAutoTDeleteArray<uint8_t> ada(new uint8_t[argbStride]); 1.110 + uint8_t* argb = ada.get(); 1.111 + for (int y = 0; y < bitmap.height(); ++y) { 1.112 + scanline_import(src + y * bitmap.rowBytes(), argb, bitmap.width(), colors); 1.113 + stream->write(argb, argbStride); 1.114 + } 1.115 + 1.116 + return true; 1.117 +} 1.118 + 1.119 + 1.120 +/////////////////////////////////////////////////////////////////////////////// 1.121 +DEFINE_ENCODER_CREATOR(ARGBImageEncoder); 1.122 +///////////////////////////////////////////////////////////////////////////////