~ubuntu-branches/ubuntu/vivid/rawstudio/vivid

« back to all changes in this revision

Viewing changes to src/rawfile.c

  • Committer: Bazaar Package Importer
  • Author(s): Bernd Zeimetz
  • Date: 2011-07-28 17:36:32 UTC
  • mfrom: (2.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20110728173632-5czluz9ye3c83zc5
Tags: 2.0-1
* [3750b2cf] Merge commit 'upstream/2.0'
* [63637468] Removing Patch, not necessary anymore.
* [2fb580dc] Add new build-dependencies.
* [c57d953b] Run dh_autoreconf due to patches in configure.in
* [13febe39] Add patch to remove the libssl requirement.
* [5ae773fe] Replace libjpeg62-dev by libjpeg8-dev :)
* [1969d755] Don't build static libraries.
* [7cfe0a2e] Add a patch to fix the plugin directory path.
  As plugins are shared libraries, they need to go into /usr/lib,
  not into /usr/share.
  Thanks to Andrew McMillan
* [c1d0d9dd] Don't install .la files for all plugins and libraries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2006-2009 Anders Brander <anders@brander.dk> and 
3
 
 * Anders Kvist <akv@lnxbx.dk>
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU General Public License
7
 
 * as published by the Free Software Foundation; either version 2
8
 
 * of the License, or (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
 
 */
19
 
 
20
 
#include <gtk/gtk.h>
21
 
#include <sys/types.h>
22
 
#include <sys/stat.h>
23
 
#include <unistd.h>
24
 
#include <fcntl.h>
25
 
#ifdef G_OS_WIN32
26
 
 #include <windows.h>
27
 
#else
28
 
 #include <arpa/inet.h>
29
 
 #include <sys/mman.h>
30
 
#endif
31
 
#include <string.h>
32
 
#include "rawfile.h"
33
 
 
34
 
struct _RAWFILE {
35
 
#ifdef G_OS_WIN32
36
 
        HANDLE filehandle;
37
 
        HANDLE maphandle;
38
 
#else
39
 
        gint fd;
40
 
#endif
41
 
        guint size;
42
 
        void *map;
43
 
        gushort byteorder;
44
 
        guint first_ifd_offset;
45
 
        guint base;
46
 
};
47
 
 
48
 
#if BYTE_ORDER == LITTLE_ENDIAN
49
 
const static gushort cpuorder = 0x4949;
50
 
#elif BYTE_ORDER == BIG_ENDIAN
51
 
const static gushort cpuorder = 0x4D4D;
52
 
#endif
53
 
 
54
 
void
55
 
raw_init(void)
56
 
{
57
 
        /* stub */
58
 
        return;
59
 
}
60
 
 
61
 
gboolean
62
 
raw_get_uint(RAWFILE *rawfile, guint pos, guint *target)
63
 
{
64
 
        if((rawfile->base+pos+4)>rawfile->size)
65
 
                return(FALSE);
66
 
        if (rawfile->byteorder == cpuorder)
67
 
                *target = *(guint *)(rawfile->map+pos+rawfile->base);
68
 
        else
69
 
                *target = ENDIANSWAP4(*(guint *)(rawfile->map+pos+rawfile->base));
70
 
        return(TRUE);
71
 
}
72
 
 
73
 
gboolean
74
 
raw_get_ushort(RAWFILE *rawfile, guint pos, gushort *target)
75
 
{
76
 
        if((rawfile->base+pos+2)>rawfile->size)
77
 
                return(FALSE);
78
 
        if (rawfile->byteorder == cpuorder)
79
 
                *target = *(gushort *)(rawfile->map+rawfile->base+pos);
80
 
        else
81
 
                *target = ENDIANSWAP2(*(gushort *)(rawfile->map+rawfile->base+pos));
82
 
        return(TRUE);
83
 
}
84
 
 
85
 
gushort
86
 
raw_get_ushort_from_string(RAWFILE *rawfile, gchar *source)
87
 
{
88
 
        gushort target;
89
 
 
90
 
        if (rawfile->byteorder == cpuorder)
91
 
                target = *(gushort *)(source);
92
 
        else
93
 
                target = ENDIANSWAP2(*(gushort *)(source));
94
 
        return(target);
95
 
}
96
 
 
97
 
gboolean
98
 
raw_get_short(RAWFILE *rawfile, guint pos, gshort *target)
99
 
{
100
 
        if((rawfile->base+pos+2)>rawfile->size)
101
 
                return(FALSE);
102
 
        if (rawfile->byteorder == cpuorder)
103
 
                *target = *(gshort *)(rawfile->map+rawfile->base+pos);
104
 
        else
105
 
                *target = ENDIANSWAP2(*(gshort *)(rawfile->map+rawfile->base+pos));
106
 
        return(TRUE);
107
 
}
108
 
 
109
 
gshort
110
 
raw_get_short_from_string(RAWFILE *rawfile, gchar *source)
111
 
{
112
 
        gushort target;
113
 
 
114
 
        if (rawfile->byteorder == cpuorder)
115
 
                target = *(gshort *)(source);
116
 
        else
117
 
                target = ENDIANSWAP2(*(gshort *)(source));
118
 
        return(target);
119
 
}
120
 
 
121
 
gboolean
122
 
raw_get_float(RAWFILE *rawfile, guint pos, gfloat *target)
123
 
{
124
 
        if((rawfile->base+pos+4)>rawfile->size)
125
 
                return(FALSE);
126
 
 
127
 
        if (rawfile->byteorder == cpuorder)
128
 
                *target = *(gfloat *)(rawfile->map+rawfile->base+pos);
129
 
        else
130
 
                *target = (gfloat) (ENDIANSWAP4(*(gint *)(rawfile->map+rawfile->base+pos)));
131
 
        return(TRUE);
132
 
}
133
 
 
134
 
gboolean
135
 
raw_get_uchar(RAWFILE *rawfile, guint pos, guchar *target)
136
 
{
137
 
        if((rawfile->base+pos+1)>rawfile->size)
138
 
                return(FALSE);
139
 
 
140
 
        *target = *(guchar *)(rawfile->map+rawfile->base+pos);
141
 
        return(TRUE);
142
 
}
143
 
 
144
 
gboolean
145
 
raw_strcmp(RAWFILE *rawfile, guint pos, const gchar *needle, gint len)
146
 
{
147
 
        if((rawfile->base+pos+len) > rawfile->size)
148
 
                return(FALSE);
149
 
        if(0 == g_ascii_strncasecmp(needle, rawfile->map+rawfile->base+pos, len))
150
 
                return(TRUE);
151
 
        else
152
 
                return(FALSE);
153
 
}
154
 
 
155
 
gboolean
156
 
raw_strcpy(RAWFILE *rawfile, guint pos, void *target, gint len)
157
 
{
158
 
        if((rawfile->base+pos+len) > rawfile->size)
159
 
                return(FALSE);
160
 
        g_memmove(target, rawfile->map+rawfile->base+pos, len);
161
 
        return(TRUE);
162
 
}
163
 
 
164
 
gchar *
165
 
raw_strdup(RAWFILE *rawfile, guint pos, gint len)
166
 
{
167
 
        if((rawfile->base+pos+len) > rawfile->size)
168
 
                return(FALSE);
169
 
        return(g_strndup(rawfile->map+rawfile->base+pos, len));
170
 
}
171
 
 
172
 
GdkPixbuf *
173
 
raw_get_pixbuf(RAWFILE *rawfile, guint pos, guint length)
174
 
{
175
 
        GdkPixbufLoader *pl;
176
 
        GdkPixbuf *pixbuf = NULL;
177
 
        gboolean cont = TRUE; /* Are we good to continue? */
178
 
        if((rawfile->base+pos+length)>rawfile->size)
179
 
                return(NULL);
180
 
 
181
 
        pl = gdk_pixbuf_loader_new();
182
 
        while((length > 100000) && cont)
183
 
        {
184
 
                cont = gdk_pixbuf_loader_write(pl, rawfile->map+rawfile->base+pos, 80000, NULL);
185
 
                length -= 80000;
186
 
                pos += 80000;
187
 
        }
188
 
        if (cont)
189
 
                gdk_pixbuf_loader_write(pl, rawfile->map+rawfile->base+pos, length, NULL);
190
 
        pixbuf = gdk_pixbuf_loader_get_pixbuf(pl);
191
 
        gdk_pixbuf_loader_close(pl, NULL);
192
 
        return(pixbuf);
193
 
}
194
 
 
195
 
RAWFILE *
196
 
raw_open_file(const gchar *filename)
197
 
{
198
 
        struct stat st;
199
 
        gint fd;
200
 
        RAWFILE *rawfile;
201
 
 
202
 
        if(stat(filename, &st))
203
 
                return(NULL);
204
 
        rawfile = g_malloc(sizeof(RAWFILE));
205
 
        rawfile->size = st.st_size;
206
 
#ifdef G_OS_WIN32
207
 
 
208
 
        rawfile->filehandle = CreateFile(filename, FILE_READ_DATA, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
209
 
        if (rawfile->filehandle == INVALID_HANDLE_VALUE)
210
 
        {
211
 
                g_free(rawfile);
212
 
                return(NULL);
213
 
        }
214
 
 
215
 
        if ((rawfile->maphandle = CreateFileMapping(rawfile->filehandle, NULL, PAGE_READONLY, 0, 0, NULL))==NULL)
216
 
        {
217
 
                g_free(rawfile);
218
 
                return(NULL);
219
 
        }
220
 
 
221
 
        rawfile->map = MapViewOfFile(rawfile->maphandle, FILE_MAP_READ, 0, 0, rawfile->size);
222
 
        if (rawfile->map == NULL)
223
 
        {
224
 
                g_free(rawfile);
225
 
                return(NULL);
226
 
        }
227
 
 
228
 
#else
229
 
        if ((fd = open(filename, O_RDONLY)) == -1)
230
 
        {
231
 
                g_free(rawfile);
232
 
                return(NULL);
233
 
        }
234
 
        rawfile->map = mmap(NULL, rawfile->size, PROT_READ, MAP_SHARED, fd, 0);
235
 
        if(rawfile->map == MAP_FAILED)
236
 
        {
237
 
                g_free(rawfile);
238
 
                return(NULL);
239
 
        }
240
 
        rawfile->fd = fd;
241
 
#endif
242
 
        rawfile->base = 0;
243
 
        rawfile->byteorder = 0x4D4D;
244
 
        return(rawfile);
245
 
}
246
 
 
247
 
guchar
248
 
raw_init_file_tiff(RAWFILE *rawfile, guint pos)
249
 
{
250
 
        guchar version = 0;
251
 
 
252
 
        if((pos+12)>rawfile->size)
253
 
                return version;
254
 
        rawfile->byteorder = *((gushort *) (rawfile->map+pos));
255
 
        raw_get_uint(rawfile, pos+4, &rawfile->first_ifd_offset);
256
 
        if (rawfile->first_ifd_offset > rawfile->size)
257
 
                return version;
258
 
 
259
 
        raw_get_uchar(rawfile, pos+2, &version);
260
 
 
261
 
        rawfile->base = pos;
262
 
        return version;
263
 
}
264
 
 
265
 
void
266
 
raw_close_file(RAWFILE *rawfile)
267
 
{
268
 
#ifdef G_OS_WIN32
269
 
        UnmapViewOfFile(rawfile->map);
270
 
        CloseHandle(rawfile->maphandle);
271
 
        CloseHandle(rawfile->filehandle);
272
 
#else
273
 
        munmap(rawfile->map, rawfile->size);
274
 
        close(rawfile->fd);
275
 
#endif
276
 
        g_free(rawfile);
277
 
        return;
278
 
}
279
 
 
280
 
void
281
 
raw_reset_base(RAWFILE *rawfile)
282
 
{
283
 
        rawfile->base = 0;
284
 
        return;
285
 
}
286
 
 
287
 
gint
288
 
raw_get_base(RAWFILE *rawfile)
289
 
{
290
 
        return rawfile->base;
291
 
}
292
 
 
293
 
gushort
294
 
raw_get_byteorder(RAWFILE *rawfile)
295
 
{
296
 
        return rawfile->byteorder;
297
 
}
298
 
 
299
 
void
300
 
raw_set_byteorder(RAWFILE *rawfile, gushort byteorder)
301
 
{
302
 
        rawfile->byteorder = byteorder;
303
 
}
304
 
 
305
 
guint
306
 
get_first_ifd_offset(RAWFILE *rawfile)
307
 
{
308
 
        return rawfile->first_ifd_offset;
309
 
}
310
 
 
311
 
void *
312
 
raw_get_map(RAWFILE *rawfile)
313
 
{
314
 
        return rawfile->map;
315
 
}
316
 
 
317
 
guint
318
 
raw_get_filesize(RAWFILE *rawfile)
319
 
{
320
 
        return rawfile->size;
321
 
}