From 2f5dae29270966fea822fa1d5210a4148a95cab2 Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 28 Sep 2008 14:09:30 +0000 Subject: [PATCH] Wrote an Oric hires file parser. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2842 92316355-f0b4-4df1-b90c-862c8a59935f --- pipi/Makefile.am | 9 ++- pipi/codec.c | 6 ++ pipi/codec/oric.c | 154 ++++++++++++++++++++++++++++++++++++++++++ pipi/pipi_internals.h | 3 + 4 files changed, 169 insertions(+), 3 deletions(-) create mode 100644 pipi/codec/oric.c diff --git a/pipi/Makefile.am b/pipi/Makefile.am index 9043d77..db739a6 100644 --- a/pipi/Makefile.am +++ b/pipi/Makefile.am @@ -39,12 +39,13 @@ libpipi_la_CFLAGS = $(codec_cflags) libpipi_la_LDFLAGS = $(codec_libs) \ -no-undefined -version-number @LT_VERSION@ -# Conditional sources +# Submodules + codec_cflags = codec_libs = -codec_sources = +codec_sources = \ + codec/oric.c -# Submodules paint_sources = \ paint/floodfill.c \ paint/line.c \ @@ -84,6 +85,8 @@ dither_sources = \ histogram_sources = \ histogram/histogram.c +# Conditional sources + if USE_SDL codec_cflags += `sdl-config --cflags` codec_libs += `sdl-config --libs` -lSDL_image diff --git a/pipi/codec.c b/pipi/codec.c index b08e73a..d1dc7f0 100644 --- a/pipi/codec.c +++ b/pipi/codec.c @@ -36,6 +36,9 @@ pipi_image_t *pipi_load(char const *name) !strncmp(name, "bayer:", 6)) ret = pipi_load_stock(name); + if(!ret) + ret = pipi_load_oric(name); + #if USE_IMLIB2 if(!ret) ret = pipi_load_imlib2(name); @@ -91,6 +94,9 @@ int pipi_save(pipi_image_t *img, const char *name) ret = pipi_save_gdi(img, name); #endif + if(!ret) + ret = pipi_save_oric(img, name); + return ret; } diff --git a/pipi/codec/oric.c b/pipi/codec/oric.c new file mode 100644 index 0000000..68129a5 --- /dev/null +++ b/pipi/codec/oric.c @@ -0,0 +1,154 @@ +/* + * libpipi Proper image processing implementation library + * Copyright (c) 2004-2008 Sam Hocevar + * 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 +#include +#include + +#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; +} + diff --git a/pipi/pipi_internals.h b/pipi/pipi_internals.h index b36a845..fb16bd8 100644 --- a/pipi/pipi_internals.h +++ b/pipi/pipi_internals.h @@ -85,5 +85,8 @@ pipi_image_t *pipi_load_gdi(const char *name); int pipi_save_gdi(pipi_image_t *img, const char *name); #endif +pipi_image_t *pipi_load_oric(const char *name); +int pipi_save_oric(pipi_image_t *img, const char *name); + #endif /* __PIPI_INTERNALS_H__ */