-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageloader.c
91 lines (87 loc) · 2.28 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
/************************************************************************
**
** 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)
{
FILE* input = fopen(filename, "r");
// read mode from the first line
char mode[3];
fscanf(input, "%s", mode);
// read row and col from the second line
uint32_t row, col;
fscanf(input, "%u %u", &row, &col);
// read 255 from the third line
int rate;
fscanf(input, "%d", &rate);
//construct an Image object
Image* images = (Image*)malloc(sizeof(Image));
images->rows = row;
images->cols = col;
images->image = (Color**)malloc(row * sizeof(Color*));
for (int i = 0; i < row; i++) {
images->image[i] = (Color*)malloc(col * sizeof(Color));
}
// read images from next lines
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
uint8_t R, G, B;
fscanf(input, "%hhu %hhu %hhu", &R, &G, &B);
images->image[i][j].R = R;
images->image[i][j].G = G;
images->image[i][j].B = B;
}
}
fclose(input);
return images;
}
//Given an image, prints to stdout (e.g. with printf) a .ppm P3 file with the image's data.
void writeData(Image *image)
{
printf("%s\n", "P3");
uint32_t row = image->rows;
uint32_t col = image->cols;
printf("%u %u\n", row, col);
printf("%d\n", 255);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
int R = image->image[i][j].R;
int G = image->image[i][j].G;
int B = image->image[i][j].B;
printf("%3d %3d %3d", R, G, B);
if (j != col - 1) {
printf(" ");
}
}
printf("\n");
}
}
//Frees an image
void freeImage(Image *image)
{
int row = image->rows;
for (int i = 0; i < row; i++) {
free(image->image[i]);
}
free(image->image);
free(image);
}