コード例 #1
0
ファイル: atktopbm.c プロジェクト: jhbsz/DIR-850L_A1
int
main(int argc, char **argv) {

    FILE *ifp;
    register bit *bitrow, *bP;
    int rows, cols, row, col, charcount;
    unsigned char *data, mask;


    pbm_init ( &argc, argv );

    if ( argc > 2 )
        pm_usage( "[raster obj]" );
    
    if ( argc == 2 )
        ifp = pm_openr( argv[1] );
    else
        ifp = stdin;

    ReadATKRaster( ifp, &cols, &rows, &data );

    pm_close( ifp );

    pbm_writepbminit( stdout, cols, rows, 0 );
    bitrow = pbm_allocrow( cols );

    for ( row = 0; row < rows; ++row )
    {
        charcount = 0;
        mask = 0x80;
        for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
        {
            if ( charcount >= 8 )
            {
                ++data;
                charcount = 0;
                mask = 0x80;
            }
            *bP = ( *data & mask ) ? PBM_BLACK : PBM_WHITE;
            ++charcount;
            mask >>= 1;
        }
        ++data;
        pbm_writepbmrow( stdout, bitrow, cols, 0 );
    }

    pm_close( stdout );
    exit( 0 );
}
コード例 #2
0
ファイル: pbmtogem.c プロジェクト: chneukirchen/netpbm-mirror
int
main( int argc, char* argv[])
    {
    FILE* ifp;
    bit* bitrow;
    int rows, cols, format, row, col;

    pbm_init( &argc, argv );

    if ( argc > 2 )
        pm_usage( "[pbmfile]" );

    if ( argc == 2 )
        ifp = pm_openr( argv[1] );
    else
        ifp = stdin;

    pbm_readpbminit( ifp, &cols, &rows, &format );

    if( rows>INT16MAX || cols>INT16MAX )
      pm_error ("Input image is too large.");


    bitrow = pbm_allocrow( cols );

    putinit (rows, cols);
    for ( row = 0; row < rows; ++row )
        {
#ifdef DEBUG
        fprintf (stderr, "row %d\n", row);
#endif
        pbm_readpbmrow( ifp, bitrow, cols, format );
        for ( col = 0; col < cols; ++col )
            putbit( bitrow[col] );
        putrow( );
        }
    flushrow ();

    pm_close( ifp );


    exit( 0 );
    }
コード例 #3
0
ファイル: pgmtexture.c プロジェクト: gguillotte/netpbm-code
int
main (int argc, const char ** argv) {

    FILE * ifP;
    gray ** grays;
    unsigned int tone[PGM_MAXMAXVAL+1];
    unsigned int r0, r45, r90;
    unsigned int d;
    unsigned int x, y;
    unsigned int row;
    int rows, cols;
    int argn;
    unsigned int itone;
    unsigned int toneCt;
    float ** p_matrix0, ** p_matrix45, ** p_matrix90, ** p_matrix135;
    float a2m[4], contrast[4], corr[4], var[4], idm[4], savg[4];
    float sentropy[4], svar[4], entropy[4], dvar[4], dentropy[4];
    float icorr[4], maxcorr[4];
    gray maxval;
    unsigned int i;
    const char * const usage = "[-d <d>] [pgmfile]";

    pm_proginit(&argc, argv);

    argn = 1;

    /* Check for flags. */
    if ( argn < argc && argv[argn][0] == '-' )
    {
        if ( argv[argn][1] == 'd' )
        {
            ++argn;
            if ( argn == argc || sscanf( argv[argn], "%u", &d ) != 1 )
                pm_usage( usage );
        }
        else
            pm_usage( usage );
        ++argn;
    }

    if ( argn < argc )
    {
        ifP = pm_openr( argv[argn] );
        ++argn;
    }
    else
        ifP = stdin;

    if ( argn != argc )
        pm_usage( usage );

    d = 1;

    grays = pgm_readpgm(ifP, &cols, &rows, &maxval);
    pm_close (ifP);

    /* Determine the number of different gray scales (not maxval) */
    for (i = 0; i <= PGM_MAXMAXVAL; ++i)
        tone[i] = -1;
    for (row = 0; row < rows; ++row) {
        unsigned int col;
        for (col = 0; col < cols; ++col)
            tone[grays[row][col]] = grays[row][col];
    }
    for (i = 0, toneCt = 0; i <= PGM_MAXMAXVAL; ++i) {
        if (tone[i] != -1)
            ++toneCt;
    }
    pm_message("(Image has %u gray levels.)", toneCt);

    /* Collapse array, taking out all zero values */
    for (row = 0, itone = 0; row <= PGM_MAXMAXVAL; ++row)
        if (tone[row] != -1)
            tone[itone++] = tone[row];
    /* Now array contains only the gray levels present (in ascending order) */

    /* Allocate memory for gray-tone spatial dependence matrix */
    p_matrix0   = matrix (0, toneCt, 0, toneCt);
    p_matrix45  = matrix (0, toneCt, 0, toneCt);
    p_matrix90  = matrix (0, toneCt, 0, toneCt);
    p_matrix135 = matrix (0, toneCt, 0, toneCt);

    for (row = 0; row < toneCt; ++row) {
        unsigned int col;
        for (col = 0; col < toneCt; ++col) {
            p_matrix0 [row][col] = p_matrix45 [row][col] = 0;
            p_matrix90[row][col] = p_matrix135[row][col] = 0;
        }
    }
    if (d > cols)
        pm_error("Image is narrower (%u columns) "
                 "than specified distance (%u)", cols, d);

    /* Find gray-tone spatial dependence matrix */
    pm_message("Computing spatial dependence matrix...");
    for (row = 0; row < rows; ++row) {
        unsigned int col;
        for (col = 0; col < cols; ++col) {
            unsigned int angle;
            for (angle = 0, x = 0; angle <= 135; angle += 45) {
                while (tone[x] != grays[row][col])
                    ++x;
                if (angle == 0 && col + d < cols) {
                    y = 0;
                    while (tone[y] != grays[row][col + d])
                        ++y;
                    ++p_matrix0[x][y];
                    ++p_matrix0[y][x];
                }
                if (angle == 90 && row + d < rows) {
                    y = 0;
                    while (tone[y] != grays[row + d][col])
                        ++y;
                    ++p_matrix90[x][y];
                    ++p_matrix90[y][x];
                }
                if (angle == 45 && row + d < rows && col >= d) {
                    y = 0;
                    while (tone[y] != grays[row + d][col - d])
                        ++y;
                    ++p_matrix45[x][y];
                    ++p_matrix45[y][x];
                }
                if (angle == 135 && row + d < rows && col + d < cols) {
                    y = 0;
                    while (tone[y] != grays[row + d][col + d])
                        ++y;
                    ++p_matrix135[x][y];
                    ++p_matrix135[y][x];
                }
            }
        }
    }
    /* Gray-tone spatial dependence matrices are complete */

    /* Find normalizing constants */
    r0  = 2 * rows * (cols - d);
    r45 = 2 * (rows - d) * (cols - d);
    r90 = 2 * (rows - d) * cols;

    /* Normalize gray-tone spatial dependence matrix */
    for (itone = 0; itone < toneCt; ++itone) {
        unsigned int jtone;
        for (jtone = 0; jtone < toneCt; ++jtone) {
            p_matrix0[itone][jtone]   /= r0;
            p_matrix45[itone][jtone]  /= r45;
            p_matrix90[itone][jtone]  /= r90;
            p_matrix135[itone][jtone] /= r45;
        }
    }
    pm_message(" ...done.");

    pm_message("Computing textural features ...");

    fprintf(stdout, "\n");
    fprintf(stdout,
            "%s         0         45         90        135        Avg\n",
            BL);

    a2m[0] = f1_a2m(p_matrix0,   toneCt);
    a2m[1] = f1_a2m(p_matrix45,  toneCt);
    a2m[2] = f1_a2m(p_matrix90,  toneCt);
    a2m[3] = f1_a2m(p_matrix135, toneCt);
    results(F1, a2m);

    contrast[0] = f2_contrast(p_matrix0,   toneCt);
    contrast[1] = f2_contrast(p_matrix45,  toneCt);
    contrast[2] = f2_contrast(p_matrix90,  toneCt);
    contrast[3] = f2_contrast(p_matrix135, toneCt);
    results(F2, contrast);


    corr[0] = f3_corr(p_matrix0,   toneCt);
    corr[1] = f3_corr(p_matrix45,  toneCt);
    corr[2] = f3_corr(p_matrix90,  toneCt);
    corr[3] = f3_corr(p_matrix135, toneCt);
    results(F3, corr);

    var[0] = f4_var(p_matrix0,   toneCt);
    var[1] = f4_var(p_matrix45,  toneCt);
    var[2] = f4_var(p_matrix90,  toneCt);
    var[3] = f4_var(p_matrix135, toneCt);
    results(F4, var);


    idm[0] = f5_idm(p_matrix0,   toneCt);
    idm[1] = f5_idm(p_matrix45,  toneCt);
    idm[2] = f5_idm(p_matrix90,  toneCt);
    idm[3] = f5_idm(p_matrix135, toneCt);
    results(F5, idm);

    savg[0] = f6_savg(p_matrix0,  toneCt);
    savg[1] = f6_savg(p_matrix45,  toneCt);
    savg[2] = f6_savg(p_matrix90,  toneCt);
    savg[3] = f6_savg(p_matrix135, toneCt);
    results(F6, savg);

    svar[0] = f7_svar(p_matrix0,   toneCt, savg[0]);
    svar[1] = f7_svar(p_matrix45,  toneCt, savg[1]);
    svar[2] = f7_svar(p_matrix90,  toneCt, savg[2]);
    svar[3] = f7_svar(p_matrix135, toneCt, savg[3]);
    results(F7, svar);

    sentropy[0] = f8_sentropy(p_matrix0,   toneCt);
    sentropy[1] = f8_sentropy(p_matrix45,  toneCt);
    sentropy[2] = f8_sentropy(p_matrix90,  toneCt);
    sentropy[3] = f8_sentropy(p_matrix135, toneCt);
    results(F8, sentropy);

    entropy[0] = f9_entropy(p_matrix0,   toneCt);
    entropy[1] = f9_entropy(p_matrix45,  toneCt);
    entropy[2] = f9_entropy(p_matrix90,  toneCt);
    entropy[3] = f9_entropy(p_matrix135, toneCt);
    results(F9, entropy);

    dvar[0] = f10_dvar(p_matrix0,   toneCt);
    dvar[1] = f10_dvar(p_matrix45,  toneCt);
    dvar[2] = f10_dvar(p_matrix90,  toneCt);
    dvar[3] = f10_dvar(p_matrix135, toneCt);
    results(F10, dvar);

    dentropy[0] = f11_dentropy(p_matrix0,   toneCt);
    dentropy[1] = f11_dentropy(p_matrix45,  toneCt);
    dentropy[2] = f11_dentropy(p_matrix90,  toneCt);
    dentropy[3] = f11_dentropy(p_matrix135, toneCt);
    results (F11, dentropy);

    icorr[0] = f12_icorr(p_matrix0,   toneCt);
    icorr[1] = f12_icorr(p_matrix45,  toneCt);
    icorr[2] = f12_icorr(p_matrix90,  toneCt);
    icorr[3] = f12_icorr(p_matrix135, toneCt);
    results(F12, icorr);

    icorr[0] = f13_icorr(p_matrix0,   toneCt);
    icorr[1] = f13_icorr(p_matrix45,  toneCt);
    icorr[2] = f13_icorr(p_matrix90,  toneCt);
    icorr[3] = f13_icorr(p_matrix135, toneCt);
    results(F13, icorr);

    maxcorr[0] = f14_maxcorr(p_matrix0,   toneCt);
    maxcorr[1] = f14_maxcorr(p_matrix45,  toneCt);
    maxcorr[2] = f14_maxcorr(p_matrix90,  toneCt);
    maxcorr[3] = f14_maxcorr(p_matrix135, toneCt);
    results(F14, maxcorr);

    pm_message(" ...done.");

    return 0;
}
コード例 #4
0
ファイル: pbmtoln03.c プロジェクト: cjd8363/Global-Illum
int
main (int argc, char **argv) {
   int argc_copy = argc ;
   char **argv_copy = argv ;
   int argn;
   const char * const usage = "[-left <nn>] [-right <nn>] [-top <nn>] "
       "[-bottom <nn>] [-formlength <nn>] [pbmfile]";

   /* Options */
   /* These defaults are for a DEC LN03 with A4 paper (2400x3400 pixels) */
   const char *opt_left_margin = "0";
   const char *opt_top_margin = opt_left_margin;
   const char *opt_right_margin = "2400";
   const char *opt_bottom_margin = "3400";
   const char *opt_form_length = opt_bottom_margin;

   int width, height, format ;

   pbm_init (&argc_copy, argv_copy) ;

   argn = 1;
   while( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) {
      if( pm_keymatch(argv[argn], "-left", 2) ) {
         if( ++argn >= argc )
            pm_usage(usage);
         opt_left_margin = argv[argn];
      }
      else
      if( pm_keymatch(argv[argn], "-right", 2) ) {
         if( ++argn >= argc )
            pm_usage(usage);
         opt_right_margin = argv[argn];
      }
      else
      if( pm_keymatch(argv[argn], "-top", 2) ) {
         if( ++argn >= argc )
            pm_usage(usage);
         opt_top_margin = argv[argn];
      }
      else
      if( pm_keymatch(argv[argn], "-bottom", 2) ) {
         if( ++argn >= argc )
            pm_usage(usage);
         opt_bottom_margin = argv[argn];
      }
      else
      if( pm_keymatch(argv[argn], "-formlength", 2) ) {
         if( ++argn >= argc )
            pm_usage(usage);
         opt_form_length = argv[argn];
      }
      else
         pm_usage(usage);
      ++argn;
   }

   if( argn < argc ) {
      input = pm_openr( argv[argn] );
      argn++;
   }
   else
      input = stdin;

   if( argn != argc )
      pm_usage(usage);


   /* Initialise pbm file */
   pbm_readpbminit (input, &width, &height, &format) ;

   if (format != PBM_FORMAT && format != RPBM_FORMAT)
      pm_error ("input not in PBM format") ;

/*
 * In explanation of the sequence below:
 *      <ESC>[!p        DECSTR  soft terminal reset
 *      <ESC>[11h       PUM     select unit of measurement
 *      <ESC>[7 I       SSU     select pixel as size unit
 *      <ESC>[?52l      DECOPM  origin is corner of printable area
 *      <ESC>[%s;%ss    DECSLRM left and right margins
 *      <ESC>[%s;%sr    DECSTBM top and bottom margins
 *      <ESC>[%st       DECSLPP form length
 *      <ESC>P0;0;1q            select sixel graphics mode
 *      "1;1            DECGRA  aspect ratio (1:1)
 */

   /* Initialise sixel file */
   printf ("\033[!p\033[11h\033[7 I\033[?52l\033[%s;%ss\033"
           "[%s;%sr\033[%st\033P0;0;1q\"1;1",
      opt_left_margin, opt_right_margin, opt_top_margin, opt_bottom_margin,
      opt_form_length);

   /* Convert data */
   convert (width, height, format) ;

   /* Terminate sixel data */
   print ("\033\\\n") ;

   /* If the program failed, it previously aborted with nonzero completion
      code, via various function calls.
   */
   return 0;
}
コード例 #5
0
ファイル: ppmcie.c プロジェクト: Eleanor66613/CS131
int
main(int argc,
     char * argv[]) {

    int argn;
    const char * const usage = "[-[no]black] [-[no]wpoint] [-[no]label] [-no[axes]] [-full]\n\
[-xy|-upvp] [-rec709|-ntsc|-ebu|-smpte|-hdtv|-cie]\n\
[-red <x> <y>] [-green <x> <y>] [-blue <x> <y>]\n\
[-white <x> <y>] [-gamma <g>]\n\
[-size <s>] [-xsize|-width <x>] [-ysize|-height <y>]";
    const struct colorSystem *cs;

    int widspec = FALSE, hgtspec = FALSE;
    int xBias, yBias;
    int upvp = FALSE;             /* xy or u'v' color coordinates? */
    int showWhite = TRUE;             /* Show white point ? */
    int showBlack = TRUE;             /* Show black body curve ? */
    int fullChart = FALSE;            /* Fill entire tongue ? */
    int showLabel = TRUE;             /* Show labels ? */
    int showAxes = TRUE;              /* Plot axes ? */

    ppm_init(&argc, argv);
    argn = 1;

    cs = &Rec709system;  /* default */
    while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0') {
        if (pm_keymatch(argv[argn], "-xy", 2)) {
            upvp = FALSE;
        } else if (pm_keymatch(argv[argn], "-upvp", 1)) {
            upvp = TRUE;
        } else if (pm_keymatch(argv[argn], "-xsize", 1) ||
                   pm_keymatch(argv[argn], "-width", 2)) {
            if (widspec) {
                pm_error("already specified a size/width/xsize");
            }
            argn++;
            if ((argn == argc) || (sscanf(argv[argn], "%d", &sxsize) != 1))
                pm_usage(usage);
            widspec = TRUE;
        } else if (pm_keymatch(argv[argn], "-ysize", 1) ||
                   pm_keymatch(argv[argn], "-height", 2)) {
            if (hgtspec) {
                pm_error("already specified a size/height/ysize");
            }
            argn++;
            if ((argn == argc) || (sscanf(argv[argn], "%d", &sysize) != 1))
                pm_usage(usage);
            hgtspec = TRUE;
        } else if (pm_keymatch(argv[argn], "-size", 2)) {
            if (hgtspec || widspec) {
                pm_error("already specified a size/height/ysize");
            }
            argn++;
            if ((argn == argc) || (sscanf(argv[argn], "%d", &sysize) != 1))
                pm_usage(usage);
            sxsize = sysize;
            hgtspec = widspec = TRUE;
        } else if (pm_keymatch(argv[argn], "-rec709", 1)) {
            cs = &Rec709system;
        } else if (pm_keymatch(argv[argn], "-ntsc", 1)) {
            cs = &NTSCsystem;
        } else if (pm_keymatch(argv[argn], "-ebu", 1)) {
            cs = &EBUsystem;
        } else if (pm_keymatch(argv[argn], "-smpte", 2)) {
            cs = &SMPTEsystem;
        } else if (pm_keymatch(argv[argn], "-hdtv", 2)) {
            cs = &HDTVsystem;                 
        } else if (pm_keymatch(argv[argn], "-cie", 1)) {
            cs = &CIEsystem;                 
        } else if (pm_keymatch(argv[argn], "-black", 3)) {
            showBlack = TRUE;         /* Show black body curve */
        } else if (pm_keymatch(argv[argn], "-wpoint", 2)) {
            showWhite = TRUE;         /* Show white point of color system */
        } else if (pm_keymatch(argv[argn], "-noblack", 3)) {
            showBlack = FALSE;        /* Don't show black body curve */
        } else if (pm_keymatch(argv[argn], "-nowpoint", 3)) {
            showWhite = FALSE;        /* Don't show white point of system */
        } else if (pm_keymatch(argv[argn], "-label", 1)) {
            showLabel = TRUE;         /* Show labels. */
        } else if (pm_keymatch(argv[argn], "-nolabel", 3)) {
            showLabel = FALSE;        /* Don't show labels */
        } else if (pm_keymatch(argv[argn], "-axes", 1)) {
            showAxes = TRUE;          /* Show axes. */
        } else if (pm_keymatch(argv[argn], "-noaxes", 3)) {
            showAxes = FALSE;         /* Don't show axes */
        } else if (pm_keymatch(argv[argn], "-full", 1)) {
            fullChart = TRUE;         /* Fill whole tongue full-intensity */
        } else if (pm_keymatch(argv[argn], "-gamma", 2)) {
            cs = &Customsystem;
            argn++;
            if ((argn == argc) ||
                (sscanf(argv[argn], "%lf", &Customsystem.gamma) != 1))
                pm_usage(usage);
        } else if (pm_keymatch(argv[argn], "-red", 1)) {
            cs = &Customsystem;
            argn++;
            if ((argn == argc) ||
                (sscanf(argv[argn], "%lf", &Customsystem.xRed) != 1))
                pm_usage(usage);
            argn++;
            if ((argn == argc) ||
                (sscanf(argv[argn], "%lf", &Customsystem.yRed) != 1))
                pm_usage(usage);
        } else if (pm_keymatch(argv[argn], "-green", 1)) {
            cs = &Customsystem;
            argn++;
            if ((argn == argc) ||
                (sscanf(argv[argn], "%lf", &Customsystem.xGreen) != 1))
                pm_usage(usage);
            argn++;
            if ((argn == argc) ||
                (sscanf(argv[argn], "%lf", &Customsystem.yGreen) != 1))
                pm_usage(usage);
        } else if (pm_keymatch(argv[argn], "-blue", 1)) {
            cs = &Customsystem;
            argn++;
            if ((argn == argc) ||
                (sscanf(argv[argn], "%lf", &Customsystem.xBlue) != 1))
                pm_usage(usage);
            argn++;
            if ((argn == argc) ||
                (sscanf(argv[argn], "%lf", &Customsystem.yBlue) != 1))
                pm_usage(usage);
        } else if (pm_keymatch(argv[argn], "-white", 1)) {
            cs = &Customsystem;
            argn++;
            if ((argn == argc) ||
                (sscanf(argv[argn], "%lf", &Customsystem.xWhite) != 1))
                pm_usage(usage);
            argn++;
            if ((argn == argc) ||
                (sscanf(argv[argn], "%lf", &Customsystem.yWhite) != 1))
                pm_usage(usage);
        } else {
            pm_usage(usage);
        }
        argn++;
    }

    if (argn != argc) {               /* Extra bogus arguments ? */
        pm_usage(usage);
    }

    pixcols = sxsize;
    pixrows = sysize;

    pixels = ppm_allocarray(pixcols, pixrows);

    /* Partition into plot area and axes and establish subwindow. */

    xBias = Sz(32);
    yBias = Sz(20);

    makeAllBlack(pixels, pixcols, pixrows);

    drawTongueOutline(pixels, pixcols, pixrows, Maxval, upvp, xBias, yBias);

    fillInTongue(pixels, pixcols, pixrows, Maxval, cs, upvp, xBias, yBias,
                 fullChart);

    if (showAxes)
        drawAxes(pixels, pixcols, pixrows, Maxval, upvp, xBias, yBias);

    if (showWhite)
        plotWhitePoint(pixels, pixcols, pixrows, Maxval,
                       cs, upvp, xBias, yBias);

    if (showBlack)
        plotBlackBodyCurve(pixels, pixcols, pixrows, Maxval,
                           upvp, xBias, yBias);

    /* Plot wavelengths around periphery of the tongue. */

    if (showAxes)
        plotMonochromeWavelengths(pixels, pixcols, pixrows, Maxval,
                                  cs, upvp, xBias, yBias);

    if (showLabel)
        writeLabel(pixels, pixcols, pixrows, Maxval, cs);

    ppm_writeppm(stdout, pixels, pixcols, pixrows, Maxval, FALSE);

    return 0;
}
コード例 #6
0
int
main(int argc, const char ** argv) {

    FILE * ifP;
    gray ** grays;
    int argn;
    unsigned int rows, cols;
    unsigned int divisor;
    unsigned int * obuf;  /* malloced */
    const char * const usage = "[-d <val>] height width [asciifile]";

    pm_proginit(&argc, argv);

    rows = 0;  /* initial value */
    cols = 0;  /* initial value */
    divisor = 1; /* initial value */
    
    argn = 1;

    if ( argc < 3 || argc > 6 )
        pm_usage( usage );

    if ( argv[argn][0] == '-' )
    {
        if ( streq( argv[argn], "-d" ) )
        {
            if ( argc == argn + 1 )
                pm_usage( usage );
            if ( sscanf( argv[argn+1], "%u", &divisor ) != 1 )
                pm_usage( usage );
            argn += 2;
        }
        else
            pm_usage( usage );
    }

    if ( sscanf( argv[argn++], "%u", &rows ) != 1 )
        pm_usage( usage );
    if ( sscanf( argv[argn++], "%u", &cols ) != 1 )
        pm_usage( usage );
    if ( rows < 1 )
        pm_error( "height is less than 1" );
    if ( cols < 1 )
        pm_error( "width is less than 1" );

    if ( argc > argn + 1 )
        pm_usage( usage );

    if ( argc == argn + 1 )
        ifP = pm_openr(argv[argn]);
    else
        ifP = stdin;

    MALLOCARRAY(obuf, cols);
    if (obuf == NULL)
        pm_error("Unable to allocate memory for %u columns", cols);

    grays = pgm_allocarray(cols, rows);

    convertAsciiToPgm(ifP, cols, rows, divisor, maxval, grays, obuf);

    pm_close(ifP);

    pgm_writepgm(stdout, grays, cols, rows, maxval, 0);

    free(obuf);
    pgm_freearray(grays, rows);

    return 0;
}
コード例 #7
0
ファイル: sputoppm.c プロジェクト: chneukirchen/netpbm-mirror
int
main(int argc, const char ** argv) {

    FILE * ifP;
    unsigned int i;
    pixel * pixelrow;
    unsigned int row;
    Pal pal;
    short screen[ROWS*COLS/4];      /* simulates the Atari's video RAM */

    pm_proginit(&argc, argv);

    /* Check args. */
    if ( argc > 2 )
        pm_usage( "[spufile]" );

    if ( argc == 2 )
        ifP = pm_openr( argv[1] );
    else
        ifP = stdin;

    /* Read the SPU file */

    /* Read the screen data. */
    for (i = 0; i < ROWS*COLS/4; ++i)
        pm_readbigshort(ifP, &screen[i]);

    readPalettes(ifP, &pal);

    pm_close(ifP);

    /* Ok, get set for writing PPM. */
    ppm_writeppminit(stdout, COLS, ROWS, MAXVAL, 0);
    pixelrow = ppm_allocrow(COLS);

    /* Now do the conversion. */
    for (row = 0; row < ROWS; ++row) {
        unsigned int col;
        for (col = 0; col < COLS; ++col) {
            /* Compute pixel value. */
            unsigned int const ind = 80 * row + ((col >> 4) << 2);
            unsigned int const b = 0x8000 >> (col & 0xf);
            unsigned int c;
            unsigned int plane;
            unsigned int x1;

            c = 0;  /* initial value */
            for (plane = 0; plane < 4; ++plane) {
                if (b & screen[ind + plane])
                    c |= (1 << plane);
            }
            /* Compute palette index. */
            x1 = 10 * c;
            if ((c & 1) != 0)
                x1 -= 5;
            else
                ++x1;
            if ((col >= x1 ) && (col < (x1 + 160)))
                c += 16;
            if (col >= (x1 + 160))
                c += 32;

            /* Set the proper color. */
            pixelrow[col] = pal.pal[row][c];
        }
        ppm_writeppmrow(stdout, pixelrow, COLS, MAXVAL, 0);
    }

    ppm_freerow(pixelrow);
    pm_close(stdout);

    return 0;
}
コード例 #8
0
ファイル: pnmtoddif.c プロジェクト: jhbsz/DIR-850L_A1
int main(int argc, char *argv[])
{
    FILE           *ifd;
    FILE       *ofd;
    int             rows, cols;
    xelval          maxval;
    int             format;
    const char     * const usage = "[-resolution x y] [pnmfile [ddiffile]]";
    int             i, j;
    char           *outfile;
    int       argn;
    int hor_resolution = 75;
    int ver_resolution = 75;
    imageparams ip;
    unsigned char  *data, *p;

    pnm_init(&argc, argv);

    for (argn = 1;argn < argc && argv[argn][0] == '-';argn++) {
        int arglen = strlen(argv[argn]);

        if (!strncmp (argv[argn],"-resolution", arglen)) {
            if (argn + 2 < argc) {
                hor_resolution = atoi(argv[argn+1]);
                ver_resolution = atoi(argv[argn+2]);
                argn += 2;
                continue;
            } else {
                pm_usage(usage);
            }
        } else {
            pm_usage(usage);
        }
    }

    if (hor_resolution <= 0 || ver_resolution <= 0) {
        fprintf(stderr,"Unreasonable resolution values: %d x %d\n",
                hor_resolution,ver_resolution);
        exit(1);
    }

    if (argn == argc - 2) {
        ifd = pm_openr(argv[argn]);
        outfile = argv[argn+1];
        if (!(ofd = fopen(outfile,"wb"))) {
            perror(outfile);
            exit(1);
        }
    } else if (argn == argc - 1) {
        ifd = pm_openr(argv[argn]);
        ofd = stdout;
    } else {
        ifd = stdin;
        ofd = stdout;
    }

    pnm_readpnminit(ifd, &cols, &rows, &maxval, &format);

    ip.width = cols;
    ip.height = rows;
    ip.h_res = hor_resolution;
    ip.v_res = ver_resolution;

    switch (PNM_FORMAT_TYPE(format)) {
    case PBM_TYPE:
        ip.bits_per_pixel = 1;
        ip.bytes_per_line = (cols + 7) / 8;
        ip.spectral = 2;
        ip.components = 1;
        ip.bits_per_component = 1;
        ip.polarity = 1;
        break;
    case PGM_TYPE:
        ip.bytes_per_line = cols;
        ip.bits_per_pixel = 8;
        ip.spectral = 2;
        ip.components = 1;
        ip.bits_per_component = 8;
        ip.polarity = 2;
        break;
    case PPM_TYPE:
        ip.bytes_per_line = 3 * cols;
        ip.bits_per_pixel = 24;
        ip.spectral = 5;
        ip.components = 3;
        ip.bits_per_component = 8;
        ip.polarity = 2;
        break;
    default:
        fprintf(stderr, "Unrecognized PBMPLUS format %d\n", format);
        exit(1);
    }

    if (!write_header(ofd,&ip)) {
        perror("Writing header");
        exit(1);
    }

    if (!(p = data = (unsigned char*)  malloc(ip.bytes_per_line))) {
        perror("allocating line buffer");
        exit(1);
    }

    switch (PNM_FORMAT_TYPE(format)) {
    case PBM_TYPE:
    {
        bit            *pixels;
        int             mask;
        int             k;

        pixels = pbm_allocrow(cols);

        for (i = 0; i < rows; i++) {
            pbm_readpbmrow(ifd, pixels, cols, format);
            mask = 0;
            p = data;
            for (j = 0, k = 0; j < cols; j++) {
                if (pixels[j] == PBM_BLACK) {
                    mask |= 1 << k;
                }
                if (k == 7) {
                    *p++ = mask;
                    mask = 0;
                    k = 0;
                } else {
                    k++;
                }
            }
            if (k != 7) {       /* Flush the rest of the column */
                *p = mask;
            }
            if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) {
                perror("Writing image data\n");
                exit(1);
            }
        }
    }
    break;
    case PGM_TYPE:
    {
        gray          *pixels = pgm_allocrow(cols);

        for (i = 0; i < rows; i++) {
            p = data;
            pgm_readpgmrow(ifd, pixels, cols, maxval, format);
            for (j = 0; j < cols; j++) {
                *p++ = (unsigned char) pixels[j];
            }
            if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) {
                perror("Writing image data\n");
                exit(1);
            }
        }
        pgm_freerow(pixels);
    }
    break;
    case PPM_TYPE:
    {
        pixel          *pixels = ppm_allocrow(cols);

        for (i = 0; i < rows; i++) {
            p = data;
            ppm_readppmrow(ifd, pixels, cols, maxval, format);
            for (j = 0; j < cols; j++) {
                *p++ = PPM_GETR(pixels[j]);
                *p++ = PPM_GETG(pixels[j]);
                *p++ = PPM_GETB(pixels[j]);
            }
            if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) {
                perror("Writing image data\n");
                exit(1);
            }
        }
        ppm_freerow(pixels);
    }
    break;
    }

    pm_close(ifd);

    free(data);

    if (!write_trailer(ofd)) {
        perror("Writing trailer");
        exit(1);
    }

    if (fclose(ofd) == EOF) {
        perror("Closing output file");
        exit(1);
    };

    return(0);
}
コード例 #9
0
int
main( int argc, char * argv[])  {

    FILE * ifp;
    int argn, extraskip;
    const char * const usage = "[-extraskip N] [macpfile]";
    int outOfSync;
    int pixelCnt;

    pbm_init( &argc, argv );

    argn = 1;      /* initial value */
    extraskip = 0; /* initial value */

    /* Check for flags. */
    if ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) {
        if ( pm_keymatch( argv[argn], "-extraskip", 2 ) ) {
            argn++;
            if ( argn == argc || sscanf( argv[argn], "%d", &extraskip ) != 1 )
                pm_usage( usage );
        }
        else
            pm_usage( usage );
        argn++;
    }

    if ( argn < argc ) {
        ifp = pm_openr( argv[argn] );
        argn++;
        }
    else
        ifp = stdin;

    if ( argn != argc )
        pm_usage( usage );

    if ( extraskip > 256 * 1024 )
        pm_error("-extraskip value too large");
    else if ( extraskip > 0 )
        skipExtraBytes( ifp, extraskip);
    else
        skipHeader( ifp );

    pbm_writepbminit( stdout, MACP_COLS, MACP_ROWS, 0 );

    ReadMacPaintFile( ifp, &outOfSync, &pixelCnt );
    /* We may not be at EOF.
       Macpaint files often have extra bytes after image data. */
    pm_close( ifp );

    if ( pixelCnt == 0 )
        pm_error("No image data.");

    else if ( pixelCnt < MACP_BYTES )
        pm_error("Compressed image data terminated prematurely.");

    else if ( outOfSync > 0 )
        pm_message("Warning: Corrupt image data.  %d rows misaligned.",
                   outOfSync);

    pm_close( stdout );
    exit( 0 );
}
コード例 #10
0
/*{{{  static void usage()*/
static void usage()
{
  pm_usage("[-hv] [-p <driver>] [file]");
  exit(1);
}
コード例 #11
0
ファイル: pbmtopgm.c プロジェクト: Eleanor66613/CS131
int
main(int argc, char *argv[]) {

    gray *outrow, maxval;
    int right, left, down, up;
    bit **inbits;
    int rows, cols;
    FILE *ifd;
    int row;
    int width, height;
    const char * const usage = "<w> <h> [pbmfile]";
   

    pgm_init( &argc, argv );

    if (argc > 4 || argc < 3)
        pm_usage(usage);

    width = atoi(argv[1]);
    height = atoi(argv[2]);
    if (width < 1 || height < 1)
        pm_error("width and height must be > 0");
    left=width/2; right=width-left;
    up=width/2; down=height-up;

    if (argc == 4)
        ifd = pm_openr(argv[3]);
    else
        ifd = stdin ;

    inbits = pbm_readpbm(ifd, &cols, &rows) ;
    
    if (width > cols)
        pm_error("You specified a sample width (%u columns) which is greater "
                 "than the image width (%u columns)", height, rows);
    if (height > rows)
        pm_error("You specified a sample height (%u rows) which is greater "
                 "than the image height (%u rows)", height, rows);

    outrow = pgm_allocrow(cols) ;
    maxval = MIN(PGM_OVERALLMAXVAL, width*height);
    pgm_writepgminit(stdout, cols, rows, maxval, 0) ;

    for (row = 0; row < rows; row++) {
        int const t = (row > up) ? (row-up) : 0;
        int const b = (row+down < rows) ? (row+down) : rows;
        int const onv = height - (t-row+up) - (row+down-b);
        unsigned int col;
        for (col = 0; col < cols; col++) {
            int const l = (col > left) ? (col-left) : 0;
            int const r = (col+right < cols) ? (col+right) : cols;
            int const onh = width - (l-col+left) - (col+right-r);
            int value;
            int x;

            value = 0;  /* initial value */

            for (x = l; x < r; ++x) {
                int y;
                for (y = t; y < b; ++y)
                    if (inbits[y][x] == PBM_WHITE) 
                        ++value;
            }
            outrow[col] = maxval*value/(onh*onv);
        }
        pgm_writepgmrow(stdout, outrow, cols, maxval, 0) ;
    }
    pm_close(ifd);

    return 0;
}
コード例 #12
0
ファイル: ppmtoicr.c プロジェクト: gguillotte/netpbm-code
int
main(int argc, const char ** const argv) {

    FILE * ifP;
    int rows, cols;
    int colorCt;
    int argn;
    unsigned int bitsPerPixel;
    pixval maxval;
    colorhist_vector chv;
    char rgb[CLUTCOLORCT];
    const char * windowName;
    int display, expand;
    int winflag;
    const char* const usage = "[-windowname windowname] [-expand expand] [-display display] [ppmfile]";
    pixel** pixels;
    colorhash_table cht;

    pm_proginit(&argc, argv);

    argn = 1;
    windowName = "untitled";
    winflag = 0;
    expand = 1;
    display = 0;

    while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
    {
        if ( pm_keymatch(argv[argn],"-windowname",2) && argn + 1 < argc )
        {
            ++argn;
            windowName = argv[argn];
            winflag = 1;
        }
        else if ( pm_keymatch(argv[argn],"-expand",2) && argn + 1 < argc )
        {
            ++argn;
            if ( sscanf( argv[argn], "%d",&expand ) != 1 )
                pm_usage( usage );
        }
        else if ( pm_keymatch(argv[argn],"-display",2) && argn + 1 < argc )
        {
            ++argn;
            if ( sscanf( argv[argn], "%d",&display ) != 1 )
                pm_usage( usage );
        }
        else
            pm_usage( usage );
    }

    if ( argn < argc )
    {
        ifP = pm_openr( argv[argn] );
        if ( ! winflag )
            windowName = argv[argn];
        ++argn;
    }
    else
        ifP = stdin;

    if ( argn != argc )
        pm_usage( usage );

    pixels = ppm_readppm(ifP, &cols, &rows, &maxval);

    pm_close(ifP);

    /* Figure out the colormap. */
    pm_message("Computing colormap..." );
    chv = ppm_computecolorhist(pixels, cols, rows, MAXCOLORCT, &colorCt);
    if (!chv)
        pm_error("Too many colors - try doing a 'pnmquant %u'", MAXCOLORCT);
    pm_message("%u colors found", colorCt );

    makeIcrColormap(chv, colorCt, maxval, rgb);

    bitsPerPixel = bppFromColorCt(colorCt);

    /* And make a hash table for fast lookup. */
    cht = ppm_colorhisttocolorhash(chv, colorCt);

    ppm_freecolorhist(chv);

    /************** Create a new window using ICR protocol *********/
    /* Format is "ESC^W;left;top;width;height;display;windowname"  */

    pm_message("Creating window %s ...", windowName);

    printf("\033^W;%d;%d;%d;%d;%d;%s^",
           0, 0, cols * expand, rows * expand, display, windowName);
    fflush(stdout);

    /****************** Download the colormap.  ********************/

    downloadColormap(rgb, windowName);

    sendOutPicture(pixels, rows, cols, cht, expand, windowName);

    return 0;
}
コード例 #13
0
ファイル: pnmtoddif.c プロジェクト: Eleanor66613/CS131
int
main(int argc, char *argv[]) {
    FILE           *ifd;
    FILE           *ofd;
    int             rows, cols;
    xelval          maxval;
    int             format;
    const char     * const usage = "[-resolution x y] [pnmfile [ddiffile]]";
    char           *outfile;
    int       argn;
    int hor_resolution = 75;
    int ver_resolution = 75;
    imageparams ip;

    pnm_init(&argc, argv);

    for (argn = 1;argn < argc && argv[argn][0] == '-';argn++) {
        int arglen = strlen(argv[argn]);

        if (!strncmp (argv[argn],"-resolution", arglen)) {
            if (argn + 2 < argc) {
                hor_resolution = atoi(argv[argn+1]);
                ver_resolution = atoi(argv[argn+2]);
                argn += 2;
                continue;
            } else {
                pm_usage(usage);
            }
        } else {
            pm_usage(usage);
        }
    }

    if (hor_resolution <= 0 || ver_resolution <= 0) {
        fprintf(stderr,"Unreasonable resolution values: %d x %d\n",
                hor_resolution,ver_resolution);
        exit(1);
    }

    if (argn == argc - 2) {
        ifd = pm_openr(argv[argn]);
        outfile = argv[argn+1];
        if (!(ofd = fopen(outfile,"wb"))) {
            perror(outfile);
            exit(1);
        }
    } else if (argn == argc - 1) {
        ifd = pm_openr(argv[argn]);
        ofd = stdout;
    } else {
        ifd = stdin;
        ofd = stdout;
    }

    pnm_readpnminit(ifd, &cols, &rows, &maxval, &format);

    ip.width = cols;
    ip.height = rows;
    ip.h_res = hor_resolution;
    ip.v_res = ver_resolution;

    switch (PNM_FORMAT_TYPE(format)) {
    case PBM_TYPE:
        ip.bits_per_pixel = 1;
        ip.bytes_per_line = (cols + 7) / 8;
        ip.spectral = 2;
        ip.components = 1;
        ip.bits_per_component = 1;
        ip.polarity = 1;
        break;
    case PGM_TYPE:
        ip.bytes_per_line = cols;
        ip.bits_per_pixel = 8;
        ip.spectral = 2;
        ip.components = 1;
        ip.bits_per_component = 8;
        ip.polarity = 2;
        break;
    case PPM_TYPE:
        ip.bytes_per_line = 3 * cols;
        ip.bits_per_pixel = 24;
        ip.spectral = 5;
        ip.components = 3;
        ip.bits_per_component = 8;
        ip.polarity = 2;
        break;
    default:
        fprintf(stderr, "Unrecognized PBMPLUS format %d\n", format);
        exit(1);
    }

    if (!write_header(ofd,&ip)) {
        perror("Writing header");
        exit(1);
    }

    convertRaster(ifd, format, maxval, cols, rows, ofd, ip.bytes_per_line);

    pm_close(ifd);

    if (!write_trailer(ofd)) {
        perror("Writing trailer");
        exit(1);
    }

    if (fclose(ofd) == EOF) {
        perror("Closing output file");
        exit(1);
    };

    return(0);
}
コード例 #14
0
ファイル: pgmminkowski.c プロジェクト: Eleanor66613/CS131
int main( int argc, char** argv ){
  
  FILE *ifp;

  gray maxval;
  int cols, rows, format;

  gray* prevrow;
  gray* thisrow;
  gray* tmprow;
  
  int* countTile;   
  int* countEdgeX;  
  int* countEdgeY; 
  int* countVertex; 

  int i, col, row;

  int maxtiles, maxedgex, maxedgey, maxvertex;
  int area, perimeter, eulerchi;

  double l2inv, linv;

  /*
   * parse arg and initialize
   */ 

  pgm_init( &argc, argv );

  if ( argc > 2 ) pm_usage( "[pgmfile]" );
  
  if ( argc == 2 )
    ifp = pm_openr( argv[1] );
  else
    ifp = stdin;

  /*
   * initialize
   */

  pgm_readpgminit( ifp, &cols, &rows, &maxval, &format );
  
  prevrow = pgm_allocrow( cols );
  thisrow = pgm_allocrow( cols );
  
  MALLOCARRAY(countTile   , maxval + 1 );
  MALLOCARRAY(countEdgeX  , maxval + 1 );
  MALLOCARRAY(countEdgeY  , maxval + 1 );
  MALLOCARRAY(countVertex , maxval + 1 );
 
  if (countTile == NULL || countEdgeX == NULL || countEdgeY == NULL ||
      countVertex == NULL)
      pm_error( "out of memory" );
  
  for ( i = 0; i <= maxval; i++ ) countTile[i]   = 0;
  for ( i = 0; i <= maxval; i++ ) countEdgeX[i]  = 0;
  for ( i = 0; i <= maxval; i++ ) countEdgeY[i]  = 0;
  for ( i = 0; i <= maxval; i++ ) countVertex[i] = 0;




  /* first row */

  pgm_readpgmrow( ifp, thisrow, cols, maxval, format );

  /* tiles */

  for ( col = 0; col < cols; ++col ) ++countTile[thisrow[col]]; 
  
  /* y-edges */

  for ( col = 0; col < cols; ++col ) ++countEdgeY[thisrow[col]]; 

  /* x-edges */

  ++countEdgeX[thisrow[0]];

  for ( col = 0; col < cols-1; ++col ) 
    ++countEdgeX[ MAX2(thisrow[col], thisrow[col+1]) ];
  
  ++countEdgeX[thisrow[cols-1]];
  
  /* shortcut: for the first row, countVertex == countEdgeX */
  
  ++countVertex[thisrow[0]];

  for ( col = 0; col < cols-1; ++col ) 
    ++countVertex[ MAX2(thisrow[col], thisrow[col+1]) ];

  ++countVertex[thisrow[cols-1]];

  

  for ( row = 1; row < rows; ++row ){  
    
    tmprow = prevrow; 
    prevrow = thisrow;
    thisrow = tmprow;
 
    pgm_readpgmrow( ifp, thisrow, cols, maxval, format );
  
    /* tiles */

    for ( col = 0; col < cols; ++col ) ++countTile[thisrow[col]]; 
    
    /* y-edges */
    
    for ( col = 0; col < cols; ++col ) 
      ++countEdgeY[ MAX2(thisrow[col], prevrow[col]) ];
    /* x-edges */
    
    ++countEdgeX[thisrow[0]];
    
    for ( col = 0; col < cols-1; ++col ) 
      ++countEdgeX[ MAX2(thisrow[col], thisrow[col+1]) ];
    
    ++countEdgeX[thisrow[cols-1]];
    
    /* vertices */

    ++countVertex[ MAX2(thisrow[0],prevrow[0]) ];

    for ( col = 0; col < cols-1; ++col ) 
      ++countVertex[
        MAX4(thisrow[col], thisrow[col+1], prevrow[col], prevrow[col+1])
      ];
    
    ++countVertex[ MAX2(thisrow[cols-1],prevrow[cols-1]) ];
    
  } /* for row */
  
  /* now thisrow contains the top row*/

  /* tiles and x-edges have been counted, now upper
     y-edges and top vertices remain */
  
  /* y-edges */

  for ( col = 0; col < cols; ++col ) ++countEdgeY[ thisrow[col] ];

  /* vertices */
  
  ++countVertex[thisrow[0]];

  for ( col = 0; col < cols-1; ++col ) 
    ++countVertex[ MAX2(thisrow[col],thisrow[col+1]) ];

  ++countVertex[ thisrow[cols-1] ];


  /* cleanup */

  maxtiles =  rows    * cols;
  maxedgex =  rows    * (cols+1);
  maxedgey = (rows+1) *  cols;
  maxvertex= (rows+1) * (cols+1);
  
  l2inv = 1.0/maxtiles;
  linv  = 0.5/(rows+cols);

  /* And print it. */
  printf( "#threshold\t tiles\tx-edges\ty-edges\tvertices\n" );
  printf( "#---------\t -----\t-------\t-------\t--------\n" );
  for ( i = 0; i <= maxval; i++ ){

    if( !(countTile[i] || countEdgeX[i] || countEdgeY[i] || countVertex[i] ) ) 
      continue; /* skip empty slots */

    area      = maxtiles;
    perimeter = 2*maxedgex + 2*maxedgey - 4*maxtiles;
    eulerchi  = maxtiles - maxedgex - maxedgey + maxvertex;

    printf( "%f\t%6d\t%7d\t%7d\t%8d\t%g\t%g\t%6d\n", (float) i/(1.0*maxval), 
        maxtiles, maxedgex, maxedgey, maxvertex,
        area*l2inv, perimeter*linv, eulerchi
        );


    maxtiles -= countTile[i];
    maxedgex -= countEdgeX[i];
    maxedgey -= countEdgeY[i];
    maxvertex-= countVertex[i];

    /*  i, countTile[i], countEdgeX[i], countEdgeY[i], countVertex[i] */

  }

  /* these should be zero: */
  printf( "#  check:\t%6d\t%7d\t%7d\t%8d\n", 
          maxtiles, maxedgex, maxedgey, maxvertex );

  pm_close( ifp );
  
  exit( 0 );
  
} /*main*/