|
- /*
- * libpipi Proper image processing implementation library
- * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
- * All Rights Reserved
- *
- * $Id$
- *
- * This library is free software. It comes without any warranty, to
- * the extent permitted by applicable law. You can redistribute it
- * and/or modify it under the terms of the Do What The Fuck You Want
- * To Public License, Version 2, as published by Sam Hocevar. See
- * http://sam.zoy.org/wtfpl/COPYING for more details.
- */
-
- /*
- * oric.c: Oric Atmos import/export functions
- */
-
- #include "config.h"
- #include "common.h"
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #include "pipi.h"
- #include "pipi_internals.h"
-
- static int load_data(const char *name, uint8_t *screen);
-
- pipi_image_t *pipi_load_oric(const char *name)
- {
- static uint8_t const pal[32] =
- {
- 0x00, 0x00, 0x00, 0xff,
- 0x00, 0x00, 0xff, 0xff,
- 0x00, 0xff, 0x00, 0xff,
- 0x00, 0xff, 0xff, 0xff,
- 0xff, 0x00, 0x00, 0xff,
- 0xff, 0x00, 0xff, 0xff,
- 0xff, 0xff, 0x00, 0xff,
- 0xff, 0xff, 0xff, 0xff,
- };
-
- uint8_t screen[8000];
- pipi_image_t *img;
- pipi_pixels_t *p;
- uint8_t *data;
- int x, y, i;
-
- if(load_data(name, screen) < 0)
- return NULL;
-
- img = pipi_new(240, 200);
- p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
- data = p->pixels;
-
- for(y = 0; y < 200; y++)
- {
- int bg = 0, fg = 7;
-
- for(x = 0; x < 40; x++)
- {
- int col;
- uint8_t c = screen[y * 40 + x];
-
- if(c & 0x40)
- {
- for(i = 0; i < 6; i++)
- {
- col = (c & (1 << (5 - i))) ? (c & 0x80) ? 7 - fg : fg
- : (c & 0x80) ? 7 - bg : bg;
- memcpy(data + (y * 240 + x * 6 + i) * 4, pal + 4 * col, 4);
- }
- }
- else if((c & 0x60) == 0x00)
- {
- if(c & 0x10)
- bg = c & 0x7;
- else
- fg = c & 0x7;
-
- col = (c & 0x80) ? 7 - bg : bg;
-
- for(i = 0; i < 6; i++)
- memcpy(data + (y * 240 + x * 6 + i) * 4, pal + 4 * col, 4);
- }
- /* else: invalid sequence */
- }
- }
-
- img->codec_priv = NULL;
-
- img->wrap = 0;
- img->u8 = 1;
-
- return img;
- }
-
- int pipi_save_oric(pipi_image_t *img, const char *name)
- {
- return -1;
- }
-
- /*
- * XXX: The following functions are local.
- */
-
- static int load_data(const char *name, uint8_t *screen)
- {
- FILE *fp;
- int ch;
-
- fp = fopen(name, "r");
- if(!fp)
- return -1;
-
- /* Skip the sync bytes */
- ch = fgetc(fp);
- if(ch != 0x16)
- goto syntax_error;
- while((ch = fgetc(fp)) == 0x16)
- ;
- if(ch != 0x24)
- goto syntax_error;
-
- /* Skip the header */
- if(fgetc(fp) != 0x00 || fgetc(fp) != 0xff || fgetc(fp) != 0x80
- || fgetc(fp) != 0x00 || fgetc(fp) != 0xbf || fgetc(fp) != 0x3f
- || fgetc(fp) != 0xa0 || fgetc(fp) != 0x00 || fgetc(fp) != 0x00)
- goto syntax_error;
-
- /* Skip the file name */
- for(;;)
- {
- ch = fgetc(fp);
- if(ch == EOF)
- goto syntax_error;
- if(ch == 0x00)
- break;
- }
-
- /* Read screen data */
- if(fread(screen, 1, 8000, fp) != 8000)
- goto syntax_error;
-
- fclose(fp);
- return 0;
-
- syntax_error:
- fclose(fp);
- return -1;
- }
|