/* * pms.c -- save loaded image as pms * (C)Copyright 1998, 99 by Hiroshi Takekawa * This file is part of Enfle. * * Last Modified: Mon Sep 27 02:35:55 1999. * $Id: pms.c,v 1.3 1999/09/28 04:48:07 sian Exp $ * * Enfle is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Enfle is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #include "enfle.h" #include "version.h" #include "plugin.h" #include "pms.h" #include "utils.h" int #ifdef PIC get_plugininfo(PluginInfo *p) #else saver_pms_get_plugininfo(PluginInfo *p) #endif { p->version = 1; p->type = _Saver; p->pluginname = "PMS Format Saver Plugin version 0.1.1"; p->pluginshortname = FORMAT_NAME; p->author = "Hiroshi Takekawa"; p->dlhandle = NULL; /* set by plugin_load */ p->functions.saver.save_image = pms_save_image; return 1; } static int pms_save_image_256(Info *info, Image *p, FILE *fp) { int i = p->width * p->height, count; unsigned char *s = p->image, c; /* compression not yet completed(implemented run-length only) */ c = *s++; count = 1; for (i--; i > 0; i--) { if (c != *s || count == 259 || i == 0) { if (count >= 4) { fputc(0xfd, fp); fputc(count - 4, fp); fputc(c, fp); } else { for (; count > 0; count--) if (c < 0xf8) fputc(c, fp); else { fputc(0xf8, fp); fputc(c, fp); } } if (i > 0) { c = *s++; count = 1; } } else { count++; s++; } } return 1; } static int pms_save_image_64k(Info *info, Image *p, FILE *fp) { int i = p->width * p->height; unsigned char *s = p->image; /* compression not yet implemented */ for (; i > 0; i--) if (*s < 0xf8) { fputc((int)*s++, fp); fputc((int)*s++, fp); } else { fputc(0xf8, fp); fputc((int)*s++, fp); fputc((int)*s++, fp); } return 1; } int pms_save_image(Info *info, Image *p, char *outfile) { FILE *fp = NULL; unsigned char *comment = "Created by enfle " VERSION " / " COPYRIGHT; unsigned char pmsh[48]; int f, if_pal; int comment_len = strlen(comment); memset(pmsh, 0, 48); pmsh[0] = 'P'; pmsh[1] = 'M'; pmsh[2] = 1; /* version */ put_little_word(pmsh + 4, 0x30 + comment_len + 1); /* headersize */ put_little_dword(pmsh + 16, p->left); put_little_dword(pmsh + 20, p->top); put_little_dword(pmsh + 24, p->width); put_little_dword(pmsh + 28, p->height); switch (p->type) { case _INDEX: pmsh[6] = 8; put_little_dword(pmsh + 32, 0x30 + comment_len + 1 + 256 * 3); put_little_dword(pmsh + 36, 0x30 + comment_len + 1); put_little_dword(pmsh + 40, 0x30); if_pal = 1; break; case _RGB24: image_reduce(p, _RGB16); case _RGB16: pmsh[6] = 16; put_little_dword(pmsh + 32, 0x30 + comment_len + 1); put_little_dword(pmsh + 36, 0); /* alpha is not yet implemented */ put_little_dword(pmsh + 40, 0x30); if_pal = 0; break; default: fprintf(stderr, "Unsupported image type: %d\n", p->type); return 0; } if ((fp = fopen(outfile, "wb")) == NULL) return 0; /* write the header */ if (fwrite(pmsh, 1, 48, fp) != 48) { perror("pms_save_image() in writing header: "); fclose(fp); return 0; } /* write the comment */ if (fwrite(comment, 1, comment_len + 1, fp) != comment_len + 1) { perror("pms_save_image() in writing comment"); fclose(fp); return 0; } /* write the palette */ if (if_pal) if (fwrite(p->colormap, 1, 256 * 3, fp) != 256 * 3) { perror("pms_save_image() in writing palette"); fclose(fp); return 0; } /* compress and write pixels */ f = if_pal ? pms_save_image_256(info, p, fp) : pms_save_image_64k(info, p, fp); fclose(fp); return f; }