📄 pbm.c
字号:
return 0; } /* Scale all RGB values up to range 0-255 */ if ( maxv<255 ) { for ( i=0 ; i<=maxv ; i++) scale[i] = (byte)( (i * 255) / maxv ); for ( i=0, pix=pic24 ; i<h ; i++ ) { for ( j=0 ; j<w*3 ; j++, pix++ ) *pix = scale[*pix]; } } /* Copy byte values to image bands */ for ( i=0, pix=pic24 ; i<h ; i++ ) { for ( j=0 ; j<w ; j++, pix+=3 ) { image->band[RED][i][j] = (Flt)(pix[0]); image->band[GREEN][i][j] = (Flt)(pix[1]); image->band[BLUE][i][j] = (Flt)(pix[2]); } } free ( (byte *)pic24 ); return 1;}/* * getint function: gets and integer value from the actual position * pointed by fp. */static int getint ( FILE *fp, Image *image ){ int c, i, firstchar; /* Note: if it sees a '#' character, all characters from there to end of line are appended to the comment string */ /* Skip forward to start of next number */ 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 ("getint", MALLOC_FAIL, ABORT); /* NOTREACHED */ image->comment[0] = '\0'; } else { tmpptr = (char *) realloc(image->comment, strlen(image->comment) + strlen(cmt) + 1); if (!tmpptr) Error ("getint", REALLOC_FAIL, ABORT); /* NOTREACHED */ image->comment = tmpptr; } strcat(image->comment, cmt); } } if ( c==EOF ) return 0; if ( c>='0' && c<='9' ) break; /* we've found what we were looking for */ /* see if we are getting garbage (non-whitespace) */ if ( c!=' ' && c!='\t' && c!='\r' && c!='\n' && c!=',' ) garbage=1; c = getc(fp); } /* we're at the start of a number, continue until we hit a non-number */ i = 0; while (1) { i = (i*10) + (c - '0'); c = getc(fp); if ( c==EOF ) return i; if ( c<'0' || c>'9' ) break; } numgot++; return i;}/* * getshort function: it is used in RAW mode to read 16-bit values. */static int getshort ( FILE *fp ){ int c1, c2; c1 = getc(fp); if ( c1 == EOF ) return 0; c2 = getc(fp); if ( c2 == EOF ) return 0; numgot++; return (c2 << 8) | c1;}/* * getbit function: reads the next bit in stream pointed by fp. */static int getbit ( FILE *fp, Image *image ){ int c; /* skip forward to start of next number */ 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; while (1) { c = getc(fp); 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 ("getbit", MALLOC_FAIL, ABORT); /* NOTREACHED */ image->comment[0] = '\0'; } else { tmpptr = (char *) realloc(image->comment, strlen(image->comment) + strlen(cmt) + 1); if (!tmpptr) Error ("getbit", REALLOC_FAIL, ABORT); /* NOTREACHED */ image->comment = tmpptr; } strcat(image->comment, cmt); } } if ( c==EOF ) return 0; if ( c=='0' || c=='1' ) break; /* we've found what we were looking for */ /* see if we are getting garbage (non-whitespace) */ if ( c!=' ' && c!='\t' && c!='\r' && c!='\n' && c!=',' ) garbage=1; c = getc(fp); } numgot++; return (c-'0');}/* * WritePBM function: writes a PBM/PGM/PPM file to the already open stream. * If (raw), writes as RAW bytes, otherwise writes as * ASCII. 'colorstyle' single-handedly determines the * type of file written: * if colorstyle==0, (Full Color) a PPM file is written, * if colorstyle==1, (Greyscale) a PGM file is written, * if colorstyle==2, (B/W stipple) a PBM file is written. */intWritePBM ( FILE *fp, byte *pic, const int w, const int h, const int colorstyle, const int bpp, const int raw, char *comment ){ int magic; byte *pix; int i, j, len; char buf[BUFSIZ]; /* Print info to user */ sprintf (buf , "%4dx%-4d %s, %s format.", w, h, (colorstyle==0) ? "PPM" : (colorstyle==1) ? "PGM" : "PBM", (raw) ? "raw" : "ascii"); fprintf (stdout, "\n[%s]", buf); /* Calc the appropriate magic number for this file type */ magic = 0; if (colorstyle==0) magic = 3; else if (colorstyle==1) magic = 2; else if (colorstyle==2) magic = 1; if (raw && magic) magic+=3; /* Write the header info */ fprintf (fp, "P%d\n",magic); fprintf (fp, "# Creator: flwt %s\n", REVDATE); if (comment) { /* write comment lines */ char *sp; sp = comment; while (*sp) { fprintf (fp, "# "); while (*sp && *sp != '\n') fputc((int)*sp++, fp); if (*sp == '\n') sp++; fputc('\n', fp); } } fprintf (fp, "%d %d\n", w, h); if ( colorstyle!=2 ) fprintf (fp, "255\n"); if ( ferror(fp) ) return 0; /* Write the image data */ if ( colorstyle==0 ) { /* 24bit RGB, 3 bytes per pixel */ for ( i=0, pix=pic, len=0; i<h; i++ ) { for ( j=0 ; j<w ; j++ ) { if ( raw ) { if ( bpp == 24 ) { fputc((int)pix[0], fp); fputc((int)pix[1], fp); fputc((int)pix[2], fp); } else { fputc((int)*pix, fp); fputc((int)*pix, fp); fputc((int)*pix, fp); } } else { if ( bpp == 24 ) fprintf (fp, "%3d %3d %3d ", pix[0], pix[1], pix[2]); else fprintf (fp, "%3d %3d %3d ", *pix, *pix, *pix); len += 12; if ( len>58 ) { fprintf (fp, "\n"); len=0; } } pix += (bpp==24) ? 3 : 1; } } } else if (colorstyle==1) { /* 8-bit greyscale */ for ( i=0, pix=pic, len=0 ; i<w*h ; i++ ) { if (raw) { fputc ( (bpp==24) ? LUMA(pix[0],pix[1],pix[2]) : *pix, fp); } else { fprintf (fp, "%3d ", (bpp==24) ? LUMA(pix[0],pix[1],pix[2]) : *pix); len += 4; if ( len>66 ) { fprintf (fp, "\n"); len=0; } } pix += (bpp==24) ? 3 : 1; } } else if (colorstyle==2) { /* 1-bit B/W stipple */ int bit, k, flipbw; char *str0, *str1; /* Shouldn't happen */ if ( bpp == 24 ) { Error ("WritePBM", CUSTOM, RETURN, "24 bpp and B/W Stipple.\n"); Error ("WritePBM", FATAL_ERROR, ABORT); /* NOTREACHED */ } /* if '0' is black, set flipbw */ flipbw = 1; str0 = (flipbw) ? "1 " : "0 "; str1 = (flipbw) ? "0 " : "1 "; bit = k = 0; /* Initial values */ for ( i=0, pix=pic, len=0 ; i<h ; i++ ) { for ( j=0, bit=0, k=0 ; j<w ; j++, pix++ ) { if ( raw ) { k = (k << 1) | *pix; bit++; if ( bit==8 ) { if ( flipbw ) k = ~k; fputc(k, fp); bit = k = 0; } } else { if (*pix) fprintf (fp, str1); else fprintf (fp, str0); len += 2; if ( len>68 ) { fprintf (fp, "\n"); len=0; } } } /* j */ if (raw && bit) { k = k << (8-bit); if (flipbw) k = ~k; fputc(k, fp); } } } if ( ferror(fp) ) return 0; return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -