summaryrefslogtreecommitdiff
path: root/src/tiff_binfile.c
blob: e782b94a214682a2bff83c70e3bde59bcd85fcac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/** \file
 * \brief libTIFF I/O and error handlers.
 * I/O uses imBinFile instead of libTIFF original handlers.
 *
 * See Copyright Notice in im_lib.h
 * $Id: tiff_binfile.c,v 1.1 2008/10/17 06:10:16 scuri Exp $
 */

#include "tiffiop.h"

#include "im_binfile.h"

#include <stdlib.h>
#include <memory.h>

static tsize_t iTIFFReadProc(thandle_t fd, tdata_t buf, tsize_t size)
{
  imBinFile* file_bin = (imBinFile*)fd;
  return imBinFileRead(file_bin, buf, size, 1);
}

static tsize_t iTIFFWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
{
  imBinFile* file_bin = (imBinFile*)fd;
  return imBinFileWrite(file_bin, buf, size, 1);
}

static toff_t iTIFFSeekProc(thandle_t fd, toff_t off, int whence)
{
  imBinFile* file_bin = (imBinFile*)fd;
  switch (whence)
  {
  case SEEK_SET:
    imBinFileSeekTo(file_bin, off);
    break;
  case SEEK_CUR:
    imBinFileSeekOffset(file_bin, off);
    break;
  case SEEK_END: 
    imBinFileSeekFrom(file_bin, off);
    break;
  }

  return imBinFileTell(file_bin);
}

static int iTIFFCloseProc(thandle_t fd)
{
  imBinFile* file_bin = (imBinFile*)fd;
  imBinFileClose(file_bin);
  return 0;
}

static toff_t iTIFFSizeProc(thandle_t fd)
{
  imBinFile* file_bin = (imBinFile*)fd;
  return imBinFileSize(file_bin);
}

static int iTIFFMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
{
  (void) fd; (void) pbase; (void) psize;
  return (0);
}

static void iTIFFUnmapProc(thandle_t fd, tdata_t base, toff_t size)
{
  (void) fd; (void) base; (void) size;
}

TIFF* TIFFFdOpen(int fd, const char* name, const char* mode)
{
  TIFF* tif;

  tif = TIFFClientOpen(name, mode, (thandle_t) fd,  iTIFFReadProc, iTIFFWriteProc,
                                                    iTIFFSeekProc, iTIFFCloseProc, 
                                                    iTIFFSizeProc, iTIFFMapProc, 
                                                    iTIFFUnmapProc);
  if (tif)
    tif->tif_fd = fd;

  return (tif);
}

TIFF* TIFFOpen(const char* name, const char* mode)
{
  imBinFile* bin_file;
  TIFF* tiff;

  if (mode[0] == 'r')
    bin_file = imBinFileOpen(name);
  else
    bin_file = imBinFileNew(name);

  if (!bin_file)
    return NULL;
  
  tiff = TIFFClientOpen(name, mode, (thandle_t)bin_file,  iTIFFReadProc, iTIFFWriteProc,
                                                          iTIFFSeekProc, iTIFFCloseProc, 
                                                          iTIFFSizeProc, iTIFFMapProc, 
                                                          iTIFFUnmapProc);
  if (!tiff)
    imBinFileClose(bin_file);

  return tiff;
}

void* _TIFFmalloc(tsize_t s)
{
  return (malloc((size_t) s));
}

void _TIFFfree(tdata_t p)
{
  free(p);
}

void* _TIFFrealloc(tdata_t p, tsize_t s)
{
  return (realloc(p, (size_t) s));
}

void _TIFFmemset(tdata_t p, int v, tsize_t c)
{
  memset(p, v, (size_t) c);
}

void _TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
{
  memcpy(d, s, (size_t) c);
}

int _TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
{
  return (memcmp(p1, p2, (size_t) c));
}

TIFFErrorHandler _TIFFwarningHandler = NULL;
TIFFErrorHandler _TIFFerrorHandler = NULL;