# resize with char texture Image returns skewed

**URL:** <https://forums.developer.nvidia.com/t/resize-with-char-texture-image-returns-skewed/2683>\
**Category:** CUDA Programming and Performance\
**Created:** [February 18, 2008, 11:57pm UTC](https://forums.developer.nvidia.com/t/resize-with-char-texture-image-returns-skewed/2683 "2008-02-18T23:57:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lord\_Binky](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Lord\_Binky](https://forums.developer.nvidia.com/u/Lord_Binky)\
**Post date:** [February 18, 2008, 11:57pm UTC](https://forums.developer.nvidia.com/t/resize-with-char-texture-image-returns-skewed/2683/1 "2008-02-18T23:57:40Z")

</div>

I’m trying to copy the data from a OpenCV IPLimage which is formatted to unsigned 8bit characters. the imagedata is a 1D array of characters that i pass into a resize function in cuda.

I have a setup where i convert directly into a 1D float array and copy that float array into the gpu and this works perfectly. The problem is I’m working with extremely large pictures that will cause the CPU to run out of memory if i try to expand the character array into a float array before copying to the gpu. But if i copy the character array directly into the gpu, the resulting image is skewed with it’s right edge running diagnal through the image. I figure i’m missing something so small because i’m so close to it working correctly and getting rid of this size limitation i have.

The resize kernel is pretty much the sample image resize code :P The actual code is shown below, where the rows and columns of the original large image are passed and the float array on the gpu that i want to store my resulting small image is g\_odata.

The resulting skewed images look like this… what the hell is wrong? beats me…

[attachment=5639:attachment]

```auto
texture<char, 2, cudaReadModeNormalizedFloat> tex;

__global__ void

resizeKernel( float* g_odata, int cols, int rows) 

{	

	//uses cuda's internal interpolation, change texture's tex.normalized to true for use.

    unsigned int x = blockIdx.x*blockDim.x + threadIdx.x;

    unsigned int y = blockIdx.y*blockDim.y + threadIdx.y;

    float u = (float)x / (float) cols;

    float v = (float)y / (float) rows;  

    g_odata[y*cols + x] = tex2D(tex, u, v);

}

```

The texture Desc is setup as either one of these ways with no changes in the output.

```auto
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(8, 0, 0, 0, cudaChannelFormatKindFloat);

or

cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<unsigned char>();

```

In the .cu file i set the texture up as such. Changeing the address mode between clamp and wrap does not change the resized output image. Neither does the filtermode. Normalized needs to be true because of the kernel setup.

```auto
   tex.addressMode[0] = cudaAddressModeWrap;

    tex.addressMode[1] = cudaAddressModeWrap;

    tex.filterMode = cudaFilterModeLinear;

    tex.normalized = 1;

    

```

Hostimage is the 1D char array and i copy it to the GPU with this code. width and height are the width and height values returned from the opencv wrapper.

```auto
size = height*width*sizeof(char);

CUDA_SAFE_CALL( cudaMallocArray( &cu_array, &tex.channelDesc, width, height )); 

CUDA_SAFE_CALL( cudaMemcpyToArray( cu_array, 0, 0, hostimage, size, cudaMemcpyHostToDevice));

CUDA_SAFE_CALL( cudaBindTextureToArray( tex, cu_array, channelDesc));

dim3 dimBlock(16, 6, 1);

dim3 dimGrid(iDivUp(r_width,dimBlock.x),iDivUp(r_height,dimBlock.y), 1);

resizeKernel<<< dimGrid, dimBlock, 0 >>>( d_data, r_width, r_height);

CUDA_SAFE_CALL(cudaMemcpy(r_image,d_data, r_size, cudaMemcpyDeviceToHost) );

```

and that’s about it, but i have no idea what explains the skewing and i’m sure it’s super simple. If anyone could help me with this it’d be really appreciated since i can’t find much about the opencv char to gpu texture and letting it convert nicely beside you should do it :| .  
[white\_out.bmp](https://forums.developer.nvidia.com/uploads/short-url/heBvBXmgwXeqt3KrTsQSBoIJ8Kp.bmp) (85.1 KB)

---

<div class="post-metadata">

**Author:** ![Lord\_Binky](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Lord\_Binky](https://forums.developer.nvidia.com/u/Lord_Binky)\
**Post date:** [February 22, 2008, 5:01pm UTC](https://forums.developer.nvidia.com/t/resize-with-char-texture-image-returns-skewed/2683/2 "2008-02-22T17:01:23Z")

</div>

ugh, so i after looking at the image knowing i was getting an extra pixel every row, i go back through the openCV IPL and there’s a widthstep value that is equivalent of cuda’s pitch. anyone know offhand if a 2dmemcopy through the 1D char array would work nicely since i’ll have a pitch and width and height of the array?

---

<div class="post-metadata">

**Author:** ![papag62](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@papag62](https://forums.developer.nvidia.com/u/papag62)\
**Post date:** [February 22, 2008, 6:59pm UTC](https://forums.developer.nvidia.com/t/resize-with-char-texture-image-returns-skewed/2683/3 "2008-02-22T18:59:42Z")

</div>

I think your u and v in the texture reference must be accessing locations outside of your image size. Try changing to:

```auto
tex.addressMode[0] = cudaAddressModeClamp;

   tex.addressMode[1] = cudaAddressModeClamp;

```

If you have a weird waterfall effect then you are definitely accessing outside the image. I would then check your block and grid size creation to make sure your getting what you are expecting.

---

<div class="post-metadata">

**Author:** ![jph4599](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jph4599](https://forums.developer.nvidia.com/u/jph4599)\
**Post date:** [June 10, 2009, 4:08pm UTC](https://forums.developer.nvidia.com/t/resize-with-char-texture-image-returns-skewed/2683/4 "2009-06-10T16:08:12Z")

</div>

For anyone that might come here looking for some hints as I did…

I managed to get the unsigned char array from an OpenCV IPLimage working with a 2D texture. As Lord Binky suggested, OpenCV pads the image array so it is divisible by 4. The pitch is stored in the widthStep field of the IPLimage struct.

Below are pieces of the code I used:

Read input image:

```auto
unsigned char *testImageData;

	IplImage *image; // This is image pointer

	image = cvLoadImage(testImage.c_str(), CV_LOAD_IMAGE_GRAYSCALE); // load the image

	if( image == 0 )

	{

		printf( "ERROR: Unable to open test image \"%s\", aborting\n", testImage.c_str());

		exit(1);

	}

	testImageData = (unsigned char*) image->imageData;

```

Copy data to the device:

```auto
cudaArray *d_testImage;

		

		// Create channel descriptor for 2D CUDA Array

		cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<unsigned char>();

		// Allocate array on device

		cudaMallocArray(&d_testImage, &channelDesc, image->width, image->height);

		CHECK_ERROR( "cudaMalloc d_testImage failed" );

		cudaMemcpy2DToArray(d_testImage, 0, 0, testImageData, sizeof(unsigned char) * image->widthStep, sizeof(unsigned char) * image->width, image->height, cudaMemcpyHostToDevice);

		CHECK_ERROR( "cudaMemcpy2DToArray d_testImage failed" );

```

Texture reference in kernel:

```auto
texture<unsigned char, 2, cudaReadModeElementType> testImageTex;

```

You can then grab data from the texture using tex2D.

Now I just wonder what kind of performance unsigned char fetching will have compared to ints or floats…

---

<div class="post-metadata">

**Author:** ![7rack](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@7rack](https://forums.developer.nvidia.com/u/7rack)\
**Post date:** [May 20, 2012, 1:29am UTC](https://forums.developer.nvidia.com/t/resize-with-char-texture-image-returns-skewed/2683/5 "2012-05-20T01:29:40Z")

</div>

> [@](#):
>
> For anyone that might come here looking for some hints as I did…
> 
> I managed to get the unsigned char array from an OpenCV IPLimage working with a 2D texture. As Lord Binky suggested, OpenCV pads the image array so it is divisible by 4. The pitch is stored in the widthStep field of the IPLimage struct.
> 
> Below are pieces of the code I used:
> 
> Read input image:
> 
> ```auto
> unsigned char *testImageData;
> 
> IplImage *image; // This is image pointer
> 
> image = cvLoadImage(testImage.c_str(), CV_LOAD_IMAGE_GRAYSCALE); // load the image
> 
> if( image == 0 )
> 
> {
> 
> printf( "ERROR: Unable to open test image \"%s\", aborting\n", testImage.c_str());
> 
> exit(1);
> 
> }
> 
> testImageData = (unsigned char*) image->imageData;
> 
> ```
> 
> Copy data to the device:
> 
> ```auto
> cudaArray *d_testImage;
> 
> 		
> 
> // Create channel descriptor for 2D CUDA Array
> 
> cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<unsigned char>();
> 
> // Allocate array on device
> 
> cudaMallocArray(&d_testImage, &channelDesc, image->width, image->height);
> 
> CHECK_ERROR( "cudaMalloc d_testImage failed" );
> 
> cudaMemcpy2DToArray(d_testImage, 0, 0, testImageData, sizeof(unsigned char) * image->widthStep, sizeof(unsigned char) * image->width, image->height, cudaMemcpyHostToDevice);
> 
> CHECK_ERROR( "cudaMemcpy2DToArray d_testImage failed" );
> 
> ```
> 
> Texture reference in kernel:
> 
> ```auto
> texture<unsigned char, 2, cudaReadModeElementType> testImageTex;
> 
> ```
> 
> You can then grab data from the texture using tex2D.
> 
> Now I just wonder what kind of performance unsigned char fetching will have compared to ints or floats…

thank you for your sharing ,but i did like the above the result was not correct,My topic,need your help ,thank you advance :)

---

<div class="post-metadata">

**Author:** ![faimage](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@faimage](https://forums.developer.nvidia.com/u/faimage)\
**Post date:** [June 13, 2013, 8:44am UTC](https://forums.developer.nvidia.com/t/resize-with-char-texture-image-returns-skewed/2683/6 "2013-06-13T08:44:27Z")

</div>

with pixels, width and height you will be able to [resize images c#](http://www.rasteredge.com/how-to/csharp-imaging/resize-image/) in a way you want. and of course the image may not get skewed.
