wexjpeg.c

来自「cpc-1631的BSP包for VxWorks操作系统」· C语言 代码 · 共 544 行 · 第 1/2 页

C
544
字号
    
    UGL_DEVICE_ID devId;

    /*
    * Initialize UGL.  Must do this before trying to do anything
    * else within UGL/WindML.
    */

    uglInitialize();

    /* Obtain the device identifier for the display */

    devId = (UGL_DEVICE_ID)uglRegistryFind (UGL_DISPLAY_TYPE,  0, 0,0)->id;

    /* Obtain the font driver */

    fontDrvId =  (UGL_FONT_DRIVER_ID)uglRegistryFind (UGL_FONT_ENGINE_TYPE,  0, 0,0)->id;

    /* Create the font */

    uglFontFindString(fontDrvId, "pixelSize=12", &fontDef);

    if ((font = uglFontCreate(fontDrvId, &fontDef)) == UGL_NULL)
	{
	printf("WindML: Font not found. Exiting.\n");
	uglDeinitialize();
	return;        
	}

    inputServiceId = (UGL_INPUT_SERVICE_ID)uglRegistryFind (UGL_INPUT_SERVICE_TYPE,  0, 0,0)->id;

    /*
    * Obtain the dimensions of the display.  We will use these
    * to center some of our objects.
    */

    uglInfo(devId, UGL_MODE_INFO_REQ, &modeInfo);
    displayWidth = modeInfo.width;
    displayHeight = modeInfo.height;

    /*
    * Create a graphics context.  Default values are set during
    * the creation.
    */

    gc = uglGcCreate(devId);

    uglFontSet(gc, font);

    /*
    * Initialize colors. UGL maintains a Color Look-Up Table (CLUT)
    * for devices that do not represent colors directly.  Essentially
    * some hardware is only able to represent a subset of colors at
    * any given time.  To manage which colors will be available for
    * rendering, UGL uses color allocation.  If the hardware is able
    * to represent colors directly, then the uglColorAlloc() function
    * still works, but it is then essentially a no-op.
    *
    * We have set up for 16 colors, so here we will "allocate" them
    * within UGL's CLUT (sometimes referred to as a "palette").  Colors
    * can also be de-allocated, or freed, with uglColorFree.
    *
    * Since the ARGB's are intermingled with the UGL_COLORs in
    * the colorTable, we must allocate each color individually.
    * If the ARGB's had a contiguous array of ARGBs, and likewise for
    * the UGL_COLORs, a single uglColorAlloc call could be made.
    * (see the windows example).
    *
    * Note that these colors will only take effect on an psuedo color system.
    * Without dithering, which is not provided in this example, high color
    * jpeg images loose much of their quality on pseudo color systems 
    * particularly when only 16 colors are allocated.
    */

    uglColorAlloc (devId, &colorTable[BLACK].rgbColor, UGL_NULL, 
                   &colorTable[BLACK].uglColor, 1);
    uglColorAlloc(devId, &colorTable[BLUE].rgbColor, UGL_NULL,
                  &colorTable[BLUE].uglColor, 1);
    uglColorAlloc(devId, &colorTable[GREEN].rgbColor, UGL_NULL,
                  &colorTable[GREEN].uglColor, 1);
    uglColorAlloc(devId, &colorTable[CYAN].rgbColor, UGL_NULL,
                  &colorTable[CYAN].uglColor, 1);

    uglColorAlloc(devId, &colorTable[RED].rgbColor, UGL_NULL,
                  &colorTable[RED].uglColor, 1);
    uglColorAlloc(devId, &colorTable[MAGENTA].rgbColor, UGL_NULL,
                  &colorTable[MAGENTA].uglColor, 1);
    uglColorAlloc(devId, &colorTable[BROWN].rgbColor, UGL_NULL,
                  &colorTable[BROWN].uglColor, 1);
    uglColorAlloc(devId, &colorTable[LIGHTGRAY].rgbColor, UGL_NULL,
                  &colorTable[LIGHTGRAY].uglColor, 1);

    uglColorAlloc(devId, &colorTable[DARKGRAY].rgbColor, UGL_NULL,
                  &colorTable[DARKGRAY].uglColor, 1);
    uglColorAlloc(devId, &colorTable[LIGHTBLUE].rgbColor, UGL_NULL,
                  &colorTable[LIGHTBLUE].uglColor, 1);
    uglColorAlloc(devId, &colorTable[LIGHTGREEN].rgbColor, UGL_NULL,
                  &colorTable[LIGHTGREEN].uglColor, 1);
    uglColorAlloc(devId, &colorTable[LIGHTCYAN].rgbColor, UGL_NULL,
                  &colorTable[LIGHTCYAN].uglColor, 1);

    uglColorAlloc(devId, &colorTable[LIGHTRED].rgbColor, UGL_NULL,
                  &colorTable[LIGHTRED].uglColor, 1);
    uglColorAlloc(devId, &colorTable[LIGHTMAGENTA].rgbColor, UGL_NULL,
                  &colorTable[LIGHTMAGENTA].uglColor, 1);
    uglColorAlloc(devId, &colorTable[YELLOW].rgbColor,  UGL_NULL,
                  &colorTable[YELLOW].uglColor, 1);
    uglColorAlloc(devId, &colorTable[WHITE].rgbColor,  UGL_NULL,
                  &colorTable[WHITE].uglColor, 1);


    /* Initialize the Jpeg library and extension */

    jpegId = uglJpegInit(devId, &jpegVersion);

    /* Check to see if Jpeg is available in the driver */

    if (jpegId == UGL_NULL) 
	{
	sprintf(errorMessage, "Jpeg not included in the driver");
	uglTextSizeGet(font, &textWidth, &textHeight, -1, errorMessage);
	uglBackgroundColorSet(gc, colorTable[BLACK].uglColor);
	uglForegroundColorSet(gc, colorTable[LIGHTRED].uglColor);
	uglTextDraw(gc, (displayWidth - textWidth) / 2, 
			(displayHeight - textHeight) / 3, -1, errorMessage);
	pause(mode);
	uglFontDestroy(font);
	uglGcDestroy (gc);
	uglDeinitialize();
	return;
	}

    /* Set the desired mode for the Jpeg engine */

    jpegMode.quality = 75;
    jpegMode.smooth = 0;
    jpegMode.scale = 1;
    uglJpegModeSet(jpegId,&jpegMode);

    ClearScreen(gc);

    /* Decode the first image */

    /* If no path was provided, assume the current directory. */

    if (path == UGL_NULL || path[0] == UGL_NULL)
        sprintf(filename, "%s", JPEG_IMG_FILE1);
    else
        sprintf(filename, "%s/%s", path, JPEG_IMG_FILE1);
    fp = fopen(filename, "rb");
    if (fp == NULL)
	{
	sprintf(errorMessage, "Unable to open %s.", JPEG_IMG_FILE1);
	uglTextSizeGet(font, &textWidth, &textHeight, -1, errorMessage);
	uglBackgroundColorSet(gc, colorTable[BLACK].uglColor);
	uglForegroundColorSet(gc, colorTable[LIGHTRED].uglColor);
	uglTextDraw(gc, (displayWidth - textWidth) / 2, 
			(displayHeight - textHeight) / 3, -1, errorMessage);
	pause(mode);
	}
    else
	{
        uglJpegToDDBFromFile (jpegId, fp, &jpegDdbId, UGL_NULL, 0, 0);
        fclose (fp);
                  
	uglBitmapSizeGet(jpegDdbId, &jpegWidth, &jpegHeight);
	uglBitmapBlt(gc, jpegDdbId, 0, 0, jpegWidth - 1, jpegHeight - 1, 
		     UGL_DEFAULT_ID, 0, 0);
	uglBitmapDestroy(devId, jpegDdbId);
	jpegDdbId = UGL_NULL;

	pause(mode);
	ClearScreen(gc);
	}

    /* Decode the second image */

    if (path == UGL_NULL || path[0] == UGL_NULL)
        sprintf(filename, "%s", JPEG_IMG_FILE2);
    else
        sprintf(filename, "%s/%s", path, JPEG_IMG_FILE2);
    fp = fopen(filename, "rb");
    if (fp == NULL) 
	{
	sprintf(errorMessage, "Unable to open %s.", JPEG_IMG_FILE2);
	uglTextSizeGet(font, &textWidth, &textHeight, -1, errorMessage);
	uglBackgroundColorSet(gc, colorTable[BLACK].uglColor);
	uglForegroundColorSet(gc, colorTable[LIGHTRED].uglColor);
	uglTextDraw(gc, (displayWidth - textWidth) / 2, 
			(displayHeight - textHeight) / 3, -1, errorMessage);
	pause(mode);
	}
    else
	{
	uglJpegToDDBFromFile (jpegId, fp, &jpegDdbId, UGL_NULL, 0, 0);
	fclose (fp);
                   
	uglBitmapSizeGet(jpegDdbId, &jpegWidth, &jpegHeight);
	uglBitmapBlt(gc, jpegDdbId, 0, 0, jpegWidth - 1, jpegHeight - 1, 
		     UGL_DEFAULT_ID, 0, 0);
	uglBitmapDestroy(devId, jpegDdbId);
	jpegDdbId = UGL_NULL;

	pause(mode);
	ClearScreen(gc);
	}

    /* Decode the third image and create a jpeg from image 3 */

    if (path == UGL_NULL || path[0] == UGL_NULL)
        sprintf(filename, "%s", JPEG_IMG_FILE3);
    else
        sprintf(filename, "%s/%s", path, JPEG_IMG_FILE3);
    fp = fopen(filename, "rb");
    if (fp == NULL) 
	{
	sprintf(errorMessage, "Unable to open %s.", JPEG_IMG_FILE3);
	uglTextSizeGet(font, &textWidth, &textHeight, -1, errorMessage);
	uglBackgroundColorSet(gc, colorTable[BLACK].uglColor);
	uglForegroundColorSet(gc, colorTable[LIGHTRED].uglColor);
	uglTextDraw(gc, (displayWidth - textWidth) / 2, 
			(displayHeight - textHeight) / 3, -1, errorMessage);
	pause(mode);
	}
    else
	{
	uglJpegToDDBFromFile (jpegId, fp, &jpegDdbId, UGL_NULL, 0, 0);
	fclose (fp);
                   
	uglBitmapSizeGet(jpegDdbId, &jpegWidth, &jpegHeight);
	uglBitmapBlt(gc, jpegDdbId, 0, 0, jpegWidth - 1, jpegHeight - 1, 
		     UGL_DEFAULT_ID, 0, 0);
	uglBitmapDestroy(devId, jpegDdbId);
	jpegDdbId = UGL_NULL;

	/* Create Jpeg image on host */

    if (path == UGL_NULL || path[0] == UGL_NULL)
        sprintf(filename, "%s", JPEG_IMG_FILE4);
    else
        sprintf(filename, "%s/%s", path, JPEG_IMG_FILE4);
    fp = fopen(filename, "wb");
	if (fp == NULL)
	    {
	    sprintf(errorMessage, "Unable to create %s.", JPEG_IMG_FILE4);
	    uglTextSizeGet(font, &textWidth, &textHeight, -1, errorMessage);
	    uglBackgroundColorSet(gc, colorTable[BLACK].uglColor);
	    uglForegroundColorSet(gc, colorTable[LIGHTRED].uglColor);
	    uglTextDraw(gc, (displayWidth - textWidth) / 2, 
			(displayHeight - textHeight) / 3, -1, errorMessage);
	    }
	else
	    {
	    uglJpegFromDDB (jpegId, UGL_DISPLAY_ID, 0, 0, jpegWidth - 1, 
			    jpegHeight - 1, fp);

	    fclose (fp);
	    }

	pause(mode);
	ClearScreen(gc);
	}

    /* Clean Up */

    uglFontDestroy(font);
    uglGcDestroy (gc);
    uglDeinitialize();

    return;
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?