-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppm.c
executable file
·73 lines (57 loc) · 1.43 KB
/
ppm.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
/*
* Hostile Takeover:
* CS248 Final Project, Fall 2004
*
* Written by Jonathan Reeves
*
* A simple 3D video game demonstrating the use of OpenGL for graphics
* rendering with a number of advanced techniques and optimizations to make
* it interesting. See the README file which came with this package for more
* details. It also includes a list of sources which were consulted while
* building this software package.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include "ppm.h"
PPMImage *LoadPPMFile(const char *Filename) {
FILE *fp;
int Data, imgSize;
int err;
PPMImage *image;
fp = fopen(Filename,"r");
if (!fp){
return NULL;
}
/* Read magic number. */
fscanf(fp, "P6\n");
/* Comment line(s). */
while ((Data=getc(fp))=='#'){
fscanf(fp,"%*[^\n]\n");
}
ungetc(Data, fp);
image = (PPMImage *)malloc(sizeof(PPMImage));
/* Image dimensions. */
if (fscanf(fp, "%d %d", &image->sizeX , &image->sizeY)!=2 ||
image->sizeX == 0 || image->sizeY == 0){
return NULL;
}
imgSize = image->sizeX*image->sizeY;
image->data = (unsigned char *)malloc(imgSize*3);
/* Maximum value in image. */
fscanf(fp, "%*d%*c");
/* Load image. */
err = fread(image->data, 3, imgSize, fp);
if(err != imgSize){
free(image->data);
free(image);
return NULL;
}
fclose(fp);
return image;
}
void DestroyPPMFile(PPMImage *img){
free(img->data);
free(img);
}