# \[Bug\] ip::horizontalFlip

**URL:** <https://discourse.libcinder.org/t/bug-ip-horizontalflip/480>\
**Category:** Developing Cinder\
**Created:** [January 3, 2017, 1:02pm UTC](https://discourse.libcinder.org/t/bug-ip-horizontalflip/480 "2017-01-03T13:02:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![okinp](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/okinp/32/161_2.png) [@okinp](https://discourse.libcinder.org/u/okinp)\
**Post date:** [January 3, 2017, 1:02pm UTC](https://discourse.libcinder.org/t/bug-ip-horizontalflip/480/1 "2017-01-03T13:02:00Z")

</div>

Hello, I just found a bug in ip::flipHorizontal.

Surface.hasAlpha() is used to decide the pixel byte increment.

In the case of BGRX hasAlpha returns false, but the increment should be 4 and not 3.

I am not sure where else hasAlpha is used to decide the byte increment and I am not sure if making hasAlpha return true for BGRX would break something else…

@andrewfb ?

I guess the following should fix the problem in the case of flipHorizontal but not anywhere else:

```
template<typename T>
void flipHorizontal( SurfaceT<T> *surface )
{
	const int32_t height = surface->getHeight();
	const int32_t width = surface->getWidth();
	const int32_t halfWidth = width / 2;
	
	const int32_t numBytes = surface->getPixelBytes();

		for( int32_t y = 0; y < height; ++y ) {
			T *rowPtr = surface->getData( ivec2( 0, y ) );
			for( int32_t x = 0; x < halfWidth; ++x ) {
				for( int c = 0; c < numBytes; ++c ) {
					T temp = rowPtr[x*numBytes+c];
					rowPtr[x*numBytes+c] = rowPtr[(width-x-1)*numBytes+c];
					rowPtr[(width-x-1)*numBytes+c] = temp;
				}
			}
		}
}
```

---

<div class="post-metadata">

**Author:** ![lithium](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/lithium/32/38_2.png) [@lithium](https://discourse.libcinder.org/u/lithium)\
**Post date:** [January 3, 2017, 2:06pm UTC](https://discourse.libcinder.org/t/bug-ip-horizontalflip/480/2 "2017-01-03T14:06:55Z")

</div>

I think you probably want to use `surface->getPixelInc()` rather than `surface->getPixelBytes()`

---

<div class="post-metadata">

**Author:** ![okinp](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/okinp/32/161_2.png) [@okinp](https://discourse.libcinder.org/u/okinp)\
**Post date:** [January 3, 2017, 2:09pm UTC](https://discourse.libcinder.org/t/bug-ip-horizontalflip/480/3 "2017-01-03T14:09:16Z")

</div>

True! thanks! I guess it worked for me because I was using a surface with 4 bytes per pixel…

Edit: hmm now that I think about it, I am not sure whats the difference between `getPixelInc()` and `getPixelBytes()` is it related to padding?

---

<div class="post-metadata">

**Author:** ![andrewfb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/andrewfb/32/2_2.png) [@andrewfb](https://discourse.libcinder.org/u/andrewfb)\
**Post date:** [January 3, 2017, 5:31pm UTC](https://discourse.libcinder.org/t/bug-ip-horizontalflip/480/4 "2017-01-03T17:31:08Z")

</div>

Sorry about that - I just pushed a fix [here](https://github.com/cinder/Cinder/commit/aeab54cfcae82bbfdb4756c3f2c9d26000c75788).

`getPixelInc()` is units of `T`, correlating with the number of channels per pixel, including a potentially unused alpha. whereas `getPixelBytes()` is actual bytes. So for a RGBA or RGBX `Surface32f`, pixel inc is 4, and pixelBytes is `4*sizeof(float)=16`. Just there for convenience.

---

<div class="post-metadata">

**Author:** ![okinp](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/okinp/32/161_2.png) [@okinp](https://discourse.libcinder.org/u/okinp)\
**Post date:** [January 4, 2017, 1:28pm UTC](https://discourse.libcinder.org/t/bug-ip-horizontalflip/480/5 "2017-01-04T13:28:24Z")

</div>

Thanks a lot for the fix and explanation Andrew!
