-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageloader.c
121 lines (115 loc) · 3.14 KB
/
imageloader.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
/************************************************************************
**
** NAME: imageloader.c
**
** DESCRIPTION: CS61C Fall 2020 Project 1
**
** AUTHOR: Dan Garcia - University of California at Berkeley
** Copyright (C) Dan Garcia, 2020. All rights reserved.
** Justin Yokota - Starter Code
** YOUR NAME HERE
**
**
** DATE: 2020-08-15
**
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include "imageloader.h"
//Opens a .ppm P3 image file, and constructs an Image object.
//You may find the function fscanf useful.
//Make sure that you close the file with fclose before returning.
Image *readData(char *filename)
{
//YOUR CODE HERE
FILE *imagefile = fopen(filename,"r");
if(imagefile == NULL){
printf("fail to open %s\n", filename);
return NULL;
}
char format[3];
int maxcolor;
fscanf(imagefile,"%s",format);
if(format[0] != 'P' || format[1] != '3'){
printf("%s wrong format\n", filename);
return NULL;
}
Image *img = (Image*)malloc(sizeof(Image));
fscanf(imagefile,"%d",&img->cols);
fscanf(imagefile,"%d",&img->rows);
fscanf(imagefile,"%d",&maxcolor);
if(img->rows <0 || img->cols <0 || maxcolor != 255){
printf("%s wrong format\n", filename);
}
int totpixel = img->rows * img->cols;
img->image = (Color **)malloc(totpixel*sizeof(Color*));
for(int i=0; i<totpixel;i++){
*(img->image+i) = (Color*)malloc(sizeof(Color));
Color* pixel = *(img->image +i);
fscanf(imagefile,"%hhu %hhu %hhu", &pixel->R,&pixel->G,&pixel->B);
}
fclose(imagefile);
return img;
}
//Given an image, prints to stdout (e.g. with printf) a .ppm P3 file with the image's data.
void writeData(Image *image)
{
printf("P3\n%d %d\n255\n",image->cols,image->rows);
Color** p = image->image;
for(int i = 0;i < image->rows;i++){
for(int j = 0; j < image->cols; j++){
printf("%3u %3u %3u\n", (*p)->R,(*p)->G,(*p)->B);
p++;
}
printf("%3u %3u %3u\n", (*p)->R,(*p)->G,(*p)->B);
p++;
}
printf("finish");
}
void writeData_in_file(Image *image){
FILE *fpWrite = fopen("data.txt","w");
if(fpWrite == NULL){
printf("no date.txt file");
}
fprintf(fpWrite,"P3\n%d %d\n255\n",image->cols,image->rows);
Color** p = image->image;
for(int i = 0;i < image->rows;i++){
for(int j = 0; j < image->cols; j++){
fprintf(fpWrite,"%3u %3u %3u ", (*p)->R,(*p)->G,(*p)->B);
p++;
}
fprintf(fpWrite,"\n");
fprintf(fpWrite,"%3u %3u %3u ", (*p)->R,(*p)->G,(*p)->B);
p++;
}
fclose(fpWrite);
}
//Frees an image
void freeImage(Image *image)
{
//YOUR CODE HERE
int totpixels = (image->rows)*(image->cols);
for(int i = 0; i < totpixels; i++){
free(*(image->image+i));
}
free(image-> image);
free(image);
}
// int main(){
// char *file = "D:/fa20-proj1-starter-master/studentOutputs/secretMessage.ppm";
// Image *readIn;
// readIn = readData(file);
// writeData(readIn);
// freeImage(readIn);
// }
// int main(int argc, char** argv){
// if (argc != 2){
// printf("usage: %s <colorfile> \n", argv[0]);
// }
// Image *readIn;
// readIn = readData(argv[1]);
// writeData(readIn);
// freeImage(readIn);
// }