Beispiel #1
0
static gint
csmfile_detect(const GwyFileDetectInfo *fileinfo,
               gboolean only_name)
{
    guint size, xres, yres;

    if (only_name)
        return g_str_has_suffix(fileinfo->name_lowercase, EXTENSION) ? 20 : 0;

    // Weed out files that do not pretend to be Windows bitmaps quickly.
    if (fileinfo->buffer_len < BMP_HEADER_SIZE
        || !read_bmp_header(fileinfo->head, &size, &xres, &yres))
        return 0;

    // Weed out incorrect Windows bitmaps but also those no extra data beyond
    // the nominal end.
    if (fileinfo->file_size <= size)
        return 0;

    // Look for "Version = CSPM" somewhere near the end.
    if (!gwy_memmem(fileinfo->tail, fileinfo->buffer_len,
                    MAGIC, MAGIC_SIZE))
        return 0;


    return 90;
}
Beispiel #2
0
void read_bmp(void)
{
    char header[HEADER_SIZE];
    FILE *input;
    int x, y;
    unsigned int *p;

    if (!png.true_color)
	G_fatal_error("PNG: cannot use BMP with indexed color");

    input = fopen(png.file_name, "rb");
    if (!input)
	G_fatal_error("PNG: couldn't open input file %s", png.file_name);

    if (fread(header, sizeof(header), 1, input) != 1)
	G_fatal_error("PNG: invalid input file %s", png.file_name);

    if (!read_bmp_header(header))
	G_fatal_error("PNG: invalid BMP header for %s", png.file_name);

    for (y = 0, p = png.grid; y < png.height; y++) {
	for (x = 0; x < png.width; x++, p++) {
	    int b = fgetc(input);
	    int g = fgetc(input);
	    int r = fgetc(input);
	    int a = fgetc(input);
	    unsigned int c = png_get_color(r, g, b, a);

	    *p = c;
	}
    }

    fclose(input);
}
Beispiel #3
0
int main(int argc, char *argv[])
{
	/* Line-Buf */
	setvbuf(stdin, buf_input, _IOLBF, BUFSIZ);

	int i;
	if (argc != 2) {
		printf("\nUSEAGE: \"dip + your_pic_name\" TRY AGIAN !\n\n");
		exit(EXIT_FAILURE);
	}

	char in_name[16] = { 0 };
	strcpy(in_name, argv[1]);

	BMP_HEADER *p_header = calloc(BMP_HEADER_SIZE, sizeof(u8));

	FILE *fd = fopen(in_name, "rb");
	if (!fd) {
		printf("\nread failure!\n");
		exit(EXIT_FAILURE);
	}

	read_bmp_header(p_header, fd);

	int color_len = p_header->bm_offset - BMP_HEADER_SIZE;
	int image_len = p_header->file_size - p_header->bm_offset;
	int width = p_header->width;
	int height = p_header->height;
	u8 color_buf[color_len];
	u8 image_buf[image_len];
	u8 *index[height];

	fread(color_buf, color_len, 1, fd);
	fread(image_buf, image_len, 1, fd);
	fclose(fd);

	/* pointer-array, namely 2D array */
	for (i = 0; i < height; i++) {
		index[i] = &image_buf[i * width];
	}

	print_header(p_header);

	image_proc(index, image_buf, color_buf, width, height, p_header);

	/* printf("addr: %x\n", &((BMP_HEADER *)0)); */
	/* can not arrive here */
	return 0;
}
Beispiel #4
0
static GwyContainer*
csmfile_load(const gchar *filename,
             G_GNUC_UNUSED GwyRunType mode,
             GError **error)
{
    GwyContainer *meta, *container = NULL;
    GHashTable *hash = NULL;
    guchar *d24, *buffer = NULL;
    gsize size = 0;
    GError *err = NULL;
    GwyDataField *dfield = NULL;
    guint xres, yres, bmpsize, header_size, maxval, i, j;
    gdouble real, zmin, zmax, q, z0;
    GwyTextHeaderParser parser;
    GwySIUnit *unit = NULL;
    gchar *value, *end, *header = NULL;
    gdouble *data;
    gint power10;

    if (!gwy_file_get_contents(filename, &buffer, &size, &err)) {
        err_GET_FILE_CONTENTS(error, &err);
        return NULL;
    }

    if (size < BMP_HEADER_SIZE) {
        err_TOO_SHORT(error);
        goto fail;
    }
    if (!read_bmp_header(buffer, &xres, &yres, &bmpsize)
        || size <= bmpsize) {
        err_FILE_TYPE(error, "CSM");
        goto fail;
    }

    header_size = size - bmpsize;
    header = g_new(gchar, header_size + 1);
    memcpy(header, buffer + bmpsize, header_size);
    header[header_size] = '\0';

    gwy_clear(&parser, 1);
    parser.key_value_separator = "=";
    hash = gwy_text_header_parse(header, &parser, NULL, NULL);

    /* Do NOT use the fields Image width, Image height from the added footer.
     * Even though it is specifically added by Benyuan it can disagree with the
     * BMP diemsions and when they disagree the BMP diemsions are apparently
     * right. */
    if (err_DIMENSION(error, xres))
        goto fail;
    if (err_DIMENSION(error, yres))
        goto fail;

    if (!(value = g_hash_table_lookup(hash, "ScanSize"))) {
        err_MISSING_FIELD(error, "ScanSize");
        goto fail;
    }
    real = g_ascii_strtod(value, NULL);
    /* Use negated positive conditions to catch NaNs */
    if (!((real = fabs(real)) > 0)) {
        g_warning("Real size is 0.0, fixing to 1.0");
        real = 1.0;
    }

    if (!(value = g_hash_table_lookup(hash, "HeightScale"))) {
        err_MISSING_FIELD(error, "HeightScale");
        goto fail;
    }
    zmax = g_ascii_strtod(value, &end);
    unit = gwy_si_unit_new_parse(end, &power10);

    /* Optional stuff for which we try to fall back. */
    if (!(value = g_hash_table_lookup(hash, "StartHeightScale")))
        zmin = 0.0;
    else
        zmin = g_ascii_strtod(value, NULL);

    if (!(value = g_hash_table_lookup(hash, "MaxValue")))
        maxval = 0xffff;
    else
        maxval = MAX(atoi(value), 1);


    dfield = gwy_data_field_new(xres, yres, real*Nanometre, real*Nanometre,
                                FALSE);
    data = gwy_data_field_get_data(dfield);

    d24 = buffer + BMP_HEADER_SIZE;
    q = pow10(power10)*(zmax - zmin)/maxval;
    z0 = pow10(power10)*zmin;
    for (i = 0; i < yres; i++) {
        gdouble *row = data + (yres-1 - i)*xres;
        for (j = 0; j < xres; j++, row++, d24 += 3)
            *row = (d24[0] + 256.0*d24[1])*q + z0;
    }

    gwy_si_unit_set_from_string(gwy_data_field_get_si_unit_xy(dfield), "m");
    gwy_data_field_set_si_unit_z(dfield, unit);

    container = gwy_container_new();
    gwy_container_set_object_by_name(container, "/0/data", dfield);
    g_object_unref(dfield);

    meta = gwy_container_new();
    g_hash_table_foreach(hash, store_meta, meta);
    gwy_container_set_object_by_name(container, "/0/meta", meta);
    g_object_unref(meta);

    if ((value = g_hash_table_lookup(hash, "sTitle"))
        && g_utf8_validate(value, -1, NULL)) {
        gwy_container_set_string_by_name(container, "/0/data/title",
                                         g_strdup(value));
    }
    else
        gwy_app_channel_title_fall_back(container, 0);

    gwy_file_channel_import_log_add(container, 0, NULL, filename);

fail:
    gwy_file_abandon_contents(buffer, size, NULL);
    GWY_OBJECT_UNREF(unit);
    if (header)
        g_free(header);
    if (hash)
        g_hash_table_destroy(hash);

    return container;
}
/*--------------------------------------------
| Name:        bmp2raw
| Description:
| Parameters:  none
| Return Type: none
| Comments:
| See:
----------------------------------------------*/
int bmp2raw(int fd, char* bmp_filename){
   bmp_header_t header;
   int xdim,ydim;
   int l=0;
   int offset;
   int fd_bmp=0;
   unsigned char l_buf[40*64]; //320 pixels by line
   unsigned char* p_l_buf=l_buf;

   if(read_bmp_header(bmp_filename,&header)<0)
      return -1;

   xdim= header.format_header.picture_width.byte0
         + header.format_header.picture_width.byte1 * 256
         + header.format_header.picture_width.byte2 * 256*256
         + header.format_header.picture_width.byte3 * 256*256*256;

   ydim= header.format_header.picture_height.byte0
         + header.format_header.picture_height.byte1 * 256
         + header.format_header.picture_height.byte2 * 256*256
         + header.format_header.picture_height.byte3 * 256*256*256;

   offset = header.file_header.data_pointer.byte0
            + header.file_header.data_pointer.byte1 *256
            + header.file_header.data_pointer.byte2 *256*256
            + header.file_header.data_pointer.byte3 *256*256*256;

   //
   fd_bmp=open(bmp_filename,O_RDONLY,0);

   //
   lseek(fd_bmp,0,SEEK_END);

   l=0;
   while(l<ydim) {
      int cb=0;
      int r=0;

      p_l_buf=l_buf;

      //read bmp file
      while( (cb+=r)<sizeof(l_buf) ) {
         lseek(fd_bmp,-(xdim/8),SEEK_CUR);
         if((r=read(fd_bmp,p_l_buf,xdim/8))<=0) {
            break;
         }
         l++;
         p_l_buf+=r;
         if(l==ydim)
            break;
         lseek(fd_bmp,-(xdim/8),SEEK_CUR);
      }
      if(!cb)
         break;
      //write in frame buffer
      //lseek(fd,(l*(xdim/8)),SEEK_SET);
      write(fd,l_buf,cb);
   }

   ioctl(fd,LCDFLSBUF,(void*)0);

   return 0;
}
Beispiel #6
0
void read_bmp_file()   //读取文件信息,是调用头信息,然后在读入文件fread(buffer, size, count, fp)
{
 read_bmp_header();
 
 fread(bitmap, 1, info.image_size, stdin);
}