-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpic.cpp
164 lines (139 loc) · 3.55 KB
/
pic.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pic.h"
/*
* pic_alloc: allocate picture memory.
* If opic!=0, then memory from opic->pix is reused (after checking that
* size is sufficient), else a new pixel array is allocated.
* Great caution should be used when freeing, if pix memory is reused!
*/
Pic *pic_alloc(int nx, int ny, int bytes_per_pixel, Pic *opic)
{
Pic *p;
int size = ny*nx*bytes_per_pixel;
ALLOC(p, Pic, 1);
p->nx = nx;
p->ny = ny;
p->bpp = bytes_per_pixel;
if (opic && opic->nx*opic->ny*opic->bpp >= p->nx*p->ny*p->bpp)
{
p->pix = opic->pix;
/* now opic and p have a common pix array */
}
else
ALLOC(p->pix, Pixel1, size);
return p;
}
void pic_free(Pic *p)
{
free(p->pix);
free(p);
}
/*
* pic_xxx routines will call ppm_xxx or tiff_xxx depending on the
* type of file.
*
* Currently, this is rather inefficient. We open the file, check the
* magic number, close it, and then reopen for the actual operation.
* It would probably be good to add a further level of interface which
* allowed us to pass around file descriptors in addition to file names.
*
* Michael Garland 17 Jan 96
*
*/
Pic_file_format pic_file_type(char *file)
{
unsigned char byte[10];
int i;
FILE *pic = fopen(file, "r");
if( !pic )
return PIC_UNKNOWN_FILE;
return PIC_PPM_FILE;
/*
for (i = 0; i < 10; i++)
byte[i] = getc(pic);
fclose(pic);
if( byte[0]=='P' && (byte[1]=='3' || byte[1]=='6') )
return PIC_PPM_FILE;
else
if( (byte[0]==0x4d && byte[1]==0x4d) || (byte[0]==0x49 && byte[1]==0x49) )
return PIC_TIFF_FILE;
else if ( byte[0]==0xff && byte[1]==0xd8 && byte[2]==0xff && byte[3]==0xe0 && strncmp(&byte[6], "JFIF", 4))
return PIC_JPEG_FILE;
else
return PIC_UNKNOWN_FILE;
*/
}
Pic_file_format pic_filename_type(char *file)
{
char *suff;
suff = strrchr(file, '.');
if (!strcmp(suff, ".jpg")) return PIC_JPEG_FILE;
if (!strcmp(suff, ".tiff") || !strcmp(suff, ".tif")) return PIC_TIFF_FILE;
if (!strcmp(suff, ".ppm")) return PIC_PPM_FILE;
return PIC_UNKNOWN_FILE;
}
int pic_get_size(char *file, int *nx, int *ny)
{
switch( pic_file_type(file) )
{
case PIC_TIFF_FILE:
//return tiff_get_size(file, nx, ny);
break;
case PIC_PPM_FILE:
return ppm_get_size(file, nx, ny);
break;
case PIC_JPEG_FILE:
//return jpeg_get_size(file, nx, ny);
break;
default:
return FALSE;
}
}
/*
* pic_read: read a TIFF or PPM file into memory.
* Normally, you should use opic==NULL.
* If opic!=NULL, then picture is read into opic->pix (after checking that
* size is sufficient), else a new Pic is allocated.
* Returns Pic pointer on success, NULL on failure.
*/
Pic *pic_read(char *file, Pic *opic)
{
switch( pic_file_type(file) )
{
case PIC_TIFF_FILE:
//return tiff_read(file, opic);
break;
case PIC_PPM_FILE:
return ppm_read(file, opic);
break;
case PIC_JPEG_FILE:
//return jpeg_read(file, opic);
break;
default:
return NULL;
}
}
/*
* pic_write: write pic to file in the specified format
* returns TRUE on success, FALSE on failure
*/
int pic_write(char *file, Pic *pic, Pic_file_format format)
{
switch( format )
{
case PIC_TIFF_FILE:
//return tiff_write(file, pic);
break;
case PIC_PPM_FILE:
return ppm_write(file, pic);
break;
case PIC_JPEG_FILE:
//return jpeg_write(file, pic);
break;
default:
fprintf(stderr, "pic_write: can't write %s, unknown format\n", file);
return FALSE;
}
}