~ubuntu-branches/ubuntu/wily/liblas/wily

« back to all changes in this revision

Viewing changes to src/tifvsi.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine
  • Date: 2009-10-02 12:36:21 UTC
  • Revision ID: james.westby@ubuntu.com-20091002123621-xrf0hhzxbwloga43
Tags: upstream-1.2.1
ImportĀ upstreamĀ versionĀ 1.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 * $Id: tifvsi.cpp 10645 2007-01-18 02:22:39Z warmerdam $
 
3
 *
 
4
 * Project:  GeoTIFF Driver
 
5
 * Purpose:  Implement system hook functions for libtiff on top of CPL/VSI,
 
6
 *           including > 2GB support.  Based on tif_unix.c from libtiff
 
7
 *           distribution.
 
8
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 
9
 *
 
10
 ******************************************************************************
 
11
 * Copyright (c) 2005, Frank Warmerdam, warmerdam@pobox.com
 
12
 *
 
13
 * Permission is hereby granted, free of charge, to any person obtaining a
 
14
 * copy of this software and associated documentation files (the "Software"),
 
15
 * to deal in the Software without restriction, including without limitation
 
16
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
17
 * and/or sell copies of the Software, and to permit persons to whom the
 
18
 * Software is furnished to do so, subject to the following conditions:
 
19
 *
 
20
 * The above copyright notice and this permission notice shall be included
 
21
 * in all copies or substantial portions of the Software.
 
22
 *
 
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
24
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
26
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
28
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
29
 * DEALINGS IN THE SOFTWARE.
 
30
 ****************************************************************************/
 
31
 
 
32
/*
 
33
 * TIFF Library UNIX-specific Routines.
 
34
 */
 
35
#include "tiffio.h"
 
36
#include "cpl_vsi.h"
 
37
 
 
38
// We avoid including xtiffio.h since it drags in the libgeotiff version
 
39
// of the VSI functions.
 
40
 
 
41
CPL_C_START
 
42
extern TIFF CPL_DLL * XTIFFClientOpen(const char* name, const char* mode, 
 
43
                                      thandle_t thehandle,
 
44
                                      TIFFReadWriteProc, TIFFReadWriteProc,
 
45
                                      TIFFSeekProc, TIFFCloseProc,
 
46
                                      TIFFSizeProc,
 
47
                                      TIFFMapFileProc, TIFFUnmapFileProc);
 
48
CPL_C_END
 
49
 
 
50
static tsize_t
 
51
_tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
 
52
{
 
53
    return VSIFReadL( buf, 1, size, (FILE *) fd );
 
54
}
 
55
 
 
56
static tsize_t
 
57
_tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
 
58
{
 
59
    return VSIFWriteL( buf, 1, size, (FILE *) fd );
 
60
}
 
61
 
 
62
static toff_t
 
63
_tiffSeekProc(thandle_t fd, toff_t off, int whence)
 
64
{
 
65
    if( VSIFSeekL( (FILE *) fd, off, whence ) == 0 )
 
66
        return (toff_t) VSIFTellL( (FILE *) fd );
 
67
    else
 
68
        return (toff_t) -1;
 
69
}
 
70
 
 
71
static int
 
72
_tiffCloseProc(thandle_t fd)
 
73
{
 
74
    return VSIFCloseL( (FILE *) fd );
 
75
}
 
76
 
 
77
static toff_t
 
78
_tiffSizeProc(thandle_t fd)
 
79
{
 
80
    vsi_l_offset  old_off;
 
81
    toff_t        file_size;
 
82
 
 
83
    old_off = VSIFTellL( (FILE *) fd );
 
84
    VSIFSeekL( (FILE *) fd, 0, SEEK_END );
 
85
    
 
86
    file_size = (toff_t) VSIFTellL( (FILE *) fd );
 
87
    VSIFSeekL( (FILE *) fd, old_off, SEEK_SET );
 
88
 
 
89
    return file_size;
 
90
}
 
91
 
 
92
static int
 
93
_tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
 
94
{
 
95
        (void) fd; (void) pbase; (void) psize;
 
96
        return (0);
 
97
}
 
98
 
 
99
static void
 
100
_tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
 
101
{
 
102
        (void) fd; (void) base; (void) size;
 
103
}
 
104
 
 
105
/*
 
106
 * Open a TIFF file for read/writing.
 
107
 */
 
108
TIFF* VSI_TIFFOpen(const char* name, const char* mode)
 
109
{
 
110
    static const char module[] = "TIFFOpen";
 
111
    int           i, a_out;
 
112
    char          access[32];
 
113
    FILE          *fp;
 
114
    TIFF          *tif;
 
115
 
 
116
    a_out = 0;
 
117
    access[0] = '\0';
 
118
    for( i = 0; mode[i] != '\0'; i++ )
 
119
    {
 
120
        if( mode[i] == 'r'
 
121
            || mode[i] == 'w'
 
122
            || mode[i] == '+'
 
123
            || mode[i] == 'a' )
 
124
        {
 
125
            access[a_out++] = mode[i];
 
126
            access[a_out] = '\0';
 
127
        }
 
128
    }
 
129
 
 
130
    strcat( access, "b" );
 
131
                    
 
132
    fp = VSIFOpenL( name, access );
 
133
    if (fp == NULL) {
 
134
        if( errno >= 0 )
 
135
            TIFFError(module,"%s: %s", name, VSIStrerror( errno ) );
 
136
        else
 
137
            TIFFError(module, "%s: Cannot open", name);
 
138
        return ((TIFF *)0);
 
139
    }
 
140
 
 
141
    tif = XTIFFClientOpen(name, mode,
 
142
                          (thandle_t) fp,
 
143
                          _tiffReadProc, _tiffWriteProc,
 
144
                          _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
 
145
                          _tiffMapProc, _tiffUnmapProc);
 
146
 
 
147
    if( tif == NULL )
 
148
        VSIFCloseL( fp );
 
149
        
 
150
    return tif;
 
151
}