VIZ++ Class: OpenGLBitmap

 VIZ++ Class Library  VIZ++ Samples  VIZ++ ClassTree 

Source code

/*
 * OpenGLBitmap.h 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


#include <viz++/opengl/OpenGLObject.h>
#include <viz++/JPGFileWriter.h>
#include <viz++/PNGFileWriter.h>

namespace VIZ {

class OpenGLBitmap :public OpenGLObject {
private:
  GLint x;
  GLint y;
  GLsizei width;
  GLsizei height;
  GLenum  format;
  GLenum  type;
  GLvoid* pixels;
  int channels;
  int depth;
  
public:
  OpenGLBitmap(GLint x, GLint y, GLsizei width, GLsizei height, 
                    int depth, GLenum format=GL_RGB, GLenum type = GL_UNSIGNED_BYTE)
  :x(x),
   y(x),
   width(width),
   height(height),
   format(format),
   type(type),
   pixels(NULL),
   channels(0),
   depth(depth)
  {
    channels = 1;
    switch (format) {
    //case GL_BGR:
    case GL_RGB:
      channels = 3;
      break;
    case GL_RGBA:
      channels = 4;
      break;
    case GL_RED:
    case GL_GREEN:
    case GL_BLUE:
    case GL_ALPHA:
     channels = 1;
     break;
    }
    size_t size = width*height*channels;
    pixels = new GLubyte[size];
    memset(pixels, '\0', size);

  }

  ~OpenGLBitmap()
  {
    delete [] pixels;
    pixels = NULL;
  }  

  GLvoid* read(GLenum buffer=GL_BACK)
  {
    glPixelStorei(GL_PACK_ALIGNMENT, 1);
    glReadBuffer(buffer);
    glReadPixels(x, y, width, height, format, type, pixels);
    return pixels;
  }

  bool saveAsJPG(const char* filename, bool vertFlip=true)
  {
    bool rc = false;
    try {
      JPGFileWriter writer(filename);
      int quality   = 100;
      writer.write(width, height, depth, channels,(unsigned char*)pixels, quality, vertFlip);
      rc = true;
    } catch (Exception& ex) {
      ex.display();
    }
    return rc;   
  }

  bool saveAsPNG(const char* filename, bool vertFlip=true)
  {
    bool rc = false;
    try {
      PNGFileWriter writer(filename);
     
      writer.write(width, height, depth, channels, (unsigned char*)pixels, vertFlip);
      rc = true;
    } catch (Exception& ex) {
      ex.display();
    }
    return rc;   
  }

};

}

Last modified: 10 Feb 2017

Copyright (c) 2009-2017 Antillia.com ALL RIGHTS RESERVED.