sdk-world v2026.11.05
PS2 SDK - world tier (copyleft-allowed, non-essential packages)
Loading...
Searching...
No Matches
libjpg_ps2_addons.c
1/*
2* Some PS2 utils for libjpeg-turbo
3*/
4
5#include <stdint.h>
6#include <stdlib.h>
7#include <stdio.h>
8#include <kernel.h>
9#include <string.h>
10#include <fcntl.h>
11#include <unistd.h>
12#include <jpeglib.h>
13#include <setjmp.h>
14
16
18 struct jpeg_error_mgr pub; /* "public" fields */
19
20 jmp_buf setjmp_buffer; /* for return to caller */
21};
22
23typedef struct my_error_mgr *my_error_ptr;
24
25METHODDEF(void)
26my_error_exit(j_common_ptr cinfo)
27{
28 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
29 my_error_ptr myerr = (my_error_ptr)cinfo->err;
30
31 /* Always display the message. */
32 /* We could postpone this until after returning, if we chose. */
33 (*cinfo->err->output_message) (cinfo);
34
35 /* Return control to the setjmp point */
36 longjmp(myerr->setjmp_buffer, 1);
37}
38
39static jpgData *_load_JPEG(struct jpeg_decompress_struct *cinfo, int mode)
40{
41 jpgData *jpg;
42
43 int textureSize = 0;
44
45 jpg = malloc(sizeof(jpgData));
46 if(jpg == NULL)
47 return NULL;
48
49 jpeg_start_decompress(cinfo);
50
51
52 textureSize = cinfo->output_width*cinfo->output_height*cinfo->out_color_components;
53
54 if( (mode == JPG_WIDTH_FIX) && (cinfo->output_width % 4) )
55 jpg->width = cinfo->output_width - (cinfo->output_width % 4);
56 else
57 jpg->width = cinfo->output_width;
58
59 jpg->height = cinfo->output_height;
60 jpg->bpp = cinfo->out_color_components * 8;
61 jpg->buffer = malloc(textureSize);
62
63 unsigned int row_stride = textureSize/cinfo->output_height;
64 unsigned char *row_pointer = (unsigned char *)jpg->buffer;
65 while (cinfo->output_scanline < cinfo->output_height) {
66 jpeg_read_scanlines(cinfo, (JSAMPARRAY)&row_pointer, 1);
67 row_pointer += row_stride;
68 }
69
70 jpeg_finish_decompress(cinfo);
71
72 return jpg;
73}
74
75jpgData *jpgFromRAW(void *data, int size, int mode)
76{
77 jpgData *ret;
78 struct jpeg_decompress_struct cinfo;
79 struct my_error_mgr jerr;
80
81 cinfo.err = jpeg_std_error(&jerr.pub);
82 jerr.pub.error_exit = my_error_exit;
83 if (setjmp(jerr.setjmp_buffer)) {
84 jpeg_destroy_decompress(&cinfo);
85 printf("jpgFromRAW: error during processing file\n");
86 return NULL;
87 }
88 jpeg_create_decompress(&cinfo);
89 jpeg_mem_src(&cinfo, (void *)data, size);
90 jpeg_read_header(&cinfo, TRUE);
91
92 ret = _load_JPEG(&cinfo, mode);
93
94 jpeg_destroy_decompress(&cinfo);
95
96 return ret;
97}
98
99jpgData *jpgFromFilename(const char *filename, int mode)
100{
101 void *data;
102 int size;
103 int fd;
104 jpgData *jpg;
105 ssize_t ret;
106
107 fd = open(filename, O_RDONLY, 0);
108 if(fd == -1) {
109 printf("jpgOpen: error opening '%s'\n", filename);
110 return NULL;
111 }
112
113 size = lseek(fd, 0, SEEK_END);
114 lseek(fd, 0, SEEK_SET);
115
116 data = malloc(size);
117 if(data == NULL) {
118 printf("jpgOpen: NOT enought memory\n");
119 close(fd);
120 return NULL;
121 }
122
123 ret = read(fd, data, size);
124 if (ret != size) {
125 printf("jpgOpen: Error reading content\n");
126 free(data);
127 close(fd);
128 return NULL;
129 }
130
131 close(fd);
132 jpg = jpgFromRAW(data, size, mode);
133 free(data);
134
135 return jpg;
136}
137
138jpgData *jpgFromFILE(FILE *in_file, int mode)
139{
140 jpgData *jpg;
141 struct jpeg_decompress_struct cinfo;
142 struct my_error_mgr jerr;
143
144 cinfo.err = jpeg_std_error(&jerr.pub);
145 jerr.pub.error_exit = my_error_exit;
146 if (setjmp(jerr.setjmp_buffer)) {
147 jpeg_destroy_decompress(&cinfo);
148 printf("jpgOpenFILE: error during processing file\n");
149 return NULL;
150 }
151 jpeg_create_decompress(&cinfo);
152 jpeg_stdio_src(&cinfo, in_file);
153 jpeg_read_header(&cinfo, TRUE);
154
155 jpg = _load_JPEG(&cinfo, mode);
156
157 jpeg_destroy_decompress(&cinfo);
158
159 return jpg;
160}
161
162void jpgFileFromJpgData(const char *filename, int quality, jpgData *jpg) {
163 struct jpeg_compress_struct cinfo;
164 struct my_error_mgr jerr;
165 FILE *outfile;
166
167 cinfo.err = jpeg_std_error(&jerr.pub);
168 jerr.pub.error_exit = my_error_exit;
169 if (setjmp(jerr.setjmp_buffer)) {
170 jpeg_destroy_compress(&cinfo);
171 printf("jpgOpenFILE: error during processing file\n");
172 return;
173 }
174 jpeg_create_compress(&cinfo);
175
176 if ((outfile = fopen(filename, "wb")) == NULL) {
177 fprintf(stderr, "can't open %s\n", filename);
178 return;
179 }
180
181 jpeg_stdio_dest(&cinfo, outfile);
182
183 cinfo.image_width = jpg->width;
184 cinfo.image_height = jpg->height;
185 cinfo.input_components = jpg->bpp/8;
186 cinfo.in_color_space = JCS_RGB;
187
188 jpeg_set_defaults(&cinfo);
189 jpeg_set_quality(&cinfo, quality, TRUE);
190
191
192 jpeg_start_compress(&cinfo, TRUE);
193
194 unsigned int row_stride = jpg->width * 3;
195 unsigned char *row_pointer = (unsigned char *)jpg->buffer;
196 while (cinfo.next_scanline < cinfo.image_height) {
197 jpeg_write_scanlines(&cinfo, (JSAMPARRAY)&row_pointer, 1);
198 row_pointer += row_stride;
199 }
200
201 jpeg_finish_compress(&cinfo);
202 fclose(outfile);
203
204 jpeg_destroy_compress(&cinfo);
205}