Skip to content

Instantly share code, notes, and snippets.

@veev
Created January 28, 2013 04:22
Show Gist options
  • Save veev/4653009 to your computer and use it in GitHub Desktop.
Save veev/4653009 to your computer and use it in GitHub Desktop.
Darkness Map histogram draw test. This shows how drawing to the texture is supposed to work with alpha transparency at first. This property is working on osx (as seen here) but not on iphone. why?
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
camWidth = 320;
camHeight = 240;
canvasWidth = 320;
canvasHeight = 50;
grabber.setVerbose(true);
grabber.initGrabber(320, 240);
int draw_position_x = 0;
canvas.allocate( canvasWidth, canvasHeight, GL_RGBA);
canvasPixels = new unsigned char [canvasWidth * canvasHeight * 4];
//set pixels to alpha initially
for(int i = 0; i < canvasWidth; i++) {
for(int j = 0; j < canvasHeight; j++) {
canvasPixels[( j*canvasWidth + i)*4 + 0] = 0;
canvasPixels[( j*canvasWidth + i)*4 + 1] = 0;
canvasPixels[( j*canvasWidth + i)*4 + 2] = 0;
canvasPixels[( j*canvasWidth + i)*4 + 3] = 0;
}
}
canvas.loadData(canvasPixels, canvasWidth, canvasHeight, GL_RGBA);
}
//--------------------------------------------------------------
void testApp::update(){
grabber.update();
if( grabber.isFrameNew()) {
int totalPixels = camWidth * camHeight * 3; //RGB so width x height x 3
unsigned char * src = grabber.getPixels();
long double redVals = 0.0;
long double greenVals = 0.0;
long double blueVals = 0.0;
for(int i = 0; i < totalPixels; i+=3) {
redVals += src[ i ];
greenVals += src[ i+1 ];
blueVals += src[ i+2 ];
}
double imageRedMean = 0, imageGreenMean = 0, imageBlueMean = 0;
int pixPerFrame = camWidth * camHeight;
imageRedMean = redVals/pixPerFrame;
imageGreenMean = greenVals/pixPerFrame;
imageBlueMean = blueVals/pixPerFrame;
averageBrightness = 0.0;
averageBrightness = ((imageRedMean*2)+imageBlueMean+(imageGreenMean*3))/6;
//draw average brightness into canvas
for(int i = 0; i < canvasHeight; i++) {
canvasPixels[( i*canvasWidth + draw_position_x)*4 + 0] = averageBrightness;
canvasPixels[( i*canvasWidth + draw_position_x)*4 + 1] = averageBrightness;
canvasPixels[( i*canvasWidth + draw_position_x)*4 + 2] = averageBrightness;
canvasPixels[( i*canvasWidth + draw_position_x)*4 + 3] = 255;
}
canvas.loadData(canvasPixels, canvasWidth, canvasHeight, GL_RGBA);
draw_position_x++;
if(draw_position_x > canvasWidth) {
draw_position_x = 0;
}
}
}
//--------------------------------------------------------------
void testApp::draw(){
grabber.draw(0, 0, camWidth, camHeight);
canvas.draw(0, camHeight - canvasHeight, canvasWidth, canvasHeight);
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}
#pragma once
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofVideoGrabber grabber;
int camWidth;
int camHeight;
ofTexture canvas;
int canvasWidth;
int canvasHeight;
unsigned char * canvasPixels;
double averageBrightness;
int draw_position_x;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment