amiga_imgv

annotate src/image.c @ 3:663471a80c21

broken + sdl emu
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Oct 2017 15:49:56 +0300
parents a4788c959918
children 0fd37effde29
rev   line source
nuclear@3 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <string.h>
nuclear@3 4 #include <errno.h>
nuclear@0 5 #include "image.h"
nuclear@3 6 #include "logger.h"
nuclear@3 7
nuclear@3 8 #ifdef __unix__
nuclear@3 9 #include <arpa/inet.h>
nuclear@3 10 #define HAVE_NTOHS
nuclear@3 11 #else
nuclear@3 12 static int bigendian(void);
nuclear@3 13 static uint16_t swap16(uint16_t x);
nuclear@3 14 static uint16_t nop16(uint16_t x);
nuclear@3 15 static uint16_t (*ntohs)(uint16_t);
nuclear@3 16 #endif
nuclear@3 17
nuclear@3 18 struct ham_image_header {
nuclear@3 19 char magic[4];
nuclear@3 20 uint16_t width, height;
nuclear@3 21 unsigned char nbitplanes, padding;
nuclear@3 22 uint16_t nchg;
nuclear@3 23 uint16_t palette[16];
nuclear@3 24 };
nuclear@0 25
nuclear@0 26 struct ham_image *load_ham_image(const char *fname)
nuclear@0 27 {
nuclear@3 28 int i, num;
nuclear@3 29 unsigned long imgsz;
nuclear@3 30 struct ham_image_header hdr;
nuclear@3 31 struct ham_image *img = 0;
nuclear@3 32 struct palchange *tail = 0;
nuclear@3 33 FILE *fp;
nuclear@3 34
nuclear@3 35 #ifndef HAVE_NTOHS
nuclear@3 36 ntohs = bigendian() ? nop16 : swap16;
nuclear@3 37 #endif
nuclear@3 38
nuclear@3 39 logmsg("opening file: %s\n", fname);
nuclear@3 40 if(!(fp = fopen(fname, "rb"))) {
nuclear@3 41 logmsg("failed to open %s: %s\n", fname, strerror(errno));
nuclear@3 42 return 0;
nuclear@3 43 }
nuclear@3 44
nuclear@3 45 if(fread(&hdr, sizeof hdr, 1, fp) < 1) {
nuclear@3 46 logmsg("unexpected eof while reading image header\n");
nuclear@3 47 goto err;
nuclear@3 48 }
nuclear@3 49 if(memcmp(hdr.magic, "HAAM", 4) != 0) {
nuclear@3 50 logmsg("failed to load image: %s: unknown file type\n", fname);
nuclear@3 51 goto err;
nuclear@3 52 }
nuclear@3 53
nuclear@3 54 if(!(img = malloc(sizeof *img))) {
nuclear@3 55 logmsg("failed to allocate image structure\n");
nuclear@3 56 goto err;
nuclear@3 57 }
nuclear@3 58 img->width = ntohs(hdr.width);
nuclear@3 59 img->height = ntohs(hdr.height);
nuclear@3 60 img->nbitplanes = hdr.nbitplanes;
nuclear@3 61
nuclear@3 62 imgsz = img->width * img->height / 8 * img->nbitplanes;
nuclear@3 63
nuclear@3 64 logmsg("header info: %dx%d, %d bpl, size: %lu\n", img->width, img->height, (int)img->nbitplanes, imgsz);
nuclear@3 65
nuclear@3 66 if(!(img->pixels = malloc(imgsz))) {
nuclear@3 67 logmsg("failed to allocate %dx%d (%d bitplane) image\n", img->width, img->height, img->nbitplanes);
nuclear@3 68 goto err;
nuclear@3 69 }
nuclear@3 70
nuclear@3 71 for(i=0; i<16; i++) {
nuclear@3 72 img->palette[i] = ntohs(hdr.palette[i]);
nuclear@3 73 }
nuclear@3 74
nuclear@3 75 num = ntohs(hdr.nchg);
nuclear@3 76 for(i=0; i<num; i++) {
nuclear@3 77 struct palchange *chg;
nuclear@3 78
nuclear@3 79 if(!(chg = malloc(sizeof *chg))) {
nuclear@3 80 logmsg("failed to allocate palchange structure\n");
nuclear@3 81 goto err;
nuclear@3 82 }
nuclear@3 83
nuclear@3 84 if(fread(&chg->line, 2, 1, fp) < 1 || fread(&chg->entry, 2, 1, fp) < 1) {
nuclear@3 85 logmsg("unexpected end of file while reading palette changelist\n");
nuclear@3 86 goto err;
nuclear@3 87 }
nuclear@3 88 chg->line = ntohs(chg->line);
nuclear@3 89 chg->entry = ntohs(chg->entry);
nuclear@3 90 chg->next = 0;
nuclear@3 91
nuclear@3 92 if(tail) {
nuclear@3 93 tail->next = chg;
nuclear@3 94 tail = chg;
nuclear@3 95 } else {
nuclear@3 96 img->chglist = tail = chg;
nuclear@3 97 }
nuclear@3 98 }
nuclear@3 99
nuclear@3 100 if(fread(img->pixels, 1, imgsz, fp) < imgsz) {
nuclear@3 101 logmsg("unexpected end of file while reading image pixels\n");
nuclear@3 102 goto err;
nuclear@3 103 }
nuclear@3 104
nuclear@3 105 fclose(fp);
nuclear@3 106 return img;
nuclear@3 107
nuclear@3 108 err:
nuclear@3 109 if(img) {
nuclear@3 110 while(img->chglist) {
nuclear@3 111 void *tmp = img->chglist;
nuclear@3 112 img->chglist = img->chglist->next;
nuclear@3 113 free(tmp);
nuclear@3 114 }
nuclear@3 115 free(img->pixels);
nuclear@3 116 free(img);
nuclear@3 117 }
nuclear@3 118 if(fp) fclose(fp);
nuclear@3 119 return 0;
nuclear@0 120 }
nuclear@0 121
nuclear@0 122 struct ham_image *gen_ham_image(int w, int h, int nbpl)
nuclear@0 123 {
nuclear@0 124 int i, x, y;
nuclear@0 125 struct ham_image *img;
nuclear@0 126 unsigned char *pptr;
nuclear@0 127 unsigned char pixval;
nuclear@0 128 /*static const uint16_t defpal[] = {
nuclear@0 129 0x000, 0xf00, 0xff0, 0x0f0, 0x0ff, 0x00f, 0xf0f, 0xfff,
nuclear@0 130 0x444, 0x800, 0x880, 0x080, 0x088, 0x008, 0x808, 0x888
nuclear@0 131 };*/
nuclear@0 132
nuclear@0 133 if(!(img = malloc(sizeof *img))) {
nuclear@0 134 return 0;
nuclear@0 135 }
nuclear@0 136 if(!(img->pixels = calloc(w * h / 8 * nbpl, 1))) {
nuclear@0 137 free(img);
nuclear@0 138 return 0;
nuclear@0 139 }
nuclear@0 140 img->width = w;
nuclear@0 141 img->height = h;
nuclear@0 142 img->chglist = 0;
nuclear@0 143
nuclear@0 144 img->nbitplanes = nbpl;
nuclear@0 145 /*memcpy(img->palette, defpal, sizeof defpal);*/
nuclear@0 146 for(i=0; i<16; i++) {
nuclear@0 147 img->palette[i] = i | (i << 4) | (i << 8);
nuclear@0 148 }
nuclear@0 149
nuclear@0 150 pptr = img->pixels;
nuclear@0 151 for(i=0; i<4; i++) {
nuclear@0 152 pptr = img->pixels + i * w / 8;
nuclear@0 153 for(y=0; y<h; y++) {
nuclear@0 154 pixval = 0;
nuclear@0 155 for(x=0; x<w; x++) {
nuclear@0 156 pixval = (pixval >> 1) | ((((x ^ y) >> i) & 1) ? 0x80 : 0);
nuclear@0 157 if((x & 7) == 7) {
nuclear@0 158 *pptr++ = pixval;
nuclear@0 159 pixval = 0;
nuclear@0 160 }
nuclear@0 161 }
nuclear@0 162 pptr += w / 8 * (img->nbitplanes - 1);
nuclear@0 163 }
nuclear@0 164 }
nuclear@0 165
nuclear@0 166 return img;
nuclear@0 167 }
nuclear@3 168
nuclear@3 169 #ifndef HAVE_NTOHS
nuclear@3 170 static int bigendian(void)
nuclear@3 171 {
nuclear@3 172 static const uint16_t x = 0xaabb;
nuclear@3 173 return *(unsigned char*)&x == 0xaa;
nuclear@3 174 }
nuclear@3 175
nuclear@3 176 static uint16_t swap16(uint16_t x)
nuclear@3 177 {
nuclear@3 178 return (x << 8) | (x >> 8);
nuclear@3 179 }
nuclear@3 180
nuclear@3 181 static uint16_t nop16(uint16_t x)
nuclear@3 182 {
nuclear@3 183 return x;
nuclear@3 184 }
nuclear@3 185 #endif