📄 txt.c
字号:
/* * -*- Mode: ANSI C -*- * $Id: txt.c,v 1.8 1996/11/20 02:38:32 fernande Exp $ * $Source: /sgi.acct/sweldens/cvs/liftpack/Files/txt.c,v $ * Author: Gabriel Fernandez * * Contains the routines for manipulating text data files * in a sequential fashion. *//* do not edit anything above this line *//* System header files */#include <stdio.h>#include <string.h>/* FLWT header files */#include <flwtdef.h>#include <file.h>#include <flwterr.h>#include <imgmem.h>#include <memchk.h>#include <txt.h>#include <util.h>/* Static variables */static int garbage;static long filesize;/* Private functions */static int GetComment ( FILE *__fp, Image *__image );/* code */intLoadTXT ( char *filename, Image *image ){ Vector A; FILE *fp; int width, height, rv, maxv, i; Flt datum; /* Set initial values */ garbage = rv = 0; image->band[RED] = (Matrix)NULL; image->band[GREEN] = (Matrix)NULL; image->band[BLUE] = (Matrix)NULL; image->comment = (char *)NULL; image->levels = (int *)NULL; /* Open the file */ fp = OpenFile (filename, "r"); /* Compute file length */ fseek (fp, 0L, 2); filesize = ftell(fp); fseek (fp, 0L, 0); /* Read magic number */ if (fscanf (fp, "TXT\n") < 0 ) { fclose (fp); Error ("LoadTXT", READ_ERROR, RETURN); return 0; } /* Check for comments */ if ( !GetComment (fp, image) ) { fclose (fp); Error ("LoadTXT", CUSTOM, RETURN, "Premature end-of-file!\n"); return 0; } if (garbage) { Error ("LoadTXT", CUSTOM, RETURN, "Garbage characters in image data."); return 0; } /* Read dimensions */ if (fscanf (fp, "%d %d\n", &width, &height) < 2) { fclose (fp); Error ("LoadTXT", READ_ERROR, RETURN); return 0; } image->width = width; image->height = height; /* Read maximum value */ if (fscanf (fp, "%d\n", &maxv) < 1) { fclose (fp); Error ("LoadTXT", READ_ERROR, RETURN); return 0; } /* Initialize image structure */ rv = (int)IMG_InitMem (image, (long)width, (long)height, 1, 8); if ( !rv ) { Error ("loadpbm", MEMORY_IMAGE, ABORT); /* NOTREACHED */ } image->frmType = F_TXT; image->colType = 1; /* Greyscale */ sprintf (image->fullInfo, "TXT, %s format. (%ld bytes)", "ascii", filesize); sprintf (image->shrtInfo, "%dx%d TXT", width, height); /* Read data */ A = image->band[GREY][0]; while (height--) { for ( i=0 ; i<width ; ++i ) {#ifndef DOUBLE if ( fscanf (fp, "%f ", &datum) == 0 ) { fclose (fp); Error ("LoadTXT", READ_ERROR, RETURN); return 0; }#else if ( fscanf (fp, "%lf ", &datum) == 0 ) { fclose (fp); Error ("LoadTXT", READ_ERROR, RETURN); return 0; }#endif *(A++) = datum; } } /* Close the file */ CloseFile (fp); return 1;}intWriteTXT ( FILE *fp, const Image image ){ int i, w, h; Vector imgPtr; char buf[BUFSIZ]; /* Print info to user */ sprintf (buf, "%4dx%-4d %s, %s format.", image.width, image.height, "TXT", "ascii"); fprintf (stdout, "\n[%s]", buf); /* Write image magic number */ if ( fprintf (fp, "TXT\n") <= 0 ) { fclose (fp); Error ("WriteTXT", WRITE_ERROR, RETURN); return 0; } /* Write comments */ fprintf(fp,"# Creator: flwt %s\n", REVDATE); if (image.comment) { /* write comment lines */ char *sp = image.comment; while (*sp) { fprintf (fp, "# "); while (*sp && *sp != '\n') fputc((int)*sp++, fp); if (*sp == '\n') sp++; fputc('\n', fp); } } /* Write rest of header info */ if ( fprintf (fp, "%d %d\n255\n", image.width, image.height) <= 0 ) { fclose (fp); Error ("WriteTXT", WRITE_ERROR, RETURN); return 0; } /* Write data */ imgPtr=&image.band[0][0][0]; w = image.width; h = image.height; while (h--) { for ( i=0 ; i<w ; ++i ) {#ifdef DOUBLE *imgPtr = ((ABS(*imgPtr) < 6e-16) ? 0.0 : *imgPtr); if ( fprintf (fp, "%.16g ", *(imgPtr++)) == 0 ) { fclose (fp); Error ("WriteTXT", WRITE_ERROR, RETURN); return 0; }#else *imgPtr = ((ABS(*imgPtr) < 6e-8) ? 0.0 : *imgPtr); if ( fprintf (fp, "%.8g ", *(imgPtr++)) == 0 ) { fclose (fp); Error ("WriteTXT", WRITE_ERROR, RETURN); return 0; }#endif } fprintf (fp, "\n"); } return 1;}/** Private Functions **//* * GetComment function: gets the comments until a different character is * reached. */static intGetComment ( FILE *fp, Image *image ){ int c, firstchar; /* Skip forward to end of comments */ c = getc(fp); while (1) { /* Eat comments */ if ( c=='#' ) { /* if we're at a comment, read to end of line */ char cmt[256], *sp, *tmpptr; sp = cmt; firstchar = 1; while (1) { c = getc(fp); if ( firstchar && c == ' ') firstchar = 0; /* loop off 1 sp after # */ else { if ( c == '\n' || c == EOF ) break; if ( (sp-cmt)<250 ) *sp++ = (char)c; } } *sp++ = '\n'; *sp = '\0'; if ( strlen(cmt) > 0 ) { /* add to image->comment */ if ( !image->comment ) { image->comment = (char *) malloc(strlen(cmt) + 1); if ( !image->comment ) Error ("GetComment", MALLOC_FAIL, ABORT); /* NOTREACHED */ image->comment[0] = '\0'; } else { tmpptr = (char *) realloc(image->comment, strlen(image->comment) + strlen(cmt) + 1); if (!tmpptr) Error ("GetComment", REALLOC_FAIL, ABORT); /* NOTREACHED */ image->comment = tmpptr; } strcat(image->comment, cmt); } if ( c==EOF ) return 0; /* see if we're getting garbage */ if ( c!=' ' && c!='\t' && c!='\r' && c!='\n' && c!=',' ) garbage=1; c = getc(fp); } else { ungetc (c, fp); break; } } return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -