/* Copyright (c) 2005 IEUS * * This program is licensed under the same terms that Microwindows * and Nano-X are licensed under. See the file LICENSE accompanying * this distribution. */ // Program to display arbitrary bitmap image on the screen #include #include #include #include #include "ieusSetColor.h" #ifndef MIN #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MAX(a,b) ((a) > (b) ? (a) : (b)) #endif #ifndef FALSE #define FALSE GR_FALSE #define TRUE GR_TRUE #endif #define MWINCLUDECOLORS #include "nano-X.h" static GR_WINDOW_ID master; static GR_GC_ID gc1; static GR_SCREEN_INFO grScreenInfo; // Screen info static void errorcatcher(GR_EVENT *ep) { printf("\nERROR (%s) from the nano-X server, %s, error # = %d\n", ep->error.name, nxErrorStrings[ep->error.code], ep->error.id); } // ********************************************************************** int openAndReadFile(char *imageFilename, char **imageData, int *filesize) { FILE *inpfil = fopen(imageFilename, "rb"); if (NULL == inpfil) { printf("ERROR: Input file %s not found\n", imageFilename); return FALSE; } // Read the file into memory so that we can just blow the image on the screen fseek(inpfil, 0L, SEEK_END); *filesize = ftell(inpfil); rewind(inpfil); // Read the bitmap file into memory *imageData = (char *) malloc(*filesize); if (NULL == *imageData) { printf("ERROR starting graphics\n"); return FALSE; } if (1 != fread(*imageData, *filesize, 1, inpfil)) { printf("ERROR starting graphics\n"); return FALSE; } fclose(inpfil); return TRUE; } // ********************************************************************** // convert_overlay_parm_to_overlay_flag - Convert command line parm to // the corresponding flag used for image-overlay on the device static void convert_overlay_parm_to_overlay_flag(char parmMnem[], int *display_op) { if (strcmp(parmMnem,"--xor") == 0) { *display_op = MWROP_XOR; printf("XOR the image\n"); } else if (strcmp(parmMnem,"--or") == 0) { *display_op = MWROP_OR; printf("OR the image\n"); } else if (strcmp(parmMnem,"--and") == 0) { *display_op = MWROP_AND; printf("AND the image\n"); } else { *display_op = MWROP_COPY; printf("COPY the image\n"); } } // ********************************************************************** // convert_overlay_parm_to_overlay_flag - Convert command line parm to // the corresponding flag used for image-overlay on the device static GR_WINDOW_ID loadImage(char *imageData, int imageSize, int *width, int *height) { GR_IMAGE_ID imageId; GR_IMAGE_INFO imageInfo; GR_WINDOW_ID bm_window; imageId = GrLoadImageFromBuffer(imageData, imageSize, 0); GrGetImageInfo(imageId, &imageInfo); *width = MIN(imageInfo.width, grScreenInfo.cols); *height = MIN(imageInfo.height, grScreenInfo.rows); bm_window = GrNewPixmap(*width, *height, NULL); GrFillRect(bm_window, gc1, 0, 0, *width, *height); GrDrawImageToFit(bm_window, gc1, 0, 0, *width, *height, imageId); GrFreeImage(imageId); return bm_window; } // ********************************************************************** int main(int argc,char **argv) { char *primaryImageData, *secondaryImageData; GR_WINDOW_ID bm_window1, bm_window2; char primaryImageFilename[250], secondaryImageFilename[250]; int display_op; int primarySize, secondarySize; int retcode; int width1, height1, width2, height2; if (argc <= 2) { printf("No image files specified...bye\n"); exit(4); } strcpy(primaryImageFilename, argv[1]); strcpy(secondaryImageFilename, argv[2]); retcode = openAndReadFile(primaryImageFilename, &primaryImageData, &primarySize); if (retcode) { retcode = openAndReadFile(secondaryImageFilename, &secondaryImageData, &secondarySize); } // Failure means we exit now... if (!retcode) { exit(2); } // Initialize the fb device color palette { int cp_retcode = IEUS_SetColorPalette(); if (0 != cp_retcode) { exit(cp_retcode); } } // Color palette updated - no do the nano-X stuff if (GrOpen() < 0) { fprintf(stderr, "cannot open graphics\n"); exit(3); } // Handle errors if they do occur... GrSetErrorHandler(errorcatcher); GrSelectEvents(GR_ROOT_WINDOW_ID, GR_EVENT_MASK_ERROR); gc1 = GrNewGC(); // Use standard method to get screen geometry GrGetScreenInfo(&grScreenInfo); // Now we can open one window and use the full display area master = GrNewWindow(GR_ROOT_WINDOW_ID, 0, 0, grScreenInfo.cols, grScreenInfo.rows, 0, // no border BLACK, BLACK); GrMapWindow(master); GrSetGCForeground(gc1, WHITE); GrSetGCBackground(gc1, BLACK); GrSetGCMode(gc1, GR_MODE_COPY); GrSetGCUseBackground(gc1, TRUE); if (argc > 2) { convert_overlay_parm_to_overlay_flag(argv[3],&display_op); } else { display_op = MWROP_COPY; } bm_window1 = loadImage(primaryImageData, primarySize, &width1, &height1); bm_window2 = loadImage(secondaryImageData, secondarySize, &width2, &height2); // combine image2 into image1 offscreen GrCopyArea(bm_window1, gc1, 0, 0, MIN(width1, width2), MIN(height1, height2), bm_window2, 0, 0, display_op); // display result onscreen GrCopyArea(master, gc1, 0, 0, MIN(width1, width2), MIN(height1, height2), bm_window1, 0, 0, MWROP_COPY); GrFlush(); // Wait for key to exit... printf("Image on screen...press key to exit..."); getc(stdin); GrClose(); return 0; }