comparison tools/lib64gfx.c @ 1381:b6782b785457

Move things around a bit.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 24 Sep 2017 19:51:23 +0300
parents 959b34402b81
children 558776ee9603
comparison
equal deleted inserted replaced
1380:959b34402b81 1381:b6782b785457
8 */ 8 */
9 #include "lib64gfx.h" 9 #include "lib64gfx.h"
10 10
11 #define BUF_SIZE_INITIAL (16*1024) 11 #define BUF_SIZE_INITIAL (16*1024)
12 #define BUF_SIZE_GROW (4*1024) 12 #define BUF_SIZE_GROW (4*1024)
13
14
15 int dmC64ImageGetNumBanks(const DMC64ImageFormat *fmt)
16 {
17 int nbanks = 0;
18 for (int i = 0; i < D64_MAX_ENCDEC_OPS; i++)
19 {
20 const DMC64EncDecOp *op = &fmt->encdecOps[i];
21 if (op->type == DT_LAST)
22 break;
23
24 if (op->bank > nbanks)
25 nbanks = op->bank;
26 }
27
28 return nbanks + 1;
29 }
30
31
32 DMC64Image *dmC64ImageAlloc(const DMC64ImageFormat *fmt)
33 {
34 DMC64Image *img = dmMalloc0(sizeof(DMC64Image));
35
36 if (img == NULL)
37 return NULL;
38
39 // Initialize image information
40 img->width = fmt->width;
41 img->height = fmt->height;
42 img->ch_width = fmt->ch_width;
43 img->ch_height = fmt->ch_height;
44 img->nbanks = dmC64ImageGetNumBanks(fmt);
45
46 // Allocate banks
47 if ((img->color = dmCalloc(img->nbanks, sizeof(Uint8 *))) == NULL ||
48 (img->bitmap = dmCalloc(img->nbanks, sizeof(Uint8 *))) == NULL ||
49 (img->screen = dmCalloc(img->nbanks, sizeof(Uint8 *))) == NULL ||
50 (img->charmem = dmCalloc(img->nbanks, sizeof(Uint8 *))) == NULL)
51 goto err;
52
53 for (int i = 0; i < img->nbanks; i++)
54 {
55 if ((img->color[i] = dmMalloc0(img->ch_width * img->ch_height)) == NULL)
56 goto err;
57
58 if ((img->bitmap[i] = dmMalloc0(img->ch_width * img->ch_height * 8)) == NULL)
59 goto err;
60
61 if ((img->screen[i] = dmMalloc0(img->ch_width * img->ch_height)) == NULL)
62 goto err;
63
64 if ((img->charmem[i] = dmMalloc0(C64_MAX_CHARS * C64_CHR_SIZE)) == NULL)
65 goto err;
66 }
67
68 return img;
69
70 err:
71 dmC64ImageFree(img);
72 return NULL;
73 }
74
75
76 void dmC64ImageFree(DMC64Image *img)
77 {
78 if (img != NULL)
79 {
80 for (int i = 0; i < img->nbanks; i++)
81 {
82 dmFree(img->color[i]);
83 dmFree(img->bitmap[i]);
84 dmFree(img->screen[i]);
85 dmFree(img->charmem[i]);
86 }
87
88 for (int i = 0; i < C64_MAX_EXTRA_DATA; i++)
89 dmFree(img->extraData[i]);
90
91 dmMemset(img, 0, sizeof(DMC64Image));
92 dmFree(img);
93 }
94 }
95
96
97 char * dmC64GetImageTypeString(char *buf, const size_t len, const int type)
98 {
99 *buf = 0;
100
101 if (type & D64_FMT_FLI)
102 strncat(buf, "FLI ", len - strlen(buf));
103
104 strncat(buf, (type & D64_FMT_MC) ? "MCol " : "HiRes ", len - strlen(buf));
105
106 if (type & D64_FMT_ILACE)
107 strncat(buf, "Ilace ", len - strlen(buf));
108
109 if (type & D64_FMT_CHAR)
110 strncat(buf, "CharMap", len - strlen(buf));
111
112 return buf;
113 }
114 13
115 14
116 // Based on Pepto's palette, stolen from VICE 15 // Based on Pepto's palette, stolen from VICE
117 DMColor dmC64Palette[C64_NCOLORS] = 16 DMColor dmC64Palette[C64_NCOLORS] =
118 { 17 {
144 return buf[offs ] == DM_GET_ADDR_LO(addr) && 43 return buf[offs ] == DM_GET_ADDR_LO(addr) &&
145 buf[offs + 1] == DM_GET_ADDR_HI(addr); 44 buf[offs + 1] == DM_GET_ADDR_HI(addr);
146 } 45 }
147 46
148 47
48 int dmC64ImageGetNumBanks(const DMC64ImageFormat *fmt)
49 {
50 int nbanks = 0;
51 for (int i = 0; i < D64_MAX_ENCDEC_OPS; i++)
52 {
53 const DMC64EncDecOp *op = &fmt->encdecOps[i];
54 if (op->type == DT_LAST)
55 break;
56
57 if (op->bank > nbanks)
58 nbanks = op->bank;
59 }
60
61 return nbanks + 1;
62 }
63
64
65 DMC64Image *dmC64ImageAlloc(const DMC64ImageFormat *fmt)
66 {
67 DMC64Image *img = dmMalloc0(sizeof(DMC64Image));
68
69 if (img == NULL)
70 return NULL;
71
72 // Initialize image information
73 img->width = fmt->width;
74 img->height = fmt->height;
75 img->ch_width = fmt->ch_width;
76 img->ch_height = fmt->ch_height;
77 img->nbanks = dmC64ImageGetNumBanks(fmt);
78
79 // Allocate banks
80 if ((img->color = dmCalloc(img->nbanks, sizeof(Uint8 *))) == NULL ||
81 (img->bitmap = dmCalloc(img->nbanks, sizeof(Uint8 *))) == NULL ||
82 (img->screen = dmCalloc(img->nbanks, sizeof(Uint8 *))) == NULL ||
83 (img->charmem = dmCalloc(img->nbanks, sizeof(Uint8 *))) == NULL)
84 goto err;
85
86 for (int i = 0; i < img->nbanks; i++)
87 {
88 if ((img->color[i] = dmMalloc0(img->ch_width * img->ch_height)) == NULL)
89 goto err;
90
91 if ((img->bitmap[i] = dmMalloc0(img->ch_width * img->ch_height * 8)) == NULL)
92 goto err;
93
94 if ((img->screen[i] = dmMalloc0(img->ch_width * img->ch_height)) == NULL)
95 goto err;
96
97 if ((img->charmem[i] = dmMalloc0(C64_MAX_CHARS * C64_CHR_SIZE)) == NULL)
98 goto err;
99 }
100
101 return img;
102
103 err:
104 dmC64ImageFree(img);
105 return NULL;
106 }
107
108
109 void dmC64ImageFree(DMC64Image *img)
110 {
111 if (img != NULL)
112 {
113 for (int i = 0; i < img->nbanks; i++)
114 {
115 dmFree(img->color[i]);
116 dmFree(img->bitmap[i]);
117 dmFree(img->screen[i]);
118 dmFree(img->charmem[i]);
119 }
120
121 for (int i = 0; i < C64_MAX_EXTRA_DATA; i++)
122 dmFree(img->extraData[i]);
123
124 dmMemset(img, 0, sizeof(DMC64Image));
125 dmFree(img);
126 }
127 }
128
129
130 char * dmC64GetImageTypeString(char *buf, const size_t len, const int type)
131 {
132 *buf = 0;
133
134 if (type & D64_FMT_FLI)
135 strncat(buf, "FLI ", len - strlen(buf));
136
137 strncat(buf, (type & D64_FMT_MC) ? "MCol " : "HiRes ", len - strlen(buf));
138
139 if (type & D64_FMT_ILACE)
140 strncat(buf, "Ilace ", len - strlen(buf));
141
142 if (type & D64_FMT_CHAR)
143 strncat(buf, "CharMap", len - strlen(buf));
144
145 return buf;
146 }
147
148
149 int dmC64ConvertCSDataToImage(DMImage *img, 149 int dmC64ConvertCSDataToImage(DMImage *img,
150 int xoffs, int yoffs, const Uint8 *buf, 150 int xoffs, int yoffs, const Uint8 *buf,
151 int width, int height, BOOL multicolor, 151 int width, int height, BOOL multicolor,
152 int *colors) 152 int *colors)
153 { 153 {