-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.c
165 lines (136 loc) · 3.77 KB
/
image.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Copyright 2005 Andrew Rice
*
* 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., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* Email: acr31@cam.ac.uk
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "image.h"
#include "options.h"
static int readline(int handle, char* buffer, int maxlen) {
int readcount = 0;
int readresult = 1;
char* start = buffer;
while(readcount < maxlen && readresult > 0) {
readresult = read(handle,buffer,1);
if (readresult == -1) {
return -1;
}
readcount+=readresult;
if (*buffer == '\n') {
*buffer = 0;
break;
}
buffer+=readresult;
}
if (*start == '#') {
return readline(handle,start,maxlen);
}
else {
return 1;
}
}
static int writeline(char* buffer, int handle) {
int result;
int length = strlen((char*)buffer);
while(length > 0) {
result = write(handle,buffer,length);
if (result == -1) return 0;
length-=result;
}
return 1;
}
/*
* Save this image to the file given. If the flag "binary" is set
* then it is assumed that we have 1-bit data. Files are always saved
* as greyscale, ascii pnms
*/
int save_image(const unsigned char* data, const char* filename, int binary) {
int handle = open(filename,O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR );
char buffer[255];
int i,j;
if (!handle) return 0;
snprintf(buffer,255,"P2\n%d %d\n255\n",IMAGE_WIDTH,IMAGE_HEIGHT);
if (!writeline(buffer,handle)) {
close(handle);
return 0;
}
for(i=0;i<IMAGE_HEIGHT;++i) {
for(j=0;j<IMAGE_WIDTH;++j) {
int value = data[IMAGE_WIDTH*i+j];
value = binary ? (value ? 255 : 0) : value;
snprintf(buffer,255,"%d\n",value);
if (!writeline(buffer,handle)) {
close(handle);
return 0;
}
}
}
close(handle);
return 1;
}
/*
* Load a greyscale ascii pnm image of size IMAGE_WIDTH x
* IMAGE_HEIGHT, allocate a suitable buffer and return a pointer to
* it. Will return NULL on any error. If you get something that is
* non-null you have to free it yourself.
*/
unsigned char* load_image(const char* filename) {
int handle = open(filename,O_RDONLY);
char buffer[255];
int width;
int height;
int colour_count;
unsigned char* result;
int failed,i,j;
if (!handle) return NULL;
if ((readline(handle,buffer,255) < 0) ||
(strncmp(buffer,"P2",2) != 0) ||
(readline(handle,buffer,255) < 0) ||
(sscanf(buffer,"%d %d",&width,&height) == 0) ||
(width != IMAGE_WIDTH || height != IMAGE_HEIGHT) ||
(readline(handle,buffer,255) < 0) ||
(sscanf(buffer,"%d",&colour_count) == 0) ||
(colour_count != 255)) {
close(handle);
return NULL;
}
result = (unsigned char*)malloc(IMAGE_HEIGHT*IMAGE_WIDTH);
failed = 0;
for(i=0;i<IMAGE_HEIGHT;++i) {
for(j=0;j<IMAGE_WIDTH;++j) {
if ((readline(handle,buffer,255) < 0) ||
(sscanf(buffer,"%d",&colour_count) == 0)) {
failed = 1;
break;
}
result[IMAGE_WIDTH*i+j] = (unsigned char)colour_count;
}
}
close(handle);
if (failed) {
free(result);
return NULL;
}
return result;
}