X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=imago%2Fsrc%2Fimago2.c;h=2480f82aa6db92916b661c5d6348e897e950471d;hb=d45bc63a8f65e07332d0b16ad5f062c20bd76414;hp=b4ec46514755f7a7978d0c00d6d39a527484d1ad;hpb=56d0fddeeaee0ecb56cddc09db44bb87bf011314;p=dos_imgv
diff --git a/imago/src/imago2.c b/imago/src/imago2.c
index b4ec465..2480f82 100644
--- a/imago/src/imago2.c
+++ b/imago/src/imago2.c
@@ -21,8 +21,13 @@ along with this program. If not, see .
#include
#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);
@@ -94,15 +99,21 @@ int img_set_pixels(struct img_pixmap *img, int w, int h, enum img_fmt fmt, void
{
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);
@@ -397,6 +408,17 @@ void img_getpixel4f(struct img_pixmap *img, int x, int y, float *r, float *g, fl
}
}
+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;
@@ -422,6 +444,7 @@ static int pixel_size(enum img_fmt fmt)
{
switch(fmt) {
case IMG_FMT_GREY8:
+ case IMG_FMT_IDX8:
return 1;
case IMG_FMT_RGB24:
return 3;