clq

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

FreeImage 画到句柄上, gcc 的 windows 版本是可以直接使用 freeimage 的 vc 格式 lib 的.[也许仅仅是针对 vc6 的 dll 版本 lib ? 有待确定]

在 cb 的 link 中加上 FreeImage 就行了,注意不要 FreeImage.lib 这样含后缀名的.

/***************************************************************
 * Name:      free_imgMain.cpp
 * Purpose:   Code for Application Frame
 * Author:     ()
 * Created:   2012-02-03
 * Copyright:  ()
 * License:
 **************************************************************/

#include "free_imgMain.h"
#include <wx/msgdlg.h>
#include "windows.h"
#include "FreeImage.h"

//(*InternalHeaders(free_imgDialog)
#include <wx/intl.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(free_imgDialog)
const long free_imgDialog::ID_BUTTON1 = wxNewId();
const long free_imgDialog::ID_BUTTON2 = wxNewId();
//*)

BEGIN_EVENT_TABLE(free_imgDialog,wxDialog)
    //(*EventTable(free_imgDialog)
    //*)
END_EVENT_TABLE()

free_imgDialog::free_imgDialog(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(free_imgDialog)
    Create(parent, id, _("wxWidgets app"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER|wxMAXIMIZE_BOX|wxMINIMIZE_BOX, _T("id"));
    Button1 = new wxButton(this, ID_BUTTON1, _("Label"), wxPoint(64,40), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    Button2 = new wxButton(this, ID_BUTTON2, _("Label"), wxPoint(88,152), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
    Center();

    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&free_imgDialog::OnButton1Click);
    //*)
}

free_imgDialog::~free_imgDialog()
{
    //(*Destroy(free_imgDialog)
    //*)
}

void free_imgDialog::OnQuit(wxCommandEvent& event)
{
    Close();
}

void free_imgDialog::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}
// ----------------------------------------------------------

/**
    FreeImage error handler
    @param fif Format / Plugin responsible for the error
    @param message Error message
*/
void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) {
    printf("\n*** ");
    if(fif != FIF_UNKNOWN) {
        printf("%s Format\n", FreeImage_GetFormatFromFIF(fif));
    }
    printf(message);
    printf(" ***\n");
}
// ----------------------------------------------------------

unsigned DLL_CALLCONV
myReadProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
    return fread(buffer, size, count, (FILE *)handle);
}

unsigned DLL_CALLCONV
myWriteProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
    return fwrite(buffer, size, count, (FILE *)handle);
}

int DLL_CALLCONV
mySeekProc(fi_handle handle, long offset, int origin) {
    return fseek((FILE *)handle, offset, origin);
}

long DLL_CALLCONV
myTellProc(fi_handle handle) {
    return ftell((FILE *)handle);
}


// ----------------------------------------------------------
//来自 freeimage 的例子
int main2(int argc, char *argv[]) {

    // call this ONLY when linking with FreeImage as a static library
#ifdef FREEIMAGE_LIB
    FreeImage_Initialise();
#endif // FREEIMAGE_LIB

    // initialize your own FreeImage error handler

    FreeImage_SetOutputMessage(FreeImageErrorHandler);

    // print version & copyright infos

    printf(FreeImage_GetVersion());
    printf("\n");
    printf(FreeImage_GetCopyrightMessage());
    printf("\n");


    if(argc != 2) {
        printf("Usage : LoadFromHandle <input file name>\n");
        return 0;
    }

    // initialize your own IO functions

    FreeImageIO io;

    io.read_proc  = myReadProc;
    io.write_proc = myWriteProc;
    io.seek_proc  = mySeekProc;
    io.tell_proc  = myTellProc;

    FILE *file = fopen(argv[1], "rb");

    if (file != NULL) {
        // find the buffer format
        FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromHandle(&io, (fi_handle)file, 0);

        if(fif != FIF_UNKNOWN) {
            // load from the file handle
            FIBITMAP *dib = FreeImage_LoadFromHandle(fif, &io, (fi_handle)file, 0);

            // save the bitmap as a PNG ...
            const char *output_filename = "test.png";

            // first, check the output format from the file name or file extension
            FREE_IMAGE_FORMAT out_fif = FreeImage_GetFIFFromFilename(output_filename);

            if(out_fif != FIF_UNKNOWN) {
                // then save the file
                FreeImage_Save(out_fif, dib, output_filename, 0);
            }

            // free the loaded FIBITMAP
            FreeImage_Unload(dib);
        }
        fclose(file);
    }

    // call this ONLY when linking with FreeImage as a static library
#ifdef FREEIMAGE_LIB
    FreeImage_DeInitialise();
#endif // FREEIMAGE_LIB

    return 0;
}
//--------------------------------------------------

void Draw2(HDC hdc, FIBITMAP *pFI)
{
    //FIBITMAP *pFI;// = ;//pDoc->m_pFI;
    if(pFI){
        BYTE* pBits = FreeImage_GetBits(pFI);
        BITMAPINFO* pIf=FreeImage_GetInfo(pFI);
        int W = FreeImage_GetWidth(pFI);
        int H = FreeImage_GetHeight(pFI);
        //CRect rectClient;GetClientRect(rectClient);
        //int dstW = rectClient.Width();
        //int dstH = rectClient.Height();
        ::SetStretchBltMode(hdc,COLORONCOLOR);
        ::SetDIBitsToDevice(hdc,0,0, W, H,0,0,0, H,pBits,pIf,DIB_RGB_COLORS);
    }
}//

// ----------------------------------------------------------

int draw(HDC hdc, char * fn) {

    // call this ONLY when linking with FreeImage as a static library
#ifdef FREEIMAGE_LIB
    FreeImage_Initialise();
#endif // FREEIMAGE_LIB

    // initialize your own FreeImage error handler

    FreeImage_SetOutputMessage(FreeImageErrorHandler);

    // print version & copyright infos

    printf(FreeImage_GetVersion());
    printf("\n");
    printf(FreeImage_GetCopyrightMessage());
    printf("\n");


    // initialize your own IO functions

    FreeImageIO io;

    io.read_proc  = myReadProc;
    io.write_proc = myWriteProc;
    io.seek_proc  = mySeekProc;
    io.tell_proc  = myTellProc;

    FILE *file = fopen(fn, "rb");

    if (file != NULL) {
        // find the buffer format
        FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromHandle(&io, (fi_handle)file, 0);

        if(fif != FIF_UNKNOWN) {
            // load from the file handle
            FIBITMAP *dib = FreeImage_LoadFromHandle(fif, &io, (fi_handle)file, 0);

            Draw2(hdc, dib);

            // save the bitmap as a PNG ...
            const char *output_filename = "test.png";

            // first, check the output format from the file name or file extension
            FREE_IMAGE_FORMAT out_fif = FreeImage_GetFIFFromFilename(output_filename);

            if(out_fif != FIF_UNKNOWN) {
                // then save the file
                FreeImage_Save(out_fif, dib, output_filename, 0);
            }

            // free the loaded FIBITMAP
            FreeImage_Unload(dib);
        }
        fclose(file);
    }

    // call this ONLY when linking with FreeImage as a static library
#ifdef FREEIMAGE_LIB
    FreeImage_DeInitialise();
#endif // FREEIMAGE_LIB

    return 0;
}

void free_imgDialog::OnButton1Click(wxCommandEvent& event)
{
    char *argv[] = {"", "c://2.jpg"};
    main2(2, argv);
    //FreeImage_

    HDC hdc = ::GetDC((HWND)this->GetHWND());

    draw(hdc, "c://1.txt");
}

--------------------------------------------------

几个问题:

1.其实有 FreeImage_Load() 函数直接加载,不用写那么多回调.

2.怎么画透明的 png 呢,也许要参考 cximage .

3.因为用了 libpng 等是不是比 cximage 快呢? cximage 的缩放是比较慢的,远慢于微软xp自带的那个图片浏览器.

 

posted on 2012-02-03 11:07  clq  阅读(775)  评论(2编辑  收藏  举报