File: src_file.c

package info (click to toggle)
fswebcam 20140113-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 612 kB
  • sloc: ansic: 8,442; makefile: 58
file content (257 lines) | stat: -rw-r--r-- 4,676 bytes parent folder | download | duplicates (3)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* fswebcam - FireStorm.cx's webcam generator                 */
/*============================================================*/
/* Copyright (C)2005-2011 Philip Heron <phil@sanslogic.co.uk> */
/*                                                            */
/* This program is distributed under the terms of the GNU     */
/* General Public License, version 2. You may use, modify,    */
/* and redistribute it under the terms of this license. A     */
/* copy should be included with this source.                  */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include "src.h"
#include "log.h"

typedef struct {
	
	FILE *f;
	
	uint8_t *start;
	size_t length;
	
} src_file_t;

int src_file_open_jpeg(src_t *src)
{
	src_file_t *s = (src_file_t *) src->state;
	uint8_t *p;
	
	src->palette = SRC_PAL_JPEG;
	
	/* Scan the JPEG segments for the SOF, which contains
	 * the width and height of the image. */
	
	p = s->start + 2;
	
	while(p - s->start < src->length - 3)
	{
		uint8_t  header;
		uint16_t length;
		
		/* Check for the segment marker. */
		if(*(p++) != 0xFF)
		{
			ERROR("Segment marker not found,");
			return(-1);
		}
		
		header = *(p++);
		length = (p[0] << 8) + p[1];
		
		/* Verify the full segment is present. */
		if((p - s->start) + length >= s->length)
		{
			ERROR("Incomplete segment.");
			return(-1);
		}
		
		if(header == 0xC0) /* SOF */
		{
			uint16_t width  = (p[5] << 8) + p[6];
			uint16_t height = (p[3] << 8) + p[4];
			
			if(src->width != width ||
			   src->height != height)
			{
				MSG("Adjusting resolution to %ix%i.",
				    width, height);
				
				src->width = width;
				src->height = height;
			}
			
			return(0);
		}
		
		if(header == 0xD9 || /* EOI */
		   header == 0xDA)   /* SOS */
		{
			WARN("%s: Unable to read resolution.", src->source);
			return(-1);
		}
		
		p += length;
	}
	
	return(0);
}

int src_file_open_png(src_t *src)
{
	src_file_t *s = (src_file_t *) src->state;
	uint32_t width, height;
	uint8_t *p;
	
	src->palette = SRC_PAL_PNG;
	
	if(s->length < 24)
	{
		ERROR("%s: Unexpected end of file.", src->source);
		return(-1);
	}
	
        p = s->start + 12;
	
	if(strncmp((char *) p, "IHDR", 4))
	{
		ERROR("%s: IHDR chunk must be first.", src->source);
		return(-1);
	}
	
	p += 4;
	
	width  = (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3];
	height = (p[4] << 24) + (p[5] << 16) + (p[6] << 8) + p[7];
	
	if(src->width  != width ||
	   src->height != height)
	{
		MSG("Adjusting resolution to %ix%i.",
		    width, height);
		
		src->width = width;
		src->height = height;
	}
	
	return(0);
}

int src_file_open(src_t *src)
{
	src_file_t *s;
	struct stat st;
	size_t size;
	
	s = calloc(sizeof(src_file_t), 1);
	if(!s)
	{
		ERROR("Out of memory.");
		return(-2);
	}
	
	src->state = (void *) s;
	
	/* Get the file's size in bytes. */
	if(stat(src->source, &st) == -1)
	{
		ERROR("Error accessing file %s", src->source);
		ERROR("stat: %s", strerror(errno));
		free(s);
		return(-2);
	}
	
	s->f = fopen(src->source, "rb");
	if(!s->f)
	{
		ERROR("Error opening file %s", src->source);
		ERROR("fopen: %s", strerror(errno));
		free(s);
		return(-2);
	}
	
	/* Allocate memory for the file. */
	s->length = st.st_size;
	s->start = (uint8_t *) malloc(s->length);
	if(!s->start)
	{
		ERROR("Out of memory.");
		fclose(s->f);
		free(s);
		return(-1);
	}
	
	/* Read the entire file into memory. */
	size = 0;
	while(!feof(s->f) && size < s->length)
		size += fread(s->start, 1, s->length, s->f);
	
	fclose(s->f);
	s->f = NULL;
	
	if(size != s->length)
	{
		ERROR("Error reading file. Read %i bytes of %i byte file.",
		      size, s->length);
		free(s->start);
		free(s);
		return(-1);
	}
	
	src->length = s->length;
	src->img    = s->start;
	
	/* Test for a JPEG file. */
	if(s->start[0] == 0xFF && s->start[1] == 0xD8)
	{
		MSG("%s: Loading JPEG file.", src->source);
		
		if(src_file_open_jpeg(src))
		{
			src_close(src);
			return(-1);
		}
		
		return(0);
	}
	
	/* Test for a PNG file. */
	if(s->start[0] == 0x89 && s->start[1] == 0x50 &&
	   s->start[2] == 0x4e && s->start[3] == 0x47)
	{
		MSG("%s: Loading PNG file.", src->source);
		
		if(src_file_open_png(src))
		{
			src_close(src);
			return(-1);
		}
		
		return(0);
	}
	
	ERROR("%s: Unknown file format.", src->source);
	src_close(src);
	
	return(-2);
}

int src_file_close(src_t *src)
{
	src_file_t *s = (src_file_t *) src->state;
	
	if(s->f) fclose(s->f);
	free(s->start);
	free(s);
	
	return(0);
}

int src_file_grab(src_t *src)
{
	return(0);
}

src_mod_t src_file = {
	"file", SRC_TYPE_FILE,
	src_file_open,
	src_file_close,
	src_file_grab
};