-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathorbis2dImagePng.c
More file actions
219 lines (173 loc) · 5.18 KB
/
Copy pathorbis2dImagePng.c
File metadata and controls
219 lines (173 loc) · 5.18 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* liborbis
* Copyright (C) 2015,2016,2017 Antonio Jose Ramos Marquez (aka bigboss) @psxdev on twitter
* Repository https://github.com/orbisdev/liborbis
*/
#include <string.h>
#include <stdlib.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <ps4link.h>
#include <png.h>
#include "orbis2d.h"
#define PNG_SIGSIZE (8)
static void orbis2dReadPngFromHost(png_structp png_ptr, png_bytep data, png_size_t length)
{
int fd = *(int *)png_get_io_ptr(png_ptr);
ps4LinkRead(fd,data,length);
}
static void orbis2dReadPngFromBuffer(png_structp png_ptr, png_bytep data, png_size_t length)
{
uint64_t *address = png_get_io_ptr(png_ptr);
memcpy(data, (void *)*address, length);
*address += length;
}
static Orbis2dTexture *orbis2dLoadPngGeneric(const void *io_ptr,png_rw_ptr read_data_fn)
{
png_structp png_ptr=png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if (png_ptr==NULL)
{
goto error_create_read;
}
png_infop info_ptr=png_create_info_struct(png_ptr);
if (info_ptr==NULL)
{
goto error_create_info;
}
png_bytep *row_ptrs=NULL;
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr,&info_ptr,(png_infopp)0);
if (row_ptrs!=NULL)
{
free(row_ptrs);
}
return NULL;
}
png_set_read_fn(png_ptr,(png_voidp)io_ptr,read_data_fn);
png_set_sig_bytes(png_ptr,PNG_SIGSIZE);
png_read_info(png_ptr,info_ptr);
unsigned int width, height;
int bit_depth, color_type;
png_get_IHDR(png_ptr,info_ptr,&width,&height,&bit_depth,&color_type,NULL,NULL,NULL);
if ((color_type==PNG_COLOR_TYPE_PALETTE && bit_depth<=8)
|| (color_type==PNG_COLOR_TYPE_GRAY && bit_depth<8)
|| png_get_valid(png_ptr,info_ptr,PNG_INFO_tRNS)
|| (bit_depth==16))
{
png_set_expand(png_ptr);
}
if (bit_depth == 16)
png_set_scale_16(png_ptr);
if (bit_depth==8 && color_type==PNG_COLOR_TYPE_RGB)
png_set_filler(png_ptr,0xFF,PNG_FILLER_AFTER);
if (color_type==PNG_COLOR_TYPE_GRAY ||
color_type==PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
if (color_type==PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(png_ptr);
png_set_filler(png_ptr,0xFF,PNG_FILLER_AFTER);
}
if (color_type==PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand_gray_1_2_4_to_8(png_ptr);
if (png_get_valid(png_ptr,info_ptr,PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
if (bit_depth<8)
png_set_packing(png_ptr);
png_read_update_info(png_ptr, info_ptr);
row_ptrs = (png_bytep *)malloc(sizeof(png_bytep)*height);
if (!row_ptrs)
goto error_alloc_rows;
Orbis2dTexture *texture = orbis2dCreateEmptyTexture(width,height);
if (!texture)
goto error_create_tex;
uint32_t *texture_data=orbis2dTextureGetDataPointer(texture);
unsigned int stride=orbis2dTextureGetStride(texture);
int i;
for (i=0;i<height;i++)
{
row_ptrs[i]=(png_bytep)(texture_data+i*stride);
}
png_read_image(png_ptr, row_ptrs);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)0);
free(row_ptrs);
return texture;
error_create_tex:
free(row_ptrs);
error_alloc_rows:
png_destroy_info_struct(png_ptr,&info_ptr);
error_create_info:
png_destroy_read_struct(&png_ptr,(png_infopp)0,(png_infopp)0);
error_create_read:
return NULL;
}
Orbis2dTexture *orbis2dLoadPngFromHost(const char *path)
{
png_byte pngsig[PNG_SIGSIZE];
int fd;
if((fd = ps4LinkOpen(path, O_RDONLY, 0777)) < 0)
{
goto exit_error;
}
if (ps4LinkRead(fd,pngsig,PNG_SIGSIZE)!=PNG_SIGSIZE)
{
goto exit_close;
}
if (png_sig_cmp(pngsig,0,PNG_SIGSIZE)!=0)
{
goto exit_close;
}
Orbis2dTexture *texture=orbis2dLoadPngGeneric((void *)&fd,orbis2dReadPngFromHost);
ps4LinkClose(fd);
return texture;
exit_close:
ps4LinkClose(fd);
exit_error:
return NULL;
}
Orbis2dTexture *orbis2dLoadPngFromHost_v2(const char *path)
{
uint8_t *buf=DataFromFile(path, 0);
return orbis2dLoadPngFromBuffer(buf); // create png from buf
}
// uses standard open/lseek/read/close to access sandbox'ed content
Orbis2dTexture *orbis2dLoadPngFromSandBox(const char *path)
{
int fd; // descriptor to manage file from /mnt/sanbox/...
int filesize; // variable to control file size
uint8_t *buf=NULL; // buffer for read from file
fd = open(path,O_RDONLY); // we open file in read only
if(fd<0) //If we can't open file, print the error and return
{
debugNetPrintf(DEBUG,"open returned error %d\n",fd);
return NULL;
}
filesize=lseek(fd,0,SEEK_END); // Seek to end to get file size
if(filesize<0) // If we get an error print it and return
{
debugNetPrintf(DEBUG,"lseek returned error %d\n",fd);
close(fd);
return NULL;
}
lseek(fd,0,SEEK_SET); //Seek back to start
buf=malloc(filesize); //Reserve memory for read buffer
if(!buf)
return NULL;
int numread=read(fd,buf,filesize); //Read filsesize bytes to buf
close(fd);
if(numread!=filesize) //if we don't get filesize bytes we are in trouble
{
debugNetPrintf(DEBUG,"read returned error %d\n",numread);
return NULL;
}
return orbis2dLoadPngFromBuffer(buf); //create png from buf
}
Orbis2dTexture *orbis2dLoadPngFromBuffer(const void *buffer)
{
if(png_sig_cmp((png_byte *)buffer,0,PNG_SIGSIZE)!=0)
{
return NULL;
}
uint64_t buffer_address=(uint64_t)buffer+PNG_SIGSIZE;
return orbis2dLoadPngGeneric((void *)&buffer_address,orbis2dReadPngFromBuffer);
}