Add support for using libtiff for decoding TIFF files (to support more TIFF formats) - libtiff is only used for now for decoding the pixels part (not for tags or directories - for minimal code changes)

This commit is contained in:
Airy ANDRE 2012-05-22 11:53:08 -07:00
parent b5c96b98fb
commit 3eda93d62a
5 changed files with 270 additions and 3 deletions

View File

@ -9621,7 +9621,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "#!/bin/sh\nmkdir -p $DERIVED_FILE_DIR\necho \"\" > $DERIVED_FILE_DIR/O2Defines_FreeType.h\n\nif [ -d /Developer/Cocotron/1.0/Windows/i386/freetype-2.3.5 ]; then\n\techo \"FreeType 2.3.5 present.\"\n\techo \"#define FREETYPE_PRESENT 1\" >> $DERIVED_FILE_DIR/O2Defines_FreeType.h\nelse\n# Create empty libfreetype for link phase\n echo \"FreeType 2.3.5 not found\"\n touch $DERIVED_FILE_DIR/libfreetype.c\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-gcc $DERIVED_FILE_DIR/libfreetype.c -c -o $DERIVED_FILE_DIR/libfreetype.o\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-ar cr $DERIVED_FILE_DIR/libfreetype.a $DERIVED_FILE_DIR/libfreetype.o\nfi\n\necho \"\" > $DERIVED_FILE_DIR/O2Defines_libjpeg.h\nif [ -d /Developer/Cocotron/1.0/Windows/i386/libjpeg ]; then\n\techo \"libjpeg present.\"\n\techo \"#define LIBJPEG_PRESENT 1\" >> $DERIVED_FILE_DIR/O2Defines_libjpeg.h\nelse\n# Create empty libjpeg for link phase\n echo \"libjpeg not found\"\n touch $DERIVED_FILE_DIR/libjpeg.c\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-gcc $DERIVED_FILE_DIR/libjpeg.c -c -o $DERIVED_FILE_DIR/libjpeg.o\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-ar cr $DERIVED_FILE_DIR/libjpeg.a $DERIVED_FILE_DIR/libjpeg.o\nfi\n\necho \"\" > $DERIVED_FILE_DIR/O2Defines_zlib.h\nif [ -d /Developer/Cocotron/1.0/Windows/i386/zlib-1.2.5 ]; then\n\techo \"zlib present.\"\n\techo \"#define ZLIB_PRESENT 1\" >> $DERIVED_FILE_DIR/O2Defines_zlib.h\nelse\n# Create empty zlib for link phase\n echo \"zlib not found\"\n touch $DERIVED_FILE_DIR/zlib.c\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-gcc $DERIVED_FILE_DIR/zlib.c -c -o $DERIVED_FILE_DIR/zlib.o\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-ar cr $DERIVED_FILE_DIR/libz.a $DERIVED_FILE_DIR/zlib.o\nfi\n\necho \"\" > $DERIVED_FILE_DIR/O2Defines_libpng.h\nif [ -d /Developer/Cocotron/1.0/Windows/i386/libpng ]; then\n\techo \"libpng present.\"\n\techo \"#define LIBPNG_PRESENT 1\" >> $DERIVED_FILE_DIR/O2Defines_libpng.h\nelse\n# Create empty libpng for link phase\n echo \"libpng not found\"\n touch $DERIVED_FILE_DIR/libpng.c\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-gcc $DERIVED_FILE_DIR/libpng.c -c -o $DERIVED_FILE_DIR/libpng.o\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-ar cr $DERIVED_FILE_DIR/libpng.a $DERIVED_FILE_DIR/libpng.o\nfi\n\n";
shellScript = "#!/bin/sh\nmkdir -p $DERIVED_FILE_DIR\necho \"\" > $DERIVED_FILE_DIR/O2Defines_FreeType.h\n\nif [ -d /Developer/Cocotron/1.0/Windows/i386/freetype-2.3.5 ]; then\n\techo \"FreeType 2.3.5 present.\"\n\techo \"#define FREETYPE_PRESENT 1\" >> $DERIVED_FILE_DIR/O2Defines_FreeType.h\nelse\n# Create empty libfreetype for link phase\n echo \"FreeType 2.3.5 not found\"\n touch $DERIVED_FILE_DIR/libfreetype.c\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-gcc $DERIVED_FILE_DIR/libfreetype.c -c -o $DERIVED_FILE_DIR/libfreetype.o\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-ar cr $DERIVED_FILE_DIR/libfreetype.a $DERIVED_FILE_DIR/libfreetype.o\nfi\n\necho \"\" > $DERIVED_FILE_DIR/O2Defines_libjpeg.h\nif [ -d /Developer/Cocotron/1.0/Windows/i386/libjpeg ]; then\n\techo \"libjpeg present.\"\n\techo \"#define LIBJPEG_PRESENT 1\" >> $DERIVED_FILE_DIR/O2Defines_libjpeg.h\nelse\n# Create empty libjpeg for link phase\n echo \"libjpeg not found\"\n touch $DERIVED_FILE_DIR/libjpeg.c\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-gcc $DERIVED_FILE_DIR/libjpeg.c -c -o $DERIVED_FILE_DIR/libjpeg.o\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-ar cr $DERIVED_FILE_DIR/libjpeg.a $DERIVED_FILE_DIR/libjpeg.o\nfi\n\necho \"\" > $DERIVED_FILE_DIR/O2Defines_libjpeg.h\nif [ -d /Developer/Cocotron/1.0/Windows/i386/libjpeg ]; then\n\techo \"libjpeg present.\"\n\techo \"#define LIBJPEG_PRESENT 1\" >> $DERIVED_FILE_DIR/O2Defines_libjpeg.h\nelse\n# Create empty libjpeg for link phase\n echo \"libjpeg not found\"\n touch $DERIVED_FILE_DIR/libjpeg.c\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-gcc $DERIVED_FILE_DIR/libjpeg.c -c -o $DERIVED_FILE_DIR/libjpeg.o\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-ar cr $DERIVED_FILE_DIR/libjpeg.a $DERIVED_FILE_DIR/libjpeg.o\nfi\n\necho \"\" > $DERIVED_FILE_DIR/O2Defines_libtiff.h\nif [ -d /Developer/Cocotron/1.0/Windows/i386/libtiff ]; then\n\techo \"libitff present.\"\n\techo \"#define LIBTIFF_PRESENT 1\" >> $DERIVED_FILE_DIR/O2Defines_libtiff.h\nelse\n# Create empty libtiff for link phase\n echo \"libtiff not found\"\n touch $DERIVED_FILE_DIR/libtiff.c\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-gcc $DERIVED_FILE_DIR/libtiff.c -c -o $DERIVED_FILE_DIR/libtiff.o\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-ar cr $DERIVED_FILE_DIR/libtiff.a $DERIVED_FILE_DIR/libtiff.o\nfi\n\necho \"\" > $DERIVED_FILE_DIR/O2Defines_libpng.h\nif [ -d /Developer/Cocotron/1.0/Windows/i386/libpng ]; then\n\techo \"libpng present.\"\n\techo \"#define LIBPNG_PRESENT 1\" >> $DERIVED_FILE_DIR/O2Defines_libpng.h\nelse\n# Create empty libpng for link phase\n echo \"libpng not found\"\n touch $DERIVED_FILE_DIR/libpng.c\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-gcc $DERIVED_FILE_DIR/libpng.c -c -o $DERIVED_FILE_DIR/libpng.o\n /Developer/Cocotron/1.0/Windows/i386/gcc-4.3.1/bin/i386-mingw32msvc-ar cr $DERIVED_FILE_DIR/libpng.a $DERIVED_FILE_DIR/libpng.o\nfi\n\n";
};
/* End PBXShellScriptBuildPhase section */
@ -11838,6 +11838,7 @@
HEADER_SEARCH_PATHS = "$(TARGET_BUILD_DIR)/usr/include";
INFOPLIST_FILE = Info.plist;
LIBRARY_SEARCH_PATHS = (
/Developer/Cocotron/1.0/Windows/i386/libtiff/lib,
/Developer/Cocotron/1.0/Windows/i386/libpng/lib,
/Developer/Cocotron/1.0/Windows/i386/libjpeg/lib,
"$(LIBRARY_SEARCH_PATHS_QUOTED_FOR_TARGET_1)",
@ -11853,6 +11854,7 @@
OTHER_LDFLAGS = (
"-static",
"-lpng",
"-ltiff",
"-ljpeg",
"-lz",
"-shared",
@ -12123,6 +12125,7 @@
HEADER_SEARCH_PATHS = "$(TARGET_BUILD_DIR)/usr/include";
INFOPLIST_FILE = Info.plist;
LIBRARY_SEARCH_PATHS = (
/Developer/Cocotron/1.0/Windows/i386/libtiff/lib,
/Developer/Cocotron/1.0/Windows/i386/libpng/lib,
"/Developer/Cocotron/1.0/Windows/i386/zlib-1.2.5/lib",
/Developer/Cocotron/1.0/Windows/i386/libjpeg/lib,
@ -12142,6 +12145,7 @@
OTHER_LDFLAGS = (
"-static",
"-lpng",
"-ltiff",
"-ljpeg",
"-lz",
"-shared",

View File

@ -0,0 +1,63 @@
#!/bin/sh
installResources=`pwd`/Resources
scriptResources=$installResources/scripts
productFolder=/Developer/Cocotron/1.0
downloadFolder=$productFolder/Downloads
if [ ""$1"" = "" ];then
targetPlatform="Windows"
else
targetPlatform=$1
fi
if [ ""$2"" = "" ];then
targetArchitecture="i386"
else
targetArchitecture=$2
fi
if [ ""$3"" = "" ];then
gccVersion="4.3.1"
else
gccVersion=$3
fi
BASEDIR=/Developer/Cocotron/1.0/$targetPlatform/$targetArchitecture
BUILD=/tmp/build_tiff
TIFFVERSION=4.0.1
$scriptResources/downloadFilesIfNeeded.sh $downloadFolder ftp://ftp.remotesensing.org/pub/libtiff//tiff-${TIFFVERSION}.tar.gz
mkdir -p $BUILD
cd $BUILD
tar -xvzf $downloadFolder/tiff-${TIFFVERSION}.tar.gz
cd tiff-${TIFFVERSION}
pwd
GCC=$(echo $BASEDIR/gcc-$gccVersion/bin/*gcc)
AS=$(echo $BASEDIR/gcc-$gccVersion/bin/*as)
AR=$(echo $BASEDIR/gcc-$gccVersion/bin/*ar)
RANLIB=$(echo $BASEDIR/gcc-$gccVersion/bin/*ranlib)
TARGET=$($GCC -dumpmachine)
COCOTRON=/Developer/Cocotron/1.0//build/$targetPlatform/$targetArchitecture
INSTALL_PREFIX=/Developer/Cocotron/1.0/Windows/i386/libtiff
BINARY_PATH=$INSTALL_PREFIX/bin
INCLUDE_PATH=$INSTALL_PREFIX/include
LIBRARY_PATH=$INSTALL_PREFIX/lib
export CFLAGS="-DTIF_PLATFORM_CONSOLE"
mkdir -p $BINARY_PATH
mkdir -p $LIBRARY_PATH
mkdir -p $INCLUDE_PATH
./configure --prefix="$INSTALL_PREFIX" -host $TARGET AR=$AR CC=$GCC RANLIB=$RANLIB AS=$AS \
--with-jpeg-include-dir=$BASEDIR/libjpeg/include --with-jpeg-lib-dir=$BASEDIR/libjpeg/lib \
--with-zlib-include-dir=$BASEDIR/zlib-1.2.5/include --with-zlib-lib-dir=$BASEDIR/zlib-1.2.5/lib \
--enable-mdi --disable-jpeg12 --disable-cxx --disable-shared
make && make install

View File

@ -7,6 +7,7 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#import <Foundation/Foundation.h>
#import "O2Defines_libtiff.h"
@class O2Decoder_TIFF;
@ -67,6 +68,9 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
double _yPosition;
double _yResolution;
unsigned _predictor;
#if LIBTIFF_PRESENT
int _idx;
#endif
}
-initWithTIFFReader:(O2Decoder_TIFF *)reader;

View File

@ -12,11 +12,197 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#import <Onyx2D/O2LZW.h>
#import <Foundation/NSDebug.h>
#import "O2Defines_libtiff.h"
#if LIBTIFF_PRESENT
#import <libtiff/include/tiffio.h>
// TIFF Reader from NSData memory
@interface O2TIFFReader : NSObject
{
NSData *_data;
toff_t _currentOffset;
}
- (id)initWithData:(NSData *)data;
// Data access methods for libtiff
- (int)write:(tdata_t)data size:(tsize_t)size;
- (int)read:(tdata_t)data size:(tsize_t)size;
- (toff_t)seek:(tsize_t)offset whence:(int)whence;
- (int)map:(tdata_t *)data size:(toff_t *)size;
- (void)unmap:(tdata_t)data size:(toff_t)size;
- (toff_t)size;
- (int)close;
@end
static tsize_t sTIFFWriteProc(thandle_t userData, tdata_t data, tsize_t size)
{
O2TIFFReader *reader = (O2TIFFReader *)userData;
return [reader write:data size:size];
}
static tsize_t sTIFFReadProc(thandle_t userData, tdata_t data, tsize_t size)
{
O2TIFFReader *reader = (O2TIFFReader *)userData;
return [reader read:data size:size];
}
static toff_t sTIFFSeekProc(thandle_t userData, toff_t offset, int whence)
{
O2TIFFReader *reader = (O2TIFFReader *)userData;
return [reader seek:offset whence:whence];
}
static int sTIFFCloseProc(thandle_t userData)
{
O2TIFFReader *reader = (O2TIFFReader *)userData;
return [reader close];
}
static toff_t sTIFFSizeProc(thandle_t userData)
{
O2TIFFReader *reader = (O2TIFFReader *)userData;
return [reader size];
}
static int sTIFFMapFileProc(thandle_t userData, tdata_t* data, toff_t* size)
{
O2TIFFReader *reader = (O2TIFFReader *)userData;
return [reader map:data size:size];
}
static void sTIFFUnmapFileProc(thandle_t userData, tdata_t data, toff_t size)
{
O2TIFFReader *reader = (O2TIFFReader *)userData;
[reader unmap:data size:size];
}
#endif
@implementation O2TIFFReader
- (id)initWithData:(NSData *)data
{
if ((self = [super init])) {
_data = [data retain];
}
return self;
}
- (void)dealloc
{
[_data release];
[super dealloc];
}
- (int)write:(tdata_t)data size:(tsize_t)size
{
return 0;
}
- (int)read:(tdata_t)data size:(tsize_t)size
{
int bytesCount = MIN(size, [_data length] - _currentOffset);
memcpy(data, [_data bytes]+_currentOffset, bytesCount);
_currentOffset += bytesCount;
return bytesCount;
}
-(toff_t)seek:(tsize_t)offset whence:(int)whence
{
/*
If whence is SEEK_SET, the offset is set to offset bytes.
If whence is SEEK_CUR, the offset is set to its current location plus offset bytes.
If whence is SEEK_END, the offset is set to the size of the file plus offset bytes.
*/
switch(whence) {
case SEEK_SET:
_currentOffset = offset;
break;
case SEEK_CUR:
_currentOffset += offset;
break;
case SEEK_END:
_currentOffset = [_data length] + offset - 1;
break;
}
if (_currentOffset > [_data length]) {
_currentOffset = -1;
}
if (_currentOffset < 0) {
_currentOffset = -1;
}
return _currentOffset;
}
- (int)map:(tdata_t *)data size:(toff_t *)size
{
*data = (tdata_t)[_data bytes];
*size = [_data length];
return 0;
}
- (void)unmap:(tdata_t)data size:(toff_t)size
{
}
-(toff_t)size
{
return [_data length];
}
-(int)close
{
return 0;
}
- (void)readImage:(void *)pixelBytes directory:(int)directory
{
const char *data = [_data bytes];
// Temporary disable warnings - we're getting too many at app launch
// and with a lot of tiff files
TIFFErrorHandler prevHandler = TIFFSetWarningHandler(NULL);
TIFF *tif = TIFFClientOpen([NSStringFromClass([self class]) UTF8String],
"r",
self,
sTIFFReadProc, sTIFFWriteProc,
sTIFFSeekProc,
sTIFFCloseProc, sTIFFSizeProc,
sTIFFMapFileProc,
sTIFFUnmapFileProc
);
uint32 width = 0;
uint32 height = 0;
TIFFSetDirectory(tif, directory);
if (TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width) != 0 &&
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height) != 0) {
// We want top-left orientation - libtiff default is bottom-left, which is giving flipped bitmaps
// libtiff is giving us pixels in the same order we need - so we can just fill the pixels using libtiff and
// no post-processing is needed
if (TIFFReadRGBAImageOriented(tif, width, height, pixelBytes, ORIENTATION_TOPLEFT, 1) == 0) {
NSLog(@"Couldn't not decode TIFF Image at index %d", directory);
}
} else {
NSLog(@"Couldn't not read TIFF Image dimensions at index %d", directory);
}
TIFFClose(tif);
TIFFSetWarningHandler(prevHandler);
}
@end
@implementation O2TIFFImageDirectory
-initWithTIFFReader:(O2Decoder_TIFF *)reader {
unsigned i,numberOfEntries=O2DecoderNextUnsigned16_TIFF(reader);
#if LIBTIFF_PRESENT
// Save the index for the current directory object
_idx = [[reader imageFileDirectory] count];
#endif
_xPosition=0;
_xResolution=72.0;
_yPosition=0;
@ -271,6 +457,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
return _imageWidth;
}
#if !LIBTIFF_PRESENT
static void decode_R8_G8_B8_A8(const unsigned char *stripBytes,unsigned byteCount,unsigned char *pixelBytes,int bytesPerRow,int *pixelBytesRowp,int height){
int pixelBytesRow=*pixelBytesRowp;
int pixelBytesCol=0;
@ -335,8 +522,17 @@ void depredict_R8G8B8A8(uint8_t *pixelBytes,unsigned bytesPerRow,unsigned height
pixelBytes+=bytesPerRow;
}
}
#endif
-(BOOL)getRGBAImageBytes:(unsigned char *)pixelBytes data:(NSData *)data {
#if LIBTIFF_PRESENT
// Note: it would be much more efficient to use LIBTIFF for all of the TIFF processing
// but we're doing minimal changes to the non-libtiff version - so we're just using LIBTIFF to do the image decoding
// for an image directory object (in order to support more image codecs than the one supported natively by cocotron)
O2TIFFReader *tiffReader = [[O2TIFFReader alloc] initWithData:data];
[tiffReader readImage:pixelBytes directory:_idx];
[tiffReader release];
#else
const unsigned char *bytes=[data bytes];
unsigned length=[data length];
unsigned strip,i;
@ -453,7 +649,7 @@ void depredict_R8G8B8A8(uint8_t *pixelBytes,unsigned bytesPerRow,unsigned height
NSLog(@"TIFF predictor error, floating point unsupported");
break;
}
#endif
return YES;
}

View File

@ -398,7 +398,7 @@ static HFONT Win32FontHandleWithName(NSString *name,int unitsPerEm){
-(Win32Font *)createGDIFontSelectedInDC:(HDC)dc pointSize:(CGFloat)pointSize angle:(CGFloat)angle {
if(_useMacMetrics){
if (pointSize <= 10.0)
pointSize=pointSize;
pointSize=pointSize;
else if (pointSize < 20.0)
pointSize=pointSize/(1.0 + 0.2*sqrtf(0.0390625*(pointSize - 10.0)));
else