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.
Related
I am trying to run this simple example in GDCM. I have installed the library c++ version and the installation works perfectly fine but I am not able to figure out how to compile and run a example.
#include "gdcmReader.h"
#include "gdcmWriter.h"
#include "gdcmAttribute.h"
#include <iostream>
int main(int argc, char *argv[])
{
if( argc < 3 )
{
std::cerr << argv[0] << " input.dcm output.dcm" << std::endl;
return 1;
}
const char *filename = argv[1];
const char *outfilename = argv[2];
// Instanciate the reader:
gdcm::Reader reader;
reader.SetFileName( filename );
if( !reader.Read() )
{
std::cerr << "Could not read: " << filename << std::endl;
return 1;
}
// If we reach here, we know for sure only 1 thing:
// It is a valid DICOM file (potentially an old ACR-NEMA 1.0/2.0 file)
// (Maybe, it's NOT a Dicom image -could be a DICOMDIR, a RTSTRUCT, etc-)
// The output of gdcm::Reader is a gdcm::File
gdcm::File &file = reader.GetFile();
// the dataset is the the set of element we are interested in:
gdcm::DataSet &ds = file.GetDataSet();
// Contruct a static(*) type for Image Comments :
gdcm::Attribute<0x0020,0x4000> imagecomments;
imagecomments.SetValue( "Hello, World !" );
// Now replace the Image Comments from the dataset with our:
ds.Replace( imagecomments.GetAsDataElement() );
// Write the modified DataSet back to disk
gdcm::Writer writer;
writer.CheckFileMetaInformationOff(); // Do not attempt to reconstruct the file meta to preserve the file
// as close to the original as possible.
writer.SetFileName( outfilename );
writer.SetFile( file );
if( !writer.Write() )
{
std::cerr << "Could not write: " << outfilename << std::endl;
return 1;
}
return 0;
}
/*
* (*) static type, means that extra DICOM information VR & VM are computed at compilation time.
* The compiler is deducing those values from the template arguments of the class.
*/
It has a few header files that it is looking for namely gdcmreader, gdcmwriter and I want to figure out the compiler flags to use to be able to run this file.
I am doing g++ a.cpp -lgdcmCommon -lgdcmDICT but that gives me the error
a.cpp:18:24: fatal error: gdcmReader.h: No such file or directory
compilation terminated.
Can you please help me out? I have searched everywhere but I can't seem to figure out how to run this file.
When using files that are in different locations of your "normal" files you must instruct the compiler and the linker how to find them.
Your code has a #include <someFile.h> command.
The <> usage means "in other path". The compiler already knows common "other paths" as for "stdio" for common libraries.
In case of "not normal", you can tell g++ where to find the headers by adding -Imydir to the command line (replace 'mydir' with the proper path)
For the libraries, static (.a) or dynamic (.so) the same history stands.
The -Lmydir tells g++ where to look for libraries.
Your command line may look like
g++ a.cpp -I/usr/include -L/usr/local/lib -lgdcmCommon -lgdcmDICT
You did not tell how did you install gdcm library, I assume that using apt system. There are two types of libraries, "normal" and "developer" ones. To be able to compile your own software, you need the latter. So, for example in Ubuntu 16.04, type apt-get install libgdcm2-dev. Then all necessary headers will be installed in /usr/include/gdcm-2.6.
I am struggling with Windows and FreeType2. I am following the tutorial and the following is shown as example code:
FT_Library library; /* handle to library */
FT_Face face; /* handle to face object */
error = FT_Init_FreeType( &library );
if ( error ) { ... }
error = FT_New_Face(library, "/usr/share/fonts/truetype/arial.ttf", 0, &face ); // <-- this does not exist...
if ( error == FT_Err_Unknown_File_Format )
{
... the font file could be opened and read, but it appears
... that its font format is unsupported
}
else if ( error )
{
... another error code means that the font file could not
... be opened or read, or simply that it is broken...
}
This /usr/share/fonts/truetype/arial.ttf just simply does not exist, how can I get the Arial font to work with this.
It varies from system to system. Check FOLDERID_Fonts.
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;
}
I'm trying to load up a Comma Separated file called POSDATA.GAMEDATA. I've looked up several places on the internet and it turns out I need to do some tweaking and / or a different class.
I tried using ifstream. However, it cannot open the file. Xcode 4.3.2 cannot seem to find my POSDATA.GAMEDATA file. I also tried to make the file using ofstream but when I use open() in both cases, the file is not opened.
My code is something like this:
using namespace std;
void FileLoader::loadFile( string p_WhichFile ) {
// Local Variables
string thisLine;
// Open POSDATA.GAMEDATA
ifstream dataStream;
dataStream.open( p_WhichFile.c_str( ) );
// Check if file is opened
if ( !dataStream ) {
cerr << "[ ERROR ] Cannot load file:" << p_WhichFile.c_str( ) << endl;
exit( 1 );
}
// Get lines of strings
while ( getline( dataStream, thisLine ) ) {
fileContents.push_back( thisLine ); // fileContents is a vector< string > object
}
dataStream.close( );
cout << "[ NOTICE ] Finished reading file" << p_WhichFile << endl;
}
I've seen CCFileUtils but I can't seem to get how to use it.
EDIT: I've tried supplying the absolute path ( /Users/LanceGray/Documents/LanceDev/COCOS2DX/cocos2dx/TestGame/Data/POSDATA.GAMEDATA ) and it worked. However, I cannot do this since the game is supposed to be used in iOS devices and Android, so the path is not always the same on each device. Any help will be grealy appreciated.
I got working by using CCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( "POSDATA.GAMEDATA" );
A more detailed explanation:
Add the files that you need in the Project by going to the Project Tree on the left and Right-click -> Add Files. What I did was I added a new folder called Data on the same level as the Resources and Classes folders and placed my POSDATA.GAMEDATA file there. In Xcode, I added a new group and added that file in that group.
Then I used ifstream to open the file.
When opening the file, use CCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( ) to get the absolute path of the file. Supply the file name on the fullPathFromRelativePath( ) as argument.
Try to run it and it should work fine.
A small example:
// FileReader.h
#include "cocos2d.h"
using namespace std;
using namespace cocos2d;
class FileReader {
private:
vector< string > mFileContents;
public:
FileReader( string pFileName, char pMode = 'r' );
};
// FileReader.cpp
#include "FileReader.h"
#include <fstream>
#include "cocos2d.h"
using namespace cocos2d;
using namespace std;
FileReader::FileReader( string pFileName, char pMode ) {
// Create input file stream
ifstream inputStream;
string thisLine;
// Open file
inputStream.open( CCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( pFileName ).c_str( ) );
// Check if it is open
if ( !inputStream.is_open( ) ) {
cerr << "[ ERROR ] Cannot open file: " << pFileName.c_str( ) << endl;
exit( 1 );
}
while ( getline( inputStream, thisLine ) ) {
// Put all lines in vector
mFileContents.push_back( thisLine );
}
inputStream.close( );
cout << "[ NOTICE ] Finished opening file: " << pFileName.c_str( ) << endl;
}
This class will load a file with the name pFileName and place it on its member variable mFileContents. ( Note that it should have a public get function like vector< string > getFileContents( ) to access the mFileContents because it is private )
EDIT: The above sample will work on iOS, however, it won't on Android devices. So to fix this, instead of using ifstream, use CCFileUtils::sharedUtils( ) -> getFileData( ) instead. In conjunction with CCFileUtils::sharedUtils( ) -> fullPathFromRelativePath( ), we will be able to achieve our goal of reading a plain text file that works on both iOS and Android.
The FileReader class would then be like this:
// FileReader.cpp
#include "FileReader.h"
#include <fstream>
#include "cocos2d.h"
using namespace cocos2d;
using namespace std;
FileReader::FileReader( string pFileName, char pMode ) {
// Initialize variables needed
unsigned long fileSize = 0;
unsigned char * fileContents = NULL;
string thisLine, result, fullPath, contents;
// Get absolute path of file
fullPath = CCFileUtils::sharedFileUtils( ) -> fullPathFromRelativePath( pFileName.c_str( ) );
// Get data of file
fileContents = CCFileUtils::sharedFileUtils( ) -> getFileData( fullPath.c_str( ) , "r", &fileSize );
contents.append( ( char * ) fileContents );
// Create a string stream so that we can use getline( ) on it
istringstream fileStringStream( contents );
// Get file contents line by line
while ( getline( fileStringStream, thisLine ) ) {
// Put all lines in vector
mFileContents.push_back( thisLine );
}
// After this, mFileContents will have an extra entry and will have the value '\x04'.
// We should remove this by popping it out the vector.
mFileContents.pop_back( );
// Delete buffer created by fileContents. This part is required.
if ( fileContents ) {
delete[ ] fileContents;
fileContents = NULL;
}
// For testing purposes
cout << "[ NOTICE ] Finished opening file: " << pFileName.c_str( ) << endl;
}
// For versions less than v2.0.1
// The version I am using is 0.12.0
unsigned long fileSize = 0;
char* pBuffer = CCFileUltils::getFileData("relative_path","r",&fileSize);
CCLOG("Data is %s",pBuffer);
You can reference from Cocos's wiki
Read/write file in cocos2d
I would like to load image to my application, but I have an error:
http://img510.imageshack.us/img510/5814/blad07864.png
This is a code of this application:
#include <stdio.h>
#include <stdlib.h>
#undef _UNICODE
#include "il.h"
#pragma comment( lib, "DevIL.lib" )
// Wow. DevIL is amazing.
// From http://gpwiki.org/index.php/DevIL:Tutorials:Basics
// The library consists of three sub-libraries:
// * IL - main DevIL library. It allows you to load and save images to files. Every function in this library have 'il' prefixed to their name.
// * ILU - this library contains functions for altering images. Every function in this library have 'ilu' prefixed to their name.
// * ILUT - this library connects DevIL with OpenGL. Every function in this library have 'ilut' prefixed to their name.
int main()
{
ilInit();
printf("DevIL has been initialized\n");
// Loading an image
ILboolean result = ilLoadImage( "tex1.png" ) ;
if( result == true )
{
printf("the image loaded successfully\n");
}
else
{
printf("The image failed to load\n" ) ;
ILenum err = ilGetError() ;
printf( "the error %d\n", err );
printf( "string is %s\n", ilGetString( err ) );
}
int size = ilGetInteger( IL_IMAGE_SIZE_OF_DATA ) ;
printf("Data size: %d\n", size );
ILubyte * bytes = ilGetData() ;
for( int i = 0 ; i < size; i++ )
{
// see we should see the byte data of the image now.
printf( "%d\n", bytes[ i ] );
}
}
I found code from this site: http://bobobobo.wordpress.com/2009/03/02/how-to-load-a-png-image-in-c/
Can you help me?
According to this post, 1290 means the image path wasn't found. Try using an absolute file path and see if it can load then.