r/cpp_questions • • 1d 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

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);
        }
    }
}