Blender  V3.3
Classes
tiff.c File Reference
#include <string.h>
#include "imbuf.h"
#include "BLI_endian_defines.h"
#include "BLI_math.h"
#include "BLI_utildefines.h"
#include "BKE_global.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
#include "IMB_filetype.h"
#include "IMB_colormanagement.h"
#include "IMB_colormanagement_intern.h"
#include <tiffio.h>

Go to the source code of this file.

Classes

struct  ImbTIFFMemFile
 

Functions

Function Implementations
static TIFF * imb_tiff_client_open (ImbTIFFMemFile *memFile, const unsigned char *mem, size_t size)
 
Save TIFF
bool imb_savetiff (ImBuf *ibuf, const char *filepath, int flags)
 

Local Declarations

Provides TIFF file loading and saving for Blender, via libtiff.

The task of loading is complicated somewhat by the fact that Blender has already loaded the file into a memory buffer. libtiff is not well configured to handle files in memory, so a client wrapper is written to surround the memory and turn it into a virtual file. Currently, reading of TIFF files is done using libtiff's RGBAImage support. This is a high-level routine that loads all images as 32-bit RGBA, handling all the required conversions between many different TIFF types internally.

Saving supports RGB, RGBA and BW (gray-scale) images correctly, with 8 bits per channel in all cases. The "deflate" compression algorithm is used to compress images.

#define IMB_TIFF_GET_MEMFILE(x)   ((ImbTIFFMemFile *)(x))
 
typedef struct ImbTIFFMemFile ImbTIFFMemFile
 
static tsize_t imb_tiff_ReadProc (thandle_t handle, tdata_t data, tsize_t n)
 
static tsize_t imb_tiff_WriteProc (thandle_t handle, tdata_t data, tsize_t n)
 
static toff_t imb_tiff_SeekProc (thandle_t handle, toff_t ofs, int whence)
 
static int imb_tiff_CloseProc (thandle_t handle)
 
static toff_t imb_tiff_SizeProc (thandle_t handle)
 
static int imb_tiff_DummyMapProc (thandle_t fd, tdata_t *pbase, toff_t *psize)
 
static void imb_tiff_DummyUnmapProc (thandle_t fd, tdata_t base, toff_t size)
 

Load TIFF

#define IMB_TIFF_NCB   4 /* number of comparison bytes used */
 
bool imb_is_a_tiff (const unsigned char *buf, size_t size)
 
static void scanline_contig_16bit (float *rectf, const unsigned short *sbuf, int scanline_w, int spp)
 
static void scanline_contig_32bit (float *rectf, const float *fbuf, int scanline_w, int spp)
 
static void scanline_separate_16bit (float *rectf, const unsigned short *sbuf, int scanline_w, int chan)
 
static void scanline_separate_32bit (float *rectf, const float *fbuf, int scanline_w, int chan)
 
static void imb_read_tiff_resolution (ImBuf *ibuf, TIFF *image)
 
static int imb_read_tiff_pixels (ImBuf *ibuf, TIFF *image)
 
void imb_inittiff (void)
 
ImBufimb_loadtiff (const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
 
void imb_loadtiletiff (ImBuf *ibuf, const unsigned char *mem, size_t size, int tx, int ty, unsigned int *rect)
 

Macro Definition Documentation

◆ IMB_TIFF_GET_MEMFILE

#define IMB_TIFF_GET_MEMFILE (   x)    ((ImbTIFFMemFile *)(x))

Definition at line 69 of file tiff.c.

◆ IMB_TIFF_NCB

#define IMB_TIFF_NCB   4 /* number of comparison bytes used */

Checks whether a given memory buffer contains a TIFF file.

This method uses the format identifiers from: http://www.faqs.org/faqs/graphics/fileformats-faq/part4/section-9.html The first four bytes of big-endian and little-endian TIFF files respectively are (hex): 4d 4d 00 2a 49 49 2a 00 Note that TIFF files on any platform can be either big- or little-endian; it's not platform-specific.

AFAICT, libtiff doesn't provide a method to do this automatically, and hence my manual comparison. - Jonathan Merritt (lancelet) 4th Sept 2005.

Definition at line 305 of file tiff.c.

Typedef Documentation

◆ ImbTIFFMemFile

Structure for in-memory TIFF file.

Function Documentation

◆ imb_inittiff()

void imb_inittiff ( void  )

Definition at line 535 of file tiff.c.

References G, G_DEBUG, and NULL.

◆ imb_is_a_tiff()

bool imb_is_a_tiff ( const unsigned char *  buf,
size_t  size 
)

Definition at line 306 of file tiff.c.

References IMB_TIFF_NCB, and size().

Referenced by imb_loadtiff().

◆ imb_loadtiff()

ImBuf* imb_loadtiff ( const unsigned char *  mem,
size_t  size,
int  flags,
char  colorspace[IM_MAX_SPACE] 
)

Loads a TIFF file.

Parameters
memMemory containing the TIFF file.
sizeSize of the mem buffer.
flagsIf flags has IB_test set then the file is not actually loaded, but all other operations take place.
Returns
A newly allocated ImBuf structure if successful, otherwise NULL.

Definition at line 542 of file tiff.c.

References ceil(), COLOR_ROLE_DEFAULT_BYTE, colorspace_set_default_role(), ImBuf::flags, ImBuf::ftype, height, IB_alphamode_detect, IB_alphamode_premul, IB_test, IB_tilecache, IM_MAX_SPACE, image(), imb_addtilesImBuf(), IMB_allocImBuf(), imb_is_a_tiff(), imb_read_tiff_pixels(), imb_tiff_client_open(), ImBuf::mipmap, ImBuf::miptot, NULL, size(), STREQ, and width.

◆ imb_loadtiletiff()

void imb_loadtiletiff ( ImBuf ibuf,
const unsigned char *  mem,
size_t  size,
int  tx,
int  ty,
unsigned int *  rect 
)

◆ imb_read_tiff_pixels()

static int imb_read_tiff_pixels ( ImBuf ibuf,
TIFF *  image 
)
static

◆ imb_read_tiff_resolution()

static void imb_read_tiff_resolution ( ImBuf ibuf,
TIFF *  image 
)
static

Definition at line 362 of file tiff.c.

References double(), image(), and ImBuf::ppm.

Referenced by imb_read_tiff_pixels().

◆ imb_savetiff()

bool imb_savetiff ( struct ImBuf ibuf,
const char *  filepath,
int  flags 
)

Saves a TIFF file.

ImBuf structures with 1, 3 or 4 bytes per pixel (GRAY, RGB, RGBA respectively) are accepted, and interpreted correctly. Note that the TIFF convention is to use pre-multiplied alpha, which can be achieved within Blender by setting premul alpha handling. Other alpha conventions are not strictly correct, but are permitted anyhow.

Parameters
ibufImage buffer.
filepathName of the TIFF file to create.
flagsCurrently largely ignored.
Returns
1 if the function is successful, 0 on failure.

Definition at line 724 of file tiff.c.

References alloc_utf16_from_8(), ImBuf::channels, ImBuf::colormanage_flag, copy_v3_v3(), ELEM, ImbFormatOptions::flag, float(), ImBuf::float_colorspace, ImBuf::foptions, free(), from, IB_mem, image(), IMB_COLORMANAGE_IS_DATA, IMB_DPI_DEFAULT, linearrgb_to_srgb(), linearrgb_to_srgb_v3_v3(), NULL, ImBuf::planes, ImBuf::ppm, ImBuf::rect, ImBuf::rect_float, usdtokens::rgb(), unit_float_to_ushort_clamp(), x, ImBuf::x, y, and ImBuf::y.

◆ imb_tiff_client_open()

static TIFF* imb_tiff_client_open ( ImbTIFFMemFile memFile,
const unsigned char *  mem,
size_t  size 
)
static

◆ imb_tiff_CloseProc()

static int imb_tiff_CloseProc ( thandle_t  handle)
static

Closes (virtually) an in-memory TIFF file.

NOTE: All this function actually does is sets the data pointer within the TIFF file to NULL. That should trigger assertion errors if attempts are made to access the file after that point. However, no such attempts should ever be made (in theory).

Parameters
handleHandle of the TIFF file (pointer to ImbTIFFMemFile).
Returns
0

Definition at line 226 of file tiff.c.

References IMB_TIFF_GET_MEMFILE, ImbTIFFMemFile::mem, NULL, ImbTIFFMemFile::offset, and ImbTIFFMemFile::size.

Referenced by imb_tiff_client_open().

◆ imb_tiff_DummyMapProc()

static int imb_tiff_DummyMapProc ( thandle_t  fd,
tdata_t *  pbase,
toff_t *  psize 
)
static

Definition at line 89 of file tiff.c.

References void.

Referenced by imb_tiff_client_open().

◆ imb_tiff_DummyUnmapProc()

static void imb_tiff_DummyUnmapProc ( thandle_t  fd,
tdata_t  base,
toff_t  size 
)
static

Definition at line 77 of file tiff.c.

References size(), and void.

Referenced by imb_tiff_client_open().

◆ imb_tiff_ReadProc()

static tsize_t imb_tiff_ReadProc ( thandle_t  handle,
tdata_t  data,
tsize_t  n 
)
static

Reads data from an in-memory TIFF file.

Parameters
handleHandle of the TIFF file (pointer to ImbTIFFMemFile).
dataBuffer to contain data (treat as (void *)).
nNumber of bytes to read.
Returns
Number of bytes actually read. 0 = EOF.

Definition at line 113 of file tiff.c.

References data, IMB_TIFF_GET_MEMFILE, ImbTIFFMemFile::mem, ImbTIFFMemFile::offset, and ImbTIFFMemFile::size.

Referenced by imb_tiff_client_open().

◆ imb_tiff_SeekProc()

static toff_t imb_tiff_SeekProc ( thandle_t  handle,
toff_t  ofs,
int  whence 
)
static

Seeks to a new location in an in-memory TIFF file.

Parameters
handleHandle of the TIFF file (pointer to ImbTIFFMemFile).
ofsOffset value (interpreted according to whence below).
whenceThis can be one of three values: SEEK_SET - The offset is set to ofs bytes. SEEK_CUR - The offset is set to its current location plus ofs bytes. SEEK_END - (This is unsupported and will return -1, indicating an error).
Returns
Resulting offset location within the file, measured in bytes from the beginning of the file. (-1) indicates an error.

Definition at line 181 of file tiff.c.

References IMB_TIFF_GET_MEMFILE, ImbTIFFMemFile::mem, and ImbTIFFMemFile::offset.

Referenced by imb_tiff_client_open().

◆ imb_tiff_SizeProc()

static toff_t imb_tiff_SizeProc ( thandle_t  handle)
static

Returns the size of an in-memory TIFF file in bytes.

Returns
Size of file (in bytes).

Definition at line 250 of file tiff.c.

References IMB_TIFF_GET_MEMFILE, ImbTIFFMemFile::mem, and ImbTIFFMemFile::size.

Referenced by imb_tiff_client_open().

◆ imb_tiff_WriteProc()

static tsize_t imb_tiff_WriteProc ( thandle_t  handle,
tdata_t  data,
tsize_t  n 
)
static

Writes data to an in-memory TIFF file.

NOTE: The current Blender implementation should not need this function. It is simply a stub.

Definition at line 157 of file tiff.c.

References data, and void.

Referenced by imb_tiff_client_open().

◆ scanline_contig_16bit()

static void scanline_contig_16bit ( float rectf,
const unsigned short *  sbuf,
int  scanline_w,
int  spp 
)
static

Definition at line 318 of file tiff.c.

Referenced by imb_read_tiff_pixels().

◆ scanline_contig_32bit()

static void scanline_contig_32bit ( float rectf,
const float fbuf,
int  scanline_w,
int  spp 
)
static

Definition at line 332 of file tiff.c.

Referenced by imb_read_tiff_pixels().

◆ scanline_separate_16bit()

static void scanline_separate_16bit ( float rectf,
const unsigned short *  sbuf,
int  scanline_w,
int  chan 
)
static

Definition at line 343 of file tiff.c.

Referenced by imb_read_tiff_pixels().

◆ scanline_separate_32bit()

static void scanline_separate_32bit ( float rectf,
const float fbuf,
int  scanline_w,
int  chan 
)
static

Definition at line 354 of file tiff.c.

Referenced by imb_read_tiff_pixels().