2015-10-17 14:01:31 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-08-26 00:09:05 +04:00
|
|
|
|
2015-04-07 05:19:00 +03:00
|
|
|
#ifndef mozilla_image_encoders_bmp_nsBMPEncoder_h
|
|
|
|
#define mozilla_image_encoders_bmp_nsBMPEncoder_h
|
|
|
|
|
2012-01-03 00:23:41 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2015-10-17 14:01:31 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2012-01-03 00:23:41 +04:00
|
|
|
|
2011-08-26 00:09:05 +04:00
|
|
|
#include "imgIEncoder.h"
|
2015-10-16 01:43:25 +03:00
|
|
|
#include "BMPHeaders.h"
|
2011-08-26 00:09:05 +04:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
|
|
|
#define NS_BMPENCODER_CID \
|
|
|
|
{ /* 13a5320c-4c91-4FA4-bd16-b081a3ba8c0b */ \
|
|
|
|
0x13a5320c, \
|
|
|
|
0x4c91, \
|
|
|
|
0x4fa4, \
|
|
|
|
{0xbd, 0x16, 0xb0, 0x81, 0xa3, 0Xba, 0x8c, 0x0b} \
|
|
|
|
}
|
|
|
|
|
2015-10-16 01:43:25 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
namespace bmp {
|
|
|
|
|
|
|
|
struct FileHeader {
|
|
|
|
char signature[2]; // String "BM".
|
|
|
|
uint32_t filesize; // File size.
|
|
|
|
int32_t reserved; // Zero.
|
|
|
|
uint32_t dataoffset; // Offset to raster data.
|
|
|
|
};
|
|
|
|
|
|
|
|
struct XYZ {
|
|
|
|
int32_t x, y, z;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct XYZTriple {
|
|
|
|
XYZ r, g, b;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct V5InfoHeader {
|
|
|
|
uint32_t bihsize; // Header size
|
|
|
|
int32_t width; // Uint16 in OS/2 BMPs
|
|
|
|
int32_t height; // Uint16 in OS/2 BMPs
|
|
|
|
uint16_t planes; // =1
|
|
|
|
uint16_t bpp; // Bits per pixel.
|
|
|
|
uint32_t compression; // See Compression for valid values
|
|
|
|
uint32_t image_size; // (compressed) image size. Can be 0 if
|
|
|
|
// compression==0
|
|
|
|
uint32_t xppm; // Pixels per meter, horizontal
|
|
|
|
uint32_t yppm; // Pixels per meter, vertical
|
|
|
|
uint32_t colors; // Used Colors
|
|
|
|
uint32_t important_colors; // Number of important colors. 0=all
|
|
|
|
// The rest of the header is not available in WIN_V3 BMP Files
|
|
|
|
uint32_t red_mask; // Bits used for red component
|
|
|
|
uint32_t green_mask; // Bits used for green component
|
|
|
|
uint32_t blue_mask; // Bits used for blue component
|
|
|
|
uint32_t alpha_mask; // Bits used for alpha component
|
|
|
|
uint32_t color_space; // 0x73524742=LCS_sRGB ...
|
|
|
|
// These members are unused unless color_space == LCS_CALIBRATED_RGB
|
|
|
|
XYZTriple white_point; // Logical white point
|
|
|
|
uint32_t gamma_red; // Red gamma component
|
|
|
|
uint32_t gamma_green; // Green gamma component
|
|
|
|
uint32_t gamma_blue; // Blue gamma component
|
|
|
|
uint32_t intent; // Rendering intent
|
|
|
|
// These members are unused unless color_space == LCS_PROFILE_*
|
|
|
|
uint32_t profile_offset; // Offset to profile data in bytes
|
|
|
|
uint32_t profile_size; // Size of profile data in bytes
|
|
|
|
uint32_t reserved; // =0
|
|
|
|
|
|
|
|
static const uint32_t COLOR_SPACE_LCS_SRGB = 0x73524742;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace bmp
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2011-08-26 00:09:05 +04:00
|
|
|
// Provides BMP encoding functionality. Use InitFromData() to do the
|
|
|
|
// encoding. See that function definition for encoding options.
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsBMPEncoder final : public imgIEncoder
|
2011-08-26 00:09:05 +04:00
|
|
|
{
|
|
|
|
typedef mozilla::ReentrantMonitor ReentrantMonitor;
|
|
|
|
public:
|
2013-07-19 06:23:31 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2011-08-26 00:09:05 +04:00
|
|
|
NS_DECL_IMGIENCODER
|
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
NS_DECL_NSIASYNCINPUTSTREAM
|
|
|
|
|
|
|
|
nsBMPEncoder();
|
|
|
|
|
|
|
|
protected:
|
2014-06-23 22:49:08 +04:00
|
|
|
~nsBMPEncoder();
|
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
enum Version
|
|
|
|
{
|
2012-07-17 18:20:14 +04:00
|
|
|
VERSION_3 = 3,
|
|
|
|
VERSION_5 = 5
|
|
|
|
};
|
|
|
|
|
2011-08-26 00:09:05 +04:00
|
|
|
// See InitData in the cpp for valid parse options
|
2016-06-01 23:43:41 +03:00
|
|
|
nsresult ParseOptions(const nsAString& aOptions, Version& aVersionOut,
|
|
|
|
uint32_t& aBppOut);
|
2011-08-26 00:09:05 +04:00
|
|
|
// Obtains data with no alpha in machine-independent byte order
|
2015-10-17 14:01:31 +03:00
|
|
|
void ConvertHostARGBRow(const uint8_t* aSrc,
|
|
|
|
const mozilla::UniquePtr<uint8_t[]>& aDest,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aPixelWidth);
|
2011-08-26 00:09:05 +04:00
|
|
|
// Thread safe notify listener
|
|
|
|
void NotifyListener();
|
|
|
|
|
|
|
|
// Initializes the bitmap file header member mBMPFileHeader
|
2012-08-22 19:56:38 +04:00
|
|
|
void InitFileHeader(Version aVersion, uint32_t aBPP, uint32_t aWidth,
|
|
|
|
uint32_t aHeight);
|
2011-08-26 00:09:05 +04:00
|
|
|
// Initializes the bitmap info header member mBMPInfoHeader
|
2012-08-22 19:56:38 +04:00
|
|
|
void InitInfoHeader(Version aVersion, uint32_t aBPP, uint32_t aWidth,
|
|
|
|
uint32_t aHeight);
|
2012-07-17 18:20:14 +04:00
|
|
|
|
2011-08-26 00:09:05 +04:00
|
|
|
// Encodes the bitmap file header member mBMPFileHeader
|
|
|
|
void EncodeFileHeader();
|
|
|
|
// Encodes the bitmap info header member mBMPInfoHeader
|
|
|
|
void EncodeInfoHeader();
|
|
|
|
// Encodes a row of image data which does not have alpha data
|
2012-08-22 19:56:38 +04:00
|
|
|
void EncodeImageDataRow24(const uint8_t* aData);
|
2011-08-26 00:09:05 +04:00
|
|
|
// Encodes a row of image data which does have alpha data
|
2012-08-22 19:56:38 +04:00
|
|
|
void EncodeImageDataRow32(const uint8_t* aData);
|
2011-08-26 00:09:05 +04:00
|
|
|
// Obtains the current offset filled up to for the image buffer
|
2012-08-22 19:56:38 +04:00
|
|
|
inline int32_t GetCurrentImageBufferOffset()
|
2011-08-26 00:09:05 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
return static_cast<int32_t>(mImageBufferCurr - mImageBufferStart);
|
2011-08-26 00:09:05 +04:00
|
|
|
}
|
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
// These headers will always contain endian independent stuff
|
2011-08-26 00:09:05 +04:00
|
|
|
// They store the BMP headers which will be encoded
|
Bug 1204394 (part 1) - Using StreamingLexer in the BMP decoder. r=seth.
This patch is a major overhaul of nsBMPDecoder.
The patch improves the code in the following ways.
- It converts nsBMPDecoder to use StreamingLexer, which makes it much easier to
read.
- It adds a detailed comment about the BMP format at the top of
nsBMPDecoder.cpp.
- It fixes lots of inconsistent indenting.
- It moves |bihsize| from |mBFH| to |mBIH| to match the file format and common
sense. The avoids the need for the confusing LENGTH/INTERNAL_LENGTH
distinction.
- It renames most of the types in BMPFileHeader.h, so they have better names,
in StudlyCaps form, and within the new |bmp| namespace.
- It removes the BMP_HEADER_LENGTH struct and inlines its values directly into
the two places they were used.
- It removes the MOZ_LOG logging done on some of the failure cases. (Most
failure cases lacked logging so why bother with some?)
- It removes over 200 lines of code, despite the addition of the big format
comment.
The patch changes the way BMPs are decoded as follows.
- It adds stricter testing of the InfoHeader length, rejecting files with bad
values.
- It moves all header sanity checking that can lead to file rejection into the
metadata decode phase. (Previously, bpp/compression consistency checking did
not occur during a metadata decode.)
- It removes BMPINFOHEADER::ALPHABITFIELDS, which was (a) a weird WinCE-only
thing, and (b) we didn't actually allow it, and (c) we used the value 4
instead of 6(!).
- It rejects the previously-accepted compression==RLE4 && bpp=1 combination
because it doesn't make sense.
- It removes a fudge in RLE absolute mode handling that permitted one pixel too
many in a row but only if the row's width was odd(!)
- It now rejects a file with a negative gap between the color table and the
pixel data.
The patch leaves the following problems unaddressed.
- If bpp==32 we totally ignore compression==BITFIELDS and treat it like
compression=RGB.
- Transparency as specified in WinBMPv{4,5} isn't handled at all.
These will be fixed in follow-ups.
All these changes affect (for the better) the results of the following tests
that will be added in part 2:
- g/pal8v4.bmp
- g/pal8v5.bmp
- q/pal8os2sp.bmp
- q/pal8os2v2.bmp
- q/pal8os2v2-16.bmp
- b/badheadersize.bmp
- b/badpalettesize.bmp
- b/badrle.bmp
--HG--
extra : rebase_source : 8ddc2f5fccce6998348097ff9f0a1072d273cdf4
2015-10-09 08:47:56 +03:00
|
|
|
mozilla::image::bmp::FileHeader mBMPFileHeader;
|
|
|
|
mozilla::image::bmp::V5InfoHeader mBMPInfoHeader;
|
2011-08-26 00:09:05 +04:00
|
|
|
|
|
|
|
// Keeps track of the start of the image buffer
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t* mImageBufferStart;
|
2011-08-26 00:09:05 +04:00
|
|
|
// Keeps track of the current position in the image buffer
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t* mImageBufferCurr;
|
2011-08-26 00:09:05 +04:00
|
|
|
// Keeps track of the image buffer size
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mImageBufferSize;
|
2011-08-26 00:09:05 +04:00
|
|
|
// Keeps track of the number of bytes in the image buffer which are read
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mImageBufferReadPoint;
|
2011-10-17 18:59:28 +04:00
|
|
|
// Stores true if the image is done being encoded
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mFinished;
|
2011-08-26 00:09:05 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStreamCallback> mCallback;
|
|
|
|
nsCOMPtr<nsIEventTarget> mCallbackTarget;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mNotifyThreshold;
|
2011-08-26 00:09:05 +04:00
|
|
|
};
|
2015-04-07 05:19:00 +03:00
|
|
|
|
|
|
|
#endif // mozilla_image_encoders_bmp_nsBMPEncoder_h
|