@@ -23,34 +23,30 @@ namespace ultralight {
2323// /
2424// / The various Bitmap formats.
2525// /
26- enum UExport BitmapFormat {
27- /* *
28- * Alpha channel only, 8-bits per pixel.
29- *
30- * Encoding: 8-bits per channel, unsigned normalized.
31- *
32- * Color-space: Linear (no gamma), alpha-coverage only.
33- */
34- kBitmapFormat_A8_UNORM ,
35-
36- /* *
37- * Blue Green Red Alpha channels, 32-bits per pixel.
38- *
39- * Encoding: 8-bits per channel, unsigned normalized.
40- *
41- * Color-space: sRGB gamma with premultiplied linear alpha channel.
42- *
43- * NOTE: Alpha is premultiplied with BGR channels _before_ sRGB gamma is
44- * applied so we can use sRGB conversion hardware and perform all
45- * blending in linear space on GPU.
46- */
47- kBitmapFormat_BGRA8_UNORM_SRGB ,
26+ enum class UExport BitmapFormat : uint8_t {
27+ // /
28+ // / Alpha channel only, 8-bits per pixel.
29+ // /
30+ // / Encoding: 8-bits per channel, unsigned normalized.
31+ // /
32+ // / Color-space: Linear (no gamma), alpha-coverage only.
33+ // /
34+ A8_UNORM,
35+
36+ // /
37+ // / Blue Green Red Alpha channels, 32-bits per pixel.
38+ // /
39+ // / Encoding: 8-bits per channel, unsigned normalized.
40+ // /
41+ // / Color-space: sRGB gamma with premultiplied linear alpha channel.
42+ // /
43+ BGRA8_UNORM_SRGB,
4844};
4945
5046// /
5147// / Macro to get the bytes per pixel from a BitmapFormat
5248// /
53- #define GetBytesPerPixel (x ) (x == kBitmapFormat_A8_UNORM ? 1 : 4 )
49+ #define GetBytesPerPixel (x ) (x == BitmapFormat::A8_UNORM ? 1 : 4 )
5450
5551// /
5652// / @brief Bitmap container with basic blitting and conversion routines.
@@ -60,11 +56,10 @@ class UExport Bitmap : public RefCounted {
6056 // /
6157 // / Create an empty Bitmap. No pixels will be allocated.
6258 // /
63- static Ref <Bitmap> Create ();
59+ static RefPtr <Bitmap> Create ();
6460
6561 // /
66- // / Create a Bitmap with a certain configuration. Pixels will be allocated
67- // / but not initialized.
62+ // / Create a Bitmap with a certain configuration. Pixels will be allocated but not initialized.
6863 // /
6964 // / @param width The width in pixels.
7065 // /
@@ -74,8 +69,7 @@ class UExport Bitmap : public RefCounted {
7469 // /
7570 // / @return A ref-pointer to a new Bitmap instance.
7671 // /
77- static Ref<Bitmap> Create (uint32_t width, uint32_t height,
78- BitmapFormat format);
72+ static RefPtr<Bitmap> Create (uint32_t width, uint32_t height, BitmapFormat format);
7973
8074 // /
8175 // / Create a Bitmap with existing pixels and configuration.
@@ -86,37 +80,27 @@ class UExport Bitmap : public RefCounted {
8680 // /
8781 // / @param format The pixel format to use.
8882 // /
89- // / @param row_bytes The number of bytes between each row (note that this
90- // / value should be >= width * bytes_per_pixel).
83+ // / @param row_bytes The number of bytes between each row (note that this value should be >=
84+ // / width * bytes_per_pixel).
9185 // /
9286 // / @param pixels Pointer to raw pixel buffer.
9387 // /
9488 // / @param size Size of the raw pixel buffer.
9589 // /
96- // / @param should_copy Whether or not a copy should be made of the pixels.
97- // / If this is false, the returned Bitmap will use the
98- // / raw pixels passed in as its own, but you are still
99- // / responsible for destroying your buffer afterwards.
100- // /
101- // / @param fixup_gamma Whether or not we should reinterpret the source
102- // / as an sRGB bitmap with premultiplied alpha applied
103- // / after the gamma function (typical of PNGs). We
104- // / expect all premultiplication to be applied before
105- // / the gamma function so we can blend properly in
106- // / linear space. Only valid for
107- // / kBitmapFormat_BGRA8_UNORM_SRGB.
90+ // / @param should_copy Whether or not a copy should be made of the pixels. If this is false
91+ // / the returned Bitmap will use the raw pixels passed in as its own, but
92+ // / you are still responsible for destroying your buffer afterwards.
10893 // /
10994 // / @return A ref-pointer to a new Bitmap instance.
11095 // /
111- static Ref<Bitmap> Create (uint32_t width, uint32_t height,
112- BitmapFormat format, uint32_t row_bytes,
113- const void * pixels, size_t size,
114- bool should_copy = true , bool fixup_gamma = false );
96+ static RefPtr<Bitmap> Create (uint32_t width, uint32_t height, BitmapFormat format,
97+ uint32_t row_bytes, const void * pixels, size_t size,
98+ bool should_copy = true );
11599
116100 // /
117101 // / Create a bitmap from a deep copy of another Bitmap.
118102 // /
119- static Ref <Bitmap> Create (const Bitmap& bitmap);
103+ static RefPtr <Bitmap> Create (const Bitmap& bitmap);
120104
121105 // /
122106 // / Get the width in pixels.
@@ -146,8 +130,8 @@ class UExport Bitmap : public RefCounted {
146130 // /
147131 // / Get the number of bytes between each row of pixels.
148132 // /
149- // / @note This value is usually calculated as width * bytes_per_pixel (bpp)
150- // / but it may be larger due to alignment rules in the allocator.
133+ // / @note This value is usually calculated as width * bytes_per_pixel (bpp) but it may be larger
134+ // / due to alignment rules in the allocator.
151135 // /
152136 virtual uint32_t row_bytes () const = 0;
153137
@@ -159,13 +143,13 @@ class UExport Bitmap : public RefCounted {
159143 virtual size_t size () const = 0;
160144
161145 // /
162- // / Whether or not this Bitmap owns the pixel buffer and will destroy it
163- // / at the end of its lifetime.
146+ // / Whether or not this Bitmap owns the pixel buffer and will destroy it at the end of its
147+ // / lifetime.
164148 // /
165149 virtual bool owns_pixels () const = 0;
166150
167151 // /
168- // / Lock the pixel buffer for reading/writing.
152+ // / Lock the pixel buffer for reading/writing.
169153 // /
170154 // / @return A pointer to the pixel buffer.
171155 // /
@@ -210,64 +194,83 @@ class UExport Bitmap : public RefCounted {
210194 // /
211195 // / @param bitmap The bitmap to copy from.
212196 // /
213- virtual void Set (Ref <Bitmap> bitmap) = 0;
197+ virtual void Set (RefPtr <Bitmap> bitmap) = 0;
214198
215199 // /
216200 // / Draw another bitmap to this bitmap.
217201 // /
218- // / @note Formats do not need to match. Bitmap formats will be converted
219- // / to one another automatically. Note that when converting from
220- // / BGRA8 to A8, only the Blue channel will be used.
202+ // / @note Formats do not need to match. Bitmap formats will be converted to one another
203+ // / automatically. Note that when converting from BGRA8 to A8, only the Blue channel will
204+ // / be used.
221205 // /
222206 // / @param src_rect The source rectangle, relative to src bitmap.
223207 // /
224208 // / @param dest_rect The destination rectangle, relative to this bitmap.
225- // /
209+ // /
226210 // / @param src The source bitmap.
227- // /
228- // / @param pad_repeat Whether or not we should pad the drawn bitmap by one
229- // / pixel of repeated edge pixels from the source bitmap.
230211 // /
231- // / @return Whether or not the operation succeeded (this can fail if the
232- // / src_rect and/or dest_rect are invalid, or if their total
233- // / dimensions do not match).
212+ // / @param pad_repeat Whether or not we should pad the drawn bitmap by one pixel of repeated
213+ // / edge pixels from the source bitmap.
214+ // /
215+ // / @return Whether or not the operation succeeded (this can fail if the src_rect and/or
216+ // / dest_rect are invalid, or if their total dimensions do not match).
234217 // /
235- virtual bool DrawBitmap (IntRect src_rect, IntRect dest_rect,
236- Ref<Bitmap> src, bool pad_repeat) = 0;
218+ virtual bool DrawBitmap (IntRect src_rect, IntRect dest_rect, RefPtr<Bitmap> src, bool pad_repeat)
219+ = 0;
237220
238221 // /
239- // / Write this Bitmap out to a PNG image. (mainly used for Debug)
222+ // / Write this Bitmap out to a PNG image.
240223 // /
241224 // / @param path The filepath to write to (opened with fopen())
242225 // /
226+ // / @param convert_to_rgba The PNG format expects RGBA format but our bitmap is stored as BGRA,
227+ // / set this to true to perform the conversion automatically.
228+ // /
229+ // / @param convert_to_straight_alpha The PNG format expects semi-transparent values to be
230+ // / stored as straight alpha instead of premultiplied alpha,
231+ // / set this to true to perform the conversion automatically.
232+ // /
243233 // / @return Whether or not the operation succeeded.
244234 // /
245- virtual bool WritePNG (const char * path) = 0;
235+ virtual bool WritePNG (const char * path, bool convert_to_rgba = true ,
236+ bool convert_to_straight_alpha = true ) const = 0;
246237
247238 // /
248- // / Make a resized copy of this bitmap by writing to a pre-allocated
249- // / destination bitmap.
239+ // / Make a resized copy of this bitmap by writing to a pre-allocated destination bitmap.
250240 // /
251- // / @param destination The bitmap to store the result in, the width and
252- // / height of the destination will be used.
241+ // / @param destination The bitmap to store the result in, the width and height of the
242+ // / destination will be used.
253243 // /
254- // / @param high_quality Whether or not a high quality resampling will be
255- // / used during the resize. (Otherwise, just uses fast
256- // / nearest-neighbor sampling)
244+ // / @param high_quality Whether or not a high quality resampling will be used during the
245+ // / resize. (Otherwise, just uses fast nearest-neighbor sampling)
257246 // /
258- // / @return Whether or not the operation succeeded. This operation is only
259- // / valid if both formats are kBitmapFormat_BGRA8_UNORM_SRGB and
260- // / both the source and destination are non-empty.
247+ // / @return Whether or not the operation succeeded. This operation is only valid if both formats
248+ // / are BitmapFormat::BGRA8_UNORM_SRGB and the source and destination are non-empty.
261249 // /
262- virtual bool Resample (Ref <Bitmap> destination, bool high_quality) = 0;
250+ virtual bool Resample (RefPtr <Bitmap> destination, bool high_quality) = 0;
263251
264252 // /
265- // / This converts a BGRA bitmap to RGBA bitmap and vice-versa by swapping
266- // / the red and blue channels.
253+ // / Convert a BGRA bitmap to RGBA bitmap and vice-versa by swapping the red and blue channels.
254+ // /
255+ // / @note Only valid if the format is BitmapFormat::BGRA8_UNORM_SRGB
267256 // /
268257 virtual void SwapRedBlueChannels () = 0;
269258
270- protected:
259+ // /
260+ // / Convert a BGRA bitmap from premultiplied alpha (the default) to straight alpha.
261+ // /
262+ // / @note Only valid if the format is BitmapFormat::BGRA8_UNORM_SRGB
263+ // /
264+ virtual void ConvertToStraightAlpha () = 0;
265+
266+ // /
267+ // / Convert a BGRA bitmap from straight alpha to premultiplied alpha.
268+ // /
269+ // / @note Only valid if the format is BitmapFormat::BGRA8_UNORM_SRGB
270+ // /
271+ virtual void ConvertToPremultipliedAlpha () = 0;
272+
273+ protected:
271274 Bitmap ();
272275 virtual ~Bitmap ();
273276 Bitmap (const Bitmap&);
@@ -276,4 +279,4 @@ class UExport Bitmap : public RefCounted {
276279
277280#pragma pack(pop)
278281
279- } // namespace ultralight
282+ } // namespace ultralight
0 commit comments