r/cpp_questions • • 2d ago

OPEN Help getting external libraries working

I'm decently inexperienced with C++ (Only did one class using the language during school, and we used std for everything). I'm not actually sure everything I need to do to get an external library working for my project.

I'm using CLion IDE, most of my settings are still untouched, and I'm trying to get CImg (with libpng) I researched the topic online, but I didn't quite figure out how to get it working... in part, because I'm not sure which part of libpng I'm supposed to use.

I usually prefer to recreate all my functions from scratch, but I couldn't really do that for images, especially if I wanted access to .png files. I'm not picky about using Cimg, I really just need something that can display, rotate, resize, and edit images together (like putting a character on a background).

I backed up my code to github, and made a public branch. If you wanna tear apart an amatuer's code, there's probably a lot to laugh at in here, but I might learn something from people pointing out all the mistakes.
https://github.com/Viteris/Jivelthresh

2 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/TeneSicarius 1d ago edited 1d ago

I did have a separate folder labeled 'library' where I put Cimg. It was mostly trying to get libpng compatibility that I was stuck on. I read that part in the source code when I did a control+f in the file for png, and now I've just been trying to deal with the cluster-f that is libpng 79 files in the first tab. Compared to Cimg's 5. I'm not sure what I import to enable the cross compatibility. Or how to properly define cimg_use_png.

Edit: Is it really just #include "png.h"? And do I have to upload an entire copy of both libraries if I want to share a playable version? libpng was really confusing documentation-wise, and I don't feel confident compared to when I make a program from scratch...

2

u/alfps 1d ago

Much as I'd like to help you it takes some (maybe long) time to install the zlib and png libraries.

png is separately compiled library so it's not enough to just include its header. You also need to link with its binaries. Which presumably you can do via CMake.

However at the cimg site I found an example program that may be useful (I've made a few mostly stylistic changes):

#define cimg_use_png        // Or in compiler invocation use `-Dcimg_use_png`.
#include <CImg.h>
namespace img = cimg_library;

using Byte = unsigned char;

auto main() -> int
{
    auto image = img::CImg<Byte>( "images/lenna.png" );
    auto visu = img::CImg<Byte>( 500, 400, 1, 3, 0 ) ;
    const Byte red[]{ 255,0,0 }, green[]{ 0,255,0 }, blue[]{ 0,0,255 };
    image.blur(2.5);
    auto main_disp = img::CImgDisplay( image,"Click a point");
    auto draw_disp = img::CImgDisplay( visu,"Intensity profile" );
    while( not main_disp.is_closed() && not draw_disp.is_closed() ) {
        main_disp.wait();
        if (main_disp.button() && main_disp.mouse_y()>=0) {
            const int y = main_disp.mouse_y();
            visu.fill(0).draw_graph(image.get_crop(0,y,0,0,image.width()-1,y,0,0),red,1,1,0,255,0);
            visu.draw_graph(image.get_crop(0,y,0,1,image.width()-1,y,0,1),green,1,1,0,255,0);
            visu.draw_graph(image.get_crop(0,y,0,2,image.width()-1,y,0,2),blue,1,1,0,255,0).display(draw_disp);
        }
    }
}