ftp.nice.ch/pub/next/graphics/viewer/ToyViewer.2.6a.s.tar.gz#/ToyViewer2.6a/src/ImageSave.bproj/paowrite.c

This is paowrite.c in view mode; [Download] [Up]

#include <stdio.h>
#include <stdlib.h>
#include <libc.h>
#include "../common.h"
#include "../strfunc.h"
#include "save.h"

static void png_write_p(FILE *fp, const commonInfo *cinf)
{
	int i, n;
	int r, g, b, a;
	int alphaindex = -1;

	/* PXO */
	fprintf(fp, "PX\n# %s\n%d %d %d\n",
		key_comm(cinf), cinf->width, cinf->height, cinf->palsteps - 1);
	if (cinf->alpha) {
		alphaindex = cinf->palsteps;
		if (alphaindex >= FIXcount)
			alphaindex = FIXcount - 1;	/* Error */
		fprintf(fp, "1 %d\n", alphaindex + 256);
	}else
		fprintf(fp, "0\n");
	for (n = 0; n < cinf->palsteps; n++) {
		unsigned char *p = cinf->palette[n];
		for (i = 0; i < 3; i++)
			putc(*p++, fp);
	}
	for (n = cinf->width * cinf->height - 1; n >= 0; n--) {
		getPixel(&r, &g, &b, &a);
		putc( (a == AlphaTransp ? alphaindex : mapping(r, g, b)), fp);
	}
}

static void png_write_f(FILE *fp, const commonInfo *cinf)
{
	int max = 255, sft = 0, i, n, pn;
	int elm[MAXPLANE];

	switch (cinf->bits) {
	case 1: max = 1;  sft = 7;  break;
	case 2: max = 3;  sft = 6;  break;
	case 4: max = 15;  sft = 4;  break;
	case 8: max = 255;  sft = 0;  break;
	}
	pn = cinf->numcolors;
	if (cinf->alpha) pn++;
	/* PAO */
	fprintf(fp, "PA\n# %s\n%d %d %d %d\n",
			key_comm(cinf), cinf->width, cinf->height, max, pn);
	n = cinf->width * cinf->height;
	while (n-- > 0) {
		(void) getPixelA(elm);
		if (sft)
			for (i = 0; i < pn; i++)
				putc(elm[i] >> sft, fp);
		else
			for (i = 0; i < pn; i++)
				putc(elm[i], fp);
	}
}

int pngwrite(FILE *fp, const commonInfo *cinf, const char *dir, BOOL interl)
{
	int err;
	char ppngPath[MAXFILENAMELEN];
	static char *ppngArg[8] = {
		NULL,		/* 0: Tool Path */
		PAO_PNG,
		NULL,		/* 2: -p (maybe) */
		NULL, NULL };

	sprintf(ppngPath, "%s/%s", dir, PAO_PNG);
	ppngArg[0] = ppngPath;
	ppngArg[2] = interl ? "-p" : NULL;	/* Progressive */

	if ((fp = openWPipe(fp, ppngArg, &err)) == NULL) {
		(void) fclose(fp);
		return err;
	}

	if (cinf->palette && cinf->palsteps > 0)
		png_write_p(fp, cinf);
	else
		png_write_f(fp, cinf);

	(void) fclose(fp);
	wait(0);	/* Don't forget */
	return 0;
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.