summaryrefslogtreecommitdiffstats
path: root/apps/plugins/imageviewer/jpegp/BUFFILEGETC.c
blob: 5124636731b0256bf25f863f6ad9ea8187f19331 (plain)
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
/* Simple buffered version of file reader.
 * JPEG decoding seems to work faster with it.
 * Not fully tested. In case of any issues try FILEGETC (see SOURCES)
 * */
#include "rb_glue.h"

static int fd;
static unsigned char buff[256]; //TODO: Adjust it...
static int length = 0;
static int cur_buff_pos = 0;
static int file_pos = 0;

extern int GETC(void)
{
    if (cur_buff_pos >= length)
    {
        length = rb->read(fd, buff, sizeof(buff));
        file_pos += length;
        cur_buff_pos = 0;
    }

    return buff[cur_buff_pos++];
}

// multibyte readers: host-endian independent - if evaluated in right order (ie. don't optimize)

extern int GETWbi(void)		// 16-bit big-endian
{
    return ( GETC()<<8 ) | GETC();
}

extern int GETDbi(void)		// 32-bit big-endian
{
    return ( GETC()<<24 ) | ( GETC()<<16 ) | ( GETC()<<8 ) | GETC();
}

extern int GETWli(void)		// 16-bit little-endian
{
    return GETC() | ( GETC()<<8 );
}

extern int GETDli(void)		// 32-bit little-endian
{
    return GETC() | ( GETC()<<8 ) | ( GETC()<<16 ) | ( GETC()<<24 );
}

// seek

extern void SEEK(int d)
{
    int newPos = cur_buff_pos + d;
    if (newPos < length && newPos >= 0)
    {
        cur_buff_pos = newPos;
        return;
    }
    file_pos = rb->lseek(fd, (cur_buff_pos - length) + d, SEEK_CUR);
    cur_buff_pos = length = 0;
}

extern void POS(int d)
{
    cur_buff_pos = length = 0;
    file_pos = d;
    rb->lseek(fd, d, SEEK_SET);
}

extern int TELL(void)
{
    return file_pos + cur_buff_pos - length;
}

// OPEN/CLOSE file

extern void *OPEN(char *f)
{
    printf("Opening %s\n", f);
    cur_buff_pos = length = file_pos = 0;
    fd = rb->open(f,O_RDONLY);

    if (  fd < 0 )
    {
        printf("Error opening %s\n", f);
        return NULL;
    }

    return &fd;
}

extern int CLOSE(void)
{
    cur_buff_pos = length = file_pos = 0;
    return rb->close(fd);
}