Home | History | Annotate | Download | only in images
      1 /*
      2  * Copyright 2007, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef IMAGE_CODEC_BMPDECODERHELPER_H__
     18 #define IMAGE_CODEC_BMPDECODERHELPER_H__
     19 
     20 ///////////////////////////////////////////////////////////////////////////////
     21 // this section is my current "glue" between google3 code and android.
     22 // will be fixed soon
     23 
     24 #include "SkTypes.h"
     25 #include <limits.h>
     26 #define DISALLOW_EVIL_CONSTRUCTORS(name)
     27 #define CHECK(predicate)  SkASSERT(predicate)
     28 typedef uint8_t uint8;
     29 typedef uint32_t uint32;
     30 
     31 template <typename T> class scoped_array {
     32 private:
     33   T* ptr_;
     34   scoped_array(scoped_array const&);
     35   scoped_array& operator=(const scoped_array&);
     36 
     37 public:
     38   explicit scoped_array(T* p = 0) : ptr_(p) {}
     39   ~scoped_array() {
     40     delete[] ptr_;
     41   }
     42 
     43   void reset(T* p = 0) {
     44     if (p != ptr_) {
     45       delete[] ptr_;
     46       ptr_ = p;
     47     }
     48   }
     49 
     50   T& operator[](int i) const {
     51     return ptr_[i];
     52   }
     53 };
     54 
     55 ///////////////////////////////////////////////////////////////////////////////
     56 
     57 namespace image_codec {
     58 
     59 class BmpDecoderCallback {
     60  public:
     61   BmpDecoderCallback() { }
     62   virtual ~BmpDecoderCallback() {}
     63 
     64   /**
     65    * This is called once for an image. It is passed the width and height and
     66    * should return the address of a buffer that is large enough to store
     67    * all of the resulting pixels (widht * height * 3 bytes). If it returns NULL,
     68    * then the decoder will abort, but return true, as the caller has received
     69    * valid dimensions.
     70    */
     71   virtual uint8* SetSize(int width, int height) = 0;
     72 
     73  private:
     74   DISALLOW_EVIL_CONSTRUCTORS(BmpDecoderCallback);
     75 };
     76 
     77 class BmpDecoderHelper {
     78  public:
     79   BmpDecoderHelper() { }
     80   ~BmpDecoderHelper() { }
     81   bool DecodeImage(const char* data,
     82                    int len,
     83                    int max_pixels,
     84                    BmpDecoderCallback* callback);
     85 
     86  private:
     87   DISALLOW_EVIL_CONSTRUCTORS(BmpDecoderHelper);
     88 
     89   void DoRLEDecode();
     90   void DoStandardDecode();
     91   void PutPixel(int x, int y, uint8 col);
     92 
     93   int GetInt();
     94   int GetShort();
     95   uint8 GetByte();
     96   int CalcShiftRight(uint32 mask);
     97   int CalcShiftLeft(uint32 mask);
     98 
     99   const uint8* data_;
    100   int pos_;
    101   int len_;
    102   int width_;
    103   int height_;
    104   int bpp_;
    105   int pixelPad_;
    106   int rowPad_;
    107   scoped_array<uint8> colTab_;
    108   uint32 redBits_;
    109   uint32 greenBits_;
    110   uint32 blueBits_;
    111   int redShiftRight_;
    112   int greenShiftRight_;
    113   int blueShiftRight_;
    114   int redShiftLeft_;
    115   int greenShiftLeft_;
    116   int blueShiftLeft_;
    117   uint8* output_;
    118   bool inverted_;
    119 };
    120 
    121 } // namespace
    122 
    123 #endif
    124