restructured project structure: move projects into sub-folders, split dispmanx-grabber from hyperion

This commit is contained in:
johan
2013-08-17 16:12:42 +02:00
parent 16c260b3dc
commit 59e13a2b5f
18 changed files with 85 additions and 56 deletions

View File

@@ -0,0 +1,14 @@
# Find the libPNG
find_package(PNG QUIET)
if(PNG_FOUND)
# Add additional includes dirs
include_directories(${PNG_INCLUDE_DIR})
add_executable(viewpng
ViewPng.cpp)
target_link_libraries(viewpng
hyperion
${PNG_LIBRARIES})
endif(PNG_FOUND)

115
src/viewpng/FbWriter.h Normal file
View File

@@ -0,0 +1,115 @@
#pragma once
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <utils/RgbImage.h>
class FbWriter
{
public:
FbWriter()
{
initialise();
}
~FbWriter()
{
if (ioctl(fbfd, FBIOPUT_VSCREENINFO, &orig_vinfo))
{
printf("Error re-setting variable information.\n");
}
close(fbfd);
}
int initialise()
{
// Open the file for reading and writing
fbfd = open("/dev/fb0", O_RDWR);
if (!fbfd)
{
printf("Error: cannot open framebuffer device.\n");
return(-1);
}
printf("The framebuffer device was opened successfully.\n");
// Get fixed screen information
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo))
{
printf("Error reading fixed information.\n");
}
// Get variable screen information
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &orig_vinfo))
{
printf("Error reading variable information.\n");
}
printf("Original %dx%d, %dbpp\n", orig_vinfo.xres, orig_vinfo.yres, orig_vinfo.bits_per_pixel );
return 0;
}
void writeImage(const RgbImage& image)
{
std::cout << "Writing image [" << image.width() << "x" << image.height() << "]" << std::endl;
// Make a copy of the original screen-info
fb_var_screeninfo vinfo = orig_vinfo;
// memcpy(&vinfo, &orig_vinfo, sizeof(fb_var_screeninfo));
// Configure the frame-buffer for the new image
vinfo.xres = image.width();
vinfo.yres = image.height();
vinfo.bits_per_pixel = 24;
if (ioctl(fbfd, FBIOPUT_VSCREENINFO, &vinfo))
{
printf("Error configuring frame-buffer");
}
ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo);
std::cout << "New set resolution: " << vinfo.xres << "x" << vinfo.yres << std::endl;
// map fb to user mem
long screensize = vinfo.yres * finfo.line_length;//vinfo.yres * vinfo.bits_per_pixel / 8;
char* fbp = (char*)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
if (!fbp)
{
// Failed to create memory map
std::cout << "Failed to create the memory map" << std::endl;
return;
}
std::cout << "Screensize : " << screensize << std::endl;
std::cout << "Max fb-index: " << (image.width()-1)*3 + (image.height()-1)*finfo.line_length << std::endl;
std::cout << "[" << vinfo.xres << "x" << vinfo.yres << "] == [" << image.width() << "x" << image.height() << "]" << std::endl;
for (unsigned iY=0; iY<image.height(); ++iY)
{
memcpy(fbp + iY*finfo.line_length, &(image(0, iY)), image.width()*3);
// for (unsigned iX=0; iX<image.width(); ++iX)
// {
// const unsigned pixOffset = iX*3 + iY*finfo.line_length;
// fbp[pixOffset ] = image(iX, iY).red;
// fbp[pixOffset+1] = image(iX, iY).green;
// fbp[pixOffset+2] = image(iX, iY).blue;
// }
}
std::cout << "FINISHED COPYING IMAGE TO FRAMEBUFFER" << std::endl;
// cleanup
munmap(fbp, screensize);
}
// The identifier of the FrameBuffer File-Device
int fbfd;
// The 'Fixed' screen information
fb_fix_screeninfo finfo;
// The original 'Variable' screen information
fb_var_screeninfo orig_vinfo;
};

155
src/viewpng/ViewPng.cpp Normal file
View File

@@ -0,0 +1,155 @@
// STL includes
#include <iostream>
// LibPNG includes
#include <png.h>
// Utils includes
#include <utils/RgbImage.h>
#include <utils/jsonschema/JsonFactory.h>
// Raspilight includes
#include <hyperion/Hyperion.h>
// Local includes
#include "FbWriter.h"
bool read_png(std::string file_name, RgbImage*& rgbImage)
{
png_structp png_ptr;
png_infop info_ptr;
FILE *fp;
if ((fp = fopen(file_name.c_str(), "rb")) == NULL)
{
return false;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL)
{
fclose(fp);
return false;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
fclose(fp);
png_destroy_read_struct(&png_ptr, NULL, NULL);
return false;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
return false;
}
png_init_io(png_ptr, fp);
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_SWAP_ALPHA | PNG_TRANSFORM_EXPAND, NULL);
png_uint_32 width = png_get_image_width(png_ptr, info_ptr);
png_uint_32 height = png_get_image_height(png_ptr, info_ptr);
png_uint_32 bitdepth = png_get_bit_depth(png_ptr, info_ptr);
png_uint_32 channels = png_get_channels(png_ptr, info_ptr);
png_uint_32 color_type = png_get_color_type(png_ptr, info_ptr);
std::cout << "BitDepth" << std::endl;
std::cout << bitdepth << std::endl;
std::cout << "Channels" << std::endl;
std::cout << channels << std::endl;
std::cout << "ColorType" << std::endl;
std::cout << color_type << std::endl;
png_bytepp row_pointers;
row_pointers = png_get_rows(png_ptr, info_ptr);
rgbImage = new RgbImage(width, height);
for (unsigned iRow=0; iRow<height; ++iRow)
{
if (color_type == PNG_COLOR_TYPE_RGB)
{
RgbColor* rowPtr = reinterpret_cast<RgbColor*>(row_pointers[iRow]);
for (unsigned iCol=0; iCol<width; ++iCol)
{
rgbImage->setPixel(iCol, iRow, rowPtr[iCol]);
}
}
else if (color_type == PNG_COLOR_TYPE_RGBA)
{
unsigned* rowPtr = reinterpret_cast<unsigned*>(row_pointers[iRow]);
for (unsigned iCol=0; iCol<width; ++iCol)
{
const unsigned argbValue = rowPtr[iCol];
rgbImage->setPixel(iCol, iRow, {uint8_t((argbValue >> 16) & 0xFF), uint8_t((argbValue >> 8) & 0xFF), uint8_t((argbValue) & 0xFF)});
}
}
else
{
// Unknown/Unimplemented color-format
return false;
}
}
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
return true;
}
int main(int argc, char** argv)
{
if (argc < 2)
{
// Missing required argument
std::cout << "Missing PNG-argumet. Usage: 'ViewPng [png-file]" << std::endl;
return 0;
}
const std::string pngFilename = argv[1];
RgbImage* image = nullptr;
if (!read_png(pngFilename, image) || image == nullptr)
{
std::cout << "Failed to load image" << std::endl;
return -1;
}
const char* homeDir = getenv("RASPILIGHT_HOME");
if (!homeDir)
{
homeDir = "/home/pi";
}
std::cout << "RASPILIGHT HOME DIR: " << homeDir << std::endl;
const std::string schemaFile = std::string(homeDir) + "/hyperion.schema.json";
const std::string configFile = std::string(homeDir) + "/hyperion.config.json";
Json::Value raspiConfig;
if (JsonFactory::load(schemaFile, configFile, raspiConfig) < 0)
{
std::cerr << "UNABLE TO LOAD CONFIGURATION" << std::endl;
return -1;
}
std::cout << "Loaded configuration: " << raspiConfig << std::endl;
FbWriter fbWriter;
Hyperion raspiLight(raspiConfig);
// raspiLight.setInputSize(image->width(), image->height());
fbWriter.writeImage(*image);
// raspiLight(*image);
sleep(5);
delete image;
}