OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tk/] [win/] [tkWinImage.c] - Diff between revs 578 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 578 Rev 1765
/*
/*
 * tkWinImage.c --
 * tkWinImage.c --
 *
 *
 *      This file contains routines for manipulation full-color images.
 *      This file contains routines for manipulation full-color images.
 *
 *
 * Copyright (c) 1995 Sun Microsystems, Inc.
 * Copyright (c) 1995 Sun Microsystems, Inc.
 *
 *
 * See the file "license.terms" for information on usage and redistribution
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 *
 * RCS: @(#) $Id: tkWinImage.c,v 1.1.1.1 2002-01-16 10:26:02 markom Exp $
 * RCS: @(#) $Id: tkWinImage.c,v 1.1.1.1 2002-01-16 10:26:02 markom Exp $
 */
 */
 
 
#include "tkWinInt.h"
#include "tkWinInt.h"
 
 
static int              DestroyImage _ANSI_ARGS_((XImage* data));
static int              DestroyImage _ANSI_ARGS_((XImage* data));
static unsigned long    ImageGetPixel _ANSI_ARGS_((XImage *image, int x, int y));
static unsigned long    ImageGetPixel _ANSI_ARGS_((XImage *image, int x, int y));
static int              PutPixel _ANSI_ARGS_((XImage *image, int x, int y,
static int              PutPixel _ANSI_ARGS_((XImage *image, int x, int y,
                            unsigned long pixel));
                            unsigned long pixel));


/*
/*
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 *
 *
 * DestroyImage --
 * DestroyImage --
 *
 *
 *      This is a trivial wrapper around ckfree to make it possible to
 *      This is a trivial wrapper around ckfree to make it possible to
 *      pass ckfree as a pointer.
 *      pass ckfree as a pointer.
 *
 *
 * Results:
 * Results:
 *      None.
 *      None.
 *
 *
 * Side effects:
 * Side effects:
 *      Deallocates the image.
 *      Deallocates the image.
 *
 *
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 */
 */
 
 
int
int
DestroyImage(imagePtr)
DestroyImage(imagePtr)
     XImage *imagePtr;          /* image to free */
     XImage *imagePtr;          /* image to free */
{
{
    if (imagePtr) {
    if (imagePtr) {
        if (imagePtr->data) {
        if (imagePtr->data) {
            ckfree((char*)imagePtr->data);
            ckfree((char*)imagePtr->data);
        }
        }
        ckfree((char*)imagePtr);
        ckfree((char*)imagePtr);
    }
    }
    return 0;
    return 0;
}
}


/*
/*
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 *
 *
 * ImageGetPixel --
 * ImageGetPixel --
 *
 *
 *      Get a single pixel from an image.
 *      Get a single pixel from an image.
 *
 *
 * Results:
 * Results:
 *      Returns the 32 bit pixel value.
 *      Returns the 32 bit pixel value.
 *
 *
 * Side effects:
 * Side effects:
 *      None.
 *      None.
 *
 *
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 */
 */
 
 
unsigned long
unsigned long
ImageGetPixel(image, x, y)
ImageGetPixel(image, x, y)
    XImage *image;
    XImage *image;
    int x, y;
    int x, y;
{
{
    unsigned long pixel = 0;
    unsigned long pixel = 0;
    unsigned char *srcPtr = &(image->data[(y * image->bytes_per_line)
    unsigned char *srcPtr = &(image->data[(y * image->bytes_per_line)
            + ((x * image->bits_per_pixel) / NBBY)]);
            + ((x * image->bits_per_pixel) / NBBY)]);
 
 
    switch (image->bits_per_pixel) {
    switch (image->bits_per_pixel) {
        case 32:
        case 32:
        case 24:
        case 24:
            pixel = RGB(srcPtr[2], srcPtr[1], srcPtr[0]);
            pixel = RGB(srcPtr[2], srcPtr[1], srcPtr[0]);
            break;
            break;
        case 16:
        case 16:
            pixel = RGB(((((WORD*)srcPtr)[0]) >> 7) & 0xf8,
            pixel = RGB(((((WORD*)srcPtr)[0]) >> 7) & 0xf8,
                    ((((WORD*)srcPtr)[0]) >> 2) & 0xf8,
                    ((((WORD*)srcPtr)[0]) >> 2) & 0xf8,
                    ((((WORD*)srcPtr)[0]) << 3) & 0xf8);
                    ((((WORD*)srcPtr)[0]) << 3) & 0xf8);
            break;
            break;
        case 8:
        case 8:
            pixel = srcPtr[0];
            pixel = srcPtr[0];
            break;
            break;
        case 4:
        case 4:
            pixel = ((x%2) ? (*srcPtr) : ((*srcPtr) >> 4)) & 0x0f;
            pixel = ((x%2) ? (*srcPtr) : ((*srcPtr) >> 4)) & 0x0f;
            break;
            break;
        case 1:
        case 1:
            pixel = ((*srcPtr) & (0x80 >> (x%8))) ? 1 : 0;
            pixel = ((*srcPtr) & (0x80 >> (x%8))) ? 1 : 0;
            break;
            break;
    }
    }
    return pixel;
    return pixel;
}
}


/*
/*
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 *
 *
 * PutPixel --
 * PutPixel --
 *
 *
 *      Set a single pixel in an image.
 *      Set a single pixel in an image.
 *
 *
 * Results:
 * Results:
 *      None.
 *      None.
 *
 *
 * Side effects:
 * Side effects:
 *      None.
 *      None.
 *
 *
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 */
 */
 
 
static int
static int
PutPixel(image, x, y, pixel)
PutPixel(image, x, y, pixel)
    XImage *image;
    XImage *image;
    int x, y;
    int x, y;
    unsigned long pixel;
    unsigned long pixel;
{
{
    unsigned char *destPtr = &(image->data[(y * image->bytes_per_line)
    unsigned char *destPtr = &(image->data[(y * image->bytes_per_line)
            + ((x * image->bits_per_pixel) / NBBY)]);
            + ((x * image->bits_per_pixel) / NBBY)]);
 
 
    switch  (image->bits_per_pixel) {
    switch  (image->bits_per_pixel) {
        case 32:
        case 32:
            /*
            /*
             * Pixel is DWORD: 0x00BBGGRR
             * Pixel is DWORD: 0x00BBGGRR
             */
             */
 
 
            destPtr[3] = 0;
            destPtr[3] = 0;
        case 24:
        case 24:
            /*
            /*
             * Pixel is triplet: 0xBBGGRR.
             * Pixel is triplet: 0xBBGGRR.
             */
             */
 
 
            destPtr[0] = (unsigned char) GetBValue(pixel);
            destPtr[0] = (unsigned char) GetBValue(pixel);
            destPtr[1] = (unsigned char) GetGValue(pixel);
            destPtr[1] = (unsigned char) GetGValue(pixel);
            destPtr[2] = (unsigned char) GetRValue(pixel);
            destPtr[2] = (unsigned char) GetRValue(pixel);
            break;
            break;
        case 16:
        case 16:
            /*
            /*
             * Pixel is WORD: 5-5-5 (R-G-B)
             * Pixel is WORD: 5-5-5 (R-G-B)
             */
             */
 
 
            (*(WORD*)destPtr) =
            (*(WORD*)destPtr) =
                ((GetRValue(pixel) & 0xf8) << 7)
                ((GetRValue(pixel) & 0xf8) << 7)
                | ((GetGValue(pixel) & 0xf8) <<2)
                | ((GetGValue(pixel) & 0xf8) <<2)
                | ((GetBValue(pixel) & 0xf8) >> 3);
                | ((GetBValue(pixel) & 0xf8) >> 3);
            break;
            break;
        case 8:
        case 8:
            /*
            /*
             * Pixel is 8-bit index into color table.
             * Pixel is 8-bit index into color table.
             */
             */
 
 
            (*destPtr) = (unsigned char) pixel;
            (*destPtr) = (unsigned char) pixel;
            break;
            break;
        case 4:
        case 4:
            /*
            /*
             * Pixel is 4-bit index in MSBFirst order.
             * Pixel is 4-bit index in MSBFirst order.
             */
             */
            if (x%2) {
            if (x%2) {
                (*destPtr) = (unsigned char) (((*destPtr) & 0xf0)
                (*destPtr) = (unsigned char) (((*destPtr) & 0xf0)
                    | (pixel & 0x0f));
                    | (pixel & 0x0f));
            } else {
            } else {
                (*destPtr) = (unsigned char) (((*destPtr) & 0x0f)
                (*destPtr) = (unsigned char) (((*destPtr) & 0x0f)
                    | ((pixel << 4) & 0xf0));
                    | ((pixel << 4) & 0xf0));
            }
            }
            break;
            break;
        case 1: {
        case 1: {
            /*
            /*
             * Pixel is bit in MSBFirst order.
             * Pixel is bit in MSBFirst order.
             */
             */
 
 
            int mask = (0x80 >> (x%8));
            int mask = (0x80 >> (x%8));
            if (pixel) {
            if (pixel) {
                (*destPtr) |= mask;
                (*destPtr) |= mask;
            } else {
            } else {
                (*destPtr) &= ~mask;
                (*destPtr) &= ~mask;
            }
            }
        }
        }
        break;
        break;
    }
    }
    return 0;
    return 0;
}
}


/*
/*
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 *
 *
 * XCreateImage --
 * XCreateImage --
 *
 *
 *      Allocates storage for a new XImage.
 *      Allocates storage for a new XImage.
 *
 *
 * Results:
 * Results:
 *      Returns a newly allocated XImage.
 *      Returns a newly allocated XImage.
 *
 *
 * Side effects:
 * Side effects:
 *      None.
 *      None.
 *
 *
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 */
 */
 
 
XImage *
XImage *
XCreateImage(display, visual, depth, format, offset, data, width, height,
XCreateImage(display, visual, depth, format, offset, data, width, height,
        bitmap_pad, bytes_per_line)
        bitmap_pad, bytes_per_line)
    Display* display;
    Display* display;
    Visual* visual;
    Visual* visual;
    unsigned int depth;
    unsigned int depth;
    int format;
    int format;
    int offset;
    int offset;
    char* data;
    char* data;
    unsigned int width;
    unsigned int width;
    unsigned int height;
    unsigned int height;
    int bitmap_pad;
    int bitmap_pad;
    int bytes_per_line;
    int bytes_per_line;
{
{
    XImage* imagePtr = (XImage *) ckalloc(sizeof(XImage));
    XImage* imagePtr = (XImage *) ckalloc(sizeof(XImage));
    imagePtr->width = width;
    imagePtr->width = width;
    imagePtr->height = height;
    imagePtr->height = height;
    imagePtr->xoffset = offset;
    imagePtr->xoffset = offset;
    imagePtr->format = format;
    imagePtr->format = format;
    imagePtr->data = data;
    imagePtr->data = data;
    imagePtr->byte_order = LSBFirst;
    imagePtr->byte_order = LSBFirst;
    imagePtr->bitmap_unit = 8;
    imagePtr->bitmap_unit = 8;
    imagePtr->bitmap_bit_order = MSBFirst;
    imagePtr->bitmap_bit_order = MSBFirst;
    imagePtr->bitmap_pad = bitmap_pad;
    imagePtr->bitmap_pad = bitmap_pad;
    imagePtr->bits_per_pixel = depth;
    imagePtr->bits_per_pixel = depth;
    imagePtr->depth = depth;
    imagePtr->depth = depth;
 
 
    /*
    /*
     * Under Windows, bitmap_pad must be on an LONG data-type boundary.
     * Under Windows, bitmap_pad must be on an LONG data-type boundary.
     */
     */
 
 
#define LONGBITS    (sizeof(LONG) * 8)
#define LONGBITS    (sizeof(LONG) * 8)
 
 
    bitmap_pad = (bitmap_pad + LONGBITS - 1) / LONGBITS * LONGBITS;
    bitmap_pad = (bitmap_pad + LONGBITS - 1) / LONGBITS * LONGBITS;
 
 
    /*
    /*
     * Round to the nearest bitmap_pad boundary.
     * Round to the nearest bitmap_pad boundary.
     */
     */
 
 
    if (bytes_per_line) {
    if (bytes_per_line) {
        imagePtr->bytes_per_line = bytes_per_line;
        imagePtr->bytes_per_line = bytes_per_line;
    } else {
    } else {
        imagePtr->bytes_per_line = (((depth * width)
        imagePtr->bytes_per_line = (((depth * width)
                + (bitmap_pad - 1)) >> 3) & ~((bitmap_pad >> 3) - 1);
                + (bitmap_pad - 1)) >> 3) & ~((bitmap_pad >> 3) - 1);
    }
    }
 
 
    imagePtr->red_mask = 0;
    imagePtr->red_mask = 0;
    imagePtr->green_mask = 0;
    imagePtr->green_mask = 0;
    imagePtr->blue_mask = 0;
    imagePtr->blue_mask = 0;
 
 
    imagePtr->f.put_pixel = PutPixel;
    imagePtr->f.put_pixel = PutPixel;
    imagePtr->f.get_pixel = ImageGetPixel;
    imagePtr->f.get_pixel = ImageGetPixel;
    imagePtr->f.destroy_image = DestroyImage;
    imagePtr->f.destroy_image = DestroyImage;
    imagePtr->f.create_image = NULL;
    imagePtr->f.create_image = NULL;
    imagePtr->f.sub_image = NULL;
    imagePtr->f.sub_image = NULL;
    imagePtr->f.add_pixel = NULL;
    imagePtr->f.add_pixel = NULL;
 
 
    return imagePtr;
    return imagePtr;
}
}


/*
/*
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 *
 *
 * XGetImage --
 * XGetImage --
 *
 *
 *      This function copies data from a pixmap or window into an
 *      This function copies data from a pixmap or window into an
 *      XImage.
 *      XImage.
 *
 *
 * Results:
 * Results:
 *      Returns a newly allocated image containing the data from the
 *      Returns a newly allocated image containing the data from the
 *      given rectangle of the given drawable.
 *      given rectangle of the given drawable.
 *
 *
 * Side effects:
 * Side effects:
 *      None.
 *      None.
 *
 *
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 */
 */
 
 
XImage *
XImage *
XGetImage(display, d, x, y, width, height, plane_mask, format)
XGetImage(display, d, x, y, width, height, plane_mask, format)
    Display* display;
    Display* display;
    Drawable d;
    Drawable d;
    int x;
    int x;
    int y;
    int y;
    unsigned int width;
    unsigned int width;
    unsigned int height;
    unsigned int height;
    unsigned long plane_mask;
    unsigned long plane_mask;
    int format;
    int format;
{
{
    TkWinDrawable *twdPtr = (TkWinDrawable *)d;
    TkWinDrawable *twdPtr = (TkWinDrawable *)d;
    XImage *imagePtr;
    XImage *imagePtr;
    HDC dc;
    HDC dc;
    char infoBuf[sizeof(BITMAPINFO) + sizeof(RGBQUAD)];
    char infoBuf[sizeof(BITMAPINFO) + sizeof(RGBQUAD)];
    BITMAPINFO *infoPtr = (BITMAPINFO*)infoBuf;
    BITMAPINFO *infoPtr = (BITMAPINFO*)infoBuf;
 
 
    if ((twdPtr->type != TWD_BITMAP) || (twdPtr->bitmap.handle == NULL)
    if ((twdPtr->type != TWD_BITMAP) || (twdPtr->bitmap.handle == NULL)
            || (format != XYPixmap) || (plane_mask != 1)) {
            || (format != XYPixmap) || (plane_mask != 1)) {
        panic("XGetImage: not implemented");
        panic("XGetImage: not implemented");
    }
    }
 
 
 
 
    imagePtr = XCreateImage(display, NULL, 1, XYBitmap, 0, NULL,
    imagePtr = XCreateImage(display, NULL, 1, XYBitmap, 0, NULL,
            width, height, 32, 0);
            width, height, 32, 0);
    imagePtr->data = ckalloc(imagePtr->bytes_per_line * imagePtr->height);
    imagePtr->data = ckalloc(imagePtr->bytes_per_line * imagePtr->height);
 
 
    dc = GetDC(NULL);
    dc = GetDC(NULL);
 
 
    GetDIBits(dc, twdPtr->bitmap.handle, 0, height, NULL,
    GetDIBits(dc, twdPtr->bitmap.handle, 0, height, NULL,
            infoPtr, DIB_RGB_COLORS);
            infoPtr, DIB_RGB_COLORS);
 
 
    infoPtr->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    infoPtr->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    infoPtr->bmiHeader.biWidth = width;
    infoPtr->bmiHeader.biWidth = width;
    infoPtr->bmiHeader.biHeight = -(LONG)height;
    infoPtr->bmiHeader.biHeight = -(LONG)height;
    infoPtr->bmiHeader.biPlanes = 1;
    infoPtr->bmiHeader.biPlanes = 1;
    infoPtr->bmiHeader.biBitCount = 1;
    infoPtr->bmiHeader.biBitCount = 1;
    infoPtr->bmiHeader.biCompression = BI_RGB;
    infoPtr->bmiHeader.biCompression = BI_RGB;
    infoPtr->bmiHeader.biCompression = 0;
    infoPtr->bmiHeader.biCompression = 0;
    infoPtr->bmiHeader.biXPelsPerMeter = 0;
    infoPtr->bmiHeader.biXPelsPerMeter = 0;
    infoPtr->bmiHeader.biYPelsPerMeter = 0;
    infoPtr->bmiHeader.biYPelsPerMeter = 0;
    infoPtr->bmiHeader.biClrUsed = 0;
    infoPtr->bmiHeader.biClrUsed = 0;
    infoPtr->bmiHeader.biClrImportant = 0;
    infoPtr->bmiHeader.biClrImportant = 0;
 
 
    GetDIBits(dc, twdPtr->bitmap.handle, 0, height, imagePtr->data,
    GetDIBits(dc, twdPtr->bitmap.handle, 0, height, imagePtr->data,
            infoPtr, DIB_RGB_COLORS);
            infoPtr, DIB_RGB_COLORS);
    ReleaseDC(NULL, dc);
    ReleaseDC(NULL, dc);
 
 
    return imagePtr;
    return imagePtr;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.