epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2007 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef IMAGE_CODEC_BMPDECODERHELPER_H__ |
| 11 | #define IMAGE_CODEC_BMPDECODERHELPER_H__ |
| 12 | |
| 13 | /////////////////////////////////////////////////////////////////////////////// |
| 14 | // this section is my current "glue" between google3 code and android. |
| 15 | // will be fixed soon |
| 16 | |
| 17 | #include "SkTypes.h" |
| 18 | #include <limits.h> |
| 19 | #define DISALLOW_EVIL_CONSTRUCTORS(name) |
| 20 | #define CHECK(predicate) SkASSERT(predicate) |
| 21 | typedef uint8_t uint8; |
| 22 | typedef uint32_t uint32; |
| 23 | |
| 24 | template <typename T> class scoped_array { |
| 25 | private: |
| 26 | T* ptr_; |
| 27 | scoped_array(scoped_array const&); |
| 28 | scoped_array& operator=(const scoped_array&); |
| 29 | |
| 30 | public: |
| 31 | explicit scoped_array(T* p = 0) : ptr_(p) {} |
| 32 | ~scoped_array() { |
| 33 | delete[] ptr_; |
| 34 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 35 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | void reset(T* p = 0) { |
| 37 | if (p != ptr_) { |
| 38 | delete[] ptr_; |
| 39 | ptr_ = p; |
| 40 | } |
| 41 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 42 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | T& operator[](int i) const { |
| 44 | return ptr_[i]; |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | /////////////////////////////////////////////////////////////////////////////// |
| 49 | |
| 50 | namespace image_codec { |
| 51 | |
| 52 | class BmpDecoderCallback { |
| 53 | public: |
| 54 | BmpDecoderCallback() { } |
| 55 | virtual ~BmpDecoderCallback() {} |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 56 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | /** |
| 58 | * This is called once for an image. It is passed the width and height and |
| 59 | * should return the address of a buffer that is large enough to store |
| 60 | * all of the resulting pixels (widht * height * 3 bytes). If it returns NULL, |
| 61 | * then the decoder will abort, but return true, as the caller has received |
| 62 | * valid dimensions. |
| 63 | */ |
| 64 | virtual uint8* SetSize(int width, int height) = 0; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 65 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | private: |
| 67 | DISALLOW_EVIL_CONSTRUCTORS(BmpDecoderCallback); |
| 68 | }; |
| 69 | |
| 70 | class BmpDecoderHelper { |
| 71 | public: |
| 72 | BmpDecoderHelper() { } |
| 73 | ~BmpDecoderHelper() { } |
| 74 | bool DecodeImage(const char* data, |
| 75 | int len, |
| 76 | int max_pixels, |
| 77 | BmpDecoderCallback* callback); |
| 78 | |
| 79 | private: |
| 80 | DISALLOW_EVIL_CONSTRUCTORS(BmpDecoderHelper); |
| 81 | |
| 82 | void DoRLEDecode(); |
| 83 | void DoStandardDecode(); |
| 84 | void PutPixel(int x, int y, uint8 col); |
| 85 | |
| 86 | int GetInt(); |
| 87 | int GetShort(); |
| 88 | uint8 GetByte(); |
| 89 | int CalcShiftRight(uint32 mask); |
| 90 | int CalcShiftLeft(uint32 mask); |
| 91 | |
| 92 | const uint8* data_; |
| 93 | int pos_; |
| 94 | int len_; |
| 95 | int width_; |
| 96 | int height_; |
| 97 | int bpp_; |
| 98 | int pixelPad_; |
| 99 | int rowPad_; |
| 100 | scoped_array<uint8> colTab_; |
| 101 | uint32 redBits_; |
| 102 | uint32 greenBits_; |
| 103 | uint32 blueBits_; |
| 104 | int redShiftRight_; |
| 105 | int greenShiftRight_; |
| 106 | int blueShiftRight_; |
| 107 | int redShiftLeft_; |
| 108 | int greenShiftLeft_; |
| 109 | int blueShiftLeft_; |
| 110 | uint8* output_; |
| 111 | bool inverted_; |
| 112 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 113 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | } // namespace |
| 115 | |
| 116 | #endif |