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