1

I need to load and pass an image to a code. I am trying to modify part of a code from this library. This library is designed for windows and the below is the code which I need to modify for linux environment. Can some one help me with its linux equivalent.

System::Drawing::Bitmap^ m_bmpImage;

System::String^ sFile = gcnew System::String(txtFilePath->Text);
m_bmpImage = gcnew System::Drawing::Bitmap(sFile);

    BitmapData^ bmpData = m_bmpImage->LockBits(
                System::Drawing::Rectangle(0, 0, m_bmpImage->Width, m_bmpImage->Height),
                ImageLockMode::ReadWrite, PixelFormat::Format24bppRgb);
            ImageData imgData;
            imgData.Resize(bmpData->Width, bmpData->Height, 3, bmpData->Stride);
            memcpy(imgData.Data, (char*)(void*)bmpData->Scan0, bmpData->Stride*bmpData->Height);

            m_bmpImage->UnlockBits(bmpData);
2vision2
  • 4,933
  • 16
  • 83
  • 164
  • 1
    This is not C++ it is C++/CLI which won't work on linux unless there is some mono magic you can use. – doctorlove Aug 20 '13 at 12:19
  • @doctorlove Yes it wont work. I need its equivalent for Linux? – 2vision2 Aug 20 '13 at 12:20
  • 2
    There are many image loading libraries available on Linux. Both specific for a single image type (like libpng for PNG image, libjpeg for JPEG images, etc), then libraries which uses these specific libraries to be able to load multiple formats. A quick search should find you something. – Some programmer dude Aug 20 '13 at 12:21
  • http://stackoverflow.com/questions/15862362/is-there-any-native-functions-in-linux-that-load-and-save-images-without-using-e – doctorlove Aug 20 '13 at 12:22

2 Answers2

1

FreeImage is good: http://freeimage.sourceforge.net; But I've always preferred OpenCV for every image processing stuff: http://opencv.org

Both are cross-platform.

lulyon
  • 6,707
  • 7
  • 32
  • 49
  • Yes I had a try with opencv and left in the middle because of lack of time. Thanks anyways for the suggestion. – 2vision2 Aug 22 '13 at 09:40
1

A simple solution is to use PGM file format for grayscale or PPM file format for RGB because they are trivial to read from code, then use ImageMagick to do the conversion (the linux command is for example convert myfile.jpg myfile.ppm). ImageMagick supports a huge number of file formats including rather exotic ones.

For example C code for reading a PPM file just using <stdio.h> is

FILE *f = fopen("filename.ppm", "rb");
if (f != NULL && fscanf(f, "P6 %i %i 255%*c", &w, &h) == 2) {
    unsigned char *pixels = malloc(w*h*3);
    if (!pixels) {
        printf("Out of memory\n");
        exit(1);
    }
    if (((int)fread(pixels, 1, w*h*3, f)) == w*h*3) {
        fclose(f);
        printf("Image is %i x %i pixels\n", w, h);
        ... process image ...
    } else {
        printf("Image file truncated\n");
        exit(1);
    }
} else {
    printf("Invalid image file\n");
    exit(1);
}

PGM (grayscale) is identical with P5 instead of P6 and just has one byte per pixel instead of 3.

Note that formally PPM/PGM files are a bit more annoying to read (e.g. there could be comments in the header) but those generated by convert are clean (for example those produced by gimp are not, unfortunately).

6502
  • 112,025
  • 15
  • 165
  • 265