/*
* tkImgPPM.c --
*
* A photo image file handler for PPM (Portable PixMap) files.
*
* Copyright (c) 1994 The Australian National University.
* Copyright (c) 1994-1996 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* Author: Paul Mackerras (paulus@cs.anu.edu.au),
* Department of Computer Science,
* Australian National University.
*
* SCCS: @(#) tkImgPPM.c 1.13 96/03/18 14:56:41
*/
#include "tkInt.h"
/*
* The maximum amount of memory to allocate for data read from the
* file. If we need more than this, we do it in pieces.
*/
/*
* Define PGM and PPM, i.e. gray images and color images.
*/
/*
* The format record for the PPM file format:
*/
char *formatString, int *widthPtr,
int *heightPtr));
char *fileName, char *formatString,
"PPM", /* name */
FileMatchPPM, /* fileMatchProc */
NULL, /* stringMatchProc */
FileReadPPM, /* fileReadProc */
NULL, /* stringReadProc */
FileWritePPM, /* fileWriteProc */
NULL, /* stringWriteProc */
};
/*
* Prototypes for local procedures defined in this file:
*/
int *heightPtr, int *maxIntensityPtr));
/*
*----------------------------------------------------------------------
*
* FileMatchPPM --
*
* This procedure is invoked by the photo image type to see if
* a file contains image data in PPM format.
*
* Results:
* The return value is >0 if the first characters in file "f" look
* like PPM data, and 0 otherwise.
*
* Side effects:
* The access position in f may change.
*
*----------------------------------------------------------------------
*/
static int
FILE *f; /* The image file, open for reading. */
char *fileName; /* The name of the image file. */
char *formatString; /* User-specified format string, or NULL. */
* returned here if the file is a valid
* raw PPM file. */
{
int dummy;
}
/*
*----------------------------------------------------------------------
*
* FileReadPPM --
*
* This procedure is called by the photo image type to read
* PPM format data from a file and write it into a given
* photo image.
*
* Results:
* A standard TCL completion code. If TCL_ERROR is returned
* then an error message is left in interp->result.
*
* Side effects:
* The access position in file f is changed, and new data is
* added to the image given by imageHandle.
*
*----------------------------------------------------------------------
*/
static int
FILE *f; /* The image file, open for reading. */
char *fileName; /* The name of the image file. */
char *formatString; /* User-specified format string, or NULL. */
* photo image to be written to. */
* be written to. */
* in image being read. */
{
unsigned char *pixelPtr;
if (type == 0) {
return TCL_ERROR;
}
if ((fileWidth <= 0) || (fileHeight <= 0)) {
"\" has dimension(s) <= 0", (char *) NULL);
return TCL_ERROR;
}
"\" has bad maximum intensity value ", buffer,
(char *) NULL);
return TCL_ERROR;
}
}
}
return TCL_OK;
}
}
else {
}
if (srcY > 0) {
}
}
if (nLines <= 0) {
nLines = 1;
}
if (nLines > h) {
nLines = h;
}
fileName, "\": ",
(char *) NULL);
return TCL_ERROR;
}
if (maxIntensity != 255) {
unsigned char *p;
*p = (((int) *p) * 255)/maxIntensity;
}
}
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* FileWritePPM --
*
* This procedure is invoked to write image data to a file in PPM
* format.
*
* Results:
* A standard TCL completion code. If TCL_ERROR is returned
* then an error message is left in interp->result.
*
* Side effects:
* Data is written to the file given by "fileName".
*
*----------------------------------------------------------------------
*/
static int
char *fileName;
char *formatString;
{
FILE *f;
int w, h;
(char *)NULL);
return TCL_ERROR;
}
goto writeerror;
}
} else {
goto writeerror;
}
}
}
}
if (fclose(f) == 0) {
return TCL_OK;
}
f = NULL;
if (f != NULL) {
fclose(f);
}
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* ReadPPMFileHeader --
*
* This procedure reads the PPM header from the beginning of a
* PPM file and returns information from the header.
*
* Results:
* The return value is PGM if file "f" appears to start with
* a valid PGM header, PPM if "f" appears to start with a valid
* PPM header, and 0 otherwise. If the header is valid,
* then *widthPtr and *heightPtr are modified to hold the
* dimensions of the image and *maxIntensityPtr is modified to
* hold the value of a "fully on" intensity value.
*
* Side effects:
* The access position in f advances.
*
*----------------------------------------------------------------------
*/
static int
FILE *f; /* Image file to read the header from */
* returned here. */
int *maxIntensityPtr; /* The maximum intensity value for
* the image is stored here. */
{
int i, numFields, c;
int type = 0;
/*
* Read 4 space-separated fields from the file, ignoring
* comments (any line that starts with "#").
*/
c = getc(f);
i = 0;
/*
* Skip comments and white space.
*/
while (1) {
c = getc(f);
}
if (c != '#') {
break;
}
do {
c = getc(f);
} while ((c != EOF) && (c != '\n'));
}
/*
* Read a field (everything up to the next white space).
*/
if (i < (BUFFER_SIZE-2)) {
buffer[i] = c;
i++;
}
c = getc(f);
}
if (i < (BUFFER_SIZE-1)) {
buffer[i] = ' ';
i++;
}
}
buffer[i] = 0;
/*
* Parse the fields, which are: id, width, height, maxIntensity.
*/
} else {
return 0;
}
!= 3) {
return 0;
}
return type;
}