tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsBMPEncoder.h (5496B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef mozilla_image_encoders_bmp_nsBMPEncoder_h
      7 #define mozilla_image_encoders_bmp_nsBMPEncoder_h
      8 
      9 #include "mozilla/ReentrantMonitor.h"
     10 #include "mozilla/UniquePtr.h"
     11 
     12 #include "imgIEncoder.h"
     13 
     14 #include "nsCOMPtr.h"
     15 
     16 #define NS_BMPENCODER_CID                     \
     17  {/* 13a5320c-4c91-4FA4-bd16-b081a3ba8c0b */ \
     18   0x13a5320c,                                \
     19   0x4c91,                                    \
     20   0x4fa4,                                    \
     21   {0xbd, 0x16, 0xb0, 0x81, 0xa3, 0Xba, 0x8c, 0x0b}}
     22 
     23 namespace mozilla {
     24 namespace image {
     25 namespace bmp {
     26 
     27 struct FileHeader {
     28  char signature[2];    // String "BM".
     29  uint32_t filesize;    // File size.
     30  int32_t reserved;     // Zero.
     31  uint32_t dataoffset;  // Offset to raster data.
     32 };
     33 
     34 struct XYZ {
     35  int32_t x, y, z;
     36 };
     37 
     38 struct XYZTriple {
     39  XYZ r, g, b;
     40 };
     41 
     42 struct V5InfoHeader {
     43  uint32_t bihsize;           // Header size
     44  int32_t width;              // Uint16 in OS/2 BMPs
     45  int32_t height;             // Uint16 in OS/2 BMPs
     46  uint16_t planes;            // =1
     47  uint16_t bpp;               // Bits per pixel.
     48  uint32_t compression;       // See Compression for valid values
     49  uint32_t image_size;        // (compressed) image size. Can be 0 if
     50                              // compression==0
     51  uint32_t xppm;              // Pixels per meter, horizontal
     52  uint32_t yppm;              // Pixels per meter, vertical
     53  uint32_t colors;            // Used Colors
     54  uint32_t important_colors;  // Number of important colors. 0=all
     55  // The rest of the header is not available in WIN_V3 BMP Files
     56  uint32_t red_mask;     // Bits used for red component
     57  uint32_t green_mask;   // Bits used for green component
     58  uint32_t blue_mask;    // Bits used for blue component
     59  uint32_t alpha_mask;   // Bits used for alpha component
     60  uint32_t color_space;  // 0x73524742=LCS_sRGB ...
     61  // These members are unused unless color_space == LCS_CALIBRATED_RGB
     62  XYZTriple white_point;  // Logical white point
     63  uint32_t gamma_red;     // Red gamma component
     64  uint32_t gamma_green;   // Green gamma component
     65  uint32_t gamma_blue;    // Blue gamma component
     66  uint32_t intent;        // Rendering intent
     67  // These members are unused unless color_space == LCS_PROFILE_*
     68  uint32_t profile_offset;  // Offset to profile data in bytes
     69  uint32_t profile_size;    // Size of profile data in bytes
     70  uint32_t reserved;        // =0
     71 
     72  static const uint32_t COLOR_SPACE_LCS_SRGB = 0x73524742;
     73 };
     74 
     75 }  // namespace bmp
     76 }  // namespace image
     77 }  // namespace mozilla
     78 
     79 // Provides BMP encoding functionality. Use InitFromData() to do the
     80 // encoding. See that function definition for encoding options.
     81 
     82 class nsBMPEncoder final : public imgIEncoder {
     83  typedef mozilla::ReentrantMonitor ReentrantMonitor;
     84 
     85 public:
     86  NS_DECL_THREADSAFE_ISUPPORTS
     87  NS_DECL_IMGIENCODER
     88  NS_DECL_NSIINPUTSTREAM
     89  NS_DECL_NSIASYNCINPUTSTREAM
     90 
     91  nsBMPEncoder();
     92 
     93 protected:
     94  ~nsBMPEncoder();
     95 
     96  enum Version { VERSION_3 = 3, VERSION_5 = 5 };
     97 
     98  // See InitData in the cpp for valid parse options
     99  nsresult ParseOptions(const nsAString& aOptions, Version& aVersionOut,
    100                        uint16_t& aBppOut);
    101  // Obtains data with no alpha in machine-independent byte order
    102  void ConvertHostARGBRow(const uint8_t* aSrc,
    103                          const mozilla::UniquePtr<uint8_t[]>& aDest,
    104                          uint32_t aPixelWidth);
    105  // Thread safe notify listener
    106  void NotifyListener();
    107 
    108  // Initializes the bitmap file header member mBMPFileHeader
    109  nsresult InitFileHeader(Version aVersion, uint16_t aBPP, uint32_t aWidth,
    110                          uint32_t aHeight);
    111  // Initializes the bitmap info header member mBMPInfoHeader
    112  nsresult InitInfoHeader(Version aVersion, uint16_t aBPP, uint32_t aWidth,
    113                          uint32_t aHeight);
    114 
    115  // Encodes the bitmap file header member mBMPFileHeader
    116  void EncodeFileHeader();
    117  // Encodes the bitmap info header member mBMPInfoHeader
    118  void EncodeInfoHeader();
    119  // Encodes a row of image data which does not have alpha data
    120  void EncodeImageDataRow24(const uint8_t* aData);
    121  // Encodes a row of image data which does have alpha data
    122  void EncodeImageDataRow32(const uint8_t* aData);
    123  // Obtains the current offset filled up to for the image buffer
    124  inline int32_t GetCurrentImageBufferOffset() {
    125    return static_cast<int32_t>(mImageBufferCurr - mImageBufferStart);
    126  }
    127 
    128  // These headers will always contain endian independent stuff
    129  // They store the BMP headers which will be encoded
    130  mozilla::image::bmp::FileHeader mBMPFileHeader;
    131  mozilla::image::bmp::V5InfoHeader mBMPInfoHeader;
    132 
    133  // Keeps track of the start of the image buffer
    134  uint8_t* mImageBufferStart;
    135  // Keeps track of the current position in the image buffer
    136  uint8_t* mImageBufferCurr;
    137  // Keeps track of the image buffer size
    138  uint32_t mImageBufferSize;
    139  // Keeps track of the number of bytes in the image buffer which are read
    140  uint32_t mImageBufferReadPoint;
    141  // Stores true if the image is done being encoded
    142  bool mFinished;
    143 
    144  nsCOMPtr<nsIInputStreamCallback> mCallback;
    145  nsCOMPtr<nsIEventTarget> mCallbackTarget;
    146  uint32_t mNotifyThreshold;
    147 };
    148 
    149 #endif  // mozilla_image_encoders_bmp_nsBMPEncoder_h