nanogui: Re: Compilinging .xBm bitmap file into Nanox pixmaps]


Previous by date: 27 Jan 2001 21:00:53 -0000 Re: Child Windows, Greg Haerr
Next by date: 27 Jan 2001 21:00:53 -0000 Java2 ME (KVM) on Nanox working, Robert Hartley
Previous in thread:
Next in thread:

Subject: [Fwd: Re: Compilinging .xBm bitmap file into Nanox pixmaps]
From: Robert Hartley ####@####.####
Date: 27 Jan 2001 21:00:53 -0000
Message-Id: <3A7337C6.51585F45@ics.com>

Hi all,

 A few days I was begging for help on converting XBM data into a pixmap.

Terry send the following code snippet, and I was able to cut and paste
the xbm2bitmap() function into my code and it worked great.

Just thought I would pass that on,

Rob

-------- Original Message --------
Subject: Re: Compilinging .xBm bitmap file into Nanox pixmaps
Date: Fri, 26 Jan 2001 10:45:24 -0700

Rob,

A while ago, I wrote a set of XBM (not XPM) routines for Nano-X.  I
submitted these to Greg, but I'm not sure if they ever made it into
the release.  I'll attach them here and hope they provide what you
need.

-- 
  Terry L Jeffress



xbm.h
=====

/* xbm.h

   Header file to priovide xbm support for Nano-X

*/

#ifndef nxxbm_h
#define nxxbm_h

#include <microwin/nano-X.h>

GR_BITMAP * xbm2bitmap ( GR_SIZE width, GR_SIZE height,
						 unsigned char * xbm );

void
DrawXBMArray ( GR_WINDOW_ID w, GR_GC_ID gc, GR_COORD x, GR_COORD y,
			   GR_SIZE width, GR_SIZE height, unsigned char * xbm );

GR_BITMAP *
ReadXBMFile ( char * filename, GR_SIZE * height, GR_SIZE * width );


#endif /* nxxbm_h */



xbm.c
=====

/* XBM image routines for Nano-X
   Written by Terry Jeffress (Century Software)
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <microwin/nano-X.h>

#define MAX_LINE 256  /* maximum line length when reading xbm files 
						 this maximum only used when reading the #defines
						 so it shouldn't cause a problem even if the file
						 has lines longer than 256 chars                  */

/* xbm2bitmap
     -- create a GR_BITMAP array from an xbm array of characters
     -- make sure you free() the bitmap array when you no longer need it
	 -- width and height measured in screen pixels

	 -- xbm bitmaps pad each row with extra bits (if necessary) so
	    that the pixels of the next row start in the next full byte.
	    Microwin bitmaps also pad each row with extra bits, but each
	    row must start with the next GR_BITMAP (unsigned short). If
		the xbm row has an odd number of bytes, then the Mwin bitmap
		must have an extra byte added to the row to make the number 
		of bytes even.

     -- xbm bitmaps also store the bits starting with the least
significant
        bit as the first pixel in the row.  Mwin stores the first pixel
		in the most significant bit in the GR_BITMAP.  Thus, we must
		reverse the order of the bits.
*/

GR_BITMAP * 
xbm2bitmap ( GR_SIZE width, GR_SIZE height, unsigned char * xbm )
{
  GR_BITMAP temp_bits;
  GR_BITMAP * bit_image;
  long chars_in_bitmap, shorts_in_bitmap, i, j;
  int padding_byte, bytes_per_line, bit;
  unsigned short value;

  bytes_per_line = ( width + 7 ) / 8;
  padding_byte = bytes_per_line % 2;
  chars_in_bitmap = bytes_per_line * height;
  shorts_in_bitmap = chars_in_bitmap / 2;
  bit_image = malloc( sizeof(GR_BITMAP) * shorts_in_bitmap + 
					  padding_byte * height );
  if ( bit_image == NULL ) {
	fprintf( stderr, "xbm2bitmap: memory allocation failed\n" );
	return 0;
  }

  for ( i = 0, j = 0; i < chars_in_bitmap; i += 2 ) {
	value = xbm[i+1];
	value = ( value << 8 ) | xbm[i];
	if ( padding_byte && !( ( i + 1 ) % bytes_per_line ) ) {
	  value &= 0xff;
	  i--;
	}
    for (bit = 0x8000, temp_bits = 0; value; bit >>= 1, value >>= 1 ) {
      if ( value & 0x01 )
		temp_bits |= bit;
	}
	bit_image[ j++ ] = temp_bits;
  }
  return bit_image;
}


/* DrawXBMArray
     -- draw an xbm graphic from an xbm array to the coordinates
	    of the spcified window using a particular graphics context

	 -- width and height measured in screen pixels
*/

void
DrawXBMArray ( GR_WINDOW_ID w, GR_GC_ID gc, GR_COORD x, GR_COORD y,
			   GR_SIZE width, GR_SIZE height, unsigned char * xbm )
{
  GR_BITMAP * bit_image;

  bit_image = xbm2bitmap( width, height, xbm );
  GrBitmap( w, gc, x, y, width, height, bit_image );
  free( bit_image );
}


/* ReadXBMFile
     -- read an xbm file from a file
     -- returns a GR_BITMAP array
     -- also returns the width and height of the xbm image in the
	    width and height parameters (measured in screen pixels)
     -- remember to free() the GR_BITMAP array when finished

	 -- this isn't the most robust implementation: C-style comments at
		the beginning of the xbm file get ignored, but might cause
		problems in the data section, especially if those comments
		include any hex values (i.e., 0x3f, etc.)

*/

GR_BITMAP *
ReadXBMFile ( char * filename, GR_SIZE * width, GR_SIZE * height )
{
  GR_BITMAP * bit_image = NULL;
  unsigned char * xbm = NULL;
  char line[MAX_LINE];
  char name[MAX_LINE];
  char * p;
  int bytes_per_line, c, flag, value, hex_digits[256];
  long chars_in_image, i;
  FILE * fp;
  unsigned int temp_int;
  unsigned int * temp;
  
  temp = &temp_int;

  /* Open file */
  fp = fopen( filename, "r" );
  if ( ! fp ) {
	fprintf( stderr, "ReadXBMFile: cannot open file %s\n", filename );
	return 0;
  }

  /* Read width and height */
  *height = 0; 
  *width = 0;
  while ( fgets( line, MAX_LINE, fp ) ) {
    if ( sscanf( line, "#define %s %u", name, temp ) == 2 ) {
	  if ( ( strlen( name ) >= 6 ) &&
		   ( strcmp( name + strlen( name ) - 6, "_width" ) == 0 ) )
		*width = *temp;
	  if ( ( strlen( name ) >= 7 ) &&
		   ( strcmp( name + strlen( name ) - 7, "_height" ) == 0 ) )
		*height = *temp;
	}
	if ( *width & *height )
	  break;
  } 
  if( !( *width & *height ) ) {
	fprintf( stderr, "Improperly formatted xbm file: %s\n", filename );
	fclose( fp );
	return 0;
  }

  /* Scan until reaching the image data */
  name[0] = '\0';
  while ( fgets( line, MAX_LINE, fp ) ) {
	if ( sscanf( line, "static unsigned char %s = {", name ) != 1 )
	  if ( sscanf( line, "static char %s = {", name ) != 1 )
		continue;
	p = strrchr( name, '_' );
	if ( p == NULL )
	  p = name;
	else
	  p++;
	if ( strcmp( "bits[]", p ) == 0 )
	  break;
  }
  if ( feof( fp ) ) {
	fprintf( stderr, "Improperly formatted xbm file: %s\n", filename );
	fclose( fp );
	return 0;
  }

  /* Allocate space for the xbm array */
  bytes_per_line = ( *width + 7 ) / 8;
  chars_in_image = (long) bytes_per_line * (long) *height;
  xbm = malloc( sizeof(char) * chars_in_image );
  if ( xbm == NULL ) {
	fprintf( stderr, "ReadXBMFile: memory allocation failed\n" );
	return 0;
  }

  /* Init hash table for hex values */
  hex_digits['0'] =  0;
  hex_digits['1'] =  1;
  hex_digits['2'] =  2;
  hex_digits['3'] =  3;
  hex_digits['4'] =  4;
  hex_digits['5'] =  5;
  hex_digits['6'] =  6;
  hex_digits['7'] =  7;
  hex_digits['8'] =  8;
  hex_digits['9'] =  9;
  hex_digits['A'] = 10;   hex_digits['a'] = 10;
  hex_digits['B'] = 11;   hex_digits['b'] = 11;
  hex_digits['C'] = 12;   hex_digits['c'] = 12;
  hex_digits['D'] = 13;   hex_digits['d'] = 13;
  hex_digits['E'] = 14;   hex_digits['e'] = 14;
  hex_digits['F'] = 15;   hex_digits['f'] = 15;
  hex_digits['x'] =  0;
  hex_digits[' '] = (-1);
  hex_digits[','] = (-1);
  hex_digits['}'] = (-1);
  hex_digits['\n']= (-1);
  hex_digits['\t']= (-1);

  /* Read data and create xbm array */
  p = xbm;
  for ( i = 0; i < chars_in_image; i++ ) {
	value = 0;
	flag = 0;
	while ( 1 ) {
	  c = fgetc( fp );
	  if ( c == EOF ) {
		fprintf( stderr, "Improperly formatted xbm file: %s\n", filename );
		free( xbm );
		fclose( fp );
		return 0;
	  }
	  c &= 0xff;
	  if ( isxdigit( c ) ) {
		value = ( value << 4 ) + hex_digits[ c ];
		flag++;
		continue;
	  }
	  if ( ( hex_digits[ c ] < 0 ) && flag )
		break;
	}
	*p++ = value;
  }

  bit_image = xbm2bitmap( *width, *height, xbm );
  free( xbm );
  fclose( fp );
  return bit_image;
}

Previous by date: 27 Jan 2001 21:00:53 -0000 Re: Child Windows, Greg Haerr
Next by date: 27 Jan 2001 21:00:53 -0000 Java2 ME (KVM) on Nanox working, Robert Hartley
Previous in thread:
Next in thread:


Powered by ezmlm-browse 0.20.