/* Wei Wang */ #include #include #include #include #include #include #define WIN_WIDTH 128 #define WIN_HEIGHT 128 #define SKIP_ROWS 0 #define SKIP_PIXELS 0 #define BITMAP_TOTAL_W 8*2 #define BITMAP_TOTAL_H 8*2 #define ALIGN 1 typedef struct _bitmap_t { GLint total_w; GLint total_h; GLubyte *data; } bitmap_t; /* Global Vars */ static bitmap_t mybitmap; static bitmap_t output_bitmap; static GLint row_len; static GLfloat i2rMapF[] = {1,1}; static GLfloat i2gMapF[] = {1,0}; static GLfloat i2bMapF[] = {1,0}; static GLfloat i2aMapF[] = {1,1}; void print_bitmap_data(bitmap_t *bitmap) { int i, j; printf("\n--------Dumping Bitmap Data--------\n"); for (i = 0; i < bitmap->total_h; i++) { printf("[%.2X] ", i); for (j = 0; j < (bitmap->total_w/8); j++) { printf("%.2X ", bitmap->data[(bitmap->total_w/8) * i + j]); } printf("\n"); } printf("\n-----------------------------------\n"); } int gen_bitmap(bitmap_t *bitmap, GLint totalW, GLint totalH) { GLint total_width, total_height; GLint i, j; GLubyte *data = NULL; int tmp; total_width = 8 * ((totalW + 7) / 8); total_height = totalH; data = (GLubyte *)malloc(total_width/8 * total_height); if (! data) return -1; for (i = 0; i < total_height; i++) { for (j = 0; j < (total_width/8); j++) { data[(total_width/8) * i + j] = (GLubyte) rand(); } } bitmap->data = data; bitmap->total_w = total_width; bitmap->total_h = total_height; return 0; } void destroy_bitmap(bitmap_t *bitmap) { if (bitmap->data) free(bitmap->data); } void zero_bitmap_data(bitmap_t *bitmap) { memset(bitmap->data, 0, bitmap->total_w/8 * bitmap->total_h); } void display(void) { glClear (GL_COLOR_BUFFER_BIT); print_bitmap_data(&mybitmap); glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT); glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE); glPixelStorei(GL_UNPACK_ROW_LENGTH, row_len); glPixelStorei(GL_UNPACK_ALIGNMENT, ALIGN); glPixelStorei(GL_UNPACK_SKIP_ROWS, SKIP_ROWS); glPixelStorei(GL_UNPACK_SKIP_PIXELS, SKIP_PIXELS); /* only draw the bitmap picture */ glRasterPos2f(0.25, 0.25); glDrawPixels(mybitmap.total_w, mybitmap.total_h, GL_COLOR_INDEX, GL_BITMAP, mybitmap.data); glTexImage2D(GL_TEXTURE_2D, 0, GL_COLOR_INDEX1_EXT, mybitmap.total_w, mybitmap.total_h, 0, GL_COLOR_INDEX, GL_BITMAP, mybitmap.data); zero_bitmap_data(&output_bitmap); glGetTexImage(GL_TEXTURE_2D, 0, GL_COLOR_INDEX, GL_BITMAP, output_bitmap.data); print_bitmap_data(&output_bitmap); glPopClientAttrib(); glFlush (); destroy_bitmap(&mybitmap); destroy_bitmap(&output_bitmap); } void init (void) { if (strstr(glGetString(GL_EXTENSIONS), "GL_EXT_paletted_texture") == NULL) { fprintf(stderr, "E: GL_EXT_paletted_texture is not supported.\n"); exit(0); } glMatrixMode(GL_PROJECTION); glLoadIdentity(); glTranslatef(-1.0, -1.0, 0.0); glScalef(2.0/WIN_WIDTH, 2.0/WIN_HEIGHT, 1.0); glDisable(GL_DITHER); glClearColor (1.0, 0.0, 1.0, 1.0); glShadeModel (GL_FLAT); gen_bitmap(&mybitmap, BITMAP_TOTAL_W, BITMAP_TOTAL_H); gen_bitmap(&output_bitmap, BITMAP_TOTAL_W, BITMAP_TOTAL_H); row_len = BITMAP_TOTAL_W; glPixelMapfv(GL_PIXEL_MAP_I_TO_R, 8, i2rMapF); glPixelMapfv(GL_PIXEL_MAP_I_TO_G, 8, i2gMapF); glPixelMapfv(GL_PIXEL_MAP_I_TO_B, 8, i2bMapF); glPixelMapfv(GL_PIXEL_MAP_I_TO_A, 8, i2aMapF); } static void key(unsigned char k, int x, int y) { switch (k) { case 27: /* Escape */ exit(0); break; default: break; } glutPostRedisplay(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA); glutInitWindowSize (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutKeyboardFunc(key); glutMainLoop(); return 0; }