SOL9 Sample: WICBitmapFileWriter

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// 2015/10/01

#define COMMONCONTROLS_V6

#include <sol/FileDialog.h>
#include <sol/COMInitializer.h>
#include <sol/PaintDC.h>
#include <sol/StringT.h>

#include <sol/wic/WICBitmapFileWriter.h>
#include <sol/directx/DirectXMainView.h>
#include <sol/directx/DirectXView.h>
#include <sol/directx/Direct2D1BitmapRenderTarget.h>
#include <sol/directx/Direct2D1EllipseGeometry.h>

#include <sol/directx/Direct2D1RectangleGeometry.h>
#include <sol/directx/Direct2D1RoundedRectangleGeometry.h>
#include <sol/directx/Direct2D1SolidColorBrush.h>
#include <sol/directx/Direct2D1GradientStopCollection.h>
#include <sol/directx/Direct2D1RadialGradientBrush.h>

#include "resource.h"

namespace SOL {

class MainView :public DirectXMainView {

private:
  //Inner class starts.
  class SimpleView : public DirectXView {
  private:
    SmartPtr<Direct2D1HwndRenderTarget> hwndTarget;

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

   void draw(Direct2D1RenderTarget* target)
   {
     try {
       //Create three target-dependent SolidColorBrushes.
       Direct2D1SolidColorBrush blueBrush (*target, D2D1::ColorF(D2D1::ColorF::Blue));

       Direct2D1SolidColorBrush greenBrush(*target, D2D1::ColorF(D2D1::ColorF::Green));

       Direct2D1SolidColorBrush redBrush(*target,  D2D1::ColorF(D2D1::ColorF::Red));
          
       target -> beginDraw();

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

       auto size = target->getSize();
       auto rectangle = D2D1::RectF(0.0f, 0.0f, size.width, size.height);
       target -> setTransform(
            D2D1::Matrix3x2F::Translation(0, size.height - 210)
            );
    
       target ->drawRectangle(D2D1::RectF( 400.0f, 200.0f, 200.0f, 100.0f), blueBrush, 6.0f);
       target ->fillRectangle(D2D1::RectF( 100.0f, 80.0f, 50.0f, 10.0f), redBrush);

       target ->drawEllipse(D2D1::Ellipse( D2D1::Point2F(180.0f, 0.0f), 80.0f, 60.0f), greenBrush, 2.0f);
       target ->fillEllipse(D2D1::Ellipse( D2D1::Point2F(180.0f, 0.0f), 20.0f, 20.0f), blueBrush);

       target ->drawRoundedRectangle(D2D1::RoundedRect( D2D1::RectF(500.0f, 0.0f, 300.0f, 80.0f), 30.0f, 30.0f), redBrush, 4.0f);
       target ->fillRoundedRectangle(D2D1::RoundedRect( D2D1::RectF(100.0f, 60.0f, 200.0f, 100.0f), 10.0f, 10.0f), greenBrush);
              
       target -> endDraw();
        
      } catch (Exception& ex) {
        ex.display();
      }
    }
    
  public:
    SimpleView(MainView* parent, const TCHAR* name, Args& args)
    :DirectXView(parent, name, args)
    {
      try {
        Direct2D1Factory* d2d1Factory = parent -> getD2D1Factory();
        hwndTarget   = new Direct2D1HwndRenderTarget(*d2d1Factory, getWindow());

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

    void saveBitmap(const TCHAR* filename)
    {
        WICBitmap* wicBitmap = NULL; 

        Direct2D1RenderTarget* wicRenderTarget = NULL;
      WICBitmapFileWriter* writer = NULL;
      try {
        StringConverter converter;
        wchar_t* url = converter.toWideChar(filename);
        WICImagingFactory* imagingFactory = getImagingFactory();
        Direct2D1Factory*  d2d1factory    = get2D1Factory();

        RECT rect;
        getClientRect(&rect);
        UINT width  = rect.right - rect.left;
        UINT height = rect.bottom - rect.top;

        wicBitmap = new WICBitmap(*imagingFactory, width, height, 
          GUID_WICPixelFormat32bppBGR,
            WICBitmapCacheOnLoad);
    
        wicRenderTarget = new Direct2D1RenderTarget(*d2d1factory, *wicBitmap, D2D1::RenderTargetProperties());

        draw(wicRenderTarget);

        writer = new WICBitmapFileWriter(*imagingFactory, url);
        delete[] url;
        
        writer -> write(*wicBitmap);
      } catch (Exception& ex) {
        ex.display();
      }
        delete writer;
        delete wicBitmap; 
        delete wicRenderTarget;
    }

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

private:
  SmartPtr<SimpleView> view;
  FileDialog  fileDialog;
  StringT<TCHAR> savedFileName;
  
public:
  //  Constructor
  MainView(Application& applet, const TCHAR* name, Args& args)
  :DirectXMainView(applet, name,
                 args.set(XmNstyle, (ulong)WS_CLIPCHILDREN) )
  {
    try {
      Args ar;
      ar.set(XmNstyle, WS_BORDER|WS_CHILD|WS_VISIBLE);
      view = new SimpleView(this, _T(""), ar);

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

    } catch (Exception& ex) {
      ex.display();
    }
      savedFileName = _T("");
    Args ar;
    ar.set(XmNfilter, 
      _T("Image File(*.bmp,*.jpg,*.png,*.tif,*.ico,*.gif)\0*.bmp;*.jpg;*.png;*.tif;*.ico;*.gif\0JPEG File*.jpg)\0*.jpg\0PNG File(*.png)\0*.png\0Tiff File(*.tif)\0*.tif\0Icon File(*.ico)\0*.ico;\0GIF File(*.gif)\0*.gif;\0"));

    fileDialog.create(this, _T(""), ar);
    restorePlacement();
  }

public:
  ~MainView()
  {
  }

private:
  long size(Event& event)
  { 
    int w, h;
    event.getSize(w, h);
    if (view.notNull()) {
      view -> reshape(10, 10, w-20, h-20);
    }
    return 0;
  }

private:
  void save(Action& action)
  {
      if (savedFileName.getLength() ==0) {
          saveAs(action);
      }
  }

private:
  void saveAs(Action& action)
  {
    Args ar;   
    TCHAR dir[MAX_PATH];
    memset(dir, (TCHAR)0, CountOf(dir));
    //Call getFileFolder method in SOL::ApplicationView.
    //This gets a previously select folder from a registry(profile of this application) for fileDialog
    if (restoreFileFolder(dir, CountOf(dir))) {
      ar.set(XmNdirectory, dir);
      fileDialog.setValues(ar);
    }
  
    if(fileDialog.save()){
      TCHAR* filename = fileDialog.getFileName();
        savedFileName = filename;

      //TCHAR* ftitle = fileDialog.getFileTitle();
      saveFileFolder(filename);
      view -> saveBitmap(filename);
      TCHAR caption[MAX_PATH];
      _stprintf_s(caption, CountOf(caption), _T("%s - %s"), filename, getAppName());
      setText(caption);
    } 
  }

};
}


//////////////////////////////////////////////
//
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 imageViewer(applet, appClass, args);
    imageViewer.realize();

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


Last modified: 8 Dec 2016

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