home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 1
/
FFMCD01.bin
/
bbs
/
graphics
/
ppmqvga.lha
/
PPMQVGA
/
src
/
ppm
/
ppmtoilbm.c
< prev
Wrap
C/C++ Source or Header
|
1993-09-01
|
31KB
|
996 lines
/* ppmtoilbm.c - read a portable pixmap and produce an IFF ILBM file
**
** Copyright (C) 1989 by Jef Poskanzer.
** Modified 20/Jun/93 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
** - 24bit support (new options -24if, -24force)
** - HAM8 support (well, anything from HAM3 to HAM(MAXPLANES))
** - now writes up to 8 (16) planes (new options -maxplanes, -fixplanes)
** - colormap file (new option -map)
** - write colormap only (new option -cmaponly)
** - only writes CAMG chunk if its a HAM-picture
** Modified 29/Aug/93 by Ingo Wilken
** - operates row-by-row whenever possible
** - faster colorscaling with lookup-table (~20% faster on HAM pictures)
** - options -ham8 and -ham6 now imply -hamforce
**
**
** std HAM 24bit cmap direct
** -------+-----+-----+-----+-----+-----
** BMHD yes yes yes yes yes
** CMAP yes (1) no yes no
** BODY yes yes yes no yes
** other - CAMG - - DCOL
** nPlanes 1-8 3-8 24 0 3-24 if configured without ILBM_BIGRAW
** nPlanes 1-16 3-16 24 0 3-48 if configured with ILBM_BIGRAW
**
** (1): grayscale colormap
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation. This software is provided "as is" without express or
** implied warranty.
*/
#include "ppm.h"
#include "ppmcmap.h"
#include "ilbm.h"
#define MODE_DIRECT 4 /* direct color ILBM */
#define MODE_CMAP 3 /* write normal file, but colormap only */
#define MODE_24 2 /* write a 24bit (deep) ILBM */
#define MODE_HAM 1 /* write a HAM */
#define MODE_NONE 0 /* write a normal picture */
#define ECS_MAXPLANES 5
#define ECS_HAMPLANES 6
#define AGA_MAXPLANES 8
#define AGA_HAMPLANES 8
#ifdef AMIGA_AGA
#define DEF_MAXPLANES AGA_MAXPLANES
#define DEF_HAMPLANES AGA_HAMPLANES
#else
#define DEF_MAXPLANES ECS_MAXPLANES
#define DEF_HAMPLANES ECS_HAMPLANES
#endif
#define DEF_DCOLPLANES 5
static int colorstobpp ARGS((int colors));
#define put_fourchars(str) (void)(fputs(str, stdout))
static void put_big_short ARGS((short s));
static void put_big_long ARGS((long l));
#define put_byte(b) (void)(putc((unsigned char)(b), stdout))
static void ppm_to_ham ARGS((FILE *fp, int cols, int rows, int maxval, int hambits));
static void ppm_to_24 ARGS((FILE *fp, int cols, int rows, int maxval));
static void ppm_to_direct ARGS((FILE *fp, int cols, int rows, int maxval, DirectColor *direct));
static void ppm_to_std ARGS((FILE *fp, int cols, int rows, int maxval, colorhist_vector chv, int colors, int nPlanes));
static void ppm_to_cmap ARGS((int maxval, colorhist_vector chv, int colors));
static void write_form_ilbm ARGS((int size));
static void write_bmhd ARGS((int cols, int rows, int nPlanes));
static void write_std_cmap ARGS((colorhist_vector chv, int colors, int maxval));
static void encode_row ARGS((rawtype *row, int cols, int nPlanes));
static int get_int_val ARGS((char *string, char *option, int bot, int top));
static pixel * next_pixrow ARGS((FILE *fp, int row));
static pixval * make_val_table ARGS((pixval oldmaxval, pixval newmaxval));
static void * xmalloc ARGS((int bytes));
static void init_read ARGS((FILE *fp, int *colsP, int *rowsP, pixval *maxvalP, int readall));
static unsigned char *coded_rowbuf;
static pixel **pixels;
static pixel *pixrow;
#define NEWDEPTH(pix, table) PPM_ASSIGN((pix), (table)[PPM_GETR(pix)], (table)[PPM_GETG(pix)], (table)[PPM_GETB(pix)])
#define MAXCOLORS (1<<maxplanes)
int
main(argc, argv)
int argc;
char *argv[];
{
FILE *ifp;
int argn, rows, cols, colors, nPlanes;
int ifmode, forcemode, maxplanes, fixplanes, hambits, mode;
pixval maxval;
colorhist_vector chv;
DirectColor dcol;
char *mapfile;
char *usage =
"[-ecs|-aga] [-ham6|-ham8] [-maxplanes|-mp n] [-fixplanes|-fp n] \
[-normal|-hamif|-hamforce|-24if|-24force|-dcif|-dcforce|-cmaponly] \
[-hambits|-hamplanes n] [-dcbits|-dcplanes r g b] \
[-map ppmfile] [ppmfile]";
ppm_init(&argc, argv);
ifmode = MODE_NONE; forcemode = MODE_NONE;
maxplanes = DEF_MAXPLANES; fixplanes = 0;
hambits = DEF_HAMPLANES;
mapfile = NULL;
dcol.r = dcol.g = dcol.b = DEF_DCOLPLANES;
argn = 1;
while( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) {
if( pm_keymatch(argv[argn], "-maxplanes", 4) || pm_keymatch(argv[argn], "-mp", 3) ) {
if( ++argn >= argc )
pm_usage(usage);
maxplanes = get_int_val(argv[argn], argv[argn-1], 1, MAXPLANES);
fixplanes = 0;
}
else
if( pm_keymatch(argv[argn], "-fixplanes", 4) || pm_keymatch(argv[argn], "-fp", 3) ) {
if( ++argn >= argc )
pm_usage(usage);
fixplanes = get_int_val(argv[argn], argv[argn-1], 1, MAXPLANES);
maxplanes = fixplanes;
}
else
if( pm_keymatch(argv[argn], "-map", 4) ) {
if( ++argn >= argc )
pm_usage(usage);
mapfile = argv[argn];
}
else
if( pm_keymatch(argv[argn], "-cmaponly", 3) ) {
forcemode = MODE_CMAP;
}
else
if( pm_keymatch(argv[argn], "-hambits", 5) || pm_keymatch(argv[argn], "-hamplanes", 5) ) {
if( ++argn > argc )
pm_usage(usage);
hambits = get_int_val(argv[argn], argv[argn-1], 3, MAXPLANES);
}
else
if( pm_keymatch(argv[argn], "-ham6", 5) ) {
hambits = ECS_HAMPLANES;
forcemode = MODE_HAM;
}
else
if( pm_keymatch(argv[argn], "-ham8", 5) ) {
hambits = AGA_HAMPLANES;
forcemode = MODE_HAM;
}
else
if( pm_keymatch(argv[argn], "-ecs", 2) ) {
maxplanes = ECS_MAXPLANES;
hambits = ECS_HAMPLANES;
}
else
if( pm_keymatch(argv[argn], "-aga", 2) ) {
maxplanes = AGA_MAXPLANES;
hambits = AGA_HAMPLANES;
}
else
if( pm_keymatch(argv[argn], "-hamif", 5) )
ifmode = MODE_HAM;
else
if( pm_keymatch(argv[argn], "-nohamif", 7) ) {
if( ifmode == MODE_HAM )
ifmode = MODE_NONE;
}
else
if( pm_keymatch(argv[argn], "-hamforce", 5) )
forcemode = MODE_HAM;
else
if( pm_keymatch(argv[argn], "-nohamforce", 7) ) {
if( forcemode == MODE_HAM )
forcemode = MODE_NONE;
}
else
if( pm_keymatch(argv[argn], "-24if", 4) )
ifmode = MODE_24;
else
if( pm_keymatch(argv[argn], "-no24if", 6) ) {
if( ifmode == MODE_24 )
ifmode = MODE_NONE;
}
else
if( pm_keymatch(argv[argn], "-24force", 4) )
forcemode = MODE_24;
else
if( pm_keymatch(argv[argn], "-no24force", 6) ) {
if( forcemode == MODE_24 )
forcemode = MODE_NONE;
}
else
if( pm_keymatch(argv[argn], "-dcif", 4) ) {
ifmode = MODE_DIRECT;
}
else
if( pm_keymatch(argv[argn], "-nodcif", 6) ) {
if( ifmode == MODE_DIRECT )
ifmode = MODE_NONE;
}
else
if( pm_keymatch(argv[argn], "-dcforce", 4) ) {
forcemode = MODE_DIRECT;
}
else
if( pm_keymatch(argv[argn], "-nodcforce", 6) ) {
if( forcemode == MODE_DIRECT )
forcemode = MODE_NONE;
}
else
if( pm_keymatch(argv[argn], "-dcbits", 4) || pm_keymatch(argv[argn], "-dcplanes", 4) ) {
char *option = argv[argn];
if( ++argn >= argc )
pm_usage(usage);
dcol.r = (unsigned char) get_int_val(argv[argn], option, 1, MAXPLANES);
if( ++argn >= argc )
pm_usage(usage);