Bug 908514 (Part 1) - Replace imagelib endian macros with MFBT's endian functions. r=jrmuizel

This commit is contained in:
Seth Fowler 2013-09-05 15:55:13 -07:00
parent 12df66267b
commit 428388057f
5 changed files with 75 additions and 112 deletions

View File

@ -10,7 +10,7 @@
#include <stdlib.h>
#include "ImageLogging.h"
#include "EndianMacros.h"
#include "mozilla/Endian.h"
#include "nsBMPDecoder.h"
#include "nsIInputStream.h"
@ -388,9 +388,9 @@ nsBMPDecoder::WriteInternal(const char* aBuffer, uint32_t aCount)
}
if (mPos == WIN_V3_HEADER_LENGTH + BITFIELD_LENGTH &&
mBIH.compression == BI_BITFIELDS) {
mBitFields.red = LITTLE_TO_NATIVE32(*(uint32_t*)mRawBuf);
mBitFields.green = LITTLE_TO_NATIVE32(*(uint32_t*)(mRawBuf + 4));
mBitFields.blue = LITTLE_TO_NATIVE32(*(uint32_t*)(mRawBuf + 8));
mBitFields.red = LittleEndian::readUint32(reinterpret_cast<uint32_t*>(mRawBuf));
mBitFields.green = LittleEndian::readUint32(reinterpret_cast<uint32_t*>(mRawBuf + 4));
mBitFields.blue = LittleEndian::readUint32(reinterpret_cast<uint32_t*>(mRawBuf + 8));
CalcBitShift();
}
while (aCount && (mPos < mBFH.dataoffset)) { // Skip whatever is between header and data
@ -448,7 +448,7 @@ nsBMPDecoder::WriteInternal(const char* aBuffer, uint32_t aCount)
break;
case 16:
while (lpos > 0) {
uint16_t val = LITTLE_TO_NATIVE16(*(uint16_t*)p);
uint16_t val = LittleEndian::readUint16(reinterpret_cast<uint16_t*>(p));
SetPixel(d,
(val & mBitFields.red) >> mBitFields.redRightShift << mBitFields.redLeftShift,
(val & mBitFields.green) >> mBitFields.greenRightShift << mBitFields.greenLeftShift,
@ -698,9 +698,9 @@ void nsBMPDecoder::ProcessFileHeader()
memcpy(&mBFH.bihsize, mRawBuf + 14, sizeof(mBFH.bihsize));
// Now correct the endianness of the header
mBFH.filesize = LITTLE_TO_NATIVE32(mBFH.filesize);
mBFH.dataoffset = LITTLE_TO_NATIVE32(mBFH.dataoffset);
mBFH.bihsize = LITTLE_TO_NATIVE32(mBFH.bihsize);
mBFH.filesize = LittleEndian::readUint32(&mBFH.filesize);
mBFH.dataoffset = LittleEndian::readUint32(&mBFH.dataoffset);
mBFH.bihsize = LittleEndian::readUint32(&mBFH.bihsize);
}
void nsBMPDecoder::ProcessInfoHeader()
@ -726,17 +726,17 @@ void nsBMPDecoder::ProcessInfoHeader()
}
// Convert endianness
mBIH.width = LITTLE_TO_NATIVE32(mBIH.width);
mBIH.height = LITTLE_TO_NATIVE32(mBIH.height);
mBIH.planes = LITTLE_TO_NATIVE16(mBIH.planes);
mBIH.bpp = LITTLE_TO_NATIVE16(mBIH.bpp);
mBIH.width = LittleEndian::readUint32(&mBIH.width);
mBIH.height = LittleEndian::readUint32(&mBIH.height);
mBIH.planes = LittleEndian::readUint16(&mBIH.planes);
mBIH.bpp = LittleEndian::readUint16(&mBIH.bpp);
mBIH.compression = LITTLE_TO_NATIVE32(mBIH.compression);
mBIH.image_size = LITTLE_TO_NATIVE32(mBIH.image_size);
mBIH.xppm = LITTLE_TO_NATIVE32(mBIH.xppm);
mBIH.yppm = LITTLE_TO_NATIVE32(mBIH.yppm);
mBIH.colors = LITTLE_TO_NATIVE32(mBIH.colors);
mBIH.important_colors = LITTLE_TO_NATIVE32(mBIH.important_colors);
mBIH.compression = LittleEndian::readUint32(&mBIH.compression);
mBIH.image_size = LittleEndian::readUint32(&mBIH.image_size);
mBIH.xppm = LittleEndian::readUint32(&mBIH.xppm);
mBIH.yppm = LittleEndian::readUint32(&mBIH.yppm);
mBIH.colors = LittleEndian::readUint32(&mBIH.colors);
mBIH.important_colors = LittleEndian::readUint32(&mBIH.important_colors);
}
} // namespace image

View File

@ -8,7 +8,7 @@
#include <stdlib.h>
#include "EndianMacros.h"
#include "mozilla/Endian.h"
#include "nsICODecoder.h"
#include "RasterImage.h"
@ -114,9 +114,9 @@ bool nsICODecoder::FillBitmapFileHeaderBuffer(int8_t *bfh)
GetRealHeight()) / 8;
}
fileSize = NATIVE32_TO_LITTLE(fileSize);
NativeEndian::swapToLittleEndianInPlace(&fileSize, 1);
memcpy(bfh + 2, &fileSize, sizeof(fileSize));
dataOffset = NATIVE32_TO_LITTLE(dataOffset);
NativeEndian::swapToLittleEndianInPlace(&dataOffset, 1);
memcpy(bfh + 10, &dataOffset, sizeof(dataOffset));
return true;
}
@ -130,7 +130,7 @@ nsICODecoder::FixBitmapHeight(int8_t *bih)
// Get the height from the BMP file information header
int32_t height;
memcpy(&height, bih + 8, sizeof(height));
height = LITTLE_TO_NATIVE32(height);
NativeEndian::swapFromLittleEndianInPlace(&height, 1);
// BMPs can be stored inverted by having a negative height
height = abs(height);
@ -151,7 +151,7 @@ nsICODecoder::FixBitmapHeight(int8_t *bih)
}
// Fix the BMP height in the BIH so that the BMP decoder can work properly
height = NATIVE32_TO_LITTLE(height);
NativeEndian::swapToLittleEndianInPlace(&height, 1);
memcpy(bih + 8, &height, sizeof(height));
return true;
}
@ -164,7 +164,7 @@ nsICODecoder::FixBitmapWidth(int8_t *bih)
// Get the width from the BMP file information header
int32_t width;
memcpy(&width, bih + 4, sizeof(width));
width = LITTLE_TO_NATIVE32(width);
NativeEndian::swapFromLittleEndianInPlace(&width, 1);
if (width > 256) {
return false;
}
@ -186,7 +186,7 @@ nsICODecoder::ExtractBPPFromBitmap(int8_t *bih)
{
int32_t bitsPerPixel;
memcpy(&bitsPerPixel, bih + 14, sizeof(bitsPerPixel));
bitsPerPixel = LITTLE_TO_NATIVE32(bitsPerPixel);
NativeEndian::swapFromLittleEndianInPlace(&bitsPerPixel, 1);
return bitsPerPixel;
}
@ -195,7 +195,7 @@ nsICODecoder::ExtractBIHSizeFromBitmap(int8_t *bih)
{
int32_t headerSize;
memcpy(&headerSize, bih, sizeof(headerSize));
headerSize = LITTLE_TO_NATIVE32(headerSize);
NativeEndian::swapFromLittleEndianInPlace(&headerSize, 1);
return headerSize;
}
@ -232,7 +232,7 @@ nsICODecoder::WriteInternal(const char* aBuffer, uint32_t aCount)
}
if (mPos == ICONCOUNTOFFSET && aCount >= 2) {
mNumIcons = LITTLE_TO_NATIVE16(((uint16_t*)aBuffer)[0]);
mNumIcons = LittleEndian::readUint16(reinterpret_cast<const uint16_t*>(aBuffer));
aBuffer += 2;
mPos += 2;
aCount -= 2;
@ -589,14 +589,14 @@ nsICODecoder::ProcessDirEntry(IconDirEntry& aTarget)
memcpy(&aTarget.mColorCount, mDirEntryArray + 2, sizeof(aTarget.mColorCount));
memcpy(&aTarget.mReserved, mDirEntryArray + 3, sizeof(aTarget.mReserved));
memcpy(&aTarget.mPlanes, mDirEntryArray + 4, sizeof(aTarget.mPlanes));
aTarget.mPlanes = LITTLE_TO_NATIVE16(aTarget.mPlanes);
aTarget.mPlanes = LittleEndian::readUint16(&aTarget.mPlanes);
memcpy(&aTarget.mBitCount, mDirEntryArray + 6, sizeof(aTarget.mBitCount));
aTarget.mBitCount = LITTLE_TO_NATIVE16(aTarget.mBitCount);
aTarget.mBitCount = LittleEndian::readUint16(&aTarget.mBitCount);
memcpy(&aTarget.mBytesInRes, mDirEntryArray + 8, sizeof(aTarget.mBytesInRes));
aTarget.mBytesInRes = LITTLE_TO_NATIVE32(aTarget.mBytesInRes);
aTarget.mBytesInRes = LittleEndian::readUint32(&aTarget.mBytesInRes);
memcpy(&aTarget.mImageOffset, mDirEntryArray + 12,
sizeof(aTarget.mImageOffset));
aTarget.mImageOffset = LITTLE_TO_NATIVE32(aTarget.mImageOffset);
aTarget.mImageOffset = LittleEndian::readUint32(&aTarget.mImageOffset);
}
bool

View File

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsCRT.h"
#include "EndianMacros.h"
#include "mozilla/Endian.h"
#include "nsBMPEncoder.h"
#include "prprf.h"
#include "nsString.h"
@ -550,22 +550,15 @@ nsBMPEncoder::InitInfoHeader(Version aVersion, uint32_t aBPP, uint32_t aWidth,
}
}
template<typename T>
static inline void
ConvertToLittle(T& value)
{
value = NATIVE32_TO_LITTLE(value);
}
// Encodes the BMP file header mBMPFileHeader
void
nsBMPEncoder::EncodeFileHeader()
{
mozilla::image::BMPFILEHEADER littleEndianBFH = mBMPFileHeader;
ConvertToLittle(littleEndianBFH.filesize);
ConvertToLittle(littleEndianBFH.reserved);
ConvertToLittle(littleEndianBFH.dataoffset);
ConvertToLittle(littleEndianBFH.bihsize);
NativeEndian::swapToLittleEndianInPlace(&littleEndianBFH.filesize, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianBFH.reserved, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianBFH.dataoffset, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianBFH.bihsize, 1);
ENCODE(&mImageBufferCurr, littleEndianBFH.signature);
ENCODE(&mImageBufferCurr, littleEndianBFH.filesize);
@ -579,36 +572,36 @@ void
nsBMPEncoder::EncodeInfoHeader()
{
mozilla::image::BITMAPV5HEADER littleEndianmBIH = mBMPInfoHeader;
ConvertToLittle(littleEndianmBIH.width);
ConvertToLittle(littleEndianmBIH.height);
ConvertToLittle(littleEndianmBIH.planes);
ConvertToLittle(littleEndianmBIH.bpp);
ConvertToLittle(littleEndianmBIH.compression);
ConvertToLittle(littleEndianmBIH.image_size);
ConvertToLittle(littleEndianmBIH.xppm);
ConvertToLittle(littleEndianmBIH.yppm);
ConvertToLittle(littleEndianmBIH.colors);
ConvertToLittle(littleEndianmBIH.important_colors);
ConvertToLittle(littleEndianmBIH.red_mask);
ConvertToLittle(littleEndianmBIH.green_mask);
ConvertToLittle(littleEndianmBIH.blue_mask);
ConvertToLittle(littleEndianmBIH.alpha_mask);
ConvertToLittle(littleEndianmBIH.color_space);
ConvertToLittle(littleEndianmBIH.white_point.r.x);
ConvertToLittle(littleEndianmBIH.white_point.r.y);
ConvertToLittle(littleEndianmBIH.white_point.r.z);
ConvertToLittle(littleEndianmBIH.white_point.g.x);
ConvertToLittle(littleEndianmBIH.white_point.g.y);
ConvertToLittle(littleEndianmBIH.white_point.g.z);
ConvertToLittle(littleEndianmBIH.white_point.b.x);
ConvertToLittle(littleEndianmBIH.white_point.b.y);
ConvertToLittle(littleEndianmBIH.white_point.b.z);
ConvertToLittle(littleEndianmBIH.gamma_red);
ConvertToLittle(littleEndianmBIH.gamma_green);
ConvertToLittle(littleEndianmBIH.gamma_blue);
ConvertToLittle(littleEndianmBIH.intent);
ConvertToLittle(littleEndianmBIH.profile_offset);
ConvertToLittle(littleEndianmBIH.profile_size);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.width, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.height, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.planes, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.bpp, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.compression, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.image_size, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.xppm, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.yppm, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.colors, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.important_colors, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.red_mask, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.green_mask, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.blue_mask, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.alpha_mask, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.color_space, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.white_point.r.x, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.white_point.r.y, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.white_point.r.z, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.white_point.g.x, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.white_point.g.y, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.white_point.g.z, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.white_point.b.x, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.white_point.b.y, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.white_point.b.z, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.gamma_red, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.gamma_green, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.gamma_blue, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.intent, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.profile_offset, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmBIH.profile_size, 1);
if (mBMPFileHeader.bihsize == OS2_BIH_LENGTH) {
uint16_t width = (uint16_t) littleEndianmBIH.width;

View File

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsCRT.h"
#include "EndianMacros.h"
#include "mozilla/Endian.h"
#include "nsBMPEncoder.h"
#include "nsPNGEncoder.h"
#include "nsICOEncoder.h"
@ -171,7 +171,7 @@ nsICOEncoder::AddImageFrame(const uint8_t* aData,
BMPImageBufferSize - BFH_LENGTH);
// We need to fix the BMP height to be *2 for the AND mask
uint32_t fixedHeight = GetRealHeight() * 2;
fixedHeight = NATIVE32_TO_LITTLE(fixedHeight);
NativeEndian::swapToLittleEndianInPlace(&fixedHeight, 1);
// The height is stored at an offset of 8 from the DIB header
memcpy(mImageBufferCurr + 8, &fixedHeight, sizeof(fixedHeight));
mImageBufferCurr += BMPImageBufferSize - BFH_LENGTH;
@ -476,9 +476,9 @@ void
nsICOEncoder::EncodeFileHeader()
{
IconFileHeader littleEndianIFH = mICOFileHeader;
littleEndianIFH.mReserved = NATIVE16_TO_LITTLE(littleEndianIFH.mReserved);
littleEndianIFH.mType = NATIVE16_TO_LITTLE(littleEndianIFH.mType);
littleEndianIFH.mCount = NATIVE16_TO_LITTLE(littleEndianIFH.mCount);
NativeEndian::swapToLittleEndianInPlace(&littleEndianIFH.mReserved, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianIFH.mType, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianIFH.mCount, 1);
memcpy(mImageBufferCurr, &littleEndianIFH.mReserved,
sizeof(littleEndianIFH.mReserved));
@ -497,12 +497,10 @@ nsICOEncoder::EncodeInfoHeader()
{
IconDirEntry littleEndianmIDE = mICODirEntry;
littleEndianmIDE.mPlanes = NATIVE16_TO_LITTLE(littleEndianmIDE.mPlanes);
littleEndianmIDE.mBitCount = NATIVE16_TO_LITTLE(littleEndianmIDE.mBitCount);
littleEndianmIDE.mBytesInRes =
NATIVE32_TO_LITTLE(littleEndianmIDE.mBytesInRes);
littleEndianmIDE.mImageOffset =
NATIVE32_TO_LITTLE(littleEndianmIDE.mImageOffset);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmIDE.mPlanes, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmIDE.mBitCount, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmIDE.mBytesInRes, 1);
NativeEndian::swapToLittleEndianInPlace(&littleEndianmIDE.mImageOffset, 1);
memcpy(mImageBufferCurr, &littleEndianmIDE.mWidth,
sizeof(littleEndianmIDE.mWidth));

View File

@ -1,28 +0,0 @@
/* 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/. */
#ifndef MOZILLA_IMAGELIB_ENDIAN_H_
#define MOZILLA_IMAGELIB_ENDIAN_H_
#if defined WORDS_BIGENDIAN || defined IS_BIG_ENDIAN || defined __BIG_ENDIAN__
// We must ensure that the entity is unsigned
// otherwise, if it is signed/negative, the MSB will be
// propagated when we shift
#define LITTLE_TO_NATIVE16(x) (((((uint16_t) x) & 0xFF) << 8) | \
(((uint16_t) x) >> 8))
#define LITTLE_TO_NATIVE32(x) (((((uint32_t) x) & 0xFF) << 24) | \
(((((uint32_t) x) >> 8) & 0xFF) << 16) | \
(((((uint32_t) x) >> 16) & 0xFF) << 8) | \
(((uint32_t) x) >> 24))
#define NATIVE32_TO_LITTLE LITTLE_TO_NATIVE32
#define NATIVE16_TO_LITTLE LITTLE_TO_NATIVE16
#else
#define LITTLE_TO_NATIVE16(x) x
#define LITTLE_TO_NATIVE32(x) x
#define NATIVE32_TO_LITTLE(x) x
#define NATIVE16_TO_LITTLE(x) x
#endif
#endif