Can't compile STK (The Synthesis Toolkit) with node-gyp (C++) - c++

I'm trying to make an audio software (a DAW) using Electron to create the window and c++ to play audio / generate audio / apply audio effects.
I have been searching for a simple, powerful, and cross-platform library to play and process audio, and I've found The Synthesis Toolkit and I'm really happy with it.
Here is the code (it's from the STK demo programs):
#include "BeeThree.h"
#include "RtAudio.h"
using namespace stk;
// The TickData structure holds all the class instances and data that
// are shared by the various processing functions.
struct TickData {
Instrmnt *instrument;
StkFloat frequency;
StkFloat scaler;
long counter;
bool done;
// Default constructor.
TickData()
: instrument(0), scaler(1.0), counter(0), done( false ) {}
};
// This tick() function handles sample computation only. It will be
// called automatically when the system needs a new buffer of audio
// samples.
int tick( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *userData )
{
TickData *data = (TickData *) userData;
register StkFloat *samples = (StkFloat *) outputBuffer;
for ( unsigned int i=0; i<nBufferFrames; i++ ) {
*samples++ = data->instrument->tick();
if ( ++data->counter % 2000 == 0 ) {
data->scaler += 0.025;
data->instrument->setFrequency( data->frequency * data->scaler );
}
}
if ( data->counter > 80000 )
data->done = true;
return 0;
}
int main()
{
// Set the global sample rate and rawwave path before creating class instances.
Stk::setSampleRate( 44100.0 );
Stk::setRawwavePath("./engine/rawwaves/");
TickData data;
RtAudio dac;
// Figure out how many bytes in an StkFloat and setup the RtAudio stream.
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 1;
RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
unsigned int bufferFrames = RT_BUFFER_SIZE;
try {
dac.openStream( &parameters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data );
}
catch ( RtAudioError& error ) {
error.printMessage();
goto cleanup;
}
try {
// Define and load the BeeThree instrument
data.instrument = new BeeThree();
}
catch ( StkError & ) {
goto cleanup;
}
data.frequency = 220.0;
data.instrument->noteOn( data.frequency, 0.5 );
try {
dac.startStream();
}
catch ( RtAudioError &error ) {
error.printMessage();
goto cleanup;
}
// Block waiting until callback signals done.
std::cin.get();
data.scaler = 0.025;
std::cin.get();
data.scaler = -1;
std::cin.get();
// Shut down the callback and output stream.
try {
dac.closeStream();
}
catch ( RtAudioError &error ) {
error.printMessage();
}
cleanup:
delete data.instrument;
return 0;
}
I managed to compile this simple demo program with g++, using this command:
g++ -D__LITTLE_ENDIAN__ -D__LINUX_ALSA__ ./engine/engine.cpp -o ./engine/engi -I./engine/include/ -L./engine/lib/ -lstk -lpthread -lasound -lm
But then I try to compile it into an engine.node file with node-gyp, I get this error:
paulux#Paulux-Laptop:~/Documents/Code/FyneWav$ node-gyp build
/usr/bin/ld : can't find -lstk
collect2: error: ld returned 1 exit status
Here's my binding.gyp file:
{
"targets": [
{
"target_name": "engine",
"sources": ["./engine/engine.cpp"],
"cflags_cc" :["-fexceptions"],
"include_dirs": [
"./engine/include/"
],
'link_settings': {
"libraries": [
"-lpthread", "-lasound" , "-lm",
"-L./engine/lib/", "-lstk"
],
},
"defines": [
"__LITTLE_ENDIAN__", "__LINUX_ALSA__"
]
}
]
}
My folders looks like this:
root
|- package-lock.json
|- package.json
|- README.md
|- binding.gyp
|- 10.1.4 (includes for v8 NodeJS addon)
|- engine
|- engine.cpp
|- include (all include files from *STK/include* archive)
|- lib
|- libstk.a (lib from the *STK/src/Release* archive)
I tried not to link stk in the binding.gyp file, but then I loaded the engine.node module in electron, I got:
Uncaught Error: /home/paulux/Documents/Code/FyneWav/build/Release/engine.node:
undefined symbol: _ZN3stk3Stk17sampleRateChangedEdd
So, the question is:
How can I link stk in node-gyp, and, if we can't, how can I make a engine.node file using other compilers (like g++ for example) ?

Finnaly ! I've found by myself !
The answer is really dumb: in my binding.gyp file, I just had to replace
-L./engine/include by
-L/home/paulux/Documents/Code/fynewav/engine/include.
I just had to change from a relative path to an absolute one...
And it took me a day to figure it out...
And I hate myself =)

Related

Pass data from OpenCV C++ to NodeJS/JS | Electron

I am trying to do a POC for Video Processing application with the following stack and struck with passing processed media stream from c++ application to Electron Front end GUI.
Electron
|
Nodejs
|
C++ Application
C++ Application will read the IP/Webcam(using OpenCV only to fetch data) and process the input stream(not with OpenCV). I am trying to figure out a way to send that stream from C++ to Electron GUI(NodeJS/JS) with good fps. Right now I compiled my C++ app using node-gyp and installed it as node package.
Also, I don't want to change my C++ Application too much( like including OpenCV as node package), because later I will use that C++ Application alone for integrating with another application.
The Challenge:
We want to execute our heavy lifting code in a separate worker thread while also sending results (stream data chunks) back to the main thread during execution.
NAN (Native Abstractions for Node.js) already provides an approach to do this with (AsyncProgressWorker).
However, we can not know if the HandleProgressCallback is actually invoked during the execution to send back our results. This can happen when our run time is simply to fast and therefore the callback is never executed.
Proposed Solution:
We simply collect our stream output in a stack (StackCollect). We attempt to clear this stack immediately and send the stream results back to the main thread (if possible) - (StackDrain). If we don't have the time to clear the stack immediately we drain (whats left) at the end of the execution run (HandleOKCallback).
Implementation Example:
demo.cpp (our C++ node/electron addon):
#include <nan.h>
#include <node.h>
#include <v8.h>
#include <iostream>
#include <string>
#include <vector>
#include <mutex>
#include <chrono>
#include <thread>
class vSync_File : public Nan::AsyncProgressWorker {
public:
~vSync_File();
vSync_File(Nan::Callback * result, Nan::Callback * chunk);
void Execute(const Nan::AsyncProgressWorker::ExecutionProgress& chunk);
void HandleOKCallback();
void HandleProgressCallback(const char *tout, size_t tout_size);
//needed for stream data collection
void StackCollect(std::string & str_chunk, const Nan::AsyncProgressWorker::ExecutionProgress& tchunk);
//drain stack
void StackDrain();
private:
Nan::Callback * chunk;
//stores stream data - use other data types for different output
std::vector<std::string> stack;
//mutex
std::mutex m;
};
vSync_File::vSync_File(Nan::Callback * result, Nan::Callback * chunk)
: Nan::AsyncProgressWorker(result), chunk(chunk) {}
vSync_File::~vSync_File() {
delete chunk;
}
void vSync_File::StackCollect(std::string & str_chunk, const Nan::AsyncProgressWorker::ExecutionProgress& tchunk) {
std::lock_guard<std::mutex> guardme(m);
stack.push_back(str_chunk);
//attempt drain
std::string dummy = "NA";
tchunk.Send(dummy.c_str(), dummy.length());
}
//Dump out stream data
void vSync_File::StackDrain() {
std::lock_guard<std::mutex> guardme(m);
for (uint i = 0; i < stack.size(); i++) {
std::string th_chunk = stack[i];
v8::Local<v8::String> chk = Nan::New<v8::String>(th_chunk).ToLocalChecked();
v8::Local<v8::Value> argv[] = { chk };
chunk->Call(1, argv, this->async_resource);
}
stack.clear();
}
//Our main job in a nice worker thread
void vSync_File::Execute(const Nan::AsyncProgressWorker::ExecutionProgress& tchunk) {
//simulate some stream output
for (unsigned int i = 0; i < 20; i++) {
std::string out_chunk;
out_chunk = "Simulated stream data " + std::to_string(i);
std::this_thread::sleep_for(std::chrono::milliseconds(300)); //so our HandleProgressCallback is invoked, otherwise we are too fast in our example here
this->StackCollect(out_chunk, tchunk);
}
}
//Back at the main thread - if we have time stream back the output
void vSync_File::HandleProgressCallback(const char *tout, size_t tout_size) {
Nan::HandleScope scope;
this->StackDrain();
}
//Back at the main thread - we are done
void vSync_File::HandleOKCallback () {
this->StackDrain(); //drain leftovers from stream stack
v8::Local<v8::String> result_mess = Nan::New<v8::String>("done reading").ToLocalChecked();
v8::Local<v8::Value> argv[] = { result_mess };
callback->Call(1, argv, this->async_resource);
}
NAN_METHOD(get_stream_data) {
Nan::Callback *result = new Nan::Callback(info[0].As<v8::Function>());
Nan::Callback *chunk = new Nan::Callback(info[1].As<v8::Function>());
AsyncQueueWorker(new vSync_File(result, chunk));
}
NAN_MODULE_INIT(Init) {
//we want stream data
Nan::Set(target, Nan::New<v8::String>("get_stream_data").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(get_stream_data)).ToLocalChecked());
}
NODE_MODULE(stream_c_electron, Init)
index.js (electron implementation example):
const stream_c_electron = require('./build/linux_x64/stream_c_electron.node');
stream_c_electron.get_stream_data(function(res) {
//we are done
console.log(res);
}, function(chk) {
console.log("a line streamed");
console.log(chk);
});
package.json:
{
"name": "stream_c_electron",
"version": "1.0.0",
"description": "stream from c++ node addon demo",
"main": "index.js",
"scripts": {
"start": "electron .",
"build_this": "HOME=~/.electron-gyp node-gyp rebuild --target=2.0.8 --arch=x64 --dist-url=https://atom.io/download/electron",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "11AND2",
"license": "MIT",
"dependencies": {
"nan": "2.11.0"
},
"devDependencies": {
"electron": "2.0.8"
}
}
binding.gyp:
{
"targets": [
{
"target_name": "stream_c_electron",
"sources": [ "c_src/demo.cpp" ],
"conditions": [
[
'OS=="linux"',
{
"cflags": ["-Wall", "-std=c++11"],
'product_dir' : 'linux_x64',
"include_dirs": [
"<!(node -e \"require('nan')\")"
]
}
]
]
}
]
}
You have to compile your c++ stuff as a static library with emscripten and load it in via import MyLib from "./MyLib"; or with require and run with node --experimental-modules --napi-modules main.mjs. Basically the idea is that the V8 engine is able to read your native code. It's also incredibly fast compared to pure javascript code.
It's actually pretty easy when you know what to do. Have a look to this sample code. It basically uses native c++ libpng library for javascript. The only tricky thing is actually interfacing c++ with javascript.
https://github.com/skanti/png-decoder-javascript/tree/devel

Most basic example of adding a OpenCV C++ add-on to node.js

So lately I've been getting into OpenCV with C++.
I've built up a few libraries and apps that I would like to export over to Nodejs, but I can't figure it out for the life of me.
I tried to check out how he did it in this repo below, but it was a lot to take in especially since this is my first add-on.
https://github.com/peterbraden/node-opencv/blob/master/binding.gyp
I don't mind it being with NAN or N-API, I just am hoping for something simple and easy to see what goes where and why.
Here is a simple OpenCV function that just opens up an image that I am trying to use as an addon with Node:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int ShowImage()
{
String imageName("./image.png");
Mat image;
image = imread(imageName, IMREAD_COLOR);
namedWindow("Display window", WINDOW_AUTOSIZE);
imshow("Display window", image);
waitKey(0);
}
There are three main files that you will need.
binding.gyp
module.cpp
index.js
binding.gyp
For me The hardest part was figuring out how to include openCV into the project. I don't know if this is correct or not but I looked at the binding.gyp file like a make file in a typical C++ project. With that in mind this is what my binding.gyp file looks like.
{
"targets": [{
"target_name": "module",
'include_dirs': [
'.',
'/user/local/lib',
],
'cflags': [
'-std=c++11',
],
'link_settings': {
'libraries': [
'-L/user/local/lib',
'-lopencv_core',
'-lopencv_imgproc',
'-lopencv_highgui'
],
},
"sources": [ "./src/module.cpp",
"./src/ImageProcessing.cpp" ]
}]
}
The ImageProcessing.cpp file that I wrote needed c++11 so that's why I added that flag it is not necessary to get openCV to work.
The key of the binding.gyp file is the link-settings. This is how you actually include openCV into your project.
Also make sure to include all of your source files in the sources list(I forgot to include my ImageProcessing.cpp file initially)
module.cpp
I used n-api so my module.cpp file looked like this
#include <node_api.h>
#include "ImageProcessing.hpp"
#include "opencv.hpp"
template <typename T>
ostream& operator<<(ostream& output, std::vector<T> const& values)
{
for (auto const& value : values)
{
output << value;
}
return output;
}
napi_value processImages(napi_env env, napi_callback_info info)
{
napi_status status;
size_t argc = 3;
napi_value argv[1];
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
char PathName[100];
size_t result;
status = napi_get_value_string_utf8(env, argv[0], PathName, 100, &result);
char FileName1[100];
status = napi_get_value_string_utf8(env, argv[1], FileName1, 100, &result);
char FileName2[100];
status = napi_get_value_string_utf8(env, argv[2], FileName2, 100, &result);
vector< vector<Point> > Anchors; //to store coordinates of all anchor points
vector< vector<Point> > Regions[4]; //to store coordinates of all corners of all pages
vector<int> Parameters; // image processing parameters
vector<string> FileList1;
vector<string> FileList2;
Mat TemplateROI[NUM_SHEET][4];
Mat Result1, Result2;
string FileName;
string testName = FileName1;
int i;
// The first function to be called only at startup of the program
// provide the path to folder where the data and reference image files are saved
getAnchorRegionRoI(PathName, &Anchors, Regions, &Parameters, TemplateROI);
vector< vector<int> > Answers;
if (Parameters.at(0)) {
namedWindow("Display1", CV_WINDOW_AUTOSIZE);
namedWindow("Display2", CV_WINDOW_AUTOSIZE);
}
napi_value outer;
status = napi_create_array(env, &outer);
//This will need to be changed to watch for new files and then process them
Answers = scanBothSides(FileName1, FileName2, "./Output/", &Result1, &Result2, &Anchors, Regions, Parameters, TemplateROI);
for(int k = 0; k<Answers.size(); k++){
napi_value inner;
status = napi_create_array(env, &inner);
int j;
for(j = 0; j<Answers[k].size(); j++){
napi_value test;
napi_create_int32(env, Answers[k][j], &test);
napi_set_element(env,inner, j, test);
}
napi_value index;
napi_create_int32(env, k, &index);
napi_set_element(env,inner, j, index);
napi_set_element(env,outer, k, inner);
}
if (Parameters.at(0)) {
if (!Result1.empty() && !Result1.empty()) {
FileName = "./Output/" + string("O ") + FileList1[i];
imwrite(FileName, Result1);
FileName = "./Output/" + string("O ") + FileList2[i];
imwrite(FileName, Result2);
resize(Result1, Result1, Size(772, 1000));
resize(Result2, Result2, Size(772, 1000));
imshow("Display1", Result1);
imshow("Display2", Result2);
waitKey(0);
}
}
if (status != napi_ok)
{
napi_throw_error(env, NULL, "Failed to parse arguments");
}
//return PathName;
return outer;
}
napi_value Init(napi_env env, napi_value exports)
{
napi_status status;
napi_value fn;
status = napi_create_function(env, NULL, 0, processImages, NULL, &fn);
if (status != napi_ok)
{
napi_throw_error(env, NULL, "Unable to wrap native function");
}
status = napi_set_named_property(env, exports, "processImages", fn);
if (status != napi_ok)
{
napi_throw_error(env, NULL, "Unable to populate exports");
}
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
This is the file that interfaces with C/C++ and node.
I had trouble with the opencv.hpp file being found so I just moved it into my working directory for now. This is why I used quotes instead of brackets to include it.
Working with the n-api took a little getting used to so make sure you read the docs here
index.js
And finally here is my index.js file
const express = require('express');
const app = express();
const addon = require('./build/Release/module');
const value = "./Data/";
let FileName1 = "./Images/Back1.jpg";
let FileName2 = "./Images/Front1.jpg";
let result = addon.processImages(value, FileName1, FileName2);
console.log("Results: "+result);
server.listen(3000, () => console.log('Example app listening on port 3000!'))
So all you have to do is require your module from the build/Release folder and then call it like any other js function.
Take a look at the module.cpp code again and you will see that in the init function you use the n-api to create a new function. I called mine processImages. This name matches the name of the processImages function at the top of the module.cpp file. Finally in my index.js file I call addon.processImages().
Tips:
I installed node-gyp globally by running npm install -g node-gyp
I compiled my code using the following command: node-gyp configure build
Try getting a simple n-api project working first then add in openCV. I used this tutorial to get started
I created a simple script which will compile OpenCV 3.4 with opencv_contrib (SIFT, SURF available) for statically linking with Native Abstractions for Node.js.
rm -rf 3rdparty/opencv
mkdir -p 3rdparty/opencv
rm -rf tmp
mkdir tmp
cd tmp
rm -rf opencv-master
rm -rf opencv_contrib-master
git clone --branch 3.4 --depth 1 https://github.com/opencv/opencv.git opencv-master
git clone --branch 3.4 --depth 1 https://github.com/opencv/opencv_contrib.git opencv_contrib-master
mkdir build
cd build
cmake \
-DCMAKE_INSTALL_PREFIX="$(pwd)/../../3rdparty/opencv\#3.4" \
-DBUILD_SHARED_LIBS=OFF \
-DENABLE_PRECOMPILED_HEADERS=YES \
-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-master/modules \
../opencv-master
cmake --build .
make install
It will help you to to get started with your computer vision project. By the way, SIFT algorithm is no longer requires a license for use as its patent expired 2020-03-06
{
"targets": [
{
"target_name": "addon",
"cflags": [
"-std=c++11",
"-stdlib=libc++"
],
"cflags_cc!": [
"-fno-rtti",
"-fno-exceptions"
],
"xcode_settings": {
"GCC_ENABLE_CPP_RTTI": "YES",
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
},
"include_dirs": [
"../../3rdparty/opencv/#3.4/include",
"<!(node -e \"require('nan')\")"
],
'libraries': [
"<!#(node utils/find-libs.js)",
"-framework OpenCL"
],
"sources": [
"./src/main.cc",
"./src/lib/MainAddon.cc"
],
}
]
}
The full source code is large enough so It published in this GitHub repository

Runtime error on Windows when trying to load image with libpng

I am using pHash and that library uses libpng. I am having issues running my program because libpng fails loading a PNG file.
Version of libpng: 1.4.19
Platform: Windows 10
Environment: Visual Studio 2015
Trivial
Just if you came up with the following questions...
Is the path to image correct? Yes
Is the image a valid PNG file? Yes
Code details
Library pHash uses CImg, the version of CImg they are using is a bit old I think:
#define cimg_version 148 // In CImg.h
I have debugged the library and the problems occurs in CImg.h (contained in the pHash VC++ project):
CImg<T>& _load_png(std::FILE *const file, const char *const filename) {
if (!file && !filename)
throw CImgArgumentException(_cimg_instance
"load_png() : Specified filename is (null).",
cimg_instance);
// Open file and check for PNG validity
if (Buffer) strcat(Buffer, "Checking PNG availability\r\n");
const char *volatile nfilename = filename; // two 'volatile' here to remove a g++ warning due to 'setjmp'.
std::FILE *volatile nfile = file?file:cimg::fopen(nfilename,"rb");
unsigned char pngCheck[8] = { 0 };
cimg::fread(pngCheck,8,(std::FILE*)nfile);
if (png_sig_cmp(pngCheck,0,8)) {
if (!file) cimg::fclose(nfile);
throw CImgIOException(_cimg_instance
"load_png() : Invalid PNG file '%s'.",
cimg_instance,
nfilename?nfilename:"(FILE*)");
}
// Setup PNG structures for read
png_voidp user_error_ptr = 0;
png_error_ptr user_error_fn = 0, user_warning_fn = 0;
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,user_error_ptr,user_error_fn,user_warning_fn);
if (!png_ptr) { // <-- PROBLEM HERE
if (!file) cimg::fclose(nfile);
throw CImgIOException(_cimg_instance
"load_png() : Failed to initialize 'png_ptr' structure for file '%s'.",
cimg_instance,
nfilename?nfilename:"(FILE*)");
...
}
The snippet shows the first part of CImg<T>& _load_png(std::FILE *const file, const char *const filename) which is called by the CImg library used by pHash.
Runtime issue
The code compiles fine but I get this error at runtime which I can see in the debugger:
CImgIOException: Failed to initialize 'png_ptr'...
In the point indicated in the code. I don't know why, it fails loading the image. The failure occurs when calling png_create_read_struct in CImg.h. That code is a bit obscure as defined through preprocessor directives. It is not clear why it is failing.
Any ideas?
Either if you are including libpng yourself or if another library is including and using libpng there are a few things to be aware of.
Which ever version of Visual Studio you are using, the libpng (dll or lib) files must be built from the same version of Visual Studio that your solution is linking against.
The platform you are using 32bit or 64bit is of concern.
Project settings when building the png library must match the build types of your current project. (Code Generation -> Runtime Library) must match. Your character set should match as well.
It is a little to difficult to tell what exactly is causing the problem but these are a few things to have a look at.
One thing I would suggest is to go to the website that provides the newest version of libpng and download it. Set a folder on your computer and create "system environment variable through windows" to point to your library. Open the solution to this library in the current version of VS you are using, build it out for both a static lib and dynamic lib (two different solutions) and build them both out for 32 bit and 64 bit saving the generated files into separated folders. Then go into the other library that depends on this and try to switch the dlls or libs and link against the new ones if possible. Also the other 3rd party library you should try to open its solution in the same version of VS and try to do a clean build from there. Then make sure you link everything properly. You may have to also modify the props file.
EDIT
I am not familiar with pHash or CImg, but I am familiar with libpng.
Here is a function in one of my projects to load in a png into a texture structure. Now this is a part of a class object that relies on many other classes, but you should be able to see from this snippet that I am successfully using libpng.
// ----------------------------------------------------------------------------
// loadPng()
bool TextureFileReader::loadPng( Texture* pTexture ) {
struct PngFile {
FILE* fp;
png_struct* pStruct;
png_info* pInfo;
// --------------------------------------------------------------------
PngFile() :
fp( NULL ),
pStruct( NULL ),
pInfo( NULL )
{} // PngFile
// --------------------------------------------------------------------
~PngFile() {
if ( NULL != fp ) {
fclose( fp );
}
if ( NULL != pStruct ) {
if ( NULL != pInfo ) {
png_destroy_read_struct( &pStruct, &pInfo, NULL );
} else {
png_destroy_read_struct( &pStruct, NULL, NULL );
}
}
} // ~PngFile
} png;
// Error Message Handling
std::ostringstream strStream;
strStream << __FUNCTION__ << " ";
if ( fopen_s( &png.fp, m_strFilenameWithPath.c_str(), "rb" ) != 0 ) {
strStream << "can not open file for reading";
throwError( strStream );
}
// Test If File Is Actually A PNG Image
const int NUM_HEADER_BYTES = 8;
png_byte headerBytes[NUM_HEADER_BYTES];
// Read The File Header
if ( fread( headerBytes, 1, NUM_HEADER_BYTES, png.fp ) != NUM_HEADER_BYTES ) {
strStream << "error reading header";
return false;
}
// Test Header
if ( png_sig_cmp( headerBytes, 0, NUM_HEADER_BYTES ) != 0 ) {
return false; // Not A PNG FILE
}
// Init PNG Read Structure - Test PNG Version Compatibility
png.pStruct = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
if ( NULL == png.pStruct ) {
strStream << "can not create struct for PNG file";
throwError( strStream );
}
// Init PNG Info Structure - Allocate Memory For Image Info
png.pInfo = png_create_info_struct( png.pStruct );
if ( NULL == png.pInfo ) {
strStream << "can not create info for PNG file";
throwError( strStream );
}
// Prepare For Error Handling
if ( setjmp( png_jmpbuf( png.pStruct ) ) ) {
strStream << "can not init error handling for PNG file";
throwError( strStream );
}
// Tell libPng Where The File Data Is
png_init_io( png.pStruct, png.fp );
// Tell libPng That You Have Already Read The Header Bytes
png_set_sig_bytes( png.pStruct, NUM_HEADER_BYTES );
// Read Image Data From The File
png_read_png( png.pStruct, png.pInfo, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_GRAY_TO_RGB, NULL );
// Show Image Attributes
png_byte colorType = png_get_color_type( png.pStruct, png.pInfo );
switch( colorType ) {
case PNG_COLOR_TYPE_RGB:
case PNG_COLOR_TYPE_RGBA: {
break;
}
default: {
strStream << "PNG is saved in an unsupported color type (" << colorType << ")";
throwError( strStream );
}
}
unsigned uHeight = png_get_image_height( png.pStruct, png.pInfo );
unsigned uBytesPerRow = png_get_rowbytes( png.pStruct, png.pInfo );
if ( 0 == uHeight || 0 == uBytesPerRow ) {
strStream << "invalid image size. Height(" << uHeight << "), Bytes per row(" << uBytesPerRow << ")";
throwError( strStream );
}
// Make Room For All Pixel Data
unsigned uTotalNumBytes = uHeight * uBytesPerRow;
pTexture->vPixelData.resize( uTotalNumBytes );
// Get All Pixel Data From PNG Image
png_bytepp ppPixelRow = png_get_rows( png.pStruct, png.pInfo );
for ( unsigned int r = 0; r < uHeight; ++r ) {
memcpy( &pTexture->vPixelData[ uBytesPerRow * ( uHeight - 1 - r ) ], ppPixelRow[r], uBytesPerRow );
}
// Store Other Values In Texture
pTexture->uWidth = png_get_image_width( png.pStruct, png.pInfo );
pTexture->uHeight = uHeight;
pTexture->hasAlphaChannel = ( colorType == PNG_COLOR_TYPE_RGBA );
return true;
} // loadPng
Looking through the source code for png_create_read_struct_2(), there are only 2 failure modes: inability to allocate memory, which is unlikely to be the problem, and a library version conflict.
If you are using a precompiled build of the pHash library, you must ensure that the copy of the libpng DLL that gets linked dynamically at runtime is the same version of the library that pHash was compiled against. The latest Windows build on pHash.org ships with libpng12.dll in the "Release" subdirectory, which is probably incompatible the version that you mentioned in the question, namely 1.4.19.
If you are building pHash from source, make sure that the libpng include files that are being used in your build process match the version being loaded at runtime.
If you're unsure exactly which DLLs are being loaded at runtime, the surest way I know to determine it would be to use Process Monitor.

QJson for Linux - Unsure How to Use QJSON Correctly

Some Background
Originally made a project on mac, now I want to use my Linux machine for the same project. The settings folder relied on:
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonParseError>
These weren't included in my qt-devel install for SL5 - it uses QT v4. So I downloaded QJson from Sourceforge and installed using cmake. Some example output:
--Installing: /usr/include/qjson/parser.h
--Installing /usr/lib/libqjson.so
That's fine. So I added to my .pro:
LIBS += -L/usr/lib/ -lqjson
INCLUDEPATH += /usr/include/qjson/
The Actual Problem
Now I have the task of translating my old settings.cpp file to parse data in this slightly new method.
{
"HwDescription": {
"ConnectionsName": "file://settings/connections.xml",
"ShelveId": 0,
"BeBoard": {
"Id": 10,
"connectionId": "board0",
"boardType": "GLIB"
}, // + more boring stuff
So now I have this json above in a QString, just as I did for my old working method, then I try to parse as per the instructions given to me. I used: #include <qjson/parser.h> and I don't think I need any forward declarations here.
QJson::Parser parser;
bool ok;
QVariantMap result = parser.parse (raw_json, &ok).toMap(); //where raw_json is a QString - this is where I get an error
if (!ok)
{
qFatal("An error occured during parsing");
exit (1);
}
The error I get:
error: no matching function to call to 'Qjson::Parser:parse(QString&, bool)
If I remove the include, the error says:
QJson has not been declared
So it must be finding the libraries at least. Any ideas on what's going wrong?
Comments aren't parsed by default in QJson.
Here is a small adaptation I made on my QJson logic to handle comments. Notice the simple Regex to remove comments.
QFile file( filename );
//File can't be opened!
if ( !file.open( QFile::ReadOnly ) )
{
qDebug("Couldn't load config file: %s", filename.toLatin1().data());
return;
}
//Strip out comments
QStringList list = QString( file.readAll() ).split('\n');
for ( int i = 0; i < list.size(); i++ )
list[i].replace( QRegExp("//[^\"]*$"), "" );
//Load the file, converting into an object file
QJsonParseError e;
QJsonObject json =
QJsonDocument::fromJson( list.join('\n').toLatin1(), &e ).object();
//Was there an error?
if ( e.error != QJsonParseError::NoError )
{
qDebug( "Json parse error: %s", e.errorString().toLatin1().data() );
return;
}

dlclose crashes when copying dynamic libraries

I have an interesting problem that seems to be unresolved by my research on the internet.
I'm trying to load libraries dynamically in my c++ project with the functions from dlfcn.h. The problem is that when I try to reload the plugins at running time (because I made a change on any of them), the main program crashes (Segmentation fault (core dumped)) when dlclose() is called.
Here is my example that reproduces the error:
main.cpp:
#include <iostream>
#include <dlfcn.h>
#include <time.h>
#include "IPlugin.h"
int main( )
{
void * lib_handle;
char * error;
while( true )
{
std::cout << "Updating the .so" << std::endl;
lib_handle = dlopen( "./test1.so", RTLD_LAZY );
if ( ! lib_handle )
{
std::cerr << dlerror( ) << std::endl;
return 1;
}
create_t fn_create = ( create_t ) dlsym( lib_handle, "create" );
if ( ( error = dlerror( ) ) != NULL )
{
std::cerr << error << std::endl;
return 1;
}
IPlugin * ik = fn_create( );
ik->exec( );
destroy_t fn_destroy = ( destroy_t ) dlsym( lib_handle, "destroy" );
fn_destroy( ik );
std::cout << "Waiting 5 seconds before unloading..." << std::endl;
sleep( 5 );
dlclose( lib_handle );
}
return 0;
}
IPlugin.h:
class IPlugin
{
public:
IPlugin( ) { }
virtual ~IPlugin( ) { }
virtual void exec( ) = 0;
};
typedef IPlugin * ( * create_t )( );
typedef void ( * destroy_t )( IPlugin * );
Test1.h:
#include <iostream>
#include "IPlugin.h"
class Test1 : public IPlugin
{
public:
Test1( );
virtual ~Test1( );
void exec( );
};
Test1.cpp:
#include "Test1.h"
Test1::Test1( ) { }
Test1::~Test1( ) { }
void Test1::exec( )
{
std::cout << "void Test1::exec( )" << std::endl;
}
extern "C"
IPlugin * create( )
{
return new Test1( );
}
extern "C"
void destroy( IPlugin * plugin )
{
if( plugin != NULL )
{
delete plugin;
}
}
To compile:
g++ main.cpp -o main -ldl
g++ -shared -fPIC Test1.cpp -o plugin/test1.so
The problem occurs when for example I change something on the Test1::exec method (changing the string to be printed or commenting the line) and while the main program sleeps I copy the new test1.so to main running directory (cp). If I use the move command (mv), no error occurs. What makes the difference between using cp or mv? Is there any way to solve this problem or to do that using cp?
I'm using Fedora 14 with g++ (GCC) 4.5.1 20100924 (Red Hat 4.5.1-4).
Thanks in advance.
The difference between cp and mv that is pertinent to this question is as follows:
cp opens the destination file and writes the new contents into it. It therefore replaces the old contents with the new contents.
mv doesn't touch the contents of the original file. Instead, it makes the directory entry point to the new file.
This turns out to be important. While the application is running, the OS keeps open handles to the executable and the shared objects. When it needs to consult one of the these files, it uses the relevant handle to access the file's contents.
If you've used cp, the contents has now been corrupted, so anything can happen (a segfault is a pretty likely outcome).
If you've used mv, the open file handle still refers to the original file, which continues to exist on disk even though there's no longer a directory entry for it.
If you've used mv to replace the shared object, you should be able to dlclose the old one and dlopen the new one. However, this is not something that I've done or would recommend.
Try this:
extern "C"
void destroy( IPlugin * plugin )
{
if( plugin != NULL && dynamic_cast<Test1*>(plugin))
{
delete static_cast<Test1*>(plugin);
}
}