bmpdecoderhelper.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #ifndef IMAGE_CODEC_BMPDECODERHELPER_H__
  17. #define IMAGE_CODEC_BMPDECODERHELPER_H__
  18. ///////////////////////////////////////////////////////////////////////////////
  19. // this section is my current "glue" between google3 code and android.
  20. // will be fixed soon
  21. ///////////////////////////////////////////////////////////////////////////////
  22. //#include "SkTypes.h"
  23. typedef unsigned char uint8_t;
  24. typedef unsigned short uint16_t;
  25. typedef unsigned int uint32_t;
  26. typedef uint16_t uint16;
  27. #ifndef NULL
  28. # define NULL ((void *)0)
  29. #endif
  30. #ifndef SkASSERT
  31. #include <assert.h>
  32. #define SkASSERT(cond) assert(cond)
  33. #endif
  34. ///////////////////////////////////////////////////////////////////////////////
  35. #include <limits.h>
  36. #define DISALLOW_EVIL_CONSTRUCTORS(name)
  37. #define CHECK(predicate) SkASSERT(predicate)
  38. typedef uint8_t uint8;
  39. typedef uint32_t uint32;
  40. template <typename T> class scoped_array {
  41. private:
  42. T* ptr_;
  43. scoped_array(scoped_array const&);
  44. scoped_array& operator=(const scoped_array&);
  45. public:
  46. explicit scoped_array(T* p = 0) : ptr_(p) {}
  47. ~scoped_array() {
  48. delete[] ptr_;
  49. }
  50. void reset(T* p = 0) {
  51. if (p != ptr_) {
  52. delete[] ptr_;
  53. ptr_ = p;
  54. }
  55. }
  56. T& operator[](int i) const {
  57. return ptr_[i];
  58. }
  59. };
  60. ///////////////////////////////////////////////////////////////////////////////
  61. namespace image_codec {
  62. class BmpDecoderCallback {
  63. public:
  64. BmpDecoderCallback() { }
  65. virtual ~BmpDecoderCallback() {}
  66. /**
  67. * This is called once for an image. It is passed the width and height and
  68. * should return the address of a buffer that is large enough to store
  69. * all of the resulting pixels (widht * height * 3 bytes). If it returns NULL,
  70. * then the decoder will abort, but return true, as the caller has received
  71. * valid dimensions.
  72. */
  73. virtual uint8* SetSize(int width, int height) = 0;
  74. private:
  75. DISALLOW_EVIL_CONSTRUCTORS(BmpDecoderCallback);
  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. private:
  86. DISALLOW_EVIL_CONSTRUCTORS(BmpDecoderHelper);
  87. void DoRLEDecode();
  88. void DoStandardDecode();
  89. void PutPixel(int x, int y, uint8 col);
  90. int GetInt();
  91. int GetShort();
  92. uint8 GetByte();
  93. int CalcShiftRight(uint32 mask);
  94. int CalcShiftLeft(uint32 mask);
  95. const uint8* data_;
  96. int pos_;
  97. int len_;
  98. int width_;
  99. int height_;
  100. int bpp_;
  101. int pixelPad_;
  102. int rowPad_;
  103. scoped_array<uint8> colTab_;
  104. uint32 redBits_;
  105. uint32 greenBits_;
  106. uint32 blueBits_;
  107. int redShiftRight_;
  108. int greenShiftRight_;
  109. int blueShiftRight_;
  110. int redShiftLeft_;
  111. int greenShiftLeft_;
  112. int blueShiftLeft_;
  113. uint8* output_;
  114. bool inverted_;
  115. };
  116. } // namespace
  117. #endif