Added GPL header to sconscripts!
[blender.git] / source / blender / imbuf / intern / cineon / logmemfile.c
1 /*
2  * Cineon image file format library routines.
3  *
4  * Copyright 2006 Joseph Eagar (joeedh@gmail.com)
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2 of the License, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Julien Enche.
21  *
22  */
23
24 /** \file blender/imbuf/intern/cineon/logmemfile.c
25  *  \ingroup imbcineon
26  */
27
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "logImageCore.h"
34 #include "logmemfile.h"
35
36 int logimage_fseek(LogImageFile *logFile, intptr_t offset, int origin)
37 {
38         if (logFile->file)
39                 fseek(logFile->file, offset, origin);
40         else { /* we're seeking in memory */
41                 if (origin == SEEK_SET) {
42                         if (offset > logFile->memBufferSize)
43                                 return 1;
44                         logFile->memCursor = logFile->memBuffer + offset;
45                 }
46                 else if (origin == SEEK_END) {
47                         if (offset > logFile->memBufferSize)
48                                 return 1;
49                         logFile->memCursor = (logFile->memBuffer + logFile->memBufferSize) - offset;
50                 }
51                 else if (origin == SEEK_CUR) {
52                         uintptr_t pos = (uintptr_t)logFile->memCursor - (uintptr_t)logFile->memBuffer;
53                         if (pos + offset > logFile->memBufferSize || pos < 0)
54                                 return 1;
55
56                         logFile->memCursor += offset;
57                 }
58         }
59         return 0;
60 }
61
62 int logimage_fwrite(void *buffer, size_t size, unsigned int count, LogImageFile *logFile)
63 {
64         if (logFile->file)
65                 return fwrite(buffer, size, count, logFile->file);
66         else { /* we're writing to memory */
67                 /* do nothing as this isn't supported yet */
68                 return count;
69         }
70 }
71
72 int logimage_fread(void *buffer, size_t size, unsigned int count, LogImageFile *logFile)
73 {
74         if (logFile->file) {
75                 return fread(buffer, size, count, logFile->file);
76         }
77         else { /* we're reading from memory */
78                 unsigned char *buf = (unsigned char *)buffer;
79                 uintptr_t pos = (uintptr_t)logFile->memCursor - (uintptr_t)logFile->memBuffer;
80                 size_t total_size = size * count;
81                 if (pos + total_size > logFile->memBufferSize) {
82                         /* how many elements can we read without overflow ? */
83                         count = (logFile->memBufferSize - pos) / size;
84                         /* recompute the size */
85                         total_size = size * count;
86                 }
87
88                 if (total_size != 0)
89                         memcpy(buf, logFile->memCursor, total_size);
90
91                 return count;
92         }
93 }
94
95 int logimage_read_uchar(unsigned char *x, LogImageFile *logFile)
96 {
97         uintptr_t pos = (uintptr_t)logFile->memCursor - (uintptr_t)logFile->memBuffer;
98         if (pos + sizeof(unsigned char) > logFile->memBufferSize)
99                 return 1;
100
101         *x = *(unsigned char *)logFile->memCursor;
102         logFile->memCursor += sizeof(unsigned char);
103         return 0;
104 }
105
106 int logimage_read_ushort(unsigned short *x, LogImageFile *logFile)
107 {
108         uintptr_t pos = (uintptr_t)logFile->memCursor - (uintptr_t)logFile->memBuffer;
109         if (pos + sizeof(unsigned short) > logFile->memBufferSize)
110                 return 1;
111
112         *x = *(unsigned short *)logFile->memCursor;
113         logFile->memCursor += sizeof(unsigned short);
114         return 0;
115 }
116
117 int logimage_read_uint(unsigned int *x, LogImageFile *logFile)
118 {
119         uintptr_t pos = (uintptr_t)logFile->memCursor - (uintptr_t)logFile->memBuffer;
120         if (pos + sizeof(unsigned int) > logFile->memBufferSize)
121                 return 1;
122
123         *x = *(unsigned int *)logFile->memCursor;
124         logFile->memCursor += sizeof(unsigned int);
125         return 0;
126 }