bug 8031 r=tor sr=dveditz

XBM support
This commit is contained in:
cbiesinger%web.de 2002-06-04 08:41:57 +00:00
parent 3e8138e699
commit ce31f6b292
6 changed files with 506 additions and 0 deletions

View File

@ -0,0 +1,54 @@
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is mozilla.org code
#
# The Initial Developer of the Original Code is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 2001 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s):
# Christian Biesinger <cbiesinger@web.de>
#
DEPTH = ../../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = imgxbm
LIBRARY_NAME = imgxbm
EXPORT_LIBRARY = 1
IS_COMPONENT = 1
MODULE_NAME = nsXBMModule
ifeq ($(OS_ARCH),WINNT)
EXTRA_DSO_LIBS = gkgfx
endif
REQUIRES = xpcom \
gfx \
gfx2 \
imglib2 \
string \
$(NULL)
CPPSRCS = nsXBMDecoder.cpp nsXBMModule.cpp
EXTRA_DSO_LDOPTS = \
$(EXTRA_DSO_LIBS) \
$(MOZ_COMPONENT_LIBS) \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,51 @@
#!nmake
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is mozilla.org code
#
# The Initial Developer of the Original Code is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 2001 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s):
# Christian Biesinger <cbiesinger@web.de>
#
DEPTH=..\..\..\..
MODULE = imgxbm
REQUIRES = xpcom \
necko \
gfx \
gfx2 \
imglib2 \
string \
$(NULL)
include <$(DEPTH)/config/config.mak>
LIBRARY_NAME = imgxbm
MODULE_NAME = nsXBMModule
OBJS = \
.\$(OBJDIR)\nsXBMModule.obj \
.\$(OBJDIR)\nsXBMDecoder.obj \
$(NULL)
LLIBS=\
$(LIBNSPR) \
$(DIST)\lib\xpcom.lib \
$(DIST)\lib\gkgfx.lib \
$(NULL)
include <$(DEPTH)\config\rules.mak>

View File

@ -0,0 +1,243 @@
/* vim:set tw=80 expandtab softtabstop=4 ts=4 sw=4: */
/* ----- BEGIN LICENSE BLOCK -----
* Version: MPL 1.1/LGPL 2.1/GPL 2.0
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla XBM Decoder.
*
* The Initial Developer of the Original Code is Christian Biesinger
* <cbiesinger@web.de>. Portions created by Christian Biesinger are
* Copyright (C) 2001 Christian Biesinger. All
* Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the LGPL or the GPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ----- END LICENSE BLOCK ----- */
/* KNOWN BUGS:
* o first #define line is assumed to be width, second height */
#include <string.h>
#include <stdlib.h>
#include "nsXBMDecoder.h"
#include "nsIInputStream.h"
#include "nsIComponentManager.h"
#include "nsIImage.h"
#include "nsMemory.h"
#include "imgIContainerObserver.h"
#include "nsRect.h"
#include "nsReadableUtils.h"
#include "imgILoad.h"
NS_IMPL_ISUPPORTS1(nsXBMDecoder, imgIDecoder)
nsXBMDecoder::nsXBMDecoder() : mBuf(nsnull), mPos(nsnull), mRow(nsnull)
{
NS_INIT_ISUPPORTS();
}
nsXBMDecoder::~nsXBMDecoder()
{
if (mBuf)
free(mBuf);
if (mRow)
delete[] mRow;
}
NS_IMETHODIMP nsXBMDecoder::Init(imgILoad *aLoad)
{
nsresult rv;
mObserver = do_QueryInterface(aLoad);
mImage = do_CreateInstance("@mozilla.org/image/container;1", &rv);
if (NS_FAILED(rv))
return rv;
mFrame = do_CreateInstance("@mozilla.org/gfx/image/frame;2", &rv);
if (NS_FAILED(rv))
return rv;
aLoad->SetImage(mImage);
mCurRow = mBufSize = mWidth = mHeight = 0;
mState = RECV_HEADER;
return NS_OK;
}
NS_IMETHODIMP nsXBMDecoder::Close()
{
mObserver->OnStopContainer(nsnull, nsnull, mImage);
mObserver->OnStopDecode(nsnull, nsnull, NS_OK, nsnull);
mObserver = nsnull;
mImage = nsnull;
mFrame = nsnull;
if (mRow) {
delete[] mRow;
mRow = nsnull;
}
return NS_OK;
}
NS_IMETHODIMP nsXBMDecoder::Flush()
{
mFrame->SetMutable(PR_FALSE);
return NS_OK;
}
NS_METHOD nsXBMDecoder::ReadSegCb(nsIInputStream* aIn, void* aClosure,
const char* aFromRawSegment, PRUint32 aToOffset,
PRUint32 aCount, PRUint32 *aWriteCount) {
nsXBMDecoder *decoder = NS_REINTERPRET_CAST(nsXBMDecoder*, aClosure);
*aWriteCount = aCount;
return decoder->ProcessData(aFromRawSegment, aCount);
}
NS_IMETHODIMP nsXBMDecoder::WriteFrom(nsIInputStream *aInStr, PRUint32 aCount, PRUint32 *aRetval)
{
return aInStr->ReadSegments(ReadSegCb, this, aCount, aRetval);
}
nsresult nsXBMDecoder::ProcessData(const char* aData, PRUint32 aCount) {
char *endPtr;
// calculate the offset since the absolute position might no longer
// be valid after realloc
const PRPtrdiff posOffset = mPos ? (mPos - mBuf) : 0;
mBuf = (char*)realloc(mBuf, mBufSize + aCount + 1);
if (!mBuf) {
mState = RECV_DONE;
return NS_ERROR_OUT_OF_MEMORY;
}
memcpy(mBuf + mBufSize, aData, aCount);
mBufSize += aCount;
mBuf[mBufSize] = 0;
mPos = mBuf + posOffset;
if (mState == RECV_HEADER) {
mPos = strstr(mBuf, "#define");
if (!mPos)
// #define not found. return for now, waiting for more data.
return NS_OK;
// Convert width and height to numbers
if (sscanf(mPos, "#define %*s %d #define %*s %d", &mWidth, &mHeight) != 2)
return NS_OK;
mImage->Init(mWidth, mHeight, mObserver);
mObserver->OnStartContainer(nsnull, nsnull, mImage);
mFrame->Init(0, 0, mWidth, mHeight, GFXFORMAT);
mImage->AppendFrame(mFrame);
mObserver->OnStartFrame(nsnull, nsnull, mFrame);
PRUint32 bpr;
mFrame->GetImageBytesPerRow(&bpr);
mRow = new PRUint8[bpr];
mState = RECV_SEEK;
mCurRow = 0;
mCurCol = 0;
}
if (mState == RECV_SEEK) {
if ((endPtr = strchr(mPos, '{')) != NULL) {
mPos = endPtr+1;
mState = RECV_DATA;
} else {
mPos = mBuf + mBufSize;
return NS_OK;
}
}
if (mState == RECV_DATA) {
#if defined(XP_MAC) || defined(XP_MACOSX)
// bytes per pixel
const PRUint32 bpp = 4;
#else
const PRUint32 bpp = 3;
#endif
PRUint32 bpr;
mFrame->GetImageBytesPerRow(&bpr);
do {
PRUint32 pixel = strtoul(mPos, &endPtr, 0);
if (endPtr == mPos)
return NS_OK; // no number to be found - need more data
if (!*endPtr)
return NS_OK; // number at the end - might be missing a digit
if (pixel == 0 && *endPtr == 'x')
return NS_OK; // 0x at the end, actual number is missing
while (*endPtr && isspace(*endPtr))
endPtr++; // skip whitespace looking for comma
if (*endPtr && (*endPtr != ',')) {
*endPtr = '\0';
mState = RECV_DONE; // strange character (or ending '}')
}
mPos = endPtr;
for (int i = 1; i <= 128; i <<= 1) {
// if bit is set, use black, else white
PRUint8 val = (pixel & i) ? 0 : 255;
#if defined(XP_MAC) || defined(XP_MACOSX)
#define DATA_OFFSET 1
mRow[mCurCol * bpp] = 0; // padding byte
#else
#define DATA_OFFSET 0
#endif
for (int j = DATA_OFFSET; j < (DATA_OFFSET + 3); j++)
mRow[mCurCol * bpp + j] = val;
mCurCol++;
if (mCurCol == mWidth)
break;
}
if (mCurCol == mWidth || mState == RECV_DONE) {
// Row finished. Set Data.
mFrame->SetImageData(mRow, bpr, mCurRow * bpr);
nsRect r(0, (mCurRow + 1), mWidth, 1);
mObserver->OnDataAvailable(nsnull, nsnull, mFrame, &r);
if ((mCurRow + 1) == mHeight) {
mState = RECV_DONE;
return mObserver->OnStopFrame(nsnull, nsnull, mFrame);
}
mCurRow++;
mCurCol = 0;
}
mPos++;
} while (*mPos && (mState == RECV_DATA));
}
else
return NS_ERROR_FAILURE;
return NS_OK;
}

View File

@ -0,0 +1,104 @@
/* vim:set tw=80 expandtab softtabstop=4 ts=4 sw=4: */
/* ----- BEGIN LICENSE BLOCK -----
* Version: MPL 1.1/LGPL 2.1/GPL 2.0
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla XBM Decoder.
*
* The Initial Developer of the Original Code is Christian Biesinger
* <cbiesinger@web.de>. Portions created by Christian Biesinger are
* Copyright (C) 2001 Christian Biesinger. All
* Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the LGPL or the GPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ----- END LICENSE BLOCK ----- */
#ifndef _nsXBMDecoder_h
#define _nsXBMDecoder_h
#include "nsCOMPtr.h"
#include "imgIDecoder.h"
#include "imgIContainer.h"
#include "imgIDecoderObserver.h"
#include "gfxIImageFrame.h"
#define NS_XBMDECODER_CID \
{ /* {dbfd145d-3298-4f3c-902f-2c5e1a1494ce} */ \
0xdbfd145d, \
0x3298, \
0x4f3c, \
{ 0x90, 0x2f, 0x2c, 0x5e, 0x1a, 0x14, 0x94, 0xce } \
}
#if defined(XP_PC) || defined(XP_BEOS) || defined(MOZ_WIDGET_PHOTON)
#define GFXFORMAT gfxIFormats::BGR
#else
#define USE_RGB
#define GFXFORMAT gfxIFormats::RGB
#endif
class nsXBMDecoder : public imgIDecoder
{
public:
NS_DECL_ISUPPORTS
NS_DECL_IMGIDECODER
nsXBMDecoder();
virtual ~nsXBMDecoder();
nsresult ProcessData(const char* aData, PRUint32 aCount);
private:
static NS_METHOD ReadSegCb(nsIInputStream* aIn, void* aClosure,
const char* aFromRawSegment, PRUint32 aToOffset,
PRUint32 aCount, PRUint32 *aWriteCount);
nsCOMPtr<imgIDecoderObserver> mObserver;
nsCOMPtr<imgIContainer> mImage;
nsCOMPtr<gfxIImageFrame> mFrame;
PRInt32 mCurRow;
PRInt32 mCurCol;
char* mBuf; // Holds the received data
char* mPos;
PRUint32 mBufSize; // number of bytes in mBuf
PRInt32 mWidth;
PRInt32 mHeight;
PRUint8* mRow; // Hold the decoded row
enum {
RECV_HEADER,
RECV_SEEK,
RECV_DATA,
RECV_DONE
} mState;
};
#endif

View File

@ -0,0 +1,53 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim:set tw=80 expandtab softtabstop=4 ts=4 sw=4:
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Chris Saari <saari@netscape.com>
* Christian Biesinger <cbiesinger@web.de>
* David Hyatt <hyatt@netscape.com>
*/
#include "nsXBMDecoder.h"
#include "nsIComponentManager.h"
#include "nsIGenericFactory.h"
#include "nsISupports.h"
#include "nsCOMPtr.h"
NS_GENERIC_FACTORY_CONSTRUCTOR(nsXBMDecoder)
static nsModuleComponentInfo components[] =
{
{ "XBM Decoder",
NS_XBMDECODER_CID,
"@mozilla.org/image/decoder;2?type=image/x-xbitmap",
nsXBMDecoderConstructor, },
{ "XBM Decoder",
NS_XBMDECODER_CID,
"@mozilla.org/image/decoder;2?type=image/x-xbm",
nsXBMDecoderConstructor, },
{ "XBM Decoder",
NS_XBMDECODER_CID,
"@mozilla.org/image/decoder;2?type=image/xbm",
nsXBMDecoderConstructor, },
};
NS_IMPL_NSGETMODULE(nsXBMModule, components)

File diff suppressed because one or more lines are too long