This is ray2tiff.c in view mode; [Download] [Up]
#import <stdio.h>
#import <appkit/tiff.h>
#import <streams/streams.h>
void
help(void)
{
fprintf(stderr, "\nray2tiff converts raw 24-bit RGB to 8-bit grayscale TIFF.\n");
fprintf(stderr, "It expects the 24-bit RGB data on standard input, and\n");
fprintf(stderr, " writes the TIFF data to standard output.\n");
fprintf(stderr, "It always requires two arguments, the width and height\n");
fprintf(stderr, " of the incoming data.\n");
exit(-667);
}
void
bad_args(void)
{
fprintf(stderr, "\nDimensions must be positive integers.\n");
exit(-668);
}
void
out_of_data(void)
{
fprintf(stderr, "\nI didn't get as much data as I expected.\n");
fprintf(stderr, "Maybe your arguments for width or height was wrong.\n");
exit(-669);
}
main(int argc, char *argv[])
{
NXStream *theStream;
int extraData = 0, pixelsDone = 0, red, green, blue, gray;
NXImageInfo theImageInfo;
char *theData;
/* interpret args, reject bad args */
if (argc != 3)
help();
theImageInfo.width = atoi(argv[1]);
theImageInfo.height = atoi(argv[2]);
if ((theImageInfo.width < 1) || (theImageInfo.height < 1))
bad_args();
theImageInfo.bitsPerSample = 8;
theImageInfo.samplesPerPixel = 1;
theImageInfo.planarConfig = 1;
theImageInfo.photoInterp = 1;
theData = (char *)malloc(theImageInfo.width * theImageInfo.height);
theStream = NXOpenFile(fileno(stdin), NX_READONLY);
/* read in data */
for (pixelsDone = 0; pixelsDone < (theImageInfo.width * theImageInfo.height); pixelsDone++) {
if ((red = NXGetc(theStream)) == EOF)
out_of_data();
if ((green = NXGetc(theStream)) == EOF)
out_of_data();
if ((blue = NXGetc(theStream)) == EOF)
out_of_data();
gray = (red + green + blue) / 3;
theData[pixelsDone] = (unsigned char)gray;
if (pixelsDone % theImageInfo.width == 0)
fprintf(stderr, ".");
}
NXWriteTIFF(NXOpenFile(fileno(stdout), NX_WRITEONLY), &theImageInfo, theData);
/*
* Hah! we're lazy! we don't close streams! we don't free allocated
* space! Hah!
*/
exit(0);
}
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.