Author: post
Date: 2010-07-01 22:09:02 +0200 (Thu, 01 Jul 2010)
New Revision: 247

Added:
   RawSpeed/ByteStreamSwap.cpp
   RawSpeed/ByteStreamSwap.h
Modified:
   RawSpeed/ByteStream.cpp
   RawSpeed/ByteStream.h
   RawSpeed/Common.h
   RawSpeed/LJpegDecompressor.cpp
   RawSpeed/NefDecoder.cpp
   RawSpeed/RawDecoder.cpp
   RawSpeed/RawSpeed.vcproj
   RawSpeed/TiffIFD.h
   RawSpeed/TiffParser.cpp
   RawSpeed/TiffParser.h
   RawSpeed/TiffParserHeaderless.cpp
   RawSpeed/TiffParserOlympus.cpp
Log:
Add (untested) support for big-endian platforms.

Modified: RawSpeed/ByteStream.cpp
===================================================================
--- RawSpeed/ByteStream.cpp     2010-07-01 20:05:57 UTC (rev 246)
+++ RawSpeed/ByteStream.cpp     2010-07-01 20:09:02 UTC (rev 247)
@@ -50,13 +50,14 @@
 
 uchar8 ByteStream::getByte() {
   if (off >= size)
-    throw IOException("Out of buffer read");
+    throw IOException("getByte:Out of buffer read");
   return buffer[off++];
 }
 
 ushort16 ByteStream::getShort() {
   if (off + 1 >= size)
-    throw IOException("Out of buffer read");
+    throw IOException("getShort: Out of buffer read");
+  return *(ushort16*)&buffer[off+=4];
   uint32 a = buffer[off++];
   uint32 b = buffer[off++];
   // !!! ENDIAN SWAP
@@ -65,13 +66,13 @@
 
 int ByteStream::getInt() {
   if (off + 4 >= size)
-    throw IOException("Out of buffer read");
+    throw IOException("getInt:Out of buffer read");
   return *(int*)&buffer[off+=4];
 }
 
 void ByteStream::setAbsoluteOffset(uint32 offset) {
   if (offset >= size)
-    throw IOException("Offset set out of buffer");
+    throw IOException("setAbsoluteOffset:Offset set out of buffer");
   off = offset;
 }
 

Modified: RawSpeed/ByteStream.h
===================================================================
--- RawSpeed/ByteStream.h       2010-07-01 20:05:57 UTC (rev 246)
+++ RawSpeed/ByteStream.h       2010-07-01 20:09:02 UTC (rev 247)
@@ -31,7 +31,6 @@
   ByteStream(const ByteStream* b);
   ~ByteStream(void);
   uint32 peekByte();
-  ushort16 getShort();
   uint32 getOffset() {return off;}
   void skipBytes(uint32 nbytes);
   uchar8 getByte();
@@ -39,8 +38,9 @@
   void skipToMarker();
   uint32 getRemainSize() { return size-off;}
   const uchar8* getData() {return &buffer[off];}
-  int getInt();
-private:
+  virtual ushort16 getShort();
+  virtual int getInt();
+protected:
   const uchar8* buffer;
   const uint32 size;            // This if the end of buffer.
   uint32 off;                  // Offset in bytes (this is next byte to 
deliver)

Added: RawSpeed/ByteStreamSwap.cpp
===================================================================
--- RawSpeed/ByteStreamSwap.cpp                         (rev 0)
+++ RawSpeed/ByteStreamSwap.cpp 2010-07-01 20:09:02 UTC (rev 247)
@@ -0,0 +1,35 @@
+#include "StdAfx.h"
+#include "ByteStreamSwap.h"
+
+namespace RawSpeed {
+
+
+ByteStreamSwap::ByteStreamSwap( const uchar8* _buffer, uint32 _size ) : 
+ByteStream(_buffer, _size)
+{}
+
+ByteStreamSwap::ByteStreamSwap( const ByteStreamSwap* b ) :
+ByteStream(b)
+{}
+
+ByteStreamSwap::~ByteStreamSwap(void)
+{
+}
+
+ushort16 ByteStreamSwap::getShort() {
+  if (off + 1 >= size)
+    throw IOException("getShort: Out of buffer read");
+  uint32 a = buffer[off++];
+  uint32 b = buffer[off++];
+  return (a << 8) | b;
+}
+
+/* NOTE: Actually unused, so not tested */
+int ByteStreamSwap::getInt() {
+  if (off + 4 >= size)
+    throw IOException("getInt: Out of buffer read");
+  return (int)buffer[off] << 24 | (int)buffer[off+1] << 16 | 
(int)buffer[off+2] << 8 | (int)buffer[off+3];
+  off+=4;
+}
+
+} // namespace RawSpeed

Added: RawSpeed/ByteStreamSwap.h
===================================================================
--- RawSpeed/ByteStreamSwap.h                           (rev 0)
+++ RawSpeed/ByteStreamSwap.h   2010-07-01 20:09:02 UTC (rev 247)
@@ -0,0 +1,19 @@
+#pragma once
+#include "ByteStream.h"
+
+#include "IOException.h"
+
+namespace RawSpeed {
+
+class ByteStreamSwap :
+  public ByteStream
+{
+public:
+  ByteStreamSwap(const uchar8* _buffer, uint32 _size);
+  ByteStreamSwap(const ByteStreamSwap* b);
+  virtual ushort16 getShort();
+  virtual int getInt();
+  virtual ~ByteStreamSwap(void);
+};
+
+} // namespace RawSpeed

Modified: RawSpeed/Common.h
===================================================================
--- RawSpeed/Common.h   2010-07-01 20:05:57 UTC (rev 246)
+++ RawSpeed/Common.h   2010-07-01 20:09:02 UTC (rev 247)
@@ -65,6 +65,11 @@
 typedef unsigned int uint32;
 typedef signed int int32;
 typedef unsigned short ushort16;
+
+typedef enum Endianness {
+  big, little, unknown
+} Endianness;
+
 
 inline void BitBlt(uchar8* dstp, int dst_pitch, const uchar8* srcp, int 
src_pitch, int row_size, int height) {
   if (height == 1 || (dst_pitch == src_pitch && src_pitch == row_size)) {
@@ -97,6 +102,19 @@
 #endif
 }
 
+inline Endianness getHostEndianness() {
+  ushort16 testvar = 0xfeff; 
+  uint32 firstbyte = ((uchar8 *)&testvar)[0];
+  if (firstbyte == 0xff)
+    return little;
+  else if (firstbyte == 0xfe)
+    return big;
+  else
+    _ASSERTE(FALSE);
+
+  // Return something to make compilers happy
+  return unknown;
+}
 inline uint32 clampbits(int x, uint32 n) { uint32 _y_temp; if( (_y_temp=x>>n) 
) x = ~_y_temp >> (32-n); return x;}
 
 

Modified: RawSpeed/LJpegDecompressor.cpp
===================================================================
--- RawSpeed/LJpegDecompressor.cpp      2010-07-01 20:05:57 UTC (rev 246)
+++ RawSpeed/LJpegDecompressor.cpp      2010-07-01 20:09:02 UTC (rev 247)
@@ -1,5 +1,7 @@
 #include "StdAfx.h"
 #include "LJpegDecompressor.h"
+#include "ByteStreamSwap.h"
+
 /*
     RawSpeed - RAW file decoder.
 
@@ -106,7 +108,12 @@
   if (!mFile->isValid(offset + size - 1))
     ThrowRDE("LJpegDecompressor::getSOF: Start offset plus size is longer than 
file. Truncated file.");
   try {
-    input = new ByteStream(mFile->getData(offset), size);
+    Endianness host_endian = getHostEndianness();
+    // JPEG is big endian
+    if (host_endian == big)
+      input = new ByteStream(mFile->getData(offset), size);
+    else 
+      input = new ByteStreamSwap(mFile->getData(offset), size);
 
     if (getNextMarker(false) != M_SOI)
       ThrowRDE("LJpegDecompressor::getSOF: Image did not start with SOI. 
Probably not an LJPEG");
@@ -138,7 +145,12 @@
   offY = offsetY;
 
   try {
-    input = new ByteStream(mFile->getData(offset), size);
+    Endianness host_endian = getHostEndianness();
+    // JPEG is big endian
+    if (host_endian == big)
+      input = new ByteStream(mFile->getData(offset), size);
+    else 
+      input = new ByteStreamSwap(mFile->getData(offset), size);
 
     if (getNextMarker(false) != M_SOI)
       ThrowRDE("LJpegDecompressor::startDecoder: Image did not start with SOI. 
Probably not an LJPEG");

Modified: RawSpeed/NefDecoder.cpp
===================================================================
--- RawSpeed/NefDecoder.cpp     2010-07-01 20:05:57 UTC (rev 246)
+++ RawSpeed/NefDecoder.cpp     2010-07-01 20:09:02 UTC (rev 247)
@@ -1,5 +1,6 @@
 #include "StdAfx.h"
 #include "NefDecoder.h"
+#include "ByteStreamSwap.h"
 /*
     RawSpeed - RAW file decoder.
 
@@ -107,10 +108,17 @@
     meta = data[0]->getEntry((TiffTag)0x8c);  // Fall back
   }
 
-  ByteStream metadata(meta->getData(), meta->count);
   try {
     NikonDecompressor decompressor(mFile, mRaw);
-    decompressor.DecompressNikon(metadata, width, height, bitPerPixel, 
offsets->getInt(), counts->getInt());
+
+    // Nikon is JPEG (Big Endian) byte order
+    if (getHostEndianness() == big)
+      decompressor.DecompressNikon(ByteStream(meta->getData(), meta->count), 
+                                   width, height, bitPerPixel, 
offsets->getInt(), counts->getInt());
+    else
+      decompressor.DecompressNikon(ByteStreamSwap(meta->getData(), 
meta->count), 
+                                   width, height, bitPerPixel, 
offsets->getInt(), counts->getInt());
+
   } catch (IOException e) {
     errors.push_back(_strdup(e.what()));
     // Let's ignore it, it may have delivered somewhat useful data.

Modified: RawSpeed/RawDecoder.cpp
===================================================================
--- RawSpeed/RawDecoder.cpp     2010-07-01 20:05:57 UTC (rev 246)
+++ RawSpeed/RawDecoder.cpp     2010-07-01 20:09:02 UTC (rev 247)
@@ -75,12 +75,12 @@
 
   } else {
 
-    if (bitPerPixel == 16)  {
+    if (bitPerPixel == 16 && getHostEndianness() == little)  {
       BitBlt(&data[offset.x*sizeof(ushort16)*cpp+y*outPitch], outPitch,
              input.getData(), inputPitch, w*mRaw->bpp, h - y);
       return;
     }
-    if (bitPerPixel == 12 && (int)w == inputPitch * 8 / 12)  {
+    if (bitPerPixel == 12 && (int)w == inputPitch * 8 / 12 && 
getHostEndianness() == little)  {
       Decode12BitRaw(input, w, h);
       return;
     }

Modified: RawSpeed/RawSpeed.vcproj
===================================================================
--- RawSpeed/RawSpeed.vcproj    2010-07-01 20:05:57 UTC (rev 246)
+++ RawSpeed/RawSpeed.vcproj    2010-07-01 20:09:02 UTC (rev 247)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="Windows-1252"?>
 <VisualStudioProject
        ProjectType="Visual C++"
-       Version="8,00"
+       Version="8.00"
        Name="RawSpeed"
        ProjectGUID="{482A0ABF-1AE3-4EE4-815A-428880DAE0C6}"
        RootNamespace="CR2reader"
@@ -297,6 +297,10 @@
                                        >
                                </File>
                                <File
+                                       RelativePath=".\ByteStreamSwap.cpp"
+                                       >
+                               </File>
+                               <File
                                        RelativePath=".\FileIOException.cpp"
                                        >
                                </File>
@@ -415,6 +419,10 @@
                                        >
                                </File>
                                <File
+                                       RelativePath=".\ByteStreamSwap.h"
+                                       >
+                               </File>
+                               <File
                                        RelativePath=".\Common.h"
                                        >
                                </File>

Modified: RawSpeed/TiffIFD.h
===================================================================
--- RawSpeed/TiffIFD.h  2010-07-01 20:05:57 UTC (rev 246)
+++ RawSpeed/TiffIFD.h  2010-07-01 20:09:02 UTC (rev 247)
@@ -27,11 +27,7 @@
 
 namespace RawSpeed {
 
-typedef enum Endianness {
-  big, little
-} Endianness;
 
-
 class TiffIFD
 {
 public:

Modified: RawSpeed/TiffParser.cpp
===================================================================
--- RawSpeed/TiffParser.cpp     2010-07-01 20:05:57 UTC (rev 246)
+++ RawSpeed/TiffParser.cpp     2010-07-01 20:09:02 UTC (rev 247)
@@ -1,5 +1,6 @@
 #include "StdAfx.h"
 #include "TiffParser.h"
+
 /*
     RawSpeed - RAW file decoder.
 
@@ -24,8 +25,9 @@
 
 namespace RawSpeed {
 
+
 TiffParser::TiffParser(FileMap* inputData): mInput(inputData), mRootIFD(0) {
-
+  host_endian = getHostEndianness();
 }
 
 
@@ -50,26 +52,26 @@
   if (mInput->getSize() < 16)
     throw TiffParserException("Not a TIFF file (size too small)");
   if (data[0] != 0x49 || data[1] != 0x49) {
-    endian = big;
+    tiff_endian = big;
     if (data[0] != 0x4D || data[1] != 0x4D)
       throw TiffParserException("Not a TIFF file (ID)");
 
     if (data[3] != 42)
       throw TiffParserException("Not a TIFF file (magic 42)");
   } else {
-    endian = little;
+    tiff_endian = little;
     if (data[2] != 42 && data[2] != 0x52 && data[2] != 0x55) // ORF has 0x52, 
RW2 0x55 - Brillant!
       throw TiffParserException("Not a TIFF file (magic 42)");
   }
 
-  if (endian == little)
+  if (tiff_endian == host_endian)
     mRootIFD = new TiffIFD();
   else
     mRootIFD = new TiffIFDBE();
 
   uint32 nextIFD;
   data = mInput->getData(4);
-  if (endian == little) {
+  if (tiff_endian == host_endian) {
     nextIFD = *(int*)data;
   } else {
     nextIFD = (unsigned int)data[0] << 24 | (unsigned int)data[1] << 16 | 
(unsigned int)data[2] << 8 | (unsigned int)data[3];
@@ -77,7 +79,7 @@
   while (nextIFD) {
     CHECKSIZE(nextIFD);
 
-    if (endian == little)
+    if (tiff_endian == host_endian)
       mRootIFD->mSubIFD.push_back(new TiffIFD(mInput, nextIFD));
     else
       mRootIFD->mSubIFD.push_back(new TiffIFDBE(mInput, nextIFD));

Modified: RawSpeed/TiffParser.h
===================================================================
--- RawSpeed/TiffParser.h       2010-07-01 20:05:57 UTC (rev 246)
+++ RawSpeed/TiffParser.h       2010-07-01 20:09:02 UTC (rev 247)
@@ -44,11 +44,13 @@
 
   virtual void parseData();
   virtual RawDecoder* getDecoder();
-  Endianness endian;
+  Endianness tiff_endian;
   TiffIFD* RootIFD() const { return mRootIFD; }
+  RawSpeed::Endianness getHostEndian() const { return host_endian; }
 protected:
   FileMap *mInput;
   TiffIFD* mRootIFD;
+  Endianness host_endian;
 };
 
 } // namespace RawSpeed

Modified: RawSpeed/TiffParserHeaderless.cpp
===================================================================
--- RawSpeed/TiffParserHeaderless.cpp   2010-07-01 20:05:57 UTC (rev 246)
+++ RawSpeed/TiffParserHeaderless.cpp   2010-07-01 20:09:02 UTC (rev 247)
@@ -26,7 +26,7 @@
 
 TiffParserHeaderless::TiffParserHeaderless(FileMap* input, Endianness _end) :
     TiffParser(input) {
-  endian = _end;
+  tiff_endian = _end;
 }
 
 TiffParserHeaderless::~TiffParserHeaderless(void) {
@@ -50,7 +50,7 @@
   if (mInput->getSize() < 12)
     throw TiffParserException("Not a TIFF file (size too small)");
 
-  if (endian == little)
+  if (tiff_endian == host_endian)
     mRootIFD = new TiffIFD();
   else
     mRootIFD = new TiffIFDBE();
@@ -59,7 +59,7 @@
   do {
     CHECKSIZE(nextIFD);
 
-    if (endian == little)
+    if (tiff_endian == host_endian)
       mRootIFD->mSubIFD.push_back(new TiffIFD(mInput, nextIFD));
     else
       mRootIFD->mSubIFD.push_back(new TiffIFDBE(mInput, nextIFD));

Modified: RawSpeed/TiffParserOlympus.cpp
===================================================================
--- RawSpeed/TiffParserOlympus.cpp      2010-07-01 20:05:57 UTC (rev 246)
+++ RawSpeed/TiffParserOlympus.cpp      2010-07-01 20:09:02 UTC (rev 247)
@@ -49,15 +49,15 @@
   if (mInput->getSize() < 16)
     throw TiffParserException("Not a TIFF file (size too small)");
   if (data[0] != 0x49 || data[1] != 0x49) {
-    endian = big;
+    tiff_endian = big;
     if (data[0] != 0x4D || data[1] != 0x4D)
       throw TiffParserException("Not a TIFF file (ID)");
 
   } else {
-    endian = little;
+    tiff_endian = little;
   }
 
-  if (endian == little)
+  if (tiff_endian == host_endian)
     mRootIFD = new TiffIFD();
   else
     mRootIFD = new TiffIFDBE();
@@ -66,7 +66,7 @@
   do {
     CHECKSIZE(nextIFD);
 
-    if (endian == little)
+    if (tiff_endian == host_endian)
       mRootIFD->mSubIFD.push_back(new TiffIFD(mInput, nextIFD));
     else
       mRootIFD->mSubIFD.push_back(new TiffIFDBE(mInput, nextIFD));


_______________________________________________
Rawstudio-commit mailing list
[email protected]
http://rawstudio.org/cgi-bin/mailman/listinfo/rawstudio-commit

Reply via email to