SOL9 Sample: WICBitmapScaler

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * WICBitmapScaler.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// 2015/10/01 This is a simple ImageViewer sample program based on WIC and DirectX APIs. 
// 2015/10/10 Modified to use WICBitmapFileReader class.

#define COMMONCONTROLS_V6

#include <sol/COMInitializer.h>

#include <sol/StringConverter.h>

#include <sol/wic/WICImagingFactory.h>
#include <sol/wic/WICFormatConverter.h>
#include <sol/wic/WICBitmapDecoder.h>
#include <sol/wic/WICBitmapFrameDecode.h>
#include <sol/wic/WICBitmapFileReader.h>
#include <sol/wic/WICBitmapScaler.h>

#include <sol/directx/DirectXView.h>
#include <sol/directx/DirectXMainView.h>

#include <sol/directx/Direct2D1Factory.h>
#include <sol/directx/Direct2D1HwndRenderTarget.h>
#include <sol/directx/Direct2D1Bitmap.h>

#include "resource.h"

namespace SOL {

class MainView :public DirectXMainView {

private:
  //Inner class starts.
  class SimpleView : public DirectXView {
  private:
    SmartPtr<Direct2D1HwndRenderTarget>  renderTarget;
    SmartPtr<Direct2D1Bitmap>            bitmap;
    StringT<wchar_t>       url;

  private:
   void resize(int w, int h)
   {
     try {
       auto size = D2D1::SizeU(w, h);
       renderTarget ->resize(size);
     } catch (Exception& ex) {
       ex.display();
     }
   }
  
   void display()
   {
     if (bitmap == NULL) {
       return;
     }
    
     try {
        if (!(renderTarget -> checkWindowState() & D2D1_WINDOW_STATE_OCCLUDED)){
          renderTarget -> beginDraw();

          renderTarget -> setTransform(D2D1::Matrix3x2F::Identity());
          renderTarget -> clear(D2D1::ColorF(D2D1::ColorF::White));

          //Draw the bitmap with its size set by read method of WICBitmapFileReader
          D2D1_SIZE_F bsize = bitmap->getSize();
          auto rectangle = D2D1::RectF(0.0f, 0.0f, bsize.width, bsize.height);
   
          renderTarget -> drawBitmap(*bitmap, rectangle);
          renderTarget -> endDraw();
        }
      } catch (Exception& ex) {
        ex.display();
      }
    }
    
  public:
    SimpleView(MainView* parent, const TCHAR* name, Args& args)
    :DirectXView(parent, name, args)
    {
      url = (const wchar_t*)args.get(XmNurl);
      
      try {
        WICImagingFactory* imagingFactory = parent -> getImagingFactory();
        Direct2D1Factory* d2d1Factory = parent -> getD2D1Factory();
        renderTarget   = new Direct2D1HwndRenderTarget(*d2d1Factory, getWindow());

        //2015/10/10
        int width  = args.get(XmNxImageSize);
        int height = args.get(XmNyImageSize);

        WICBitmapFileReader reader(*imagingFactory, url);

        //Scaling an original bitmapImage to the specified width and height.
        bitmap = reader.read(*renderTarget, width, height); 
    
      } catch (Exception& ex) {
        ex.display();
      }
    }

    ~SimpleView()
    {
    }
  };
  // Inner class ends.

private:
  SmartPtr<SimpleView> view1;
  SmartPtr<SimpleView> view2;
    
public:
  //Constructor
  MainView(Application& applet, const TCHAR* name, Args& args)
  :DirectXMainView(applet, name,
                 args.set(XmNstyle, (ulong)WS_CLIPSIBLINGS|WS_CLIPCHILDREN) )
  {
    try {
      const TCHAR* directory = (const TCHAR*)args.get(XmNapplicationDirectory);
      StringConverter converter;
    
      wchar_t* dir = converter.toWideChar(directory);

      wchar_t url1[MAX_PATH];
      wchar_t url2[MAX_PATH];
      swprintf_s(url1, CountOf(url1), L"%s\\..\\images\\%s", dir, L"flower1.jpg");
      swprintf_s(url2, CountOf(url2), L"%s\\..\\images\\%s", dir, L"flower2.png");
      delete [] dir;

      Args ar;
      ar.set(XmNurl, url1);
        ar.set(XmNxImageSize, 240);
      view1 = new SimpleView(this, _T(""), ar);
      
      ar.reset();
      ar.set(XmNurl, url2);
      ar.set(XmNyImageSize, 200);
      view2 = new SimpleView(this, _T(""), ar);

      addEventHandler(WM_SIZE, this, 
        (Handler)&MainView::size, NULL);

    } catch (Exception& ex) {
      ex.display();
    }
    restorePlacement();
  }

public:
  ~MainView()
  {
  }

private:
  long size(Event& event)
  { 
    int w, h;
    event.getSize(w, h);
    if (view1.notNull() && view2.notNull()) {
      view1 -> reshape(0, 0, w/2-1, h);
      view2 -> reshape(w/2+1, 0, w/2-1, h);
    }
    return 0;
  }
};
}


//////////////////////////////////////////////
//
void  Main(int argc, TCHAR** argv)
{
  const TCHAR* appClass = appName(argv[0]);

  TCHAR directory[MAX_PATH];
  appDirectory(argv[0], directory, CountOf(directory));
  
  try {
    HeapSetInformation(nullptr, HeapEnableTerminationOnCorruption, nullptr, 0);

    COMInitializer initializer( COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    
    Application applet(appClass, argc, argv);

    Args args; 
    args.set(XmNapplicationDirectory, directory);
    MainView view(applet, appClass, args);
    view.realize();

    applet.run();
  } catch (Exception& ex) {
    ex.display();
  }
}


Last modified: 8 Dec 2016

Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.