-
Notifications
You must be signed in to change notification settings - Fork 23
/
images.c
65 lines (57 loc) · 1.27 KB
/
images.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef MEMORYDEBUG
#include "memorydebug.h"
#endif
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#ifdef USECUDAHOSTALLOC
#include "thnets.h"
void *cmalloc(size_t size)
{
void *ptr = 0;
errcheck(cudaHostAlloc(&ptr, size, cudaHostAllocMapped));
return ptr;
}
#else
#define cmalloc(a) malloc(a)
#endif
typedef struct {
char filename[255];
unsigned char *bitmap;
int width, height, cp;
} img_t;
int loadimage(const char *path, img_t *image)
{
const char *p = strrchr(path, '.');
if(!p)
return -1;
const char *fn = strrchr(path, '/');
if(fn)
fn++;
else fn = path;
strcpy(image->filename, fn);
image->bitmap = stbi_load(path, &image->width, &image->height, &image->cp, 0);
if(image->cp == 4)
{
free(image->bitmap);
image->bitmap = stbi_load(path, &image->width, &image->height, &image->cp, 3);
}
if(!image->bitmap)
return -1;
#ifdef USECUDAHOSTALLOC
unsigned char *data = image->bitmap;
image->bitmap = cmalloc(image->cp * image->width * image->height);
if(image->bitmap)
{
memcppy(image->bitmap, data, image->cp * image->width * image->height);
free(data);
return 0;
} else {
free(data);
return -1;
}
#endif
return 0;
}