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

📄 pnghand.cpp

📁 Wxpython Implemented on Windows CE, Source code
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/////////////////////////////////////////////////////////////////////////////
// Name:        pnghand.cpp
// Purpose:     Implements a PNG reader class + handler
// Author:      Julian Smart
// Modified by: 
// Created:     04/01/98
// RCS-ID:      $Id: pnghand.cpp,v 1.4 2005/09/23 12:54:24 MR Exp $
// Copyright:   (c) Julian Smart
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#  pragma hdrstop
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#if wxUSE_IOSTREAMH
#   include <fstream.h>
#else
#   include <fstream>
#endif

#ifndef __DARWIN__
#  include <windows.h>
#endif
#include "wx/msgdlg.h"
#include "wx/palette.h"
#include "wx/bitmap.h"
#include "wx/mac/pnghand.h"
#include "wx/mac/pngread.h"
#include "wx/mac/private.h"

extern "C" {
#include "png.h"
}

extern "C" void png_read_init PNGARG((png_structp png_ptr));
extern "C" void png_write_init PNGARG((png_structp png_ptr));

extern CTabHandle wxMacCreateColorTable( int numColors ) ;
extern void wxMacDestroyColorTable( CTabHandle colors )  ;
extern void wxMacSetColorTableEntry( CTabHandle newColors , int index , int red , int green ,  int blue ) ;
extern GWorldPtr wxMacCreateGWorld( int width , int height , int depth ) ;
extern void wxMacDestroyGWorld( GWorldPtr gw ) ;

void
ima_png_error(png_struct *png_ptr, char *message)
{
    wxMessageBox(wxString::FromAscii(message), wxT("PNG error"));
    longjmp(png_ptr->jmpbuf, 1);
}


// static wxGifReaderIter* iter;
wxPalette *wxCopyPalette(const wxPalette *cmap);

wxPNGReader::wxPNGReader(void)
{
    filetype = 0;
    RawImage = NULL;      //  Image data
    
    Width = 0; Height = 0;       //  Dimensions
    Depth = 0;           // (bits x pixel)
    ColorType = 0;        // Bit 1 = Palette used
    // Bit 2 = Color used
    // Bit 3 = Alpha used
    
    EfeWidth = 0;         // Efective Width
    
    lpbi = NULL;
    bgindex = -1;
    m_palette = 0;
    imageOK = FALSE;
}

wxPNGReader::wxPNGReader ( char* ImageFileName )
{
    imageOK = FALSE;
    filetype = 0;
    RawImage = NULL;      //  Image data
    
    Width = 0; Height = 0;       //  Dimensions
    Depth = 0;           // (bits x pixel)
    ColorType = 0;        // Bit 1 = m_palette used
    // Bit 2 = Color used
    // Bit 3 = Alpha used
    
    EfeWidth = 0;         // Efective Width
    
    lpbi = NULL;
    bgindex = -1;
    m_palette = 0;
    
    imageOK = ReadFile (ImageFileName);
}

void
wxPNGReader::Create(int width, int height, int depth, int colortype)
{
    Width = width; Height = height; Depth = depth;
    ColorType = (colortype>=0) ? colortype: ((Depth>8) ? COLORTYPE_COLOR: 0);
    delete m_palette;
    m_palette = NULL;
    delete[] RawImage;
    RawImage = NULL;
    
    if (lpbi) {
        wxMacDestroyGWorld( (GWorldPtr) lpbi ) ;
    }
    lpbi = wxMacCreateGWorld( Width , Height , Depth);
    if (lpbi)
    {
        EfeWidth = (long)(((long)Width*Depth + 31) / 32) * 4;
        int bitwidth = width ;
        if ( EfeWidth > bitwidth )
            bitwidth = EfeWidth ;
        
        RawImage = (byte*) new char[ ( bitwidth * Height * ((Depth+7)>>3) ) ];
        imageOK = TRUE;
    }
}

wxPNGReader::~wxPNGReader ( )
{
    if (RawImage != NULL) {
        delete[] RawImage ;
        RawImage = NULL;
    }
    if (lpbi)  {
        wxMacDestroyGWorld( (GWorldPtr) lpbi ) ;
        lpbi = NULL;
    }
    if (m_palette != NULL) {
        delete m_palette;
        m_palette = NULL;
    }
}


int wxPNGReader::GetIndex(int x, int y)
{
    if (!Inside(x, y) || (Depth>8)) return -1;
    
    ImagePointerType ImagePointer = RawImage + EfeWidth*y + (x*Depth >> 3);
    int index = (int)(*ImagePointer);
    return index;
}

bool wxPNGReader::GetRGB(int x, int y, byte* r, byte* g, byte* b)
{
    if (!Inside(x, y)) return FALSE;
    
    if (m_palette) {
        return m_palette->GetRGB(GetIndex(x, y), r, g, b);
        /*   PALETTEENTRY entry;
        ::GetPaletteEntries((HPALETTE) m_palette->GetHPALETTE(), GetIndex(x, y), 1, &entry);
        *r = entry.peRed;
        *g = entry.peGreen;
        *b = entry.peBlue;  */
    } else {
        ImagePointerType ImagePointer = RawImage + EfeWidth*y + (x*Depth >> 3);
        *b = ImagePointer[0];
        *g = ImagePointer[1];
        *r = ImagePointer[2];
    }
    return TRUE;
}


bool wxPNGReader::SetIndex(int x, int y, int index)
{
    if (!Inside(x, y) || (Depth>8)) return FALSE;
    
    ImagePointerType ImagePointer = RawImage + EfeWidth*y + (x*Depth >> 3);
    *ImagePointer = index;
    
    return TRUE;
}

bool wxPNGReader::SetRGB(int x, int y, byte r, byte g, byte b)
{
    if (!Inside(x, y)) return FALSE;
    
    if (ColorType & COLORTYPE_PALETTE)
    {
        if (!m_palette) return FALSE;
        SetIndex(x, y, m_palette->GetPixel(r, g, b));
        
    } else {
        ImagePointerType ImagePointer = RawImage + EfeWidth*y + (x*Depth >> 3);
        ImagePointer[0] = b;
        ImagePointer[1] = g;
        ImagePointer[2] = r;
    }
    
    return TRUE;
}

bool wxPNGReader::SetPalette(wxPalette* colourmap)
{
    delete m_palette ;
    if (!colourmap)
        return FALSE;
    ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR);
    m_palette = new wxPalette( *colourmap );
    return true ;
    //  return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
}

bool
wxPNGReader::SetPalette(int n, byte *r, byte *g, byte *b)
{
    delete m_palette ;
    m_palette = new wxPalette();
    if (!m_palette)
        return FALSE;
    
    if (!g) g = r;
    if (!b) b = g;
    m_palette->Create(n, r, g, b);
    ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR);
    return true ;
    //    return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
}

bool
wxPNGReader::SetPalette(int n, rgb_color_struct *rgb_struct)
{
    delete m_palette ;
    m_palette = new wxPalette();
    if (!m_palette)
        return FALSE;
    
    byte r[256], g[256], b[256];
    
    for(int i=0; i<n; i++)
    {
        r[i] = rgb_struct[i].red;
        g[i] = rgb_struct[i].green;
        b[i] = rgb_struct[i].blue;
    }
    // Added by JACS copying from Andrew Davison's additions
    // to GIF-reading code
    // Make transparency colour black...
    if (bgindex != -1)
        r[bgindex] = g[bgindex] = b[bgindex] = 0;
    
    m_palette->Create(n, r, g, b);
    ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR);
    return true ;
    //    return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
}

void wxPNGReader::NullData()
{
    if (lpbi)  {
        wxMacDestroyGWorld( (GWorldPtr) lpbi ) ;
        lpbi = NULL;
    }
    if (m_palette != NULL) {
        delete m_palette;
        m_palette = NULL;
    }
}

wxBitmap* wxPNGReader::GetBitmap(void)
{
    wxBitmap *bitmap = new wxBitmap;
    if ( InstantiateBitmap(bitmap) )
        return bitmap;
    else
    {
        delete bitmap;
        return NULL;
    }
}

bool wxPNGReader::InstantiateBitmap(wxBitmap *bitmap)
{
    if ( lpbi )
    {
        bitmap->SetHBITMAP((WXHBITMAP) lpbi);
        bitmap->SetWidth(GetWidth());
        bitmap->SetHeight(GetHeight());
        bitmap->SetDepth(GetDepth());
        if ( GetDepth() > 1 && m_palette )
            bitmap->SetPalette(*m_palette);
        bitmap->SetOk(TRUE);
        
        
        // Make a mask if appropriate
        /*
        if ( bgindex > -1 )
        {
        wxMask *mask = CreateMask();
        bitmap->SetMask(mask);
        }
        */
        lpbi = NULL ; // bitmap has taken over ownership
        return TRUE;
    }
    else
    {
        return FALSE;
    }
    /*
    HDC dc = ::CreateCompatibleDC(NULL);
    
      if (dc)
      {
      // tmpBitmap is a dummy, to satisfy ::CreateCompatibleDC (it
      // is a memory dc that must have a bitmap selected into it)
      HDC dc2 = GetDC(NULL);
      HBITMAP tmpBitmap = ::CreateCompatibleBitmap(dc2, GetWidth(), GetHeight());
      ReleaseDC(NULL, dc2);
      HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, tmpBitmap);
      
        if ( m_palette )
        {
        HPALETTE oldPal = ::SelectPalette(dc, (HPALETTE) m_palette->GetHPALETTE(), FALSE);
        ::RealizePalette(dc);
        }
        
          HBITMAP hBitmap = ::CreateDIBitmap(dc, lpbi,
          CBM_INIT, RawImage, (LPBITMAPINFO) lpbi, DIB_PAL_COLORS);
          
            ::SelectPalette(dc, NULL, TRUE);
            ::SelectObject(dc, oldBitmap);
            ::DeleteObject(tmpBitmap);
            ::DeleteDC(dc);
            
              if ( hBitmap )
              {
              bitmap->SetHBITMAP((WXHBITMAP) hBitmap);
              bitmap->SetWidth(GetWidth());
              bitmap->SetHeight(GetHeight());
              bitmap->SetDepth(GetDepth());
              if ( GetDepth() > 1 && m_palette )
              bitmap->SetPalette(*m_palette);
              bitmap->SetOk(TRUE);
              
                
                  // Make a mask if appropriate
                  if ( bgindex > -1 )
                  {
                  wxMask *mask = CreateMask();
                  bitmap->SetMask(mask);
                  }
                  return TRUE;
                  }
                  else
                  {
                  return FALSE;
                  }
                  }
                  else
                  {
                  return FALSE;
                  }
    */
    return false ;
}

wxPalette *wxCopyPalette(const wxPalette *cmap)
{
    wxPalette *newCmap = new wxPalette( *cmap ) ;
    return newCmap;
}

wxMask *wxPNGReader::CreateMask(void)
{
/*
HBITMAP hBitmap = ::CreateBitmap(GetWidth(), GetHeight(), 1, 1, NULL);

  HDC dc = ::CreateCompatibleDC(NULL);
  HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, hBitmap);
  
    int bgIndex = GetBGIndex();
    
      int x,y;
      
        for (x=0; x<GetWidth(); x++)
        {
        for (y=0; y<GetHeight(); y++)
        {
        int index = GetIndex(x, y);
        if ( index == bgIndex )
        ::SetPixel(dc, x, GetHeight() - y - 1, RGB(0, 0, 0));
        else
        ::SetPixel(dc, x, GetHeight() - y - 1, RGB(255, 255, 255));
        
          }
          }
          ::SelectObject(dc, oldBitmap);
          wxMask *mask = new wxMask;
          mask->SetMaskBitmap((WXHBITMAP) hBitmap);
          return mask;
    */
    return NULL ;
}

bool wxPNGReader::ReadFile(char * ImageFileName)
{
    int number_passes;
    
    if (ImageFileName)
        strcpy(filename, ImageFileName);
    
    FILE *fp;
    png_struct *png_ptr;
    png_info *info_ptr;
    wxPNGReaderIter iter(this);
    
    /* open the file */
    fp = fopen( ImageFileName , "rb" );
    
    if (!fp)
        return FALSE;
    
    /* allocate the necessary structures */
    png_ptr = new (png_struct);
    if (!png_ptr)
    {
        fclose(fp);
        return FALSE;
    }
    
    info_ptr = new (png_info);
    if (!info_ptr)
    {
        fclose(fp);
        delete png_ptr;
        return FALSE;
    }
    /* set error handling */
    if (setjmp(png_ptr->jmpbuf))
    {
        png_read_destroy(png_ptr, info_ptr, (png_info *)0);
        fclose(fp);
        delete png_ptr;
        delete info_ptr;
        
        /* If we get here, we had a problem reading the file */
        return FALSE;

⌨️ 快捷键说明

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