/*  xbm_to_gl.c  uses xbmread to input a .xbm file, needs xbm.h */

#include <GL/glut.h>

static char filename[] = "s01.xbm";
static GLubyte rgbpix[4*100*100];   /* size bigger than smlogo.xbm */
static int width = 71;              /* also read in */
static int height = 96; 
static int RGBA = 0xFF000000;       /* check if black, alpha =1.0*/ 
static int RGBA_back = 0xFFFFFFFF;  /* white background */

static char filename2[] = "d13.xbm";
static GLubyte rgbpix2[4*100*100];   /* size for line.xbm */
static int width2 = 71;              /* also read in */
static int height2 = 96; 
static int RGBA2 = 0xFF0000FF;       /* check if red, alpha=1.0 */
static int RGBA2_back = 0xFEDFEFEF;  /* light grey white-1 */

void display(void)
{
  char *p;
  
  /* clear window */
  glClear(GL_COLOR_BUFFER_BIT); 
  glLoadIdentity ();
  glColor3f(0.0, 0.0, 0.0);


  /* draw .xbm files */  
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  glRasterPos2f(-.75, -.75);
  glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, rgbpix);
  glRasterPos2f(0.0, 0.0);
  glDrawPixels(width2, height2, GL_RGBA, GL_UNSIGNED_BYTE, rgbpix2);
  
  
  /* draw text */
  glEnable(GL_LINE_SMOOTH);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glEnable(GL_BLEND);
  glLoadIdentity ();
  glTranslatef(-0.2, -0.6, 0.0);
  glScalef(0.0015, 0.0015, 0.0);
  for(p=filename; *p; p++)
    glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
  glLoadIdentity ();
  glTranslatef(0.1, 0.7, 0.0);
  glScalef(0.0015, 0.0015, 0.0);
  for(p=filename2; *p; p++)
    glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
  glFlush(); 
}

void myreshape(int h, int w)
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glViewport(0,0,h,w);
}

void init()
{
  int status, i, j, k;
  
  printf("test_xbmread calling xbmread on file %s\n", filename);
  status = xbmread("s01.xbm", RGBA, RGBA_back, &width, &height, rgbpix);
  printf("xbmread returned status=%d, width=%d, height=%d, RGBA=%X, RGBA_back=%X \n",
         status, width, height, RGBA, RGBA_back);
  status = xbmread("d13.xbm", RGBA2, RGBA2_back, &width2, &height2, rgbpix2);
  printf("xbmread returned status=%d, width2=%d, height2=%d, RGBA2=%X, RGBA2_back=%X \n",
         status, width2, height2, RGBA2, RGBA2_back);

  /* set clear color to yellow */
  glClearColor (0.0, 1.0, 1.0, 0.0);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glViewport(0,0,300,300);
}

int main(int argc, char* argv[])
{
  glutInit(&argc,argv);
  glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);  
  glutInitWindowSize(300,300);
  glutInitWindowPosition(100,100); 
  
  glutCreateWindow(argv[0]);
  glutReshapeFunc(myreshape);
  glutDisplayFunc(display);
  init();
  glutMainLoop();
  return 0;
}

