forked from schani/rwimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadimage.c
149 lines (127 loc) · 3.15 KB
/
readimage.c
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
/* -*- c -*- */
/*
* readimage.c
*
* rwimg
*
* Copyright (C) 2000-2006 Mark Probst
* Copyright (C) 2006 Xavier Martin
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "readimage.h"
#ifdef RWIMG_JPEG
#include "rwjpeg.h"
#endif
#ifdef RWIMG_PNG
#include "rwpng.h"
#endif
#ifdef RWIMG_GIF
#include "rwgif.h"
#endif
image_reader_t*
open_image_reading (const char *filename)
{
unsigned char magic[4];
FILE *file;
void *data;
int width, height;
image_reader_t *reader;
image_read_func_t read_func;
image_reader_free_func_t free_func;
file = fopen(filename, "rb");
if (file == 0)
return 0;
if (fread(magic, 4, 1, file) != 1)
{
fclose(file);
return 0;
}
fclose(file);
if (memcmp(magic, "\xff\xd8", 2) == 0)
{
#ifdef RWIMG_JPEG
data = open_jpeg_file_reading(filename, &width, &height);
read_func = jpeg_read_lines;
free_func = jpeg_free_reader_data;
#else
return 0;
#endif
}
else if (memcmp(magic, "\x89PNG", 4) == 0)
{
#ifdef RWIMG_PNG
data = open_png_file_reading(filename, &width, &height);
read_func = png_read_lines;
free_func = png_free_reader_data;
#else
return 0;
#endif
}
else if (memcmp(magic, "GIF8", 4) == 0)
{
#ifdef RWIMG_GIF
data = open_gif_file(filename, &width, &height);
read_func = gif_read_lines;
free_func = gif_free_data;
#else
return 0;
#endif
}
else
return 0;
if (data == 0)
return 0;
reader = (image_reader_t*)malloc(sizeof(image_reader_t));
assert(reader != 0);
reader->width = width;
reader->height = height;
reader->num_lines_read = 0;
reader->data = data;
reader->read_func = read_func;
reader->free_func = free_func;
return reader;
}
void
read_lines (image_reader_t *reader, unsigned char *lines, int num_lines)
{
assert(reader->num_lines_read + num_lines <= reader->height);
reader->read_func(reader->data, lines, num_lines);
reader->num_lines_read += num_lines;
}
void
free_image_reader (image_reader_t *reader)
{
reader->free_func(reader->data);
free(reader);
}
unsigned char*
read_image (const char *filename, int *width, int *height)
{
image_reader_t *reader = open_image_reading(filename);
unsigned char *data;
if (reader == 0)
return 0;
*width = reader->width;
*height = reader->height;
data = (unsigned char*)malloc(*width * *height * 3);
read_lines(reader, data, *height);
free_image_reader(reader);
return data;
}