comparison tools/lib64gfx.c @ 2206:7694b5c8edc1

Add dmC64PaletteFromC64Palette(), used by dmC64SetImagePalette().
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 14 Jun 2019 03:27:50 +0300
parents cbac4912992c
children 90ec1ec89c56
comparison
equal deleted inserted replaced
2205:3a25e85f0203 2206:7694b5c8edc1
86 }; 86 };
87 87
88 const int ndmC64DefaultPalettes = sizeof(dmC64DefaultPalettes) / sizeof(dmC64DefaultPalettes[0]); 88 const int ndmC64DefaultPalettes = sizeof(dmC64DefaultPalettes) / sizeof(dmC64DefaultPalettes[0]);
89 89
90 90
91 int dmC64SetImagePalette(DMImage *img, const DMC64Palette *ppal, const BOOL mixed) 91 int dmC64PaletteFromC64Palette(DMPalette **ppal, const DMC64Palette *pcpal, const BOOL mixed)
92 { 92 {
93 const DMC64Palette *cpal = ppal; 93 const DMC64Palette *cpal = pcpal;
94 int res; 94 int res;
95
96 if (img == NULL)
97 return DMERR_NULLPTR;
98 95
99 if (cpal == NULL) 96 if (cpal == NULL)
100 cpal = &dmC64DefaultPalettes[0]; 97 cpal = &dmC64DefaultPalettes[0];
101 98
102 // Free previous palette
103 if (!img->constpal)
104 dmPaletteFree(img->pal);
105
106 img->constpal = FALSE;
107
108 // Allocate and create new 99 // Allocate and create new
109 if (mixed) 100 if (mixed)
110 { 101 {
111 // Mixed 256 color palette 102 // Mixed 256 color palette
112 if ((res = dmPaletteAlloc(&(img->pal), D64_NCOLORS * D64_NCOLORS, -1)) != DMERR_OK) 103 if ((res = dmPaletteAlloc(ppal, D64_NCOLORS * D64_NCOLORS, -1)) != DMERR_OK)
113 return res; 104 return res;
114 105
115 for (int n1 = 0, n = 0; n1 < D64_NCOLORS; n1++) 106 for (int n1 = 0, n = 0; n1 < D64_NCOLORS; n1++)
116 { 107 {
117 const DMColor *col1 = &cpal->colors[n1]; 108 const DMColor *col1 = &cpal->colors[n1];
118 for (int n2 = 0; n2 < D64_NCOLORS; n2++) 109 for (int n2 = 0; n2 < D64_NCOLORS; n2++)
119 { 110 {
120 const DMColor *col2 = &cpal->colors[n2]; 111 const DMColor *col2 = &cpal->colors[n2];
121 img->pal->colors[n].r = (col1->r + col2->r) / 2; 112 (*ppal)->colors[n].r = (col1->r + col2->r) / 2;
122 img->pal->colors[n].g = (col1->g + col2->g) / 2; 113 (*ppal)->colors[n].g = (col1->g + col2->g) / 2;
123 img->pal->colors[n].b = (col1->b + col2->b) / 2; 114 (*ppal)->colors[n].b = (col1->b + col2->b) / 2;
124 n++; 115 n++;
125 } 116 }
126 } 117 }
127 } 118 }
128 else 119 else
129 { 120 {
130 // Standard palette, just copy it 121 // Standard palette, just copy it
131 if ((res = dmPaletteAlloc(&(img->pal), D64_NCOLORS, 255)) != DMERR_OK) 122 if ((res = dmPaletteAlloc(ppal, 256, 255)) != DMERR_OK)
132 return res; 123 return res;
133 124
134 memcpy(img->pal->colors, cpal->colors, img->pal->ncolors * sizeof(DMColor)); 125 memcpy((*ppal)->colors, cpal->colors, D64_NCOLORS * sizeof(DMColor));
135 } 126 }
136 127
137 return DMERR_OK; 128 return DMERR_OK;
129 }
130
131
132 int dmC64SetImagePalette(DMImage *img, const DMC64Palette *cpal, const BOOL mixed)
133 {
134 if (img == NULL)
135 return DMERR_NULLPTR;
136
137 // Free previous palette
138 if (!img->constpal)
139 dmPaletteFree(img->pal);
140
141 img->constpal = FALSE;
142
143 return dmC64PaletteFromC64Palette(&img->pal, cpal, mixed);
138 } 144 }
139 145
140 146
141 BOOL dmCompareAddr16(const DMGrowBuf *buf, const size_t offs, const Uint16 addr) 147 BOOL dmCompareAddr16(const DMGrowBuf *buf, const size_t offs, const Uint16 addr)
142 { 148 {