source: libpipi/trunk/pipi/codec/oric.c @ 2844

Last change on this file since 2844 was 2844, checked in by Sam Hocevar, 12 years ago

Fix headers.

File size: 3.4 KB
Line 
1/*
2 *  libpipi       Pathetic image processing interface library
3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id$
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 * oric.c: Oric Atmos import/export functions
17 */
18
19#include "config.h"
20#include "common.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include "pipi.h"
27#include "pipi_internals.h"
28
29static int load_data(const char *name, uint8_t *screen);
30
31pipi_image_t *pipi_load_oric(const char *name)
32{
33    static uint8_t const pal[32] =
34    {
35        0x00, 0x00, 0x00, 0xff,
36        0x00, 0x00, 0xff, 0xff,
37        0x00, 0xff, 0x00, 0xff,
38        0x00, 0xff, 0xff, 0xff,
39        0xff, 0x00, 0x00, 0xff,
40        0xff, 0x00, 0xff, 0xff,
41        0xff, 0xff, 0x00, 0xff,
42        0xff, 0xff, 0xff, 0xff,
43    };
44
45    uint8_t screen[8000];
46    pipi_image_t *img;
47    pipi_pixels_t *p;
48    uint8_t *data;
49    int x, y, i;
50
51    if(load_data(name, screen) < 0)
52        return NULL;
53
54    img = pipi_new(240, 200);
55    p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
56    data = p->pixels;
57
58    for(y = 0; y < 200; y++)
59    {
60        int bg = 0, fg = 7;
61
62        for(x = 0; x < 40; x++)
63        {
64            int col;
65            uint8_t c = screen[y * 40 + x];
66
67            if(c & 0x40)
68            {
69                for(i = 0; i < 6; i++)
70                {
71                    col = (c & (1 << (5 - i))) ? (c & 0x80) ? 7 - fg : fg
72                                               : (c & 0x80) ? 7 - bg : bg;
73                    memcpy(data + (y * 240 + x * 6 + i) * 4, pal + 4 * col, 4);
74                }
75            }
76            else if((c & 0x60) == 0x00)
77            {
78                if(c & 0x10)
79                    bg = c & 0x7;
80                else
81                    fg = c & 0x7;
82
83                col = (c & 0x80) ? 7 - bg : bg;
84
85                for(i = 0; i < 6; i++)
86                    memcpy(data + (y * 240 + x * 6 + i) * 4, pal + 4 * col, 4);
87            }
88            /* else: invalid sequence */
89        }
90    }
91
92    img->codec_priv = NULL;
93
94    img->wrap = 0;
95    img->u8 = 1;
96
97    return img;
98}
99
100int pipi_save_oric(pipi_image_t *img, const char *name)
101{
102    return -1;
103}
104
105/*
106 * XXX: The following functions are local.
107 */
108
109static int load_data(const char *name, uint8_t *screen)
110{
111    FILE *fp;
112    int ch;
113
114    fp = fopen(name, "r");
115    if(!fp)
116        return -1;
117
118    /* Skip the sync bytes */
119    ch = fgetc(fp);
120    if(ch != 0x16)
121        goto syntax_error;
122    while((ch = fgetc(fp)) == 0x16)
123        ;
124    if(ch != 0x24)
125        goto syntax_error;
126
127    /* Skip the header */
128    if(fgetc(fp) != 0x00 || fgetc(fp) != 0xff || fgetc(fp) != 0x80
129        || fgetc(fp) != 0x00 || fgetc(fp) != 0xbf || fgetc(fp) != 0x3f
130        || fgetc(fp) != 0xa0 || fgetc(fp) != 0x00 || fgetc(fp) != 0x00)
131        goto syntax_error;
132
133    /* Skip the file name */
134    for(;;)
135    {
136        ch = fgetc(fp);
137        if(ch == EOF)
138            goto syntax_error;
139        if(ch == 0x00)
140            break;
141    }
142
143    /* Read screen data */
144    if(fread(screen, 1, 8000, fp) != 8000)
145        goto syntax_error;
146
147    fclose(fp);
148    return 0;
149
150syntax_error:
151    fclose(fp);
152    return -1;
153}
154
Note: See TracBrowser for help on using the repository browser.