-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtgaimage.c
74 lines (65 loc) · 1.57 KB
/
tgaimage.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
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "tgaimage.h"
void tga_write(const char *filename, tga_image *image)
{
FILE *fp = NULL;
fp = fopen(filename, "wb");
if (fp == NULL) return;
uint8_t dataChannels = 3;
uint8_t fileChannels = 3;
int width = image->width;
int height = image->height;
tga_color *dataBGR = image->dataBGR;
uint8_t header[18] = {0,
0,
2,
0,
0,
0,
0,
0,
0,
0,
0,
0,
(uint8_t)(width % 256),
(uint8_t)(width / 256),
(uint8_t)(height % 256),
(uint8_t)(height / 256),
(uint8_t)(fileChannels * 8),
0x20};
fwrite(&header, 18, 1, fp);
for (uint32_t i = 0; i < width * height; i++)
{
fputc(dataBGR[i].b, fp);
fputc(dataBGR[i].g, fp);
fputc(dataBGR[i].r, fp);
}
fclose(fp);
}
static inline void _set(tga_image *self, int x, int y, tga_color color) {
tga_color *dataBGR = self->dataBGR;
int width = self->width;
dataBGR[x + y * width].b = color.b;
dataBGR[x + y * width].g = color.g;
dataBGR[x + y * width].r = color.r;
}
tga_image tga_image_init(int width, int height)
{
tga_image new_tga_image;
uint8_t dataChannels = 3;
new_tga_image.dataBGR = calloc(width * height * dataChannels, sizeof(uint8_t));
new_tga_image.set = _set;
new_tga_image.width = width;
new_tga_image.height = height;
return new_tga_image;
}
tga_color tga_new_color(uint8_t r, uint8_t g, uint8_t b) {
tga_color new_color = { .r = r, .g = g, .b = b };
return new_color;
}