amiga_imgv

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