⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 icptest.c

📁 用于TM1300/PNX1300系列DSP(主要用于视频处理)的外部设备的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
 *                        Int bytesPerPixel;
 *                        Int srcStride; Source image stride, pitch, 
 *                                       or line offset
 *                        UInt8 *destAddr; Char pointer to destination 
 *                                         address. Should
 *                                         be within the video frame buffer.
 *                        Int destStride; Destination image stride, 
 *                                        pitch, or line offset
 *                        Int adjustsize; adjustSize gives the number of 
 *                                        pixels in the output display. 
 *                                        4 for RGB 24+alpha,
 *                                        3 for RGB24, 2 for RGB16, and 
 *                                        1 for RGB 8.
 * Function Result      : Return zero on success
 *                        ICP_ERR_INVALID_HEIGHT; Source or destination 
 *                            height <= 0.
 *                        ICP_ERR_INVALID_WIDTH; Source or destination 
 *                            width <=0.
 *                        ICP_ERR_INVALID_STRIDE; Source or destination 
 *                            stride <= 0.
 * Side effects         : If the destination address is not inside the 
 *                            video frame buffer
 *                        then the imageView may hang. This function 
 *                        does not check the validity of the destination 
 *                        address.
 */

Int 
imageView(UInt8 * srcAddr,
      Int width, Int height, Int bytesPerPixel, Int srcStride,
      UInt8 * destAddr, Int destStride, Int adjustSize)
{
    Int         i, j;
    UInt8      *dest = destAddr;
    UInt8      *src = srcAddr;
    Int         bytesToCopy = width * bytesPerPixel;
    UInt8       ch;


    /* Error processing */
    if (height <= 0)
        return (ICP_ERR_INVALID_HEIGHT);
    else if (width <= 0)
        return (ICP_ERR_INVALID_WIDTH);
    else if (srcStride <= 0 || destStride <= 0)
        return (ICP_ERR_INVALID_STRIDE);

    if (adjustSize == 0) {
        for (i = 0; i < height; i++) {
            memcpy(dest, src, bytesToCopy);
            dest += destStride;
            src += srcStride;
        }
    }
    else if (adjustSize == 4) {
        for (i = 0; i < height; i++) {
            for (j = 0; j < bytesToCopy; j++) {
                *dest++ = *src;
                *dest++ = *src;
                *dest++ = *src++;
                *dest++ = 0xff;
            }
            dest += destStride - 4 * bytesToCopy;
            src += srcStride - bytesToCopy;
        }
    }
    else if (adjustSize == 3) {
        for (i = 0; i < height; i++) {
            for (j = 0; j < bytesToCopy; j++) {
                *dest++ = *src;
                *dest++ = *src;
                *dest++ = *src++;
            }
            dest += destStride - 3 * bytesToCopy;
            src += srcStride - bytesToCopy;
        }
    }
    else if (adjustSize == 2) {
        for (i = 0; i < height; i++) {
            for (j = 0; j < bytesToCopy; j++) {
                ch = *src++;
                ch = ch & 0xf8;
                *dest++ = (ch >> 3) | (ch << 2);
                *dest++ = (ch >> 1) | (ch >> 6);
            }
            dest += destStride - 2 * bytesToCopy;
            src += srcStride - bytesToCopy;
        }
    }
    else if (adjustSize == 1) {
        for (i = 0; i < height; i++) {
            for (j = 0; j < bytesToCopy; j++) {
                *dest++ = *src++;
            }
            dest += destStride - bytesToCopy;
            src += srcStride - bytesToCopy;
        }
    }
    return 0;
}

/*
 * Function             : get_parameters
 *                        Reads the command line parameters.
 * Parameters           : Int argc;
 *                        Char *argv[];
 *                        UInt32 *disp_addr; returns the display address, 
 *                                           if legal.
 *                        Int *stride;       return the display stride.
 *                        Int *bpp;          returns bytes per pixel.
 *                        Int *not555a;      For identifying RGB16.
 * Function Result      : Returns the command line parameters.
 *                        If nothing entered then assumes the default values.
 */

#define TRUE 1
#define FALSE 0
void 
get_parameters(Int argc, Char * argv[],
               UInt32 * disp_addr, Int * stride, Int * bpp, Int * not555a)
{
    Int         ok;
    Char        temp;
    Int        flags = 0;

    ok = TRUE;
    while (ok && (--argc > 0)) {
        if ((*++argv)[0] == '-') {
            /* It is an option. */
            if (strcmp(*argv, "-d") == 0) {
                if (--argc > 0) {
                    if (sscanf(*++argv, "%x %c", disp_addr, &temp) != 1) {
                        printf("Error: hex. number expected as -d argument.\n");
                        ok = FALSE;
                    }
		    else {
		      flags |= 0x01;
		    }
		}
                else {
                    printf("Error in display address: option "
                           "-d requires argument.\n");
                    ok = FALSE;
                }
            }
            else if (strcmp(*argv, "-s") == 0) {
                if (--argc > 0) {
                    if (sscanf(*++argv, "%d %c", stride, &temp) != 1) {
                        fprintf(stderr, "Error: Integer expected as -s "
                                        "argument.\n");
                        ok = FALSE;
                    }
		    else {
		      flags |= 0x02;
		    }
                }
                else {
                    fprintf(stderr, "Error: option -s requires argument.\n");
                    ok = FALSE;
                }

            }
            else if (strcmp(*argv, "-b") == 0) {
                if (--argc > 0) {
                    if (sscanf(*++argv, "%d %c", bpp, &temp) != 1) {
                        fprintf(stderr, "Error: Integer expected as "
                                        "-b argument.\n");
                        ok = FALSE;
                    }
		    else {
		      flags |= 0x04;
		    }
                }
                else {
                    fprintf(stderr, "Error: option -b requires argument.\n");
                    ok = FALSE;
                }

            }
            else if (strcmp(*argv, "-not555a") == 0) {
                *not555a = 1;
            }
            else {
                printf("Error: invalid option '%s'.\n", *argv);
                ok = FALSE;
            }
        }
    }

    if ((!ok) || (flags != 0x07)) {
        printf("Usage: icptest.out <parameters>\n");
        printf("Parameters:\n");
	printf("  -d <hexadecimal> PCI Video address (eg. -d 0xFE000000)\n");
	printf("  -s <integer>     Video stride (eg. -s 2400 (for 800x600x3 display)\n");
        printf("  -b <integer>     Bytes per pixel (eg. -b 3)\n");
        printf("  -not555a         When RGB 16 mode is required. RGB 15 + alpha is default\n");
        exit(1);
    }
    return;

}


/*
 * Function             : get_input
 *                        Displays the initial message and gets 
 *                        the requested choice.
 * Parameters           : Int *filterType; Choice of ICP filter to be used
 *                        Float *scale; Scale factor associated with 
 *                        the filter, if any.
 * Function Result      : Return zero on success
 * Postcondition        : Values are returned in filterType and scale.
 *
 */

Int 
get_input(Int * filterType)
{

    Char        ins[80];
    Int         i;

    printf("            ICP Demo Program                \n");
    printf("            ----------------                \n");
    printf("\n");
    printf(" 1. Image scaling                               \n");
    printf(" 2. RGB 15+alpha overlay and alpha blending     \n");
    printf(" 3. Bit mask                                    \n");
    printf(" 4. Chroma keying                               \n");
    printf(" 5. Exit                                        \n");
    printf("\n");
    printf("Enter your choice:    ");
    gets(ins);
    i = sscanf(ins, "%x", filterType);

    printf("\n");

    if (i != 1) {
        printf("Invalid input ... \n");
        printf("\n");
        return (-1);
    }

    return (0);

}

int 
CreateBlackImage(
         int hsize,
         int vsize,
         UInt8 ** bufY,
         UInt8 ** bufU,
         UInt8 ** bufV
)
{
    int         i;
    int         j;
    int         ySize;
    int         uvSize;
    UInt8      *temp;

    /* get memory */
    ySize = hsize * vsize + 1024;
    /* get memory */
    ySize = hsize * vsize;
    uvSize = ySize >> 2;
    *bufY = (UInt8 *) _cache_malloc(ySize, -1);
    if (!bufY)
        return (1);
    *bufU = (UInt8 *) _cache_malloc(uvSize, -1);
    if (!bufU)
        return (2);
    *bufV = (UInt8 *) _cache_malloc(uvSize, -1);
    if (!bufV)
        return (3);

    temp = *bufY;
    for (i = 0; i < vsize; i++)
        for (j = 0; j < hsize; j++) {
            *temp = 0x00;
            temp++;
        }

    temp = *bufU;
    for (i = 0; i < vsize; i++)
        for (j = 0; j < hsize / 4; j++) {
            *temp = 0x80;
            temp++;
        }

    temp = *bufV;
    for (i = 0; i < vsize; i++)
        for (j = 0; j < hsize / 4; j++) {
            *temp = 0x80;
            temp++;
        }

    _cache_copyback(*bufY, ySize);
    _cache_copyback(*bufU, uvSize);
    _cache_copyback(*bufV, uvSize);

    return (0);
}



/*
 * Function             : CreateImage
 *                        Creates a bit mask image for making only a certain
 *                        part of the image visible.
 * Parameters           : UInt8 *bitmask; PoInter to where the 
 *                                        bitmask must be created
 *                        Int srcHeight;  Source image height
 *                        Int srcWidth;   Source image width
 *                        Int visible;    Number of image lines to be  visible.
 * Function Result      : Return zero on success
 * Precondition         : Memory for the bit mask image has been allocated.
 *
 */

Int 
CreateImage(UInt8 * bitMask, Int srcHeight, Int srcWidth, Int visible)
{
    UInt8      *image;
    Int         i, j, k;

    image = bitMask;
    for (k = 0; k < 2; k += 1) {
        for (i = 0; i < srcWidth / 8; i++) {
            for (j = (k * srcHeight / 2); j < (k * srcHeight / 2) + visible; j++) {

                *image = 0xff;
                image++;

            }
        }
        for (i = 0; i < srcWidth / 8; i++) {
            for (j = (k * srcHeight / 2) + visible; j < ((k + 1) * srcHeight / 2); j++) {

                *image = 0x00;
                image++;

            }
        }
    }

    /*
     * Copyback input image to memory
     */
    _cache_copyback(bitMask, srcHeight * srcWidth);

    return 0;
}

/*
 * Function             : icpGetChromaKey
 *                        Creates a chroma key image which is 
 *                        elliptical in shape. Shape of the ellipse 
 *                        is controlled by two parameters a and b.
 * Parameters           : UInt8 *destImage; PoInter to where the 
 *                                          image must be created
 *                        Int ostride;      Stride of the overlay image, 
 *                                          chroma works only
 *                                          for overlays.
 *                        Int owidth;       Image width
 *                        Int oheight;      Image height
 *                        Float a;
 *                        Float b;          Radii of elliptical object.
 * Function Result      : Return zero on success
 * Precondition         : Memory for the destination image has been allocated.
 *
 */

int 
icpGetChromaKey(UInt8 * destImage, Int ostride, Int owidth, Int oheight,
        Float a, Float b)
{

    Int         i, j;
    Int         rad = (owidth > oheight) ? oheight / 2 : owidth / 2;
    Int         centerx = owidth / 2;
    Int         centery = oheight / 2;
    UInt8      *image;

    image = destImage;
    _cache_invalidate(image, 2 * ostride * oheight);

    image = destImage;
    for (i = 0; i < oheight; i++) {
        for (j = 0; j < ostride; j++) {

            if (((Float) ((i - centery) * (i - centery)) / ((Float) (rad * rad) * a * a))
                + ((Float) ((j - centerx) * (j - centerx)) / ((Float) (rad * rad) * b * b))
                > 1) {
                *image = 0x00;
                *(image + 1) = 0x00;
                image += 2;
            }
            else {

#ifdef __LITTLE_ENDIAN__
                *(image + 1) |= 0x80;
#else
#if defined(__BIG_ENDIAN__) && defined(__TCS_WinNT__)
                *(image + 1) |= 0x80;
#else
                *(image) |= 0x80;
#endif
#endif

                image += 2;
            }
        }
    }

    _cache_copyback(destImage, 2 * ostride * oheight);

    return 0;

}                /* end icpGetChromaKey */

⌨️ 快捷键说明

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