# How to load a picture by GPU?

**URL:** <https://forums.developer.nvidia.com/t/how-to-load-a-picture-by-gpu/3577>\
**Category:** CUDA Programming and Performance\
**Created:** [May 2, 2008, 1:08pm UTC](https://forums.developer.nvidia.com/t/how-to-load-a-picture-by-gpu/3577 "2008-05-02T13:08:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zythum](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@zythum](https://forums.developer.nvidia.com/u/zythum)\
**Post date:** [May 2, 2008, 1:08pm UTC](https://forums.developer.nvidia.com/t/how-to-load-a-picture-by-gpu/3577/1 "2008-05-02T13:08:41Z")

</div>

Hello,

i wanted to know if it’s possible to load an image (JPEG, TIFF or whatever) from a folder by CUDA?

Can we do arrays with more than one dimension?

Thank you

zythum

---

<div class="post-metadata">

**Author:** ![seibert](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/seibert/32/520688_2.png) [@seibert](https://forums.developer.nvidia.com/u/seibert)\
**Post date:** [May 2, 2008, 2:11pm UTC](https://forums.developer.nvidia.com/t/how-to-load-a-picture-by-gpu/3577/2 "2008-05-02T14:11:52Z")

</div>

To load a 2D image onto the GPU, you have to open the file and load the pixel information into an array on the CPU however you would normally do that (nothing to do with CUDA). Then you use cudaMallocArray() to allocate memory on the GPU for the image, and finally cudaMemcpyToArray() the CPU memory to the GPU memory.

Take a look at the simpleTexture example in the CUDA SDK to see how this works. Note that this example uses textures, which are very useful for most 2D image processing algorithms.

---

<div class="post-metadata">

**Author:** ![kristleifur](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kristleifur/32/12055_2.png) [@kristleifur](https://forums.developer.nvidia.com/u/kristleifur)\
**Post date:** [May 2, 2008, 2:46pm UTC](https://forums.developer.nvidia.com/t/how-to-load-a-picture-by-gpu/3577/3 "2008-05-02T14:46:49Z")

</div>

I’ve used ImageMagick to try to open various formats and get float / int / short / char arrays, and put them on the device.

---

<div class="post-metadata">

**Author:** ![zythum](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@zythum](https://forums.developer.nvidia.com/u/zythum)\
**Post date:** [May 2, 2008, 2:53pm UTC](https://forums.developer.nvidia.com/t/how-to-load-a-picture-by-gpu/3577/4 "2008-05-02T14:53:38Z")

</div>

thank you.

i’m a newbie in programing, so could you show me how to code for loading a picture?

---

<div class="post-metadata">

**Author:** ![kristleifur](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kristleifur/32/12055_2.png) [@kristleifur](https://forums.developer.nvidia.com/u/kristleifur)\
**Post date:** [May 4, 2008, 10:14am UTC](https://forums.developer.nvidia.com/t/how-to-load-a-picture-by-gpu/3577/5 "2008-05-04T10:14:35Z")

</div>

> [@](#):
>
> thank you.
> 
> i’m a newbie in programing, so could you show me how to code for loading a picture?
> 
> [snapback]371629[/snapback]

Hi,

sure. Here’s some code I have that loads an ImageMagick image into a 1D host array. This converts the image to grayscale, and converts the data to float as well. It’s not hard to modify this for int / short / whatever, to use a 2D array, or to read color info. I’m not exactly sure how transferring the 2D array to the device would work, but that’s another problem.

```auto
#include "Magick++.h" // include ImageMagick C++ bindings

#include <string>

#include <iostream>

using namespace Magick;

using namespace std;

int main(int argc, char *argv[])

{

    InitializeMagick(*argv); // mandatory once per program run

   string imageName(argv[1]);

   Magick::Image ImageIN;

   ImageIN.read(imageName);

   int height = ImageIN.rows();

    int width = ImageIN.columns();

   int size = height*width;

    cout << "Image width: " << width << endl;

    cout << "Image height: " << height << endl;

   float imgData;

    Magick::PixelPacket* pp = ImageIN.getPixels(0,0,width,height);

   for(int i = 0; i < size; i++)

    {

        imgData[i] = (float) pp[i].red / 65535.0f;

        // for float values, divide by USHORT_MAX because my ImageMagick  version uses unsigned shorts internally

    }

   //TODO: cudaMemcpy(imgData_DEVICE, imgData, size * sizeof(float), cudaMemcpyHostToDevice);

}

```

---

<div class="post-metadata">

**Author:** ![zythum](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@zythum](https://forums.developer.nvidia.com/u/zythum)\
**Post date:** [May 6, 2008, 6:52am UTC](https://forums.developer.nvidia.com/t/how-to-load-a-picture-by-gpu/3577/6 "2008-05-06T06:52:56Z")

</div>

Ok thank you

---

<div class="post-metadata">

**Author:** ![kristleifur](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kristleifur/32/12055_2.png) [@kristleifur](https://forums.developer.nvidia.com/u/kristleifur)\
**Post date:** [May 6, 2008, 3:11pm UTC](https://forums.developer.nvidia.com/t/how-to-load-a-picture-by-gpu/3577/7 "2008-05-06T15:11:04Z")

</div>

> [@](#):
>
> Ok thank you
> 
> [snapback]373160[/snapback]

There’s a couple of library-oriented things missing from my description.

Assuming you’re using the Makefile / common.mk stuff from the NVIDIA sample projects. You’ll need **-lMagick++** in your Makefile, like so:

```auto
LIB             := -lglut -lMagick++

```

Try this, it might be enough.

Actually, to get this to run myself a couple months ago, I did something not so good: I copied and hacked the NVIDIA\_CUDA\_SDK/common/common.mk makefile. I changed a line:

```auto
LIB       := -L$(CUDA_INSTALL_PATH)/lib -L$(LIBDIR) -L$(COMMONDIR)/lib <b>/usr/lib/libMagick++.so</b>

```

I’m not really sure hacking common.mk is necessary.

I hope someone posts a cleaner solution for linking this stuff.

---

<div class="post-metadata">

**Author:** ![romses](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@romses](https://forums.developer.nvidia.com/u/romses)\
**Post date:** [May 6, 2008, 4:38pm UTC](https://forums.developer.nvidia.com/t/how-to-load-a-picture-by-gpu/3577/8 "2008-05-06T16:38:15Z")

</div>

Hi

I tried to use Imagemagick as in the example above.

I copied all the sourcefiles from projects/template into a new folder, and edited the sources.

After this, i replaced all the sources in template\_gold.cpp with the example above.

I cleard the template.cu.

Now, i have the problem, that i got some compiler errors!

```auto
template_gold.cpp:8: warning: unused parameter 'argc'

obj/release/template_gold.cpp_o: In function `main':

template_gold.cpp:(.text+0x90): undefined reference to `Magick::InitializeMagick(char const*)'

template_gold.cpp:(.text+0xb4): undefined reference to `Magick::Image::Image()'

template_gold.cpp:(.text+0xc3): undefined reference to `Magick::Image::read(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'

template_gold.cpp:(.text+0xce): undefined reference to `Magick::Image::constImage() const'

template_gold.cpp:(.text+0xdf): undefined reference to `Magick::Image::constImage() const'

template_gold.cpp:(.text+0x172): undefined reference to `Magick::Image::getPixels(int, int, unsigned int, unsigned int)'

template_gold.cpp:(.text+0x1b1): undefined reference to `Magick::Image::~Image()'

template_gold.cpp:(.text+0x206): undefined reference to `Magick::Image::~Image()'

collect2: ld returned 1 exit status

make: *** [../../bin/linux/release/template] Error 1

```

Can anybody help me?

Greets

Christopher
