76 const unsigned char *
mem;
82 #define IMB_TIFF_GET_MEMFILE(x) ((ImbTIFFMemFile *)(x))
128 tsize_t nRemaining, nCopy;
134 if (!mfile || !mfile->
mem) {
135 fprintf(stderr,
"imb_tiff_ReadProc: !mfile || !mfile->mem!\n");
148 if (nCopy > nRemaining) {
158 srcAddr = (
void *)(&(mfile->
mem[mfile->
offset]));
159 memcpy((
void *)
data, srcAddr, nCopy);
176 printf(
"imb_tiff_WriteProc: this function should not be called.\n");
201 if (!mfile || !mfile->
mem) {
202 fprintf(stderr,
"imb_tiff_SeekProc: !mfile || !mfile->mem!\n");
212 new_offset = mfile->
offset + ofs;
217 "imb_tiff_SeekProc: "
218 "Unsupported TIFF SEEK type.\n");
223 mfile->
offset = new_offset;
245 if (!mfile || !mfile->
mem) {
246 fprintf(stderr,
"imb_tiff_CloseProc: !mfile || !mfile->mem!\n");
269 if (!mfile || !mfile->
mem) {
270 fprintf(stderr,
"imb_tiff_SizeProc: !mfile || !mfile->mem!\n");
275 return (toff_t)(mfile->
size);
285 return TIFFClientOpen(
"(Blender TIFF Interface Layer)",
287 (thandle_t)(memFile),
318 #define IMB_TIFF_NCB 4
321 const char big_endian[
IMB_TIFF_NCB] = {0x4d, 0x4d, 0x00, 0x2a};
322 const char lil_endian[
IMB_TIFF_NCB] = {0x49, 0x49, 0x2a, 0x00};
332 const unsigned short *sbuf,
337 for (i = 0; i < scanline_w; i++) {
338 rectf[i * 4 + 0] = sbuf[i * spp + 0] / 65535.0;
339 rectf[i * 4 + 1] = (spp >= 3) ? sbuf[i * spp + 1] / 65535.0 : sbuf[i * spp + 0] / 65535.0;
340 rectf[i * 4 + 2] = (spp >= 3) ? sbuf[i * spp + 2] / 65535.0 : sbuf[i * spp + 0] / 65535.0;
341 rectf[i * 4 + 3] = (spp == 4) ? (sbuf[i * spp + 3] / 65535.0) : 1.0;
348 for (i = 0; i < scanline_w; i++) {
349 rectf[i * 4 + 0] = fbuf[i * spp + 0];
350 rectf[i * 4 + 1] = (spp >= 3) ? fbuf[i * spp + 1] : fbuf[i * spp + 0];
351 rectf[i * 4 + 2] = (spp >= 3) ? fbuf[i * spp + 2] : fbuf[i * spp + 0];
352 rectf[i * 4 + 3] = (spp == 4) ? fbuf[i * spp + 3] : 1.0f;
357 const unsigned short *sbuf,
362 for (i = 0; i < scanline_w; i++) {
363 rectf[i * 4 + chan] = sbuf[i] / 65535.0;
370 for (i = 0; i < scanline_w; i++) {
371 rectf[i * 4 + chan] = fbuf[i];
381 TIFFGetFieldDefaulted(image, TIFFTAG_RESOLUTIONUNIT, &unit);
382 TIFFGetFieldDefaulted(image, TIFFTAG_XRESOLUTION, &xres);
383 TIFFGetFieldDefaulted(image, TIFFTAG_YRESOLUTION, &yres);
385 if (unit == RESUNIT_CENTIMETER) {
404 short bitspersample, spp, config;
406 int ib_flag = 0, row, chan;
408 unsigned short *sbuf =
NULL;
410 TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bitspersample);
411 TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp);
412 TIFFGetField(image, TIFFTAG_PLANARCONFIG, &config);
426 unsigned short extraSampleTypes[1];
427 extraSampleTypes[0] = EXTRASAMPLE_ASSOCALPHA;
428 TIFFSetField(image, TIFFTAG_EXTRASAMPLES, 1, extraSampleTypes);
433 scanline = TIFFScanlineSize(image);
435 if (bitspersample == 32) {
437 fbuf = (
float *)_TIFFmalloc(scanline);
442 else if (bitspersample == 16) {
444 sbuf = (
unsigned short *)_TIFFmalloc(scanline);
459 if (!(bitspersample == 32 || bitspersample == 16)) {
460 success |= TIFFReadRGBAImage(image, ibuf->
x, ibuf->
y, tmpibuf->
rect, 0);
463 else if (config == PLANARCONFIG_CONTIG) {
464 for (row = 0; row < ibuf->
y; row++) {
465 size_t ib_offset = (size_t)ibuf->
x * 4 * ((
size_t)ibuf->
y - ((size_t)row + 1));
467 if (bitspersample == 32) {
468 success |= TIFFReadScanline(image, fbuf, row, 0);
471 else if (bitspersample == 16) {
472 success |= TIFFReadScanline(image, sbuf, row, 0);
478 else if (config == PLANARCONFIG_SEPARATE) {
482 for (chan = 0; chan < 4; chan++) {
483 for (row = 0; row < ibuf->
y; row++) {
484 size_t ib_offset = (size_t)ibuf->
x * 4 * ((
size_t)ibuf->
y - ((size_t)row + 1));
486 if (bitspersample == 32) {
487 if (chan == 3 && spp == 3) {
490 else if (chan >= spp) {
491 success |= TIFFReadScanline(image, fbuf, row, 0);
494 success |= TIFFReadScanline(image, fbuf, row, chan);
498 else if (bitspersample == 16) {
499 if (chan == 3 && spp == 3) {
502 else if (chan >= spp) {
503 success |= TIFFReadScanline(image, fbuf, row, 0);
506 success |= TIFFReadScanline(image, sbuf, row, chan);
516 if (bitspersample < 16) {
529 ibuf->
mall |= ib_flag;
530 ibuf->
flags |= ib_flag;
532 tmpibuf->
mall &= ~ib_flag;
536 if (bitspersample == 32) {
539 else if (bitspersample == 16) {
551 TIFFSetErrorHandler(
NULL);
590 printf(
"imb_loadtiff: could not open TIFF IO layer.\n");
595 TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &
width);
596 TIFFGetField(image, TIFFTAG_IMAGELENGTH, &
height);
597 TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp);
599 ib_depth = (spp == 3) ? 24 : 32;
603 ibuf->
ftype = IMB_FTYPE_TIF;
607 "imb_loadtiff: could not allocate memory for TIFF "
616 unsigned short extra, *extraSampleTypes;
618 found = TIFFGetField(image, TIFFTAG_EXTRASAMPLES, &extra, &extraSampleTypes);
620 if (found && (extraSampleTypes[0] == EXTRASAMPLE_ASSOCALPHA)) {
636 TIFFGetField(image, TIFFTAG_PIXAR_TEXTUREFORMAT, &
format);
639 int numlevel = TIFFNumberOfDirectories(image);
642 for (level = 0; level < numlevel; level++) {
643 if (!TIFFSetDirectory(image, level)) {
652 hbuf->miplevel = level;
653 hbuf->ftype = ibuf->
ftype;
654 ibuf->
mipmap[level - 1] = hbuf;
662 TIFFGetField(image, TIFFTAG_TILEWIDTH, &hbuf->tilex);
663 TIFFGetField(image, TIFFTAG_TILELENGTH, &hbuf->tiley);
665 hbuf->xtiles =
ceil(hbuf->x / (
float)hbuf->tilex);
666 hbuf->ytiles =
ceil(hbuf->y / (
float)hbuf->tiley);
677 fprintf(stderr,
"imb_loadtiff: Failed to read tiff image.\n");
690 ImBuf *ibuf,
const unsigned char *mem,
size_t size,
int tx,
int ty,
unsigned int *rect)
699 printf(
"imb_loadtiff: could not open TIFF IO layer for loading mipmap level.\n");
703 if (TIFFSetDirectory(image, ibuf->
miplevel)) {
704 TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &
width);
705 TIFFGetField(image, TIFFTAG_IMAGELENGTH, &
height);
710 if (TIFFReadRGBATile(
711 image, tx * ibuf->
tilex, (ibuf->
ytiles - 1 - ty) * ibuf->
tiley, rect) == 1) {
712 if (ibuf->
tiley > ibuf->
y) {
715 sizeof(
int) * ibuf->
tilex * ibuf->
y);
719 printf(
"imb_loadtiff: failed to read tiff tile at mipmap level %d\n", ibuf->
miplevel);
724 printf(
"imb_loadtiff: mipmap level %d has unexpected size %ux%u instead of %dx%d\n",
733 printf(
"imb_loadtiff: could not find mipmap level %d\n", ibuf->
miplevel);
764 uint16 samplesperpixel, bitspersample;
766 unsigned char *pixels =
NULL;
768 unsigned short *pixels16 =
NULL, *to16 =
NULL;
771 int x,
y, from_i, to_i, i;
772 int compress_mode = COMPRESSION_NONE;
778 if ((samplesperpixel > 4) || (samplesperpixel == 2)) {
780 "imb_savetiff: unsupported number of bytes per "
794 compress_mode = COMPRESSION_DEFLATE;
797 compress_mode = COMPRESSION_LZW;
800 compress_mode = COMPRESSION_PACKBITS;
807 "imb_savetiff: creation of in-memory TIFF files is "
808 "not yet supported.\n");
815 image = TIFFOpenW(wname,
"w");
818 image = TIFFOpen(filepath,
"w");
822 fprintf(stderr,
"imb_savetiff: could not open TIFF for writing.\n");
827 npixels = ibuf->
x * ibuf->
y;
828 if (bitspersample == 16) {
829 pixels16 = (
unsigned short *)_TIFFmalloc(npixels * samplesperpixel *
sizeof(
unsigned short));
832 pixels = (
unsigned char *)_TIFFmalloc(npixels * samplesperpixel *
sizeof(
unsigned char));
835 if (pixels ==
NULL && pixels16 ==
NULL) {
836 fprintf(stderr,
"imb_savetiff: could not allocate pixels array.\n");
842 if (bitspersample == 16) {
852 TIFFSetField(image, TIFFTAG_BITSPERSAMPLE, bitspersample);
853 TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
855 if (samplesperpixel == 4) {
856 unsigned short extraSampleTypes[1];
858 if (bitspersample == 16) {
859 extraSampleTypes[0] = EXTRASAMPLE_ASSOCALPHA;
862 extraSampleTypes[0] = EXTRASAMPLE_UNASSALPHA;
866 TIFFSetField(image, TIFFTAG_EXTRASAMPLES, 1, extraSampleTypes);
867 TIFFSetField(image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
869 else if (samplesperpixel == 3) {
871 TIFFSetField(image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
873 else if (samplesperpixel == 1) {
875 TIFFSetField(image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
880 for (
x = 0;
x < ibuf->
x;
x++) {
881 for (
y = 0;
y < ibuf->
y;
y++) {
882 from_i = ((size_t)channels_in_float) * (
y * ibuf->
x +
x);
883 to_i = samplesperpixel * ((ibuf->
y -
y - 1) * ibuf->
x +
x);
889 if (
ELEM(channels_in_float, 3, 4)) {
902 if (channels_in_float == 4) {
903 rgb[3] = fromf[from_i + 3];
911 rgb[0] = fromf[from_i];
916 rgb[1] = rgb[2] = rgb[0];
920 for (i = 0; i < samplesperpixel; i++, to_i++) {
925 for (i = 0; i < samplesperpixel; i++, to_i++, from_i++) {
926 to[to_i] =
from[from_i];
933 TIFFSetField(image, TIFFTAG_IMAGEWIDTH, ibuf->
x);
934 TIFFSetField(image, TIFFTAG_IMAGELENGTH, ibuf->
y);
935 TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, ibuf->
y);
936 TIFFSetField(image, TIFFTAG_COMPRESSION, compress_mode);
937 TIFFSetField(image, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
938 TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
940 if (ibuf->
ppm[0] > 0.0 && ibuf->
ppm[1] > 0.0) {
941 xres = (
float)(ibuf->
ppm[0] * 0.0254);
942 yres = (
float)(ibuf->
ppm[1] * 0.0254);
948 TIFFSetField(image, TIFFTAG_XRESOLUTION, xres);
949 TIFFSetField(image, TIFFTAG_YRESOLUTION, yres);
950 TIFFSetField(image, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
951 if (TIFFWriteEncodedStrip(image,
953 (bitspersample == 16) ? (
unsigned char *)pixels16 : pixels,
954 (
size_t)ibuf->
x * ibuf->
y * samplesperpixel * bitspersample / 8) ==
956 fprintf(stderr,
"imb_savetiff: Could not write encoded TIFF.\n");
typedef float(TangentPoint)[2]
void BLI_kdtree_nd_() free(KDTree *tree)
MINLINE void linearrgb_to_srgb_v3_v3(float srgb[3], const float linear[3])
float linearrgb_to_srgb(float c)
void copy_vn_fl(float *array_tar, const int size, const float val)
void copy_vn_ushort(unsigned short *array_tar, const int size, const unsigned short val)
MINLINE void copy_v3_v3(float r[3], const float a[3])
typedef double(DMatrix)[4][4]
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
@ COLOR_ROLE_DEFAULT_BYTE
struct ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
void IMB_freeImBuf(struct ImBuf *ibuf)
bool imb_addtilesImBuf(struct ImBuf *ibuf)
void IMB_convert_rgba_to_abgr(struct ImBuf *ibuf)
Contains defines and structs used throughout the imbuf module.
@ IMB_COLORMANAGE_IS_DATA
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
void colorspace_set_default_role(char *colorspace, int size, int role)
MINLINE unsigned short unit_float_to_ushort_clamp(float val)
struct ImBuf * mipmap[IMB_MIPMAP_LEVELS]
ImbFormatOptions foptions
struct ColorSpace * float_colorspace
const unsigned char * mem
bool imb_is_a_tiff(const unsigned char *buf, size_t size)
static int imb_tiff_DummyMapProc(thandle_t fd, tdata_t *pbase, toff_t *psize)
#define IMB_TIFF_GET_MEMFILE(x)
static toff_t imb_tiff_SeekProc(thandle_t handle, toff_t ofs, int whence)
static tsize_t imb_tiff_WriteProc(thandle_t handle, tdata_t data, tsize_t n)
struct ImbTIFFMemFile ImbTIFFMemFile
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 toff_t imb_tiff_SizeProc(thandle_t handle)
void imb_loadtiletiff(ImBuf *ibuf, const unsigned char *mem, size_t size, int tx, int ty, unsigned int *rect)
ImBuf * imb_loadtiff(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
static TIFF * imb_tiff_client_open(ImbTIFFMemFile *memFile, const unsigned char *mem, size_t size)
static void scanline_separate_32bit(float *rectf, const float *fbuf, int scanline_w, int chan)
bool imb_savetiff(ImBuf *ibuf, const char *filepath, int flags)
static tsize_t imb_tiff_ReadProc(thandle_t handle, tdata_t data, tsize_t n)
static void imb_tiff_DummyUnmapProc(thandle_t fd, tdata_t base, toff_t size)
static int imb_read_tiff_pixels(ImBuf *ibuf, TIFF *image)
static int imb_tiff_CloseProc(thandle_t handle)
static void imb_read_tiff_resolution(ImBuf *ibuf, TIFF *image)
static void scanline_contig_16bit(float *rectf, const unsigned short *sbuf, int scanline_w, int spp)
wchar_t * alloc_utf16_from_8(const char *in8, size_t add)
ccl_device_inline float3 ceil(const float3 &a)