summaryrefslogtreecommitdiff
path: root/apps/plugins/imageviewer/jpegp/BUFFILEGETC.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/imageviewer/jpegp/BUFFILEGETC.c')
-rw-r--r--apps/plugins/imageviewer/jpegp/BUFFILEGETC.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/apps/plugins/imageviewer/jpegp/BUFFILEGETC.c b/apps/plugins/imageviewer/jpegp/BUFFILEGETC.c
new file mode 100644
index 0000000000..5124636731
--- /dev/null
+++ b/apps/plugins/imageviewer/jpegp/BUFFILEGETC.c
@@ -0,0 +1,94 @@
1/* Simple buffered version of file reader.
2 * JPEG decoding seems to work faster with it.
3 * Not fully tested. In case of any issues try FILEGETC (see SOURCES)
4 * */
5#include "rb_glue.h"
6
7static int fd;
8static unsigned char buff[256]; //TODO: Adjust it...
9static int length = 0;
10static int cur_buff_pos = 0;
11static int file_pos = 0;
12
13extern int GETC(void)
14{
15 if (cur_buff_pos >= length)
16 {
17 length = rb->read(fd, buff, sizeof(buff));
18 file_pos += length;
19 cur_buff_pos = 0;
20 }
21
22 return buff[cur_buff_pos++];
23}
24
25// multibyte readers: host-endian independent - if evaluated in right order (ie. don't optimize)
26
27extern int GETWbi(void) // 16-bit big-endian
28{
29 return ( GETC()<<8 ) | GETC();
30}
31
32extern int GETDbi(void) // 32-bit big-endian
33{
34 return ( GETC()<<24 ) | ( GETC()<<16 ) | ( GETC()<<8 ) | GETC();
35}
36
37extern int GETWli(void) // 16-bit little-endian
38{
39 return GETC() | ( GETC()<<8 );
40}
41
42extern int GETDli(void) // 32-bit little-endian
43{
44 return GETC() | ( GETC()<<8 ) | ( GETC()<<16 ) | ( GETC()<<24 );
45}
46
47// seek
48
49extern void SEEK(int d)
50{
51 int newPos = cur_buff_pos + d;
52 if (newPos < length && newPos >= 0)
53 {
54 cur_buff_pos = newPos;
55 return;
56 }
57 file_pos = rb->lseek(fd, (cur_buff_pos - length) + d, SEEK_CUR);
58 cur_buff_pos = length = 0;
59}
60
61extern void POS(int d)
62{
63 cur_buff_pos = length = 0;
64 file_pos = d;
65 rb->lseek(fd, d, SEEK_SET);
66}
67
68extern int TELL(void)
69{
70 return file_pos + cur_buff_pos - length;
71}
72
73// OPEN/CLOSE file
74
75extern void *OPEN(char *f)
76{
77 printf("Opening %s\n", f);
78 cur_buff_pos = length = file_pos = 0;
79 fd = rb->open(f,O_RDONLY);
80
81 if ( fd < 0 )
82 {
83 printf("Error opening %s\n", f);
84 return NULL;
85 }
86
87 return &fd;
88}
89
90extern int CLOSE(void)
91{
92 cur_buff_pos = length = file_pos = 0;
93 return rb->close(fd);
94}