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

📄 wreimage.c

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
*               DESCRIBE IT HERE!
*
****************************************************************************/


#include <windows.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include "wresall.h"
#include "wreglbl.h"
#include "wreresin.h"
#include "wregcres.h"
#include "wreftype.h"
#include "wremem.h"
#include "wrenew.h"
#include "wreimage.h"
#include "bitmap.h"
#include "wrdll.h"
#include "wrbitmap.h"
#include "wricon.h"

/****************************************************************************/
/* macro definitions                                                        */
/****************************************************************************/
#define CALC_PAD( size, bound ) (((bound) - ((size) % (bound))) % (bound))

/****************************************************************************/
/* external function prototypes                                             */
/****************************************************************************/

/****************************************************************************/
/* static function prototypes                                               */
/****************************************************************************/
static  Bool    WREFindImageId          ( WRECurrentResInfo *image,
                                          uint_16 type, uint_16 id,
                                          WResLangType *ltype );
static  Bool    WREAddCursorHotspot     ( BYTE **cursor, uint_32 *size,
                                          CURSORHOTSPOT *hs );
static  uint_16 WREFindUnusedImageId    ( WREResInfo *info, uint_16 start );
static  Bool    WREGetAndAddCursorImage ( BYTE *data, WResDir dir,
                                          CURSORDIRENTRY *cd, int ord );
static  Bool    WREGetAndAddIconImage   ( BYTE *data, WResDir dir,
                                          ICONDIRENTRY *id, int ord );
static  Bool    WRECreateCursorResHeader( RESCURSORHEADER **rch,
                                          uint_32 *rchsize, BYTE *data,
                                          uint_32 data_size );
static  Bool    WRECreateIconResHeader  ( RESICONHEADER **rih, uint_32 *rihsize,
                                          BYTE *data, uint_32 data_size );
//static        Bool    WREIsCorrectImageGroup  ( WRECurrentResInfo *group,
//                                        uint_16 type, uint_16 id, Bool );
//static        Bool    WREStripCursorHotspot   ( BYTE **cursor, uint_32 *size );
//static        Bool    WREStripCursorDirectory ( BYTE **cursor, uint_32 *size );

/****************************************************************************/
/* static variables                                                         */
/****************************************************************************/

uint_16 WREFindUnusedImageId( WREResInfo *info, uint_16 start )
{
    WRECurrentResInfo   image;
    Bool                found;
    Bool                rollover;

    found = FALSE;
    rollover = FALSE;
    image.info = info;
    if( start == 0 ) {
        start = 1;
    }
    if( start == 1 ) {
        rollover = TRUE;
    }

    while( TRUE ) {
        if( start > 0x7fff ) {
            if( !rollover ) {
                rollover = TRUE;
                start = 1;
            } else {
                break;
            }
        }
        if( !WREFindImageId( &image, (uint_16)RT_ICON, start, NULL ) ) {
            if( !WREFindImageId( &image, (uint_16)RT_CURSOR, start, NULL ) ) {
                found = TRUE;
                break;
            }
        }
        start++;
    }

    if( !found ) {
        start = 0;
    }

    return( start );
}

Bool WREIsCorrectImageGroup( WRECurrentResInfo *group, uint_16 type,
                             uint_16 id )
{
    RESICONHEADER       *ih;
    RESCURSORHEADER     *ch;
    int                 i;
    Bool                ok;

    ok = ( group && group->info && group->info->info && group->lang &&
           ( ( type == (uint_16)RT_GROUP_ICON ) ||
             ( type == (uint_16)RT_GROUP_CURSOR ) ) );


    if( ok ) {
        if( group->lang->data == NULL ) {
            group->lang->data = WREGetCurrentResData( group );
            ok = ( group->lang->data != NULL );
        }
    }

    if( ok ) {
        ok = FALSE;
        if( type == (uint_16)RT_GROUP_ICON ) {
            ih = (RESICONHEADER *)group->lang->data;
            for( i = 0; !ok && i < ih->cwCount; i++ ) {
                ok = ( id == (uint_16)ih->idEntries[i].wNameOrdinal );
            }
        } else {
            ch = (RESCURSORHEADER *)group->lang->data;
            for( i = 0; !ok && i < ch->cwCount; i++ ) {
                ok = ( id == (uint_16)ch->cdEntries[i].wNameOrdinal );
            }
        }
    }

    return( ok );
}

Bool WREFindImageId( WRECurrentResInfo *image, uint_16 type, uint_16 id,
                     WResLangType *ltype )
{
    Bool                ok;

    ok = ( image && image->info );

    if( ok ) {
        ok = WRFindImageId( image->info->info, &image->type, &image->res,
                            &image->lang, type, id, ltype );
    }

    if( !ok ) {
        image->type = NULL;
        image->lang = NULL;
        image->lang = NULL;
    }

    return( ok );
}

Bool WREDeleteGroupImages( WRECurrentResInfo *group, uint_16 type )
{
    Bool                ok;

    ok = ( group && group->info && group->lang );

    if( ok ) {
        ok = WRDeleteGroupImages( group->info->info, group->lang, type );
    }

    return( ok );
}

Bool WREAppendDataToData( BYTE **d1, uint_32 *d1size, BYTE *d2, uint_32 d2size )
{
    if( !d1 || !d1size || !d2 || !d2size ) {
        return( FALSE );
    }

    *d1 = WREMemRealloc( *d1, *d1size + d2size );
    if( !*d1 ) {
        return( FALSE );
    }

    memcpy( *d1 + *d1size, d2, d2size );
    *d1size += d2size;

    return( TRUE );
}

Bool WREAddCursorImageToData( WRECurrentResInfo *image, BYTE **data,
                              uint_32 *size, CURSORHOTSPOT *hotspot )
{
    int         hs_size; // size of hotspot info
    Bool        ok;

    ok = ( image && image->info && image->info->info && image->lang &&
           data && size && hotspot );

    if( ok ) {
        if( image->lang->data == NULL ) {
            image->lang->data = WREGetCurrentResData( image );
            ok = ( image->lang->data != NULL );
        }
    }

    if( ok ) {
        hs_size = sizeof(CURSORHOTSPOT);
        memcpy( hotspot, image->lang->data, hs_size );
        ok = WREAppendDataToData( data, size,
                                  (BYTE *)image->lang->data + hs_size,
                                  image->lang->Info.Length - hs_size );
    }

    return( ok );
}

Bool WREAddIconImageToData( WRECurrentResInfo *image,
                            BYTE **data, uint_32 *size )
{
    Bool        ok;

    ok = ( image && image->info && image->info->info && image->lang &&
           data && size );

    if( ok ) {
        if( image->lang->data == NULL ) {
            image->lang->data = WREGetCurrentResData( image );
            ok = ( image->lang->data != NULL );
        }
    }

    if( ok ) {
        ok = WREAppendDataToData( data, size, image->lang->data,
                                  image->lang->Info.Length );
    }

    return( ok );
}

Bool WRECreateCursorDataFromGroup( WRECurrentResInfo *group,
                                   BYTE **data, uint_32 *size )
{
    WRECurrentResInfo   image;
    WResLangType        lt;
    RESCURSORHEADER     *rch;
    CURSORHEADER        *ch;
    CURSORHOTSPOT       hotspot;
    uint_16             ord;
    uint_32             osize;
    int                 i;
    Bool                ok;

    ok = ( group && group->info && group->info->info && group->lang &&
           data && size );

    if( ok ) {
        if( group->lang->data == NULL ) {
            group->lang->data = WREGetCurrentResData( group );
            ok = ( group->lang->data != NULL );
        }
    }

    if( ok ) {
        image.info = group->info;
        rch = (RESCURSORHEADER *)group->lang->data;
        *size = sizeof( CURSORHEADER );
        *size += sizeof(CURSORDIRENTRY)*(rch->cwCount-1);
        *data = (BYTE *)WREMemAlloc( *size );
        ch = (CURSORHEADER *)*data;
        ok = ( *data != NULL );
    }

    if( ok ) {
        memcpy( ch, rch, sizeof(WORD)*3 );
    }

    if( ok ) {
        for( i = 0; ok && i < rch->cwCount; i++ ) {
            ord = (uint_16)rch->cdEntries[i].wNameOrdinal;
            lt = group->lang->Info.lang;
            ok = WREFindImageId( &image, (uint_16)RT_CURSOR, ord, &lt );
            if( ok ) {
                osize = *size;
                ok = WREAddCursorImageToData( &image, data, size, &hotspot );
                if( ok ) {
                    ch = (CURSORHEADER *)*data;
                    ch->cdEntries[i].bWidth = rch->cdEntries[i].bWidth;
                    ch->cdEntries[i].bHeight = rch->cdEntries[i].bHeight/2;
                    ch->cdEntries[i].bColorCount = 0;
                    ch->cdEntries[i].bReserved = 0;
                    ch->cdEntries[i].wXHotspot = hotspot.xHotspot;
                    ch->cdEntries[i].wYHotspot = hotspot.yHotspot;
                    ch->cdEntries[i].dwBytesInRes = *size - osize;
                    ch->cdEntries[i].dwImageOffset = osize;
                }
            }
        }
    }

    if( !ok ) {
        if( *data != NULL ) {
            WREMemFree( *data );
            *data = NULL;
        }
        *size = 0;
    }

    return( ok );
}

Bool WRECreateIconDataFromGroup( WRECurrentResInfo *group,
                                 BYTE **data, uint_32 *size )
{
    WResLangType        lt;
    WRECurrentResInfo   image;
    RESICONHEADER       *rih;
    ICONHEADER          *ih;
    uint_16             ord;
    uint_32             osize;
    int                 i;
    Bool                ok;

    ok = ( group && group->info && group->info->info && group->lang &&
           data && size );

    if( ok ) {
        if( group->lang->data == NULL ) {
            group->lang->data = WREGetCurrentResData( group );
            ok = ( group->lang->data != NULL );
        }
    }

    if( ok ) {
        image.info = group->info;
        rih = (RESICONHEADER *)group->lang->data;
        *size = sizeof( ICONHEADER );
        *size += sizeof(ICONDIRENTRY)*(rih->cwCount-1);
        *data = (BYTE *)WREMemAlloc( *size );
        ih = (ICONHEADER *)*data;
        ok = ( *data != NULL );
    }

    if( ok ) {
        memcpy( ih, rih, sizeof(WORD)*3 );
    }

    if( ok ) {
        for( i = 0; ok && i < rih->cwCount; i++ ) {
            ord = (uint_16) rih->idEntries[i].wNameOrdinal;
            lt = group->lang->Info.lang;
            ok = WREFindImageId( &image, (uint_16)RT_ICON, ord, &lt );
            if( ok ) {
                osize = *size;
                ok = WREAddIconImageToData( &image, data, size );
                if( ok ) {
                    ih = (ICONHEADER *)*data;
                    ih->idEntries[i].bWidth = rih->idEntries[i].bWidth;
                    ih->idEntries[i].bHeight = rih->idEntries[i].bHeight;
                    ih->idEntries[i].bColorCount = rih->idEntries[i].bColorCount;
                    //ih->idEntries[i].wPlanes = rih->idEntries[i].wPlanes;
                    //ih->idEntries[i].wBitCount = rih->idEntries[i].wBitCount;
                    ih->idEntries[i].wPlanes = 0;
                    ih->idEntries[i].wBitCount = 0;
                    ih->idEntries[i].bReserved = 0;
                    ih->idEntries[i].dwBytesInRes = *size - osize;
                    ih->idEntries[i].dwImageOffset = osize;
                }
            }
        }
    }

    if( !ok ) {
        if( *data != NULL ) {
            WREMemFree( *data );
            *data = NULL;
        }
        *size = 0;
    }

    return( ok );
}

Bool WREGetAndAddCursorImage( BYTE *data, WResDir dir,
                              CURSORDIRENTRY *cd, int ord )
{
    BYTE                *cursor;
    int                 dup;
    uint_32             size;
    WResID              *tname;
    WResID              *rname;
    WResLangType        lang;
    CURSORHOTSPOT       hotspot;
    Bool                ok;

⌨️ 快捷键说明

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