2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00: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/. */
|
2001-01-22 21:53:32 +00:00
|
|
|
|
|
|
|
|
2008-10-20 15:21:02 +00:00
|
|
|
#include "nsITransferable.h"
|
2001-01-22 21:53:32 +00:00
|
|
|
#include "nsImageClipboard.h"
|
|
|
|
#include "nsGfxCIID.h"
|
2006-09-13 19:52:44 +00:00
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "imgIEncoder.h"
|
|
|
|
#include "nsLiteralString.h"
|
2011-04-17 01:22:41 +00:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2001-01-22 21:53:32 +00:00
|
|
|
|
2012-07-31 12:11:55 +00:00
|
|
|
#define BFH_LENGTH 14
|
|
|
|
|
2001-01-22 21:53:32 +00:00
|
|
|
/* Things To Do 11/8/00
|
|
|
|
|
|
|
|
Check image metrics, can we support them? Do we need to?
|
|
|
|
Any other render format? HTML?
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsImageToClipboard ctor
|
|
|
|
//
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 01:50:15 +00:00
|
|
|
// Given an imgIContainer, convert it to a DIB that is ready to go on the win32 clipboard
|
2001-01-22 21:53:32 +00:00
|
|
|
//
|
2012-09-07 02:48:16 +00:00
|
|
|
nsImageToClipboard::nsImageToClipboard(imgIContainer* aInImage, bool aWantDIBV5)
|
|
|
|
: mImage(aInImage)
|
|
|
|
, mWantDIBV5(aWantDIBV5)
|
2001-01-22 21:53:32 +00:00
|
|
|
{
|
|
|
|
// nothing to do here
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsImageToClipboard dtor
|
|
|
|
//
|
|
|
|
// Clean up after ourselves. We know that we have created the bitmap
|
|
|
|
// successfully if we still have a pointer to the header.
|
|
|
|
//
|
|
|
|
nsImageToClipboard::~nsImageToClipboard()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// GetPicture
|
|
|
|
//
|
|
|
|
// Call to get the actual bits that go on the clipboard. If an error
|
|
|
|
// ocurred during conversion, |outBits| will be null.
|
|
|
|
//
|
2001-03-27 06:02:54 +00:00
|
|
|
// NOTE: The caller owns the handle and must delete it with ::GlobalRelease()
|
2001-01-22 21:53:32 +00:00
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsImageToClipboard :: GetPicture ( HANDLE* outBits )
|
|
|
|
{
|
|
|
|
NS_ASSERTION ( outBits, "Bad parameter" );
|
|
|
|
|
2001-03-27 06:02:54 +00:00
|
|
|
return CreateFromImage ( mImage, outBits );
|
2001-01-22 21:53:32 +00:00
|
|
|
|
|
|
|
} // GetPicture
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// CalcSize
|
|
|
|
//
|
|
|
|
// Computes # of bytes needed by a bitmap with the specified attributes.
|
|
|
|
//
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t
|
|
|
|
nsImageToClipboard :: CalcSize ( int32_t aHeight, int32_t aColors, WORD aBitsPerPixel, int32_t aSpanBytes )
|
2001-01-22 21:53:32 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t HeaderMem = sizeof(BITMAPINFOHEADER);
|
2001-01-22 21:53:32 +00:00
|
|
|
|
|
|
|
// add size of pallette to header size
|
|
|
|
if (aBitsPerPixel < 16)
|
|
|
|
HeaderMem += aColors * sizeof(RGBQUAD);
|
|
|
|
|
|
|
|
if (aHeight < 0)
|
|
|
|
aHeight = -aHeight;
|
|
|
|
|
|
|
|
return (HeaderMem + (aHeight * aSpanBytes));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// CalcSpanLength
|
|
|
|
//
|
|
|
|
// Computes the span bytes for determining the overall size of the image
|
|
|
|
//
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t
|
|
|
|
nsImageToClipboard::CalcSpanLength(uint32_t aWidth, uint32_t aBitCount)
|
2001-01-22 21:53:32 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t spanBytes = (aWidth * aBitCount) >> 5;
|
2001-01-22 21:53:32 +00:00
|
|
|
|
|
|
|
if ((aWidth * aBitCount) & 0x1F)
|
|
|
|
spanBytes++;
|
|
|
|
spanBytes <<= 2;
|
|
|
|
|
|
|
|
return spanBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// CreateFromImage
|
|
|
|
//
|
|
|
|
// Do the work to setup the bitmap header and copy the bits out of the
|
|
|
|
// image.
|
|
|
|
//
|
|
|
|
nsresult
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 01:50:15 +00:00
|
|
|
nsImageToClipboard::CreateFromImage ( imgIContainer* inImage, HANDLE* outBitmap )
|
2001-01-22 21:53:32 +00:00
|
|
|
{
|
2013-03-05 23:39:48 +00:00
|
|
|
nsresult rv;
|
2012-07-30 14:20:58 +00:00
|
|
|
*outBitmap = nullptr;
|
2006-08-17 19:10:40 +00:00
|
|
|
|
2013-12-13 08:34:24 +00:00
|
|
|
nsRefPtr<gfxASurface> surface =
|
|
|
|
inImage->GetFrame(imgIContainer::FRAME_CURRENT,
|
|
|
|
imgIContainer::FLAG_SYNC_DECODE);
|
2013-03-05 23:39:48 +00:00
|
|
|
NS_ENSURE_TRUE(surface, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsRefPtr<gfxImageSurface> frame(surface->GetAsReadableARGB32ImageSurface());
|
|
|
|
NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
|
2006-08-17 19:10:40 +00:00
|
|
|
|
2012-07-31 12:11:55 +00:00
|
|
|
nsCOMPtr<imgIEncoder> encoder = do_CreateInstance("@mozilla.org/image/encoder;2?type=image/bmp", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t format;
|
2012-07-31 12:11:55 +00:00
|
|
|
nsAutoString options;
|
2012-09-07 02:48:16 +00:00
|
|
|
if (mWantDIBV5) {
|
2013-05-07 14:25:21 +00:00
|
|
|
options.AppendLiteral("version=5;bpp=");
|
2012-09-07 02:48:16 +00:00
|
|
|
} else {
|
2013-05-07 14:25:21 +00:00
|
|
|
options.AppendLiteral("version=3;bpp=");
|
2012-09-07 02:48:16 +00:00
|
|
|
}
|
2012-07-31 12:11:55 +00:00
|
|
|
switch (frame->Format()) {
|
2014-01-23 18:26:40 +00:00
|
|
|
case gfxImageFormat::ARGB32:
|
2012-07-31 12:11:55 +00:00
|
|
|
format = imgIEncoder::INPUT_FORMAT_HOSTARGB;
|
|
|
|
options.AppendInt(32);
|
|
|
|
break;
|
2014-01-23 18:26:40 +00:00
|
|
|
case gfxImageFormat::RGB24:
|
2012-07-31 12:11:55 +00:00
|
|
|
format = imgIEncoder::INPUT_FORMAT_RGB;
|
|
|
|
options.AppendInt(24);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2006-08-17 19:10:40 +00:00
|
|
|
|
2012-07-31 12:11:55 +00:00
|
|
|
rv = encoder->InitFromData(frame->Data(), 0, frame->Width(),
|
|
|
|
frame->Height(), frame->Stride(),
|
|
|
|
format, options);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t size;
|
2012-07-31 12:11:55 +00:00
|
|
|
encoder->GetImageBufferUsed(&size);
|
|
|
|
NS_ENSURE_TRUE(size > BFH_LENGTH, NS_ERROR_FAILURE);
|
|
|
|
HGLOBAL glob = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_ZEROINIT,
|
|
|
|
size - BFH_LENGTH);
|
|
|
|
if (!glob)
|
2006-08-17 19:10:40 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2012-07-31 12:11:55 +00:00
|
|
|
char *dst = (char*) ::GlobalLock(glob);
|
|
|
|
char *src;
|
|
|
|
rv = encoder->GetImageBuffer(&src);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-08-17 19:10:40 +00:00
|
|
|
|
2012-07-31 12:11:55 +00:00
|
|
|
::CopyMemory(dst, src + BFH_LENGTH, size - BFH_LENGTH);
|
|
|
|
::GlobalUnlock(glob);
|
|
|
|
|
2006-08-17 19:10:40 +00:00
|
|
|
*outBitmap = (HANDLE)glob;
|
|
|
|
return NS_OK;
|
2001-01-22 21:53:32 +00:00
|
|
|
}
|
|
|
|
|
2006-09-13 19:52:44 +00:00
|
|
|
nsImageFromClipboard :: nsImageFromClipboard ()
|
2001-01-22 21:53:32 +00:00
|
|
|
{
|
|
|
|
// nothing to do here
|
|
|
|
}
|
|
|
|
|
|
|
|
nsImageFromClipboard :: ~nsImageFromClipboard ( )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2006-09-13 19:52:44 +00:00
|
|
|
// GetEncodedImageStream
|
2001-01-22 21:53:32 +00:00
|
|
|
//
|
2008-10-20 15:21:02 +00:00
|
|
|
// Take the raw clipboard image data and convert it to aMIMEFormat in the form of a nsIInputStream
|
2001-01-22 21:53:32 +00:00
|
|
|
//
|
|
|
|
nsresult
|
2008-10-20 15:21:02 +00:00
|
|
|
nsImageFromClipboard ::GetEncodedImageStream (unsigned char * aClipboardData, const char * aMIMEFormat, nsIInputStream** aInputStream )
|
2001-01-22 21:53:32 +00:00
|
|
|
{
|
2006-09-13 19:52:44 +00:00
|
|
|
NS_ENSURE_ARG_POINTER (aInputStream);
|
2008-10-20 15:21:02 +00:00
|
|
|
NS_ENSURE_ARG_POINTER (aMIMEFormat);
|
2006-09-13 19:52:44 +00:00
|
|
|
nsresult rv;
|
2012-07-30 14:20:58 +00:00
|
|
|
*aInputStream = nullptr;
|
2006-09-13 19:52:44 +00:00
|
|
|
|
|
|
|
// pull the size information out of the BITMAPINFO header and
|
|
|
|
// initialize the image
|
|
|
|
BITMAPINFO* header = (BITMAPINFO *) aClipboardData;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t width = header->bmiHeader.biWidth;
|
|
|
|
int32_t height = header->bmiHeader.biHeight;
|
2006-09-13 19:52:44 +00:00
|
|
|
// neg. heights mean the Y axis is inverted and we don't handle that case
|
|
|
|
NS_ENSURE_TRUE(height > 0, NS_ERROR_FAILURE);
|
|
|
|
|
2012-07-31 15:29:25 +00:00
|
|
|
unsigned char * rgbData = new unsigned char[width * height * 3 /* RGB */];
|
|
|
|
|
|
|
|
if (rgbData) {
|
|
|
|
BYTE * pGlobal = (BYTE *) aClipboardData;
|
|
|
|
// Convert the clipboard image into RGB packed pixel data
|
|
|
|
rv = ConvertColorBitMap((unsigned char *) (pGlobal + header->bmiHeader.biSize), header, rgbData);
|
|
|
|
// if that succeeded, encode the bitmap as aMIMEFormat data. Don't return early or we risk leaking rgbData
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString encoderCID(NS_LITERAL_CSTRING("@mozilla.org/image/encoder;2?type="));
|
2012-07-31 15:29:25 +00:00
|
|
|
|
|
|
|
// Map image/jpg to image/jpeg (which is how the encoder is registered).
|
|
|
|
if (strcmp(aMIMEFormat, kJPGImageMime) == 0)
|
|
|
|
encoderCID.Append("image/jpeg");
|
|
|
|
else
|
|
|
|
encoderCID.Append(aMIMEFormat);
|
|
|
|
nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(encoderCID.get(), &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)){
|
|
|
|
rv = encoder->InitFromData(rgbData, 0, width, height, 3 * width /* RGB * # pixels in a row */,
|
|
|
|
imgIEncoder::INPUT_FORMAT_RGB, EmptyString());
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
encoder->QueryInterface(NS_GET_IID(nsIInputStream), (void **) aInputStream);
|
|
|
|
}
|
2001-01-22 21:53:32 +00:00
|
|
|
}
|
2012-07-31 15:29:25 +00:00
|
|
|
delete [] rgbData;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
2001-01-22 21:53:32 +00:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
} // GetImage
|
|
|
|
|
2006-09-13 19:52:44 +00:00
|
|
|
//
|
|
|
|
// InvertRows
|
|
|
|
//
|
|
|
|
// Take the image data from the clipboard and invert the rows. Modifying aInitialBuffer in place.
|
|
|
|
//
|
|
|
|
void
|
2012-08-22 15:56:38 +00:00
|
|
|
nsImageFromClipboard::InvertRows(unsigned char * aInitialBuffer, uint32_t aSizeOfBuffer, uint32_t aNumBytesPerRow)
|
2006-09-13 19:52:44 +00:00
|
|
|
{
|
|
|
|
if (!aNumBytesPerRow)
|
|
|
|
return;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t numRows = aSizeOfBuffer / aNumBytesPerRow;
|
2006-09-13 19:52:44 +00:00
|
|
|
unsigned char * row = new unsigned char[aNumBytesPerRow];
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t currentRow = 0;
|
|
|
|
uint32_t lastRow = (numRows - 1) * aNumBytesPerRow;
|
2006-09-13 19:52:44 +00:00
|
|
|
while (currentRow < lastRow)
|
|
|
|
{
|
|
|
|
// store the current row into a temporary buffer
|
|
|
|
memcpy(row, &aInitialBuffer[currentRow], aNumBytesPerRow);
|
|
|
|
memcpy(&aInitialBuffer[currentRow], &aInitialBuffer[lastRow], aNumBytesPerRow);
|
|
|
|
memcpy(&aInitialBuffer[lastRow], row, aNumBytesPerRow);
|
|
|
|
lastRow -= aNumBytesPerRow;
|
|
|
|
currentRow += aNumBytesPerRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] row;
|
|
|
|
}
|
2001-01-22 21:53:32 +00:00
|
|
|
|
|
|
|
//
|
2006-09-13 19:52:44 +00:00
|
|
|
// ConvertColorBitMap
|
2001-01-22 21:53:32 +00:00
|
|
|
//
|
2012-07-31 15:29:25 +00:00
|
|
|
// Takes the clipboard bitmap and converts it into a RGB packed pixel values.
|
2001-01-22 21:53:32 +00:00
|
|
|
//
|
2006-09-13 19:52:44 +00:00
|
|
|
nsresult
|
|
|
|
nsImageFromClipboard::ConvertColorBitMap(unsigned char * aInputBuffer, PBITMAPINFO pBitMapInfo, unsigned char * aOutBuffer)
|
2001-01-22 21:53:32 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint8_t bitCount = pBitMapInfo->bmiHeader.biBitCount;
|
|
|
|
uint32_t imageSize = pBitMapInfo->bmiHeader.biSizeImage; // may be zero for BI_RGB bitmaps which means we need to calculate by hand
|
|
|
|
uint32_t bytesPerPixel = bitCount / 8;
|
2006-09-13 19:52:44 +00:00
|
|
|
|
|
|
|
if (bitCount <= 4)
|
|
|
|
bytesPerPixel = 1;
|
|
|
|
|
|
|
|
// rows are DWORD aligned. Calculate how many real bytes are in each row in the bitmap. This number won't
|
|
|
|
// correspond to biWidth.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t rowSize = (bitCount * pBitMapInfo->bmiHeader.biWidth + 7) / 8; // +7 to round up
|
2006-09-13 19:52:44 +00:00
|
|
|
if (rowSize % 4)
|
|
|
|
rowSize += (4 - (rowSize % 4)); // Pad to DWORD Boundary
|
|
|
|
|
|
|
|
// if our buffer includes a color map, skip over it
|
|
|
|
if (bitCount <= 8)
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t bytesToSkip = (pBitMapInfo->bmiHeader.biClrUsed ? pBitMapInfo->bmiHeader.biClrUsed : (1 << bitCount) ) * sizeof(RGBQUAD);
|
2006-09-13 19:52:44 +00:00
|
|
|
aInputBuffer += bytesToSkip;
|
|
|
|
}
|
2001-01-22 21:53:32 +00:00
|
|
|
|
2006-09-13 19:52:44 +00:00
|
|
|
bitFields colorMasks; // only used if biCompression == BI_BITFIELDS
|
|
|
|
|
|
|
|
if (pBitMapInfo->bmiHeader.biCompression == BI_BITFIELDS)
|
|
|
|
{
|
|
|
|
// color table consists of 3 DWORDS containing the color masks...
|
2012-08-22 15:56:38 +00:00
|
|
|
colorMasks.red = (*((uint32_t*)&(pBitMapInfo->bmiColors[0])));
|
|
|
|
colorMasks.green = (*((uint32_t*)&(pBitMapInfo->bmiColors[1])));
|
|
|
|
colorMasks.blue = (*((uint32_t*)&(pBitMapInfo->bmiColors[2])));
|
2006-09-13 19:52:44 +00:00
|
|
|
CalcBitShift(&colorMasks);
|
|
|
|
aInputBuffer += 3 * sizeof(DWORD);
|
2001-01-22 21:53:32 +00:00
|
|
|
}
|
2006-09-13 19:52:44 +00:00
|
|
|
else if (pBitMapInfo->bmiHeader.biCompression == BI_RGB && !imageSize) // BI_RGB can have a size of zero which means we figure it out
|
|
|
|
{
|
|
|
|
// XXX: note use rowSize here and not biWidth. rowSize accounts for the DWORD padding for each row
|
|
|
|
imageSize = rowSize * pBitMapInfo->bmiHeader.biHeight;
|
2001-01-22 21:53:32 +00:00
|
|
|
}
|
|
|
|
|
2006-09-13 19:52:44 +00:00
|
|
|
// The windows clipboard image format inverts the rows
|
|
|
|
InvertRows(aInputBuffer, imageSize, rowSize);
|
|
|
|
|
|
|
|
if (!pBitMapInfo->bmiHeader.biCompression || pBitMapInfo->bmiHeader.biCompression == BI_BITFIELDS)
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t index = 0;
|
|
|
|
uint32_t writeIndex = 0;
|
2006-09-13 19:52:44 +00:00
|
|
|
|
|
|
|
unsigned char redValue, greenValue, blueValue;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint8_t colorTableEntry = 0;
|
|
|
|
int8_t bit; // used for grayscale bitmaps where each bit is a pixel
|
|
|
|
uint32_t numPixelsLeftInRow = pBitMapInfo->bmiHeader.biWidth; // how many more pixels do we still need to read for the current row
|
|
|
|
uint32_t pos = 0;
|
2006-09-13 19:52:44 +00:00
|
|
|
|
|
|
|
while (index < imageSize)
|
|
|
|
{
|
|
|
|
switch (bitCount)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
for (bit = 7; bit >= 0 && numPixelsLeftInRow; bit--)
|
|
|
|
{
|
|
|
|
colorTableEntry = (aInputBuffer[index] >> bit) & 1;
|
|
|
|
aOutBuffer[writeIndex++] = pBitMapInfo->bmiColors[colorTableEntry].rgbRed;
|
|
|
|
aOutBuffer[writeIndex++] = pBitMapInfo->bmiColors[colorTableEntry].rgbGreen;
|
|
|
|
aOutBuffer[writeIndex++] = pBitMapInfo->bmiColors[colorTableEntry].rgbBlue;
|
|
|
|
numPixelsLeftInRow--;
|
|
|
|
}
|
|
|
|
pos += 1;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
// each aInputBuffer[index] entry contains data for two pixels.
|
|
|
|
// read the first pixel
|
|
|
|
colorTableEntry = aInputBuffer[index] >> 4;
|
|
|
|
aOutBuffer[writeIndex++] = pBitMapInfo->bmiColors[colorTableEntry].rgbRed;
|
|
|
|
aOutBuffer[writeIndex++] = pBitMapInfo->bmiColors[colorTableEntry].rgbGreen;
|
|
|
|
aOutBuffer[writeIndex++] = pBitMapInfo->bmiColors[colorTableEntry].rgbBlue;
|
|
|
|
numPixelsLeftInRow--;
|
|
|
|
|
|
|
|
if (numPixelsLeftInRow) // now read the second pixel
|
|
|
|
{
|
|
|
|
colorTableEntry = aInputBuffer[index] & 0xF;
|
|
|
|
aOutBuffer[writeIndex++] = pBitMapInfo->bmiColors[colorTableEntry].rgbRed;
|
|
|
|
aOutBuffer[writeIndex++] = pBitMapInfo->bmiColors[colorTableEntry].rgbGreen;
|
|
|
|
aOutBuffer[writeIndex++] = pBitMapInfo->bmiColors[colorTableEntry].rgbBlue;
|
|
|
|
numPixelsLeftInRow--;
|
|
|
|
}
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
aOutBuffer[writeIndex++] = pBitMapInfo->bmiColors[aInputBuffer[index]].rgbRed;
|
|
|
|
aOutBuffer[writeIndex++] = pBitMapInfo->bmiColors[aInputBuffer[index]].rgbGreen;
|
|
|
|
aOutBuffer[writeIndex++] = pBitMapInfo->bmiColors[aInputBuffer[index]].rgbBlue;
|
|
|
|
numPixelsLeftInRow--;
|
|
|
|
pos += 1;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t num = 0;
|
|
|
|
num = (uint8_t) aInputBuffer[index+1];
|
2006-09-13 19:52:44 +00:00
|
|
|
num <<= 8;
|
2012-08-22 15:56:38 +00:00
|
|
|
num |= (uint8_t) aInputBuffer[index];
|
2006-09-13 19:52:44 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
redValue = ((uint32_t) (((float)(num & 0xf800) / 0xf800) * 0xFF0000) & 0xFF0000)>> 16;
|
|
|
|
greenValue = ((uint32_t)(((float)(num & 0x07E0) / 0x07E0) * 0x00FF00) & 0x00FF00)>> 8;
|
|
|
|
blueValue = ((uint32_t)(((float)(num & 0x001F) / 0x001F) * 0x0000FF) & 0x0000FF);
|
2006-09-13 19:52:44 +00:00
|
|
|
|
|
|
|
// now we have the right RGB values...
|
|
|
|
aOutBuffer[writeIndex++] = redValue;
|
|
|
|
aOutBuffer[writeIndex++] = greenValue;
|
|
|
|
aOutBuffer[writeIndex++] = blueValue;
|
|
|
|
numPixelsLeftInRow--;
|
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
case 24:
|
|
|
|
if (pBitMapInfo->bmiHeader.biCompression == BI_BITFIELDS)
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t val = *((uint32_t*) (aInputBuffer + index) );
|
2006-09-13 19:52:44 +00:00
|
|
|
aOutBuffer[writeIndex++] = (val & colorMasks.red) >> colorMasks.redRightShift << colorMasks.redLeftShift;
|
|
|
|
aOutBuffer[writeIndex++] = (val & colorMasks.green) >> colorMasks.greenRightShift << colorMasks.greenLeftShift;
|
|
|
|
aOutBuffer[writeIndex++] = (val & colorMasks.blue) >> colorMasks.blueRightShift << colorMasks.blueLeftShift;
|
|
|
|
numPixelsLeftInRow--;
|
|
|
|
pos += 4; // we read in 4 bytes of data in order to process this pixel
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aOutBuffer[writeIndex++] = aInputBuffer[index+2];
|
2012-07-31 15:29:25 +00:00
|
|
|
aOutBuffer[writeIndex++] = aInputBuffer[index+1];
|
2006-09-13 19:52:44 +00:00
|
|
|
aOutBuffer[writeIndex++] = aInputBuffer[index];
|
|
|
|
numPixelsLeftInRow--;
|
|
|
|
pos += bytesPerPixel; // 3 bytes for 24 bit data, 4 bytes for 32 bit data (we skip over the 4th byte)...
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// This is probably the wrong place to check this...
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
index += bytesPerPixel; // increment our loop counter
|
|
|
|
|
|
|
|
if (!numPixelsLeftInRow)
|
|
|
|
{
|
|
|
|
if (rowSize != pos)
|
|
|
|
{
|
|
|
|
// advance index to skip over remaining padding bytes
|
|
|
|
index += (rowSize - pos);
|
|
|
|
}
|
|
|
|
numPixelsLeftInRow = pBitMapInfo->bmiHeader.biWidth;
|
|
|
|
pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // while we still have bytes to process
|
|
|
|
}
|
2001-01-22 21:53:32 +00:00
|
|
|
|
2006-09-13 19:52:44 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-01-22 21:53:32 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
void nsImageFromClipboard::CalcBitmask(uint32_t aMask, uint8_t& aBegin, uint8_t& aLength)
|
2006-09-13 19:52:44 +00:00
|
|
|
{
|
|
|
|
// find the rightmost 1
|
2012-08-22 15:56:38 +00:00
|
|
|
uint8_t pos;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool started = false;
|
2006-09-13 19:52:44 +00:00
|
|
|
aBegin = aLength = 0;
|
|
|
|
for (pos = 0; pos <= 31; pos++)
|
|
|
|
{
|
|
|
|
if (!started && (aMask & (1 << pos)))
|
|
|
|
{
|
|
|
|
aBegin = pos;
|
2011-10-02 02:16:19 +00:00
|
|
|
started = true;
|
2006-09-13 19:52:44 +00:00
|
|
|
}
|
|
|
|
else if (started && !(aMask & (1 << pos)))
|
|
|
|
{
|
|
|
|
aLength = pos - aBegin;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-01-22 21:53:32 +00:00
|
|
|
|
2006-09-13 19:52:44 +00:00
|
|
|
void nsImageFromClipboard::CalcBitShift(bitFields * aColorMask)
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint8_t begin, length;
|
2006-09-13 19:52:44 +00:00
|
|
|
// red
|
|
|
|
CalcBitmask(aColorMask->red, begin, length);
|
|
|
|
aColorMask->redRightShift = begin;
|
|
|
|
aColorMask->redLeftShift = 8 - length;
|
|
|
|
// green
|
|
|
|
CalcBitmask(aColorMask->green, begin, length);
|
|
|
|
aColorMask->greenRightShift = begin;
|
|
|
|
aColorMask->greenLeftShift = 8 - length;
|
|
|
|
// blue
|
|
|
|
CalcBitmask(aColorMask->blue, begin, length);
|
|
|
|
aColorMask->blueRightShift = begin;
|
|
|
|
aColorMask->blueLeftShift = 8 - length;
|
|
|
|
}
|