-
Notifications
You must be signed in to change notification settings - Fork 46
/
lutro_stb_image.c
64 lines (56 loc) · 1.87 KB
/
lutro_stb_image.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
#include <stdint.h>
#include "lutro_stb_image.h"
#include "streams/file_stream.h"
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_PNG
#define STBI_NO_STDIO
#define STBI_NO_LINEAR
#define STBI_NO_HDR
#define STBI_NO_THREAD_LOCALS
#include "lutro.h"
#include "stb/stb_image.h"
/**
* Load the given image into the data buffer.
*
* @return 1 on success, 0 on error.
*/
int lutro_stb_image_load(const char* filename, uint32_t** data, unsigned int* width, unsigned int* height) {
void* buf;
int64_t len;
int x, y, channels_in_file;
int channels = 4;
// Load the file data.
if (filestream_read_file(filename, &buf, &len) <= 0) {
fprintf(stderr, "failed to read file %s\n", filename);
*data = NULL; // Put a null pointer, in case caller doesn't check the return value
return 0;
}
// Load the data as an image.
stbi_uc* output = stbi_load_from_memory((stbi_uc const*)buf, (int)len, &x, &y, &channels_in_file, channels);
*width = x;
*height = y;
free(buf); // Allocated in libretro:filestream_read_file, don't trace it on ludo
// Ensure the image loaded successfully.
if (output == NULL) {
fprintf(stderr, "failed to load data from %s\n", filename);
*data = NULL; // Put a null pointer, in case caller doesn't check the return value
return 0;
}
// Flip the bits to have them in the format Lutro uses.
{
int rValue = 0;
int bValue = 2;
for (int xIndex = 0; xIndex < x; xIndex++) {
for (int yIndex = 0; yIndex < y; yIndex++) {
int index = xIndex + x * yIndex;
stbi_uc tmp;
tmp = output[index * channels + rValue];
output[index * channels + rValue] = output[index * channels + bValue];
output[index * channels + bValue] = tmp;
}
}
}
// Return the output as a success.
*data = (uint32_t*)output;
return 1;
}