make tiff2vips into a class

This commit is contained in:
John Cupitt 2011-12-05 17:39:11 +00:00
parent 4b1df2da35
commit 0b8c31f85e
13 changed files with 1872 additions and 123 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@ libvips-scan
libvips-scan.c
Makefile.in
TAGS
tags
*.o
.*.swp
*.lo

19
TODO
View File

@ -1,3 +1,22 @@
- try:
$ vips copy z.tif[page=12] babe.v
get a couple of leaked objects from the error
- make im_tiff2vips.c into a stub
- see vips_image_cache(): it currently writes to @out allocated by its caller
should this be called vips_image_write_cache()? or wrapped in the style
of vips_copy()?
- "header fred.png" does not work, since header uses im_open() which uses
VipsForeign

View File

@ -437,7 +437,7 @@ vips_tile_cache_init( VipsTileCache *cache )
}
/**
* vips_tile_cache:
* vips_tilecache:
* @in: input image
* @out: output image
* @tile_width: width of tiles in cache
@ -464,7 +464,7 @@ vips_tile_cache_init( VipsTileCache *cache )
* Returns: 0 on success, -1 on error.
*/
int
vips_tile_cache( VipsImage *in, VipsImage **out, ... )
vips_tilecache( VipsImage *in, VipsImage **out, ... )
{
va_list ap;
int result;

View File

@ -1,13 +1,16 @@
noinst_LTLIBRARIES = libforeign.la
libforeign_la_SOURCES = \
vips2jpeg.c \
tiff.h \
vips2tiff.c \
tiff2vips.c \
tiffload.c \
tiffsave.c \
vips2jpeg.c \
jpeg2vips.c \
jpeg.h \
jpegload.c \
jpegsave.c \
tiffsave.c \
vipssave.c \
vipsload.c \
foreign.c

View File

@ -1016,6 +1016,7 @@ vips_foreign_operation_init( void )
extern GType vips_foreign_save_jpeg_file_get_type( void );
extern GType vips_foreign_save_jpeg_buffer_get_type( void );
extern GType vips_foreign_save_jpeg_mime_get_type( void );
extern GType vips_foreign_load_tiff_get_type( void );
extern GType vips_foreign_save_tiff_get_type( void );
extern GType vips_foreign_load_vips_get_type( void );
extern GType vips_foreign_save_vips_get_type( void );
@ -1029,6 +1030,7 @@ vips_foreign_operation_init( void )
#endif /*HAVE_JPEG*/
#ifdef HAVE_TIFF
vips_foreign_load_tiff_get_type();
vips_foreign_save_tiff_get_type();
#endif /*HAVE_TIFF*/

View File

@ -80,10 +80,6 @@
typedef struct _VipsForeignLoadJpeg {
VipsForeignLoad parent_object;
/* Filename for load.
*/
char *filename;
/* Shrink by this much during load.
*/
int shrink;
@ -242,6 +238,7 @@ vips_foreign_load_jpeg_file_init( VipsForeignLoadJpegFile *file )
* @flags: image flags
* @shrink: shrink by this much on load
* @fail: fail on warnings
* @...: %NULL-terminated list of optional named arguments
*
* Read a JPEG file into a VIPS image. It can read most 8-bit JPEG images,
* including CMYK and YCbCr.
@ -373,6 +370,7 @@ vips_foreign_load_jpeg_buffer_init( VipsForeignLoadJpegBuffer *buffer )
* @buf: memory area to load
* @len: size of memory area
* @out: image to write
* @...: %NULL-terminated list of optional named arguments
*
* Read a JPEG-formatted memory block into a VIPS image. It can read most
* 8-bit JPEG images, including CMYK and YCbCr.

View File

@ -207,6 +207,7 @@ vips_foreign_save_jpeg_file_init( VipsForeignSaveJpegFile *file )
* @filename: file to write to
* @Q: quality factor
* @profile: attach this ICC profile
* @...: %NULL-terminated list of optional named arguments
*
* Write a VIPS image to a file as JPEG.
*
@ -316,6 +317,7 @@ vips_foreign_save_jpeg_buffer_init( VipsForeignSaveJpegBuffer *file )
* @len: return output length here
* @Q: JPEG quality factor
* @profile: attach this ICC profile
* @...: %NULL-terminated list of optional named arguments
*
* As vips_jpegsave(), but save to a memory buffer.
*
@ -412,6 +414,7 @@ vips_foreign_save_jpeg_mime_init( VipsForeignSaveJpegMime *mime )
* @in: image to save
* @Q: JPEG quality factor
* @profile: attach this ICC profile
* @...: %NULL-terminated list of optional named arguments
*
* As vips_jpegsave(), but save as a mime jpeg on stdout.
*

58
libvips/foreign/tiff.h Normal file
View File

@ -0,0 +1,58 @@
/* common defs for tiff read/write
*/
/*
Copyright (C) 1991-2005 The National Gallery
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
#ifndef VIPS_TIFF_H
#define VIPS_TIFF_H
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus*/
void vips__thandler_error( const char *module, const char *fmt, va_list ap );
void vips__thandler_warning( const char *module, const char *fmt, va_list ap );
int vips__tiff_write( VipsImage *in, const char *filename,
VipsForeignTiffCompression compression, int Q,
VipsForeignTiffPredictor predictor,
char *profile,
gboolean tile, int tile_width, int tile_height,
gboolean pyramid,
gboolean squash,
VipsForeignTiffResunit resunit, double xres, double yres,
gboolean bigtiff );
int vips__tiff_read( const char *filename, VipsImage *out, int page );
int vips__tiff_read_header( const char *filename, VipsImage *out, int page );
gboolean vips__istifftiled( const char *filename );
#ifdef __cplusplus
}
#endif /*__cplusplus*/
#endif /*VIPS_TIFF_H*/

1544
libvips/foreign/tiff2vips.c Normal file

File diff suppressed because it is too large Load Diff

195
libvips/foreign/tiffload.c Normal file
View File

@ -0,0 +1,195 @@
/* load tiff from a file
*
* 5/12/11
* - from tiffload.c
*/
/*
This file is part of VIPS.
VIPS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/*
#define DEBUG_VERBOSE
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vips/vips.h>
#include <vips/buf.h>
#include <vips/internal.h>
#include "tiff.h"
typedef struct _VipsForeignLoadTiff {
VipsForeignLoad parent_object;
/* Filename for load.
*/
char *filename;
/* Load this page.
*/
int page;
} VipsForeignLoadTiff;
typedef VipsForeignLoadClass VipsForeignLoadTiffClass;
G_DEFINE_TYPE( VipsForeignLoadTiff, vips_foreign_load_tiff,
VIPS_TYPE_FOREIGN_LOAD );
static gboolean
vips_foreign_load_tiff_is_a( const char *filename )
{
unsigned char buf[2];
if( vips__get_bytes( filename, buf, 2 ) )
if( (buf[0] == 'M' && buf[1] == 'M') ||
(buf[0] == 'I' && buf[1] == 'I') )
return( TRUE );
return( FALSE );
}
/* TIFF flags function.
*/
static VipsForeignFlags
vips_foreign_load_tiff_get_flags( VipsForeignLoad *load )
{
VipsForeignLoadTiff *tiff = (VipsForeignLoadTiff *) load;
VipsForeignFlags flags;
flags = 0;
if( vips__istifftiled( tiff->filename ) )
flags |= VIPS_FOREIGN_PARTIAL;
return( flags );
}
static int
vips_foreign_load_tiff_header( VipsForeignLoad *load )
{
VipsForeignLoadTiff *tiff = (VipsForeignLoadTiff *) load;
if( vips__tiff_read_header( tiff->filename, load->out, tiff->page ) )
return( -1 );
return( 0 );
}
static int
vips_foreign_load_tiff_load( VipsForeignLoad *load )
{
VipsForeignLoadTiff *tiff = (VipsForeignLoadTiff *) load;
if( vips__tiff_read( tiff->filename, load->real, tiff->page ) )
return( -1 );
return( 0 );
}
static const char *tiff_suffs[] = { ".tif", ".tiff", NULL };
static void
vips_foreign_load_tiff_class_init( VipsForeignLoadTiffClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "tiffload";
object_class->description = _( "load tiff from file" );
foreign_class->suffs = tiff_suffs;
load_class->is_a = vips_foreign_load_tiff_is_a;
load_class->get_flags = vips_foreign_load_tiff_get_flags;
load_class->header = vips_foreign_load_tiff_header;
load_class->load = vips_foreign_load_tiff_load;
VIPS_ARG_STRING( class, "filename", 1,
_( "Filename" ),
_( "Filename to load from" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsForeignLoadTiff, filename ),
NULL );
VIPS_ARG_INT( class, "page", 10,
_( "Page" ),
_( "Load this page from the file" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignLoadTiff, page ),
0, 100000, 0 );
}
static void
vips_foreign_load_tiff_init( VipsForeignLoadTiff *tiff )
{
}
/**
* vips_tiffload:
* @filename: file to load
* @out: decompressed image
* @page: load this page
* @...: %NULL-terminated list of optional named arguments
*
* Read a TIFF file into a VIPS image. It is a full baseline TIFF 6 reader,
* with extensions for tiled images, multipage images, LAB colour space,
* pyramidal images and JPEG compression. including CMYK and YCbCr.
*
* @page means load this page from the file. By default the first page (page
* 0) is read.
*
* Any ICC profile is read and attached to the VIPS image.
*
* See also: vips_image_new_from_file().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_tiffload( const char *filename, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "tiffload", ap, filename, out );
va_end( ap );
return( result );
}

View File

@ -46,6 +46,8 @@
#include <vips/vips.h>
#include "tiff.h"
typedef struct _VipsForeignSaveTiff {
VipsForeignSave parent_object;
@ -70,16 +72,6 @@ typedef struct _VipsForeignSaveTiff {
gboolean bigtiff;
} VipsForeignSaveTiff;
int vips__tiff_write( VipsImage *in, const char *filename,
VipsForeignTiffCompression compression, int Q,
VipsForeignTiffPredictor predictor,
char *profile,
gboolean tile, int tile_width, int tile_height,
gboolean pyramid,
gboolean squash,
VipsForeignTiffResunit resunit, double xres, double yres,
gboolean bigtiff );
typedef VipsForeignSaveClass VipsForeignSaveTiffClass;
G_DEFINE_TYPE( VipsForeignSaveTiff, vips_foreign_save_tiff,
@ -304,38 +296,19 @@ vips_foreign_save_tiff_init( VipsForeignSaveTiff *tiff )
* @xres; horizontal resolution
* @yres; vertical resolution
* @bigtiff; write a BigTiff file
* @...: %NULL-terminated list of optional named arguments
*
* Write a VIPS image to a file as TIFF.
*
* Use @compression to set the tiff compression. Currently jpeg, packbits,
* fax4, lzw, none and deflate are supported. The default is no compression.
* JPEG compression is a good lossy compressor for photographs, packbits is
* good for 1-bit images, and deflate is the best lossless compression TIFF
* can do. LZW has patent problems and is no longer recommended.
*
* Use @Q to set the JPEG compression factor. Default 75.
*
* Use @profile to give the filename of a profile to be em,bedded in the TIFF.
* This does not affect the pixels which are written, just the way
* they are tagged. You can use the special string "none" to mean
* "don't attach a profile".
*
* If no profile is specified and the VIPS header
* contains an ICC profile named VIPS_META_ICC_NAME ("icc-profile-data"), the
* profile from the VIPS header will be attached.
*
* You can embed options in the filename. They have the form:
*
* |[
* filename.tif:<emphasis>compression</emphasis>,<emphasis>layout</emphasis>,<emphasis>multi-res</emphasis>,<emphasis>format</emphasis>,<emphasis>resolution</emphasis>,<emphasis>icc</emphasis>, <emphasis>bigtiff</emphasis>
* ]|
*
* <itemizedlist>
* <listitem>
* <para>
* <emphasis>compression</emphasis>
* should be one of "none" (no compression), "jpeg" (JPEG compression),
* "deflate" (ZIP compression), "packbits" (TIFF packbits compression),
* "ccittfax4" (CCITT Group 4 fax encoding), "lzw" (Lempel-Ziv compression).
*
* "jpeg" compression can be followed by a ":" character and a JPEG quality
* level; "lzw" and "deflate" can be followed by a ":" and predictor value.
* The default compression type is "none", the default JPEG quality factor
* is 75.
* Use @predictor to set the predictor for lzw and deflate compression.
*
* Predictor is not set by default. There are three predictor values recognised
* at the moment (2007, July): 1 is no prediction, 2 is a horizontal
@ -345,81 +318,37 @@ vips_foreign_save_tiff_init( VipsForeignSaveTiff *tiff )
* photos or scanned images and bit depths > 8. Try it to find whether it
* works for your images.
*
* JPEG compression is a good lossy compressor for photographs, packbits is
* good for 1-bit images, and deflate is the best lossless compression TIFF
* can do. LZW has patent problems and is no longer recommended.
* </para>
* </listitem>
* <listitem>
* <para>
* <emphasis>layout</emphasis>
* should be "strip" (strip layout) or "tile" (tiled layout).
* Use @profile to give the filename of a profile to be embedded in the TIFF.
* This does not affect the pixels which are written, just the way
* they are tagged. You can use the special string "none" to mean
* "don't attach a profile".
*
* "tile" layout can be followed by a ":" character and the horizontal and
* vertical tile size, separated by a "x" character. The default layout is
* "strip", and the default tile size is 128 by 128 pixels.
* </para>
* </listitem>
* <listitem>
* <para>
* <emphasis>multi-res</emphasis>
* should be "flat" (single image) or "pyramid" (many images arranged in a
* pyramid). The default multi-res mode is "flat".
* </para>
* </listitem>
* <listitem>
* <para>
* <emphasis>format</emphasis>
* shoiuld be "manybit" (don't bit-reduce images) or "onebit" (one band 8
* bit images are saved as 1 bit). The default format is "multibit".
* </para>
* </listitem>
* <listitem>
* <para>
* <emphasis>resolution</emphasis>
* should be "res_cm" (output resolution unit is pixels per centimetre) or
* "res_inch" (output resolution unit is pixels per inch). The default
* If no profile is specified and the VIPS header
* contains an ICC profile named VIPS_META_ICC_NAME ("icc-profile-data"), the
* profile from the VIPS header will be attached.
*
* Set @tile to TRUE to write a tiled tiff. By default tiff are written in
* strips. Use @tile_width and @tile_height to set the tile size. The defaiult
* is 128 by 128.
*
* Set @pyramid to write the image as a set of images, one per page, of
* decreasing size.
*
* Set @squash to make 8-bit uchar images write as 1-bit TIFFs with zero
* pixels written as 0 and non-zero as 1.
*
* Use @resunit to override the default resolution unit.
* The default
* resolution unit is taken from the header field "resolution-unit"
* (#IM_META_RESOLUTION_UNIT in C). If this field is not set, then
* (#VIPS_META_RESOLUTION_UNIT in C). If this field is not set, then
* VIPS defaults to cm.
*
* The unit can optionally be followed by a ":" character and the
* horizontal and vertical resolution, separated by a "x" character.
* You can have a single number with no "x" and set the horizontal and
* vertical resolutions together.
* </para>
* </listitem>
* <listitem>
* <para>
* <emphasis>icc</emphasis>
* Attach this ICC profile.
* This does not affect the pixels which are written, just the way
* they are tagged.
* </para>
* </listitem>
* <listitem>
* <para>
* <emphasis>bigtiff</emphasis>
* Set this to 8 to enable bigtiff output. Bigtiff is a variant of the TIFF
* Use @xres and @yres to override the default horizontal and vertical
* resolutions. By default these values are taken from the VIPS image header.
*
* Set @bigtiff to attempt to write a bigtiff.
* Bigtiff is a variant of the TIFF
* format that allows more than 4GB in a file.
* </para>
* </listitem>
* </itemizedlist>
*
* Example:
*
* |[
* im_vips2jpeg( in, "fred.tif:jpeg,tile,pyramid" );
* ]|
*
* Will write "fred.tif" as a tiled jpeg-compressed pyramid.
*
* |[
* im_vips2jpeg( in, "fred.tif:packbits,tile,,onebit" );
* ]|
*
* Writes a tiled one bit TIFF image (provided fred.v is a one band 8 bit
* image) compressed with packbits.
*
* See also: vips_tiffload(), vips_image_write_file().
*

View File

@ -175,6 +175,8 @@
#include <tiffio.h>
#include "tiff.h"
/* Max no of tiles we buffer in a layer. Enough to buffer a line of 64x64
* tiles on a 100k pixel across image.
*/
@ -249,11 +251,6 @@ typedef struct tiff_write {
GMutex *write_lock; /* Lock TIFF*() calls with this */
} TiffWrite;
/* Use these from im_tiff2vips().
*/
void im__thandler_error( char *module, char *fmt, va_list ap );
void im__thandler_warning( char *module, char *fmt, va_list ap );
/* Open TIFF for output.
*/
static TIFF *
@ -1452,8 +1449,8 @@ vips__tiff_write( VipsImage *in, const char *filename,
/* Override the default TIFF error handler.
*/
TIFFSetErrorHandler( (TIFFErrorHandler) im__thandler_error );
TIFFSetWarningHandler( (TIFFErrorHandler) im__thandler_warning );
TIFFSetErrorHandler( vips__thandler_error );
TIFFSetWarningHandler( vips__thandler_warning );
/* Check input image.
*/

View File

@ -137,7 +137,7 @@ typedef enum {
int vips_copy( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));
int vips_tile_cache( VipsImage *in, VipsImage **out, ... )
int vips_tilecache( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));
int vips_embed( VipsImage *in, VipsImage **out,
int x, int y, int width, int height, ... )