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