amiga_imgv

annotate tools/convhammer/main.c @ 1:6320a20f17c5

file formats in comments
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 25 Oct 2017 21:05:24 +0300
parents a4788c959918
children f75893a33234
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@1 3 #include <string.h>
nuclear@1 4 #include <arpa/inet.h>
nuclear@0 5 #include "image.h"
nuclear@0 6
nuclear@1 7 /* hammer dump format (input)
nuclear@1 8 * byte order: little endian
nuclear@1 9 *
nuclear@1 10 * 4 bytes: palette changes per scanline (cps)
nuclear@1 11 * 256 * cps * 2 bytes: [ idx | r | g | b ]
nuclear@1 12 * 320 * 256 bytes: linear HAM pixels
nuclear@1 13 */
nuclear@1 14
nuclear@1 15 /* interleaved ham format (output)
nuclear@1 16 * byte order: big endian
nuclear@1 17 *
nuclear@1 18 * 4 bytes: magic: "HAAM"
nuclear@1 19 * 2 bytes: width
nuclear@1 20 * 2 bytes: height
nuclear@1 21 * 1 byte: number of bitplanes
nuclear@1 22 * 1 byte: padding
nuclear@1 23 * 2 bytes: number of palette change descriptors (nchg)
nuclear@1 24 * 16 * 2 bytes: initial palette [ x | r | g | b ]
nuclear@1 25 * nchg * 4 bytes: [ scanline ] [ idx | r | g | b ]
nuclear@1 26 * width * height / 8 * bitplanes bytes: pixels interleaved scanlines
nuclear@1 27 * [ row0 bpl0 ] ... [ row0 bplN ] [ row1 bpl0 ] ... [ row1 bplN ] ...
nuclear@1 28 */
nuclear@1 29
nuclear@0 30 struct ham_image *load_hammer(const char *fname);
nuclear@0 31 int save_ham(struct ham_image *img, const char *fname);
nuclear@1 32 void free_image(struct ham_image *img);
nuclear@0 33
nuclear@0 34 int main(int argc, char **argv)
nuclear@0 35 {
nuclear@1 36 int i;
nuclear@1 37 struct ham_image *img;
nuclear@1 38 char *name, *dot;
nuclear@1 39
nuclear@1 40 for(i=1; i<argc; i++) {
nuclear@1 41 if(!(img = load_hammer(argv[i]))) {
nuclear@1 42 fprintf(stderr, "failed to load hammer dump: %s\n", argv[i]);
nuclear@1 43 continue;
nuclear@1 44 }
nuclear@1 45
nuclear@1 46 if(!(name = malloc(strlen(argv[i] + 8)))) {
nuclear@1 47 perror("failed to allocate output name buffer");
nuclear@1 48 abort();
nuclear@1 49 }
nuclear@1 50 strcpy(name, argv[i]);
nuclear@1 51
nuclear@1 52 if((dot = strrchr(name, '.'))) {
nuclear@1 53 *dot = 0;
nuclear@1 54 }
nuclear@1 55 strcat(name, ".ham");
nuclear@1 56
nuclear@1 57 if(save_ham(name, img) == -1) {
nuclear@1 58 fprintf(stderr, "failed to save ham image: %s\n", name);
nuclear@1 59 free_image(img);
nuclear@1 60 continue;
nuclear@1 61 }
nuclear@1 62 }
nuclear@1 63 return 0;
nuclear@0 64 }
nuclear@1 65
nuclear@1 66 struct ham_image *load_hammer(const char *fname)
nuclear@1 67 {
nuclear@1 68 }
nuclear@1 69
nuclear@1 70 int save_ham(struct ham_image *img, const char *fname)
nuclear@1 71 {
nuclear@1 72 uint16_t val;
nuclear@1 73 FILE *fp;
nuclear@1 74
nuclear@1 75 if(!(fp = fopen(fname, "wb"))) {
nuclear@1 76 fprintf(stderr, "failed to open %s for writing: %s\n", fname, strerror(errno));
nuclear@1 77 return -1;
nuclear@1 78 }
nuclear@1 79 fprintf(fp, "HAAM");
nuclear@1 80
nuclear@1 81 val = htons(img->width);
nuclear@1 82 fwrite(fp, 2, 1, &val);
nuclear@1 83 val = htons(img->height);
nuclear@1 84 fwrite(fp, 2, 1, &val);
nuclear@1 85 /* TODO: continue */
nuclear@1 86 }
nuclear@1 87
nuclear@1 88 void free_image(struct ham_image *img)
nuclear@1 89 {
nuclear@1 90 if(img) {
nuclear@1 91 free(img->pixels);
nuclear@1 92 while(img->chglist) {
nuclear@1 93 void *tmp = img->chglist;
nuclear@1 94 img->chglist = img->chglist->next;
nuclear@1 95 free(tmp);
nuclear@1 96 }
nuclear@1 97 free(img);
nuclear@1 98 }
nuclear@1 99 }