Beispiel #1
0
literalt bv_utilst::overflow_sub(
  const bvt &op0, const bvt &op1, representationt rep)
{
  if(rep==SIGNED)
  {
    // We special-case x-INT_MIN, which is >=0 if
    // x is negative, always representable, and
    // thus not an overflow.
    literalt op1_is_int_min=is_int_min(op1);
    literalt op0_is_negative=op0[op0.size()-1];

    return
      prop.lselect(op1_is_int_min,
        !op0_is_negative,
        overflow_add(op0, negate(op1), SIGNED));
  }
  else if(rep==UNSIGNED)
  {
    // overflow is simply _negated_ carry-out
    return !carry_out(op0, inverted(op1), const_literal(true));
  }
  else
    assert(false);
}
Beispiel #2
0
int
main(int argc, char * argv[]) {

    FILE* ifp;
    bit* bitrow;
    int rows, cols, format;
    int padright;
    int row;
    const char * inputFilename;
    const char *name;
    int itemsperline;
    int bitsperitem;
    int item;
    int firstitem;
    const char hexchar[] = "0123456789abcdef";

    pbm_init(&argc, argv);

    if (argc-1 > 1)
        pm_error("Too many arguments (%d).  The only valid argument is an "
                 "input file name.", argc-1);
    else if (argc-1 == 1) 
        inputFilename = argv[1];
    else
        inputFilename = "-";

    generateName(inputFilename, &name);
    ifp = pm_openr(inputFilename);
    
    pbm_readpbminit(ifp, &cols, &rows, &format);
    bitrow = pbm_allocrow(cols);
    
    /* Compute padding to round cols up to the nearest multiple of 8. */
    overflow_add(cols, 8);
    padright = ((cols + 7)/8) * 8 - cols;

    printf("#define %s_width %d\n", name, cols);
    printf("#define %s_height %d\n", name, rows);
    printf("static char %s_bits[] = {\n", name);

    itemsperline = 0;
    bitsperitem = 0;
    item = 0;
    firstitem = 1;

#define PUTITEM \
    { \
    if ( firstitem ) \
        firstitem = 0; \
    else \
        putchar( ',' ); \
    if ( itemsperline == 15 ) \
        { \
        putchar( '\n' ); \
        itemsperline = 0; \
        } \
    if ( itemsperline == 0 ) \
        putchar( ' ' ); \
    ++itemsperline; \
    putchar('0'); \
    putchar('x'); \
    putchar(hexchar[item >> 4]); \
    putchar(hexchar[item & 15]); \
    bitsperitem = 0; \
    item = 0; \
    }

#define PUTBIT(b) \
    { \
    if ( bitsperitem == 8 ) \
        PUTITEM; \
    if ( (b) == PBM_BLACK ) \
        item += 1 << bitsperitem; \
    ++bitsperitem; \
    }

    for (row = 0; row < rows; ++row) {
        int col;
        pbm_readpbmrow(ifp, bitrow, cols, format);
        for (col = 0; col < cols; ++col)
            PUTBIT(bitrow[col]);
        for (col = 0; col < padright; ++col)
            PUTBIT(0);
    }

    pm_close(ifp);

    if (bitsperitem > 0)
        PUTITEM;
    printf("};\n");

    pbm_freerow(bitrow);

    strfree(name);

    exit(0);
}
Beispiel #3
0
int
main(int argc, char * argv[]) {
    FILE* ifp;
    xel* xelrow;
    xel* newxelrow;
    xel bgxel;
    int rows, cols, format; 
    int newformat, newcols; 
    int row;
    xelval maxval, newmaxval;
    double shearfac;

    struct cmdline_info cmdline;

    pnm_init( &argc, argv );

    parse_command_line( argc, argv, &cmdline );

    ifp = pm_openr( cmdline.input_filespec );

    pnm_readpnminit( ifp, &cols, &rows, &maxval, &format );
    xelrow = pnm_allocrow( cols );

    /* Promote PBM files to PGM. */
    if ( !cmdline.noantialias && PNM_FORMAT_TYPE(format) == PBM_TYPE ) {
        newformat = PGM_TYPE;
        newmaxval = PGM_MAXMAXVAL;
        pm_message( "promoting from PBM to PGM - "
                    "use -noantialias to avoid this" );
    } else {
        newformat = format;
        newmaxval = maxval;
    }

    shearfac = tan( cmdline.angle );
    if ( shearfac < 0.0 )
        shearfac = -shearfac;

    if(rows * shearfac >= INT_MAX-1)
    	pm_error("image too large");
    
    overflow_add(rows * shearfac, cols+1);
    
    newcols = rows * shearfac + cols + 0.999999;

    pnm_writepnminit( stdout, newcols, rows, newmaxval, newformat, 0 );
    newxelrow = pnm_allocrow( newcols );

    bgxel = pnm_backgroundxelrow( xelrow, cols, newmaxval, format );

    for ( row = 0; row < rows; ++row ) {
        double shearCols;

        pnm_readpnmrow( ifp, xelrow, cols, newmaxval, format );

        if ( cmdline.angle > 0.0 )
            shearCols = row * shearfac;
        else
            shearCols = ( rows - row ) * shearfac;

        shear_row(xelrow, cols, newxelrow, newcols, 
                  shearCols, format, bgxel, !cmdline.noantialias);

        pnm_writepnmrow( stdout, newxelrow, newcols, newmaxval, newformat, 0 );
    }

    pm_close( ifp );
    pm_close( stdout );

    exit( 0 );
}
Beispiel #4
0
/* 
 *  Purpose : Open a file for input or ouput as controlled by the mode
 *  parameter.  If no file name is specified (ie. file_name is null) then
 *  a pointer to stdin or stdout will be returned.  The calling routine may
 *  call this routine with a file name of "-".  For this case rle_open_f
 *  will return a pointer to stdin or stdout depending on the mode.
 *    If the user specifies a non-null file name and an I/O error occurs
 *  when trying to open the file, rle_open_f will terminate execution with
 *  an appropiate error message.
 *
 *  parameters
 *   input:
 *     prog_name: 	name of the calling program.
 *     file_name : 	name of the file to open
 *     mode : 		either "r" for read or input file or "w" for write or
 *            		output file
 *
 *   output:
 *     a file pointer
 * 
 */
FILE *
rle_open_f_noexit(const char * const prog_name, 
                  const char * const file_name, 
                  const char * const mode ) {

    FILE *fp;
    void perror();
    CONST_DECL char *err_str;
    const char *cp;
    char *combuf;

    if ( *mode == 'w' || *mode == 'a' )
        fp = stdout;     /* Set the default value */
    else
        fp = stdin;
    
    if ( file_name != NULL && strcmp( file_name, "-" ) != 0 )
    {
#ifndef NO_OPEN_PIPES
        /* Check for dead children. */
        if ( catching_children > 0 )
        {
            int i, j;

            /* Check all children to see if any are dead, reap them if so. */
            for ( i = 0; i < catching_children; i++ )
            {
                /* The assumption here is that if it's dead, the kill
                 * will fail, but, because we haven't waited for
                 * it yet, it's a zombie.
                 */
                if (kill(pids[i], 0) < 0) {
                    int opid = pids[i], pid = 0;
                    /* Wait for processes & delete them from the list,
                     * until we get the one we know is dead.
                     * When removing one earlier in the list than
                     * the one we found, decrement our loop index.
                     */
                    while (pid != opid) {
                        pid = wait( NULL );
                        for ( j = 0;
                              j < catching_children && pids[j] != pid;
                              j++ )
                            ;
                        if ( pid < 0 )
                            break;
                        if ( j < catching_children ) {
                            if ( i >= j )
                                i--;
                            for ( j++; j < catching_children; j++ )
                                pids[j-1] = pids[j];
                            catching_children--;
                        }
                    }
                }
            }
        }

        /*  Real file, not stdin or stdout.  If name ends in ".Z",
         *  pipe from/to un/compress (depending on r/w mode).
         *  
         *  If it starts with "|", popen that command.
         */
	    
        cp = file_name + strlen( (char*) file_name ) - 2;
        /* Pipe case. */
        if ( *file_name == '|' && 0 /* BOLLOCKS ARE WE DOING THIS ANY MORE */)
        {
            int thepid;		/* PID from my_popen */
            if ( (fp = my_popen( file_name + 1, mode, &thepid )) == NULL )
            {
                err_str = "%s: can't invoke <<%s>> for %s: ";
                goto err;
            }
            /* One more child to catch, eventually. */
            if (catching_children < MAX_CHILDREN) {
                pids[catching_children++] = thepid;
            }
        }

        /* Compress case. */
        else if ( /* SMOKING SOMETHING */ 0 && cp > file_name && *cp == '.' && *(cp + 1) == 'Z' )
        {
            int thepid;		/* PID from my_popen. */
            overflow_add(20, strlen(file_name));
            combuf = (char *)malloc( 20 + strlen( file_name ) );
            if ( combuf == NULL )
            {
                err_str = "%s: out of memory opening (compressed) %s for %s";
                goto err;
            }

            if ( *mode == 'w' )
                sprintf( combuf, "compress > %s", file_name );
            else if ( *mode == 'a' )
                sprintf( combuf, "compress >> %s", file_name );
            else
                sprintf( combuf, "compress -d < %s", file_name );

            fp = my_popen( combuf, mode, &thepid );
            free( combuf );

            if ( fp == NULL )
            {
                err_str =
                    "%s: can't invoke 'compress' program, "
                    "trying to open %s for %s";
                goto err;
            }
            /* One more child to catch, eventually. */
            if (catching_children < MAX_CHILDREN) {
                pids[catching_children++] = thepid;
            }
        }
        else
#endif /* !NO_OPEN_PIPES */
        {
            /* Ordinary, boring file case. */
            /* In the original code, the code to add the "b" was
               conditionally included only if the macro STDIO_NEEDS_BINARY was
               defined.  But for Netpbm, there is no need make a distinction;
               we always add the "b".  -BJH 2000.07.20.  
            */
            char mode_string[32];	/* Should be enough. */

            /* Concatenate a 'b' onto the mode. */
            mode_string[0] = mode[0];
            mode_string[1] = 'b';
            strcpy( mode_string + 2, mode + 1 );
        
            if ( (fp = fopen(file_name, mode_string)) == NULL )
            {
                err_str = "%s: can't open %s for %s: ";
                goto err;
            }
        }
    }

    return fp;

err:
	fprintf( stderr, err_str,
             prog_name, file_name,
             (*mode == 'w') ? "output" :
             (*mode == 'a') ? "append" :
             "input" );
	perror( "" );
	return NULL;

}
Beispiel #5
0
static void 
addEntryToIcon(MS_Ico       const MSIconData, 
               const char * const xorPpmFname,
               const char * const andPgmFname,
               bool         const trueTransparent) {

    IC_Entry entry;
    FILE * xorfile;
    pixel ** xorPPMarray;
    gray ** andPGMarray;
    ICON_bmp xorBitmap;
    ICON_bmp andBitmap;
    int rows, cols;
    int bpp, colors;
    int entry_cols;
    IC_Palette palette;
    colorhash_table  xorCht;
    colorhash_table  andCht; 
    const char * error;
   
    pixval xorMaxval;
    gray andMaxval;

    MALLOCVAR_NOFAIL(entry);

   /*
    * Read the xor PPM.
    */
    xorfile = pm_openr(xorPpmFname);
    xorPPMarray = ppm_readppm(xorfile, &cols, &rows, &xorMaxval);
    pm_close(xorfile);
    /*
    * Since the entry uses 1 byte to hold the width and height of the icon, the
    * image can't be more than 256 x 256.
    */
    if (rows > 255 || cols > 255) {
        pm_error("Max size for a icon is 255 x 255 (1 byte fields).  "
                 "%s is %d x %d", xorPpmFname, cols, rows);
    }
   
    if (verbose) pm_message("read PPM: %dw x %dh, maxval = %d", 
                            cols, rows, xorMaxval);

    makePalette(xorPPMarray, cols, rows, xorMaxval, 
                &palette, &xorCht, &colors, &error);

    if (error)
        pm_error("Unable to make palette for '%s'.  %s", xorPpmFname, error);
   /*
    * All the icons I found seemed to pad the palette to the max entries
    * for that bitdepth.
    * 
    * The spec indicates this isn't neccessary, but I'll follow this behaviour
    * just in case.
    */
    if (colors < 3) {
        bpp = 1;
        entry_cols = 2;
    } else if (colors < 17) {
        bpp = 4;
        entry_cols = 16;
    } else {
        bpp = 8;
        entry_cols = 256;
    }

    getOrFakeAndMap(andPgmFname, cols, rows,
                    &andPGMarray, &andMaxval, &andCht, &error);
    if (error)
        pm_error("Error in and map for '%s'.  %s", xorPpmFname, error);

    if (andPGMarray && trueTransparent)
        blackenTransparentAreas(xorPPMarray, cols, rows, 
                                andPGMarray, andMaxval);

    xorBitmap = createBitmap(bpp, xorPPMarray, cols, rows, xorCht);
    andBitmap = createAndBitmap(andPGMarray, cols, rows, andMaxval);
    /*
     * Fill in the entry data fields.
    */
    entry->width         = cols;
    entry->height        = rows;
    entry->color_count   = entry_cols;
    entry->reserved      = 0;
    entry->planes        = 1;
    /* 
    * all the icons I looked at ignored this value...
    */
    entry->bitcount      = bpp;
    entry->ih            = createInfoHeader(entry, xorBitmap, andBitmap);
    entry->colors        = palette->colors;
    overflow2(4, entry->color_count);
    overflow_add(xorBitmap->size, andBitmap->size);
    overflow_add(xorBitmap->size + andBitmap->size, 40);
    overflow_add(xorBitmap->size + andBitmap->size + 40, 4 * entry->color_count);
    entry->size_in_bytes = 
        xorBitmap->size + andBitmap->size + 40 + (4 * entry->color_count);
    if (verbose) 
        pm_message("entry->size_in_bytes = %d + %d + %d = %d",
                   xorBitmap->size ,andBitmap->size, 
                   40, entry->size_in_bytes );
    /*
    * We don't know the offset ATM, set to 0 for now.
    * Have to calculate this at the end.
    */
    entry->file_offset   = 0;
    entry->xorBitmapOut  = xorBitmap->data;
    entry->andBitmapOut  = andBitmap->data;
    entry->xBytesXor     = xorBitmap->xBytes;
    entry->xBytesAnd     = andBitmap->xBytes;  
    /*
    * Add the entry to the entries array.
    */
    overflow_add(MSIconData->count,1);
    MSIconData->count++;
    /* 
    * Perhaps I should use something that allocs a decent amount at start...
    */
    MSIconData->entries = 
        realloc2 (MSIconData->entries, MSIconData->count * sizeof(IC_Entry *));
    MSIconData->entries[MSIconData->count-1] = entry;
}