Ejemplo n.º 1
0
void bmeps_header(FILE *out, char *name, unsigned long w, unsigned long h)
{
  char *ptr;
  use_trans = 0;
  fprintf(out, "%%!PS-Adobe-%d.0 EPSF-%d.0\n",
    bmeps_pslevel, bmeps_pslevel
  );
  bmeps_bb(out, w, h);
  if(show_dsc_comments) {
    fprintf(out, "%%%%Creator: bmp2eps\n");
    fprintf(out, "%%%%Title: ");
    if(name) {
      ptr = name;
      while(*ptr) {
        switch(*ptr) {
          case '\r':
          case '\n':
          break;
          case '\\':
            fputc('/', out);
          break;
          default:
            fputc(*ptr, out);
          break;
        }
        ptr++;
      }
    } else {
      fprintf(out, "Bitmap image");
    }
    fprintf(out, "\n");
  }
  if(show_dsc_comments) {
    fprintf(out, "%%%%Pages: 1\n");
    fprintf(out, "%%%%EndComments\n");
  }
}
Ejemplo n.º 2
0
static int bmp_run( FILE *out, FILE *in, char *name, unsigned long *w, unsigned long *h, int cmd )
{
    int             success = 0;
    unsigned long   width;
    unsigned long   height;
    unsigned long   x, y;
    int             bpp;    /* bits per pixel */
//    int             alpha, trans, altrig;
    int             rowbytes;
//    int             mix;    /* mix foreground and background */
//    int             specbg; /* specified background from command line */
//    int             bg_red, bg_green, bg_blue;
    unsigned char   *row, **rows, **rowp;
    bmp_file_header bmfh;
    bmp_info_header bmih;
    bmp_rgb_quad    *bmpal = NULL;
    size_t          palsize = 0;
    size_t          hdrsize;

    if( !in )
        return 0;

//    alpha    = bmeps_get_alpha();
//    trans    = bmeps_get_trans();
//    altrig   = bmeps_get_altrig();
//    mix      = bmeps_get_mix();
//    specbg   = bmeps_get_specbg();
//    bg_red   = bmeps_get_bg_red();
//    bg_green = bmeps_get_bg_green();
//    bg_blue  = bmeps_get_bg_blue();
    rewind( in );

    // Check for the 'BM' header
    if( !fread( &bmfh, sizeof( bmfh ), 1, in ) || ( bmfh.type != 0x4d42 ) )
        return 0;

    if( !fread( &bmih, sizeof( bmih ), 1, in ) )
        return 0;

    bpp = bmih.bit_count;

    if( bpp != 24 ) {
        palsize = bmih.clr_used * sizeof( bmp_rgb_quad );
        if( palsize == 0 ) {
            switch( bpp ) {
            case 4:
                palsize = 16 * sizeof( bmp_rgb_quad );
                break;
            case 8:
                palsize = 256 * sizeof( bmp_rgb_quad );
                break;
            }
        }
        bmpal = (bmp_rgb_quad *)malloc( palsize );
        if( !fread( bmpal, palsize, 1, in ) ) {
            free( bmpal );
            return 0;
        }
    }

    hdrsize = sizeof( bmfh ) + sizeof( bmih ) + palsize;
    width   = bmih.width;
    height  = bmih.height;

    switch( cmd ) {
    case 0:
        if( out ) {
            bmeps_header( out, (name ? name : default_name), width, height );
            if( bmeps_get_draft() ) {
                success = 1;
                bmeps_draft( out, width, height );
            } else {
                rowbytes = width * sizeof( bmp_rgb_triplet );
                rowbytes = ( rowbytes + 3 ) & ~3;   // Dword aligned
                rows = (unsigned char **)malloc( height * sizeof( unsigned char * ));
                if( rows ) {
                    success = 1;
                    rowp = rows;
                    for( y = 0; y < height; y++ ) {
                        *rowp = NULL;
                        row = (unsigned char *)malloc( rowbytes * sizeof( unsigned char));
                        if( row )
                            *rowp = row;
                        else
                            success = 0;

                        rowp++;
                    }

                    if( bmfh.off_bits > hdrsize )
                        success = 0;

                    if( success ) {
                        // Read bitmap data from file - stored bottom up!
                        rowp--;
                        switch( bpp ) {
                        case 24:
                            // Just dump file data into memory
                            for( y = 0; y < height; y++ ) {
                                if( !fread( *rowp, rowbytes, 1, in ) ) {
                                    success = 0;
                                    break;
                                }
                                rowp--;
                            }
                            break;
                        case 8: {
                                unsigned char   *frow;
                                size_t          frowbytes;

                                frowbytes = width * sizeof( unsigned char );
                                frowbytes = ( frowbytes + 3 ) & ~3;   // Dword aligned
                                frow = (unsigned char *)malloc( frowbytes );
                                // Expand 8bpp data using palette
                                for( y = 0; y < height; y++ ) {
                                    if( !fread( frow, frowbytes, 1, in ) ) {
                                        success = 0;
                                        break;
                                    }
                                    row = *rowp;
                                    for( x = 0; x < width; x++ ) {
                                        row[x*3+0] = bmpal[frow[x]].blue;
                                        row[x*3+1] = bmpal[frow[x]].green;
                                        row[x*3+2] = bmpal[frow[x]].red;
                                    }
                                    rowp--;
                                }
                                free( frow );
                            }
                            break;
                        case 4: {
                                unsigned char   *frow, nibble;
                                size_t          frowbytes;

                                frowbytes = ( width * sizeof( unsigned char ) + 1 ) / 2;
                                frowbytes = ( frowbytes + 3 ) & ~3;   // Dword aligned
                                frow = (unsigned char *)malloc( frowbytes );
                                // Expand 4bpp data using palette
                                for( y = 0; y < height; y++ ) {
                                    if( !fread( frow, frowbytes, 1, in ) ) {
                                        success = 0;
                                        break;
                                    }
                                    row = *rowp;
                                    for( x = 0; x < width; x++ ) {
                                        nibble = frow[x/2];
                                        if( x & 1 )
                                            nibble &= 0x0F;
                                        else
                                            nibble >>= 4;

                                        row[x*3+0] = bmpal[nibble].blue;
                                        row[x*3+1] = bmpal[nibble].green;
                                        row[x*3+2] = bmpal[nibble].red;
                                    }
                                    rowp--;
                                }
                                free( frow );
                            }
                            break;
                        default:
                            success = 0;
                        }
                    }

                    if( success ) {
                        bmeps_begin_image( out, width, height );
                        rowp = rows;
                        for( y = 0; y < height; y++ ) {
                            row = *(rowp++);
                            for( x = 0; x < width; x++ ) {
                                bmeps_add_rgb( row[x*3+2], row[x*3+1], row[x*3+0] );
                            }
                        }
                        bmeps_end_image( out );
                    }
                    /* done with rows */
                    rowp = rows;
                    for( y = 0; y < height; y++ ) {
                        row = *rowp;
                        if( row != NULL )
                            free( row );
                        *(rowp++) = NULL;
                    }
                    free( rows );
                }
            }
            bmeps_footer(out);
        }
        break;
    case 1:
        if( out ) {
            success = 1;
            bmeps_bb( out, width, height );
        }
        break;
    case 2:
        if( w && h ) {
            success = 1;
            *w = width;
            *h = height;
        }
        break;
    }
    /* done with it */
    if( bmpal != NULL )
        free( bmpal );
    return( success );
}