?? file.c
字號:
/* * -*- Mode: ANSI C -*- * $Id: file.c,v 1.4 1996/09/17 16:10:27 fernande Exp $ * $Source: /sgi.acct/sweldens/cvs/liftpack/Files/file.c,v $ * Author: Gabriel Fernandez * * Functions to manipulate different file formats such as GIF, PNM, * BMP, TXT, and WTF. *//* do not edit anything above this line *//* System header files */#include <stdio.h>#include <string.h>/* FLWT header files */#include <flwtdef.h>#include <flwterr.h>#include <file.h>#include <memchk.h>#include <pbm.h>#include <txt.h>#include <util.h>#include <wtf.h>/* code *//* * OpenFile function: given a file name and a set of options, a file * is opened with the fopen() function verifying for * any error that may occur. * The pointer to the file is returned by the function. */FILE *OpenFile ( const char *filename, const char *options ){ FILE *fp = NULL; /* Open the given file for reading or writing */ /* Check for its validity */ if ( (fp = fopen (filename, options)) == NULL ) { Error ("OpenFile", NO_FILE, ABORT, filename); } return fp;}/* * CloseFile function: the file pointed by fp is closed using fclose(). */voidCloseFile ( FILE *fp ){ /* fprintf (stdout, "Closing file.\n"); */ fclose (fp);}/* * SetOutputType function: verifies for the given output type to be * compatible with the input filetype. It also * checks for the validation of the given * output type. */voidSetOutputType ( char *outType, const FileFormat inType, Image *image, const boolean packets ){ /* Set output file type */ if ( !strcmp (outType, "pbm") ) { image->frmType = F_PBMRAW; image->colType = 2; } else if ( !strcmp (outType, "pgm") ) { image->frmType = F_PBMRAW; image->colType = 1; } else if ( !strcmp (outType, "ppm") ) { image->frmType = F_PBMRAW; image->colType = 0; } else if ( !strcmp (outType, "gif") ) { Error ("SetOutputType", CUSTOM, RETURN, "GIF not supported yet. Using same type as input file.\n"); *outType = '\0'; SetOutputType (outType, inType, image, packets); } else if ( !strcmp (outType, "bmp") ) { Error ("SetOutputType", CUSTOM, RETURN, "BMP not supported yet. Using same type as input file.\n"); *outType = '\0'; SetOutputType (outType, inType, image, packets); } else if ( !strcmp (outType, "txt") ) { image->frmType = F_TXT; } else if ( !strcmp (outType, "wgm") ) { if ( !packets ) { image->frmType = F_WTFWLT; } else { image->frmType = F_WTFPCK; } } else if ( !strcmp (outType, "wpm") ) { if ( !packets ) { image->frmType = F_WTFWLT; } else { image->frmType = F_WTFPCK; } } else { /* Set outType depending on inType */ switch (inType) { case FMT_GIF: image->frmType = F_GIF; break; case FMT_PBM: image->frmType = F_PBMRAW; break; case FMT_BMP: image->frmType = F_BMP; break; case FMT_TXT: image->frmType = F_TXT; break; case FMT_WTF: image->frmType = F_WTFWLT; break; default: Error ("SetOutputType", NO_FORMAT, ABORT); /* NOTREACHED */ } }}/* * ReadFileType function: opens filename, which should be an actual * file as this point, and reads the first * couple of bytes. Figures out what the file * is like to be and returns the appropriate * FMT_*** code. */FileFormatReadFileType ( const char *filename ){ FILE *fp; byte magicno[10]; FileFormat type; unsigned int n; if ( !filename ) /* this shouldn't happen */ return FMT_ERROR; fp = OpenFile ( filename, "r" ); n = fread (magicno, (size_t)1, (size_t)10, fp); CloseFile (fp); if ( n<10 ) return FMT_UNKNOWN; /* files less then 10 bytes long... */ /*****************/ /* Set file type */ /*****************/ type = FMT_UNKNOWN; /* default value */ if ( strncmp( (char *)magicno, "GIF87a", (size_t)6 ) == 0 || strncmp( (char *)magicno, "GIF89a", (size_t)6 ) == 0 ) type = FMT_GIF; else if ( magicno[0] == 'P' && magicno[1] >= '1' && magicno[1] <= '6' ) type = FMT_PBM; else if ( magicno[0] == 'B' && magicno[1] == 'M' ) type = FMT_BMP; else if ( strncmp( (char *)magicno, "TXT", (size_t)3 ) == 0 ) type = FMT_TXT; else if ( magicno[0] == 'W' && magicno[1] >= '1' && magicno[1] <= '4' ) type = FMT_WTF; return type;}/* * ReadFile function: the given file is read using the appropiated * function established by filetype. The results * of the reading, i.e. the data and information * related to it, are stored in the image structure. * Returns '0' if not successful. */intReadFile ( char *filename, const FileFormat filetype, Image *image ){ int rv = 0; /* Check type and call appropriate routine */ switch (filetype) { case FMT_GIF: /* rv = LoadGIF ( filename, image ); */ break; case FMT_PBM: rv = LoadPBM ( filename, image ); break; case FMT_BMP: /* rv = LoadBMP ( filename, image ); */ break; case FMT_TXT: rv = LoadTXT ( filename, image ); break; case FMT_WTF: rv = LoadWTF ( filename, image ); break; default: Error ( "ReadFile", NO_FORMAT, ABORT ); } return rv;}/* * WriteFile function: opens file, does appropriate color * pre-processing, calls save routine based on * chosen format. Returns '0' if successful. */intWriteFile ( char *filename, const Image image ){ FILE *fp; byte *pic, *pix; int i, j, w, h, rv; /* Output image dimensions */ w = image.width; h = image.height; /* Copy data into byte vector, if necessary */ pic = NULL; if ( image.frmType == F_PBMRAW || image.frmType == F_PBMASCII ) { if ( image.colorPlanes == 1 ) { pic = (byte *) calloc ((size_t)(w*h), (size_t)1); if ( !pic ) { Error ("WriteFile", CALLOC_FAIL, ABORT); /* NOTREACHED */ } for ( i=0, pix=pic ; i<h ; i++ ) { for ( j=0 ; j<w ; j++, pix++ ) *pix = (byte)ROUND( image.band[GREY][i][j], 0, 255 ); } } else if ( image.colorPlanes == 3 ) { pic = (byte *) calloc ((size_t)(w*h*3), (size_t)1); if ( !pic ) { Error ("WriteFile", CALLOC_FAIL, ABORT); /* NOTREACHED */ } for ( i=0, pix=pic ; i<h ; i++ ) { for ( j=0 ; j<w ; j++, pix+=3 ) { pix[0] = (byte)ROUND( image.band[RED][i][j], 0, 255 ); pix[1] = (byte)ROUND( image.band[GREEN][i][j], 0, 255 ); pix[2] = (byte)ROUND( image.band[BLUE][i][j], 0, 255 ); } } } else { /* color planes == 2 */ /* don't do anything */ } } /* Open the file for writing */ fp = OpenFile (filename, "w"); /* Check type and call appropriate routine */ rv = 0; switch (image.frmType) { case F_GIF: /* rv = WriteGIF ( ); */ break; case F_PBMRAW: rv = WritePBM (fp, pic, w, h, image.colType, image.bpp, 1, image.comment); break; case F_PBMASCII: rv = WritePBM (fp, pic, w, h, image.colType, image.bpp, 0, image.comment); break; case F_BMP: /* rv = WriteBMP ( ); */ break; case F_TXT: rv = WriteTXT ( fp, image ); break; case F_WTFWLT: rv = WriteWTF (fp, &image.band[0][0][0], w, h, image.colType, image.bpp, image.levels, image.blocks, 1, image.comment); break; case F_WTFPCK: rv = WriteWTF (fp, &image.band[0][0][0], w, h, image.colType, image.bpp, image.levels, image.blocks, 0, image.comment); break; default: Error ( "WriteFile", NO_FORMAT, ABORT ); /* NOTREACHED */ } /* Close output file */ CloseFile (fp); /* Free memory */ if ( image.frmType == F_PBMRAW || image.frmType == F_PBMASCII ) free (pic); return rv;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -