/***********************************************************
* Artsoft Retro-Game Library *
*----------------------------------------------------------*
-* (c) 1994-2001 Artsoft Entertainment *
+* (c) 1994-2002 Artsoft Entertainment *
* Holger Schemel *
* Detmolder Strasse 189 *
* 33604 Bielefeld *
#include "image.h"
#include "pcx.h"
#include "misc.h"
+#include "setup.h"
+/* ========================================================================= */
+/* PLATFORM SPECIFIC IMAGE FUNCTIONS */
+/* ========================================================================= */
+
#if defined(TARGET_X11)
/* for MS-DOS/Allegro, exclude all except newImage() and freeImage() */
depth = 8;
#endif
- image = checked_malloc(sizeof(Image));
- image->data = checked_malloc(width * height * bytes_per_pixel);
+ image = checked_calloc(sizeof(Image));
+ image->data = checked_calloc(width * height * bytes_per_pixel);
image->width = width;
image->height = height;
image->depth = depth;
XImage *ximage;
XImageInfo *ximageinfo;
byte *src_ptr, *dst_ptr;
+ char *error = "Image_to_Pixmap(): %s";
if (image->type == IMAGETYPE_TRUECOLOR && depth == 8)
- Error(ERR_EXIT, "cannot handle true-color images on 8-bit display");
+ {
+ SetError(error, "cannot handle true-color images on 8-bit display");
+ return NULL;
+ }
if (!global_cmap)
{
bluestep = 256 / bluecolors;
redbottom = greenbottom = bluebottom = 0;
redtop = greentop = bluetop = 0;
+
for (a=0; a<visual->map_entries; a++)
{
if (redbottom < 256)
/* something completely unexpected happened */
fprintf(stderr, "Image_to_Pixmap: XAllocColor failed on a TrueColor/Directcolor visual\n");
+
free(redvalue);
free(greenvalue);
free(bluevalue);
free(ximageinfo);
+
return NULL;
}
while ((bluebottom < 256) && (bluebottom < bluetop))
bluevalue[bluebottom++] = xcolor.pixel & visual->blue_mask;
}
+
break;
}
}
if (!color_found) /* no more free color cells */
- Error(ERR_EXIT, "cannot allocate enough color cells");
+ {
+ SetError(error, "cannot allocate enough color cells");
+ return NULL;
+ }
xcolor.pixel = xcolor2.pixel;
xcolor_private[xcolor.pixel] = xcolor;
break;
default:
- Error(ERR_RETURN, "display class not supported");
- Error(ERR_EXIT, "DirectColor, TrueColor or PseudoColor display needed");
- break;
+ Error(ERR_RETURN,"DirectColor, TrueColor or PseudoColor display needed");
+ SetError(error, "display class not supported");
+
+ return NULL;
}
#if DEBUG_TIMING
dst_ptr += display_bytes_per_pixel;
}
}
+
break;
}
dst_ptr += display_bytes_per_pixel;
}
}
+
break;
}
default:
- Error(ERR_RETURN, "image type not supported");
- Error(ERR_EXIT, "RGB or TrueColor image needed");
- break;
+ Error(ERR_RETURN, "RGB or TrueColor image needed");
+ SetError(error, "image type not supported");
+
+ return NULL;
}
+
break;
}
}
}
}
+
break;
}
default:
- Error(ERR_RETURN, "display class not supported");
- Error(ERR_EXIT, "DirectColor, TrueColor or PseudoColor display needed");
- break;
+ Error(ERR_RETURN,"DirectColor, TrueColor or PseudoColor display needed");
+ SetError(error, "display class not supported");
+
+ return NULL;
}
if (redvalue)
XPutImage(ximageinfo->display, ximageinfo->pixmap, gc,
ximage, 0, 0, 0, 0, ximage->width, ximage->height);
- free(ximage->data);
- ximage->data = NULL;
XDestroyImage(ximage);
- return(ximageinfo);
+ return ximageinfo;
}
void freeXImage(Image *image, XImageInfo *ximageinfo)
/* convert image structure to X11 Pixmap */
if (!(ximageinfo = Image_to_Pixmap(display, screen, visual,
window, gc, depth, image)))
- Error(ERR_EXIT, "cannot convert Image to Pixmap");
+ {
+ freeImage(image);
+
+ return PCX_OtherError;
+ }
/* if a private colormap has been created, install it */
if (ximageinfo->cmap != DefaultColormap(display, screen))
*pixmap = ximageinfo->pixmap;
*pixmap_mask = ximageinfo->pixmap_mask;
+ /* free generic image and ximageinfo after native Pixmap has been created */
+ free(ximageinfo);
+ freeImage(image);
+
return PCX_Success;
}
#endif /* PLATFORM_UNIX */
#endif /* TARGET_X11 */
+
+
+/* ========================================================================= */
+/* PLATFORM INDEPENDANT IMAGE FUNCTIONS */
+/* ========================================================================= */
+
+struct ImageInfo
+{
+ char *source_filename;
+ int num_references;
+
+ Bitmap *bitmap;
+};
+typedef struct ImageInfo ImageInfo;
+
+static struct ArtworkListInfo *image_info = NULL;
+
+static void *Load_PCX(char *filename)
+{
+ ImageInfo *img_info;
+
+#if 0
+ printf("loading PCX file '%s'\n", filename);
+#endif
+
+ img_info = checked_calloc(sizeof(ImageInfo));
+
+ if ((img_info->bitmap = LoadImage(filename)) == NULL)
+ {
+ Error(ERR_WARN, "cannot read image file '%s': LoadImage() failed: %s",
+ filename, GetError());
+ free(img_info);
+ return NULL;
+ }
+
+ img_info->source_filename = getStringCopy(filename);
+
+ return img_info;
+}
+
+static void FreeImage(void *ptr)
+{
+ ImageInfo *image = (ImageInfo *)ptr;
+
+ if (image == NULL)
+ return;
+
+ if (image->bitmap)
+ FreeBitmap(image->bitmap);
+
+ if (image->source_filename)
+ free(image->source_filename);
+
+ free(image);
+}
+
+struct FileInfo *getCurrentImageList()
+{
+ return image_info->file_list;
+}
+
+Bitmap *getBitmapFromImageID(int graphic)
+{
+ ImageInfo **img_info = (ImageInfo **)image_info->artwork_list;
+
+ return (img_info[graphic] != NULL ? img_info[graphic]->bitmap : NULL);
+}
+
+char *getTokenFromImageID(int graphic)
+{
+ struct FileInfo *file_list = (struct FileInfo *)image_info->file_list;
+
+ return file_list[graphic].token;
+}
+
+char *getImageConfigFilename()
+{
+ return getCustomArtworkConfigFilename(image_info->type);
+}
+
+void InitImageList(struct ConfigInfo *config_list,
+ struct ConfigInfo *config_suffix_list,
+ int num_file_list_entries)
+{
+ int i;
+
+ image_info = checked_calloc(sizeof(struct ArtworkListInfo));
+
+ image_info->type = ARTWORK_TYPE_GRAPHICS;
+
+ image_info->num_file_list_entries = num_file_list_entries;
+ image_info->num_suffix_list_entries = 0;
+ for (i=0; config_suffix_list[i].token != NULL; i++)
+ image_info->num_suffix_list_entries++;
+
+ image_info->file_list =
+ getFileListFromConfigList(config_list, config_suffix_list,
+ num_file_list_entries);
+ image_info->suffix_list = config_suffix_list;
+ image_info->custom_setup_list = NULL;
+
+ image_info->artwork_list =
+ checked_calloc(num_file_list_entries * sizeof(ImageInfo *));
+
+ image_info->content_list = NULL;
+
+ image_info->load_artwork = Load_PCX;
+ image_info->free_artwork = FreeImage;
+}
+
+void ReloadCustomImages()
+{
+#if 0
+ printf("DEBUG: reloading images '%s' ...\n", artwork.gfx_current_identifier);
+#endif
+
+ ReloadCustomArtworkList(image_info);
+}
+
+void FreeAllImages()
+{
+ FreeCustomArtworkList(image_info);
+}