Fixed memory overwrite bug. This fixes the png writer and led control.

This commit is contained in:
T. van der Zwan 2013-08-03 23:24:22 +02:00
parent 240218a6bd
commit cbbb1d740b
11 changed files with 167 additions and 166 deletions

View File

@ -23,7 +23,7 @@ include_directories(${CMAKE_SOURCE_DIR}/include)
# Prefer static linking over dynamic
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.so")
#set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_BUILD_TYPE "Release")
# enable C++11
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall")

View File

@ -25,6 +25,8 @@ public:
void operator() (const RgbImage& inputImage);
private:
void writeImage(const RgbImage& inputImage);
private:
RgbImage* mBuffer;

View File

@ -3,6 +3,7 @@
#include <cassert>
#include <cstring>
#include <vector>
// Local includes
#include "RgbColor.h"
@ -33,13 +34,17 @@ public:
inline void copy(const RgbImage& other)
{
std::cout << "This image size: [" << width() << "x" << height() << "]. Other image size: [" << other.width() << "x" << other.height() << "]" << std::endl;
assert(other.mWidth == mWidth);
assert(other.mHeight == mHeight);
memcpy(mColors, other.mColors, mWidth*mHeight*sizeof(RgbColor));
}
RgbColor* memptr()
{
return mColors;
}
private:
inline unsigned toIndex(const unsigned x, const unsigned y) const
@ -47,19 +52,9 @@ private:
return y*mWidth + x;
}
RgbImage(const RgbImage&)
{
// empty
}
RgbImage& operator=(const RgbImage& other)
{
return *this;
}
private:
unsigned mWidth;
unsigned mHeight;
const unsigned mWidth;
const unsigned mHeight;
/** The colors of the image */
RgbColor* mColors;

View File

@ -21,8 +21,8 @@ HyperionPng::~HyperionPng()
std::cout << "HyperionPng is being deleted" << std::endl;
delete mBuffer;
// mWriter->close();
// delete mWriter;
mWriter->close();
delete mWriter;
}
void HyperionPng::setInputSize(const unsigned width, const unsigned height)
@ -38,18 +38,24 @@ RgbImage& HyperionPng::image()
void HyperionPng::commit()
{
this->operator ()(*mBuffer);
writeImage(*mBuffer);
}
void HyperionPng::operator() (const RgbImage& inputImage)
{
writeImage(inputImage);
}
void HyperionPng::writeImage(const RgbImage& inputImage)
{
// Write only every n'th frame
if (mFrameCnt%mWriteFrequency == 0)
if (mFrameCnt%10 == 0)
{
// Set the filename for the PNG
char filename[64];
sprintf(filename, "/home/pi/RASPI_%04ld.png", mFileIndex);
sprintf(filename, "/home/pi/RASPI_%04lu.png", mFileIndex);
mWriter->pngwriter_rename(filename);
mWriter->resize(inputImage.width(), inputImage.height());
// Plot the pixels from the image to the PNG-Writer
for (unsigned y=0; y<inputImage.width(); ++y)
@ -61,10 +67,12 @@ void HyperionPng::operator() (const RgbImage& inputImage)
}
}
std::cout << "Writing the PNG" << std::endl;
// Write-out the current frame and prepare for the next
mWriter->write_png();
++mFileIndex;
std::cout << "PNGWRITER FINISHED" << std::endl;
}
++mFrameCnt;
}

View File

@ -10,19 +10,16 @@
RgbImage::RgbImage(const unsigned width, const unsigned height, const RgbColor background) :
mWidth(width),
mHeight(height),
mColors(nullptr)
mColors(new RgbColor[width*height])
{
mColors = new RgbColor[width*height];
for (RgbColor* color = mColors; color <= mColors+(mWidth*mHeight); ++color)
for (unsigned i=0; i<width*height; ++i)
{
*color = background;
mColors[i] = background;
}
}
RgbImage::~RgbImage()
{
std::cout << "RgbImage(" << this << ") is being deleted" << std::endl;
delete[] mColors;
}

View File

@ -8,8 +8,23 @@
#include <json/json.h>
#include <utils/jsonschema/JsonFactory.h>
#include "dispmanx-helper.h"
static volatile bool sRunning = true;
void signal_handler(int signum)
{
std::cout << "RECEIVED SIGNAL: " << signum << std::endl;
sRunning = false;
}
int main(int /*argc*/, char** /*argv*/)
{
// Install signal-handlers to exit the processing loop
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
const char* homeDir = getenv("RASPILIGHT_HOME");
if (!homeDir)
{
@ -28,47 +43,7 @@ int main(int /*argc*/, char** /*argv*/)
}
Hyperion hyperion(raspiConfig);
const unsigned width = 64;
const unsigned height = 64;
hyperion.setInputSize(width, height);
volatile bool running = true;
// Open the connection to the displaydisplay
DISPMANX_DISPLAY_HANDLE_T display = vc_dispmanx_display_open(0);
DISPMANX_MODEINFO_T info;
int ret = vc_dispmanx_display_get_info(display, &info);
assert(ret == 0);
// Create the resources for capturing image
uint32_t vc_image_ptr;
DISPMANX_RESOURCE_HANDLE_T resource = vc_dispmanx_resource_create(
VC_IMAGE_RGB888,
width,
height,
&vc_image_ptr);
assert(resource);
VC_RECT_T rectangle;
vc_dispmanx_rect_set(&rectangle, 0, 0, width, height);
RgbImage* image_ptr = &(hyperion.image());
void* image_vp = reinterpret_cast<void*>(image_ptr);
const uint32_t pitch = width * 3;
timespec updateInterval;
updateInterval.tv_sec = 0;
updateInterval.tv_nsec = 100000000;
while(running)
{
vc_dispmanx_snapshot(display, resource, VC_IMAGE_ROT0);
vc_dispmanx_resource_read_data(resource, &rectangle, image_vp, pitch);
hyperion.commit();
nanosleep(&updateInterval, NULL);
}
dispmanx_process(hyperion, sRunning);
return 0;
}

59
src/dispmanx-helper.h Normal file
View File

@ -0,0 +1,59 @@
#pragma once
// VC includes
#include <bcm_host.h>
template <typename Hyperion_T>
int dispmanx_process(Hyperion_T& hyperion, volatile bool& running)
{
// Configure the used image size
const unsigned width = 64;
const unsigned height = 64;
hyperion.setInputSize(width, height);
// Initiase BCM
bcm_host_init();
// Open the connection to the displaydisplay
DISPMANX_DISPLAY_HANDLE_T display = vc_dispmanx_display_open(0);
DISPMANX_MODEINFO_T info;
int ret = vc_dispmanx_display_get_info(display, &info);
assert(ret == 0);
// Create the resources for capturing image
uint32_t vc_image_ptr;
DISPMANX_RESOURCE_HANDLE_T resource = vc_dispmanx_resource_create(
VC_IMAGE_RGB888,
width,
height,
&vc_image_ptr);
assert(resource);
VC_RECT_T rectangle;
vc_dispmanx_rect_set(&rectangle, 0, 0, width, height);
void* image_ptr = hyperion.image().memptr();
const uint32_t pitch = width * 3;
timespec updateInterval;
updateInterval.tv_sec = 0;
updateInterval.tv_nsec = 100000000;
while(running)
{
vc_dispmanx_snapshot(display, resource, VC_IMAGE_ROT0);
vc_dispmanx_resource_read_data(resource, &rectangle, image_ptr, pitch);
hyperion.commit();
nanosleep(&updateInterval, NULL);
}
// Clean up resources
vc_dispmanx_resource_delete(resource);
vc_dispmanx_display_close(display);
// De-init BCM
bcm_host_deinit();
return 0;
}

View File

@ -2,12 +2,11 @@
// STL includes
#include <csignal>
// VC includes
#include <bcm_host.h>
// Hyperion includes
#include <hyperionpng/HyperionPng.h>
#include "../dispmanx-helper.h"
static volatile bool sRunning = true;
void signal_handler(int signum)
@ -16,68 +15,6 @@ void signal_handler(int signum)
sRunning = false;
}
template <typename Hyperion_T>
int dispmanx_process(Hyperion_T& hyperion, volatile bool& running)
{
// Configure the used image size
const unsigned width = 64;
const unsigned height = 64;
hyperion.setInputSize(width, height);
// Initiase BCM
bcm_host_init();
// Open the connection to the displaydisplay
DISPMANX_DISPLAY_HANDLE_T display = vc_dispmanx_display_open(0);
DISPMANX_MODEINFO_T info;
int ret = vc_dispmanx_display_get_info(display, &info);
assert(ret == 0);
// Create the resources for capturing image
uint32_t vc_image_ptr;
DISPMANX_RESOURCE_HANDLE_T resource = vc_dispmanx_resource_create(
VC_IMAGE_RGB888,
width,
height,
&vc_image_ptr);
assert(resource);
VC_RECT_T rectangle;
vc_dispmanx_rect_set(&rectangle, 0, 0, width, height);
RgbImage* image_ptr = &(hyperion.image());
void* image_vp = reinterpret_cast<void*>(image_ptr);
const uint32_t pitch = width * 3;
timespec updateInterval;
updateInterval.tv_sec = 0;
updateInterval.tv_nsec = 100000000;
while(running)
{
std::cout << "Grabbing a frame from display" << std::endl;
vc_dispmanx_snapshot(display, resource, VC_IMAGE_ROT0);
vc_dispmanx_resource_read_data(resource, &rectangle, image_vp, pitch);
std::cout << "Commiting the frame to Hyperion" << std::endl;
// hyperion.commit();
std::cout << "Waiting for next grab" << std::endl;
nanosleep(&updateInterval, NULL);
}
std::cout << "Cleaning VC resources" << std::endl;
// Clean up resources
vc_dispmanx_resource_delete(resource);
vc_dispmanx_display_close(display);
std::cout << "Uninitialising BCM-Host" << std::endl;
// De-init BCM
bcm_host_deinit();
std::cout << "Exit success" << std::endl;
return 0;
}
int main(int /*argc*/, char** /*argv*/)
{

View File

@ -20,6 +20,10 @@ add_executable(Test2BobLight
target_link_libraries(Test2BobLight
bob2hyperion)
add_executable(TestRgbImage
TestRgbImage.cpp)
target_link_libraries(TestRgbImage
hyperion-utils)
# Find the libPNG
find_package(PNG REQUIRED QUIET)
@ -27,11 +31,11 @@ find_package(PNG REQUIRED QUIET)
# Add additional includes dirs
include_directories(${PNG_INCLUDE_DIR})
if(PNG_FOUND)
#if(PNG_FOUND)
add_executable(TestHyperionPng
TestHyperionPng.cpp)
target_link_libraries(TestHyperionPng
bob2hyperion-png)
endif(PNG_FOUND)
hyperion-png)
#endif(PNG_FOUND)

View File

@ -2,42 +2,44 @@
// STL includes
#include <iostream>
// Boblight includes
#include <boblight.h>
// HyperionPNG includes
#include <hyperionpng/HyperionPng.h>
template <typename Hyperion_T>
void process(Hyperion_T& hyperion)
{
hyperion.setInputSize(64, 64);
// Obtain reference to buffer
RgbImage& image = hyperion.image();
// Write some data to the image
std::cout << "Write data to buffer-image" << std::endl;
for (unsigned y=0; y<image.height(); ++y)
{
for (unsigned x=0; x<image.width(); ++x)
{
const RgbColor color = {255, 0, 0};
image(x,y) = color;
}
}
std::cout << "Commit image to write png" << std::endl;
for (unsigned i=0; i<40; ++i)
{
// Commit the image (writing first png)
hyperion.commit();
}
std::cout << "FINISHED" << std::endl;
}
int main()
{
std::cout << "Initialisaing Boblight" << std::endl;
void* blPng = boblight_init();
// Construct instance of Hyperion-PNG
std::cout << "Initialisaing Hyperion PNG" << std::endl;
HyperionPng hyperion;
int width = 112;
int height = 64;
std::cout << "Defining scan range (" << width << "x" << height << ")" << std::endl;
boblight_setscanrange(blPng, width, height);
int colorPtr[3];
colorPtr[0] = 255;
colorPtr[1] = 0;
colorPtr[2] = 0;
std::cout << "Using color [" << colorPtr[0] << "; " << colorPtr[1] << "; " << colorPtr[2] << "]" << std::endl;
int nrOfFrames = 150;
std::cout << "Generating " << nrOfFrames << " frames" << std::endl;
for (int iFrame=0; iFrame<nrOfFrames; ++iFrame)
{
for (int iWidth=0; iWidth<width; ++iWidth)
{
for (int iHeight=0; iHeight<height; ++iHeight)
{
boblight_addpixelxy(blPng, iWidth, iHeight, colorPtr);
}
}
boblight_sendrgb(blPng, 0, NULL);
}
std::cout << "Destroying Boblight" << std::endl;
boblight_destroy(blPng);
process(hyperion);
return 0;
}

22
test/TestRgbImage.cpp Normal file
View File

@ -0,0 +1,22 @@
// Utils includes
#include <utils/RgbImage.h>
int main()
{
std::cout << "Constructing image" << std::endl;
RgbImage image(64, 64, RgbColor::BLACK);
std::cout << "Writing image" << std::endl;
for (unsigned y=0; y<64; ++y)
{
for (unsigned x=0; x<64; ++x)
{
image(x,y) = RgbColor::RED;
}
}
std::cout << "Finished (destruction will be performed)" << std::endl;
return 0;
}