#include <string.h>
#include "imago2.h"
#include "ftmodule.h"
+#include "byteord.h"
#include "chkalloc.h"
+/* calculate int-aligned offset to colormap, right after the end of the pixel data */
+#define CMAPPTR(fb, fbsz) \
+ (struct img_colormap*)((((uintptr_t)fb) + (fbsz) + sizeof(int) - 1) & ~(sizeof(int) - 1))
+
static int pixel_size(enum img_fmt fmt);
static size_t def_read(void *buf, size_t bytes, void *uptr);
static size_t def_write(void *buf, size_t bytes, void *uptr);
{
void *newpix;
int pixsz = pixel_size(fmt);
+ int bsz = w * h * pixsz;
+
+ if(fmt == IMG_FMT_IDX8) {
+ /* add space for the colormap, and space to align it to sizeof(int) */
+ bsz += sizeof(struct img_colormap) + sizeof(int) - 1;
+ }
- if(!(newpix = chk_malloc(w * h * pixsz))) {
+ if(!(newpix = chk_malloc(bsz))) {
return -1;
}
if(pix) {
memcpy(newpix, pix, w * h * pixsz);
} else {
- memset(newpix, 0, w * h * pixsz);
+ memset(newpix, 0, bsz);
}
chk_free(img->pixels);
}
}
+struct img_colormap *img_colormap(struct img_pixmap *img)
+{
+ int cmap_offs;
+
+ if(img->fmt != IMG_FMT_IDX8 || !img->pixels) {
+ return 0;
+ }
+
+ return CMAPPTR(img->pixels, img->width * img->height * img->pixelsz);
+}
+
void img_io_set_user_data(struct img_io *io, void *uptr)
{
io->uptr = uptr;
{
switch(fmt) {
case IMG_FMT_GREY8:
+ case IMG_FMT_IDX8:
return 1;
case IMG_FMT_RGB24:
return 3;