Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug? ofPixels allocates wrong amount of memory #5369

Closed
elliotwoods opened this issue Nov 26, 2016 · 2 comments
Closed

Bug? ofPixels allocates wrong amount of memory #5369

elliotwoods opened this issue Nov 26, 2016 · 2 comments

Comments

@elliotwoods
Copy link
Contributor

elliotwoods commented Nov 26, 2016

looking at:
https://github.com/openframeworks/openFrameworks/blob/master/libs/openFrameworks/graphics/ofPixels.cpp#L465

it seems that we always allocate the wrong amount of memory whenever PixelType != unsigned char since we're allocating an array of PixelType with array size being the desired size of memory in bytes (i.e. if PixelType is float, we're actually allocating 4* as much memory as we need).

perhaps instead we should be using pixels = new PixelType[pixelsSize];

@elliotwoods
Copy link
Contributor Author

for reference pasting the function here:

template<typename PixelType>
void ofPixels_<PixelType>::allocate(size_t w, size_t h, ofPixelFormat format){
	if (w == 0 || h == 0 || format == OF_PIXELS_UNKNOWN) {
		return;
	}

	size_t newSize = bytesFromPixelFormat(w,h,format);
	size_t oldSize = getTotalBytes();
	//we check if we are already allocated at the right size
	if(bAllocated && newSize==oldSize){
		pixelFormat = format;
		width = w;
		height = h;
		return; //we don't need to allocate
	}

	//we do need to allocate, clear the data
	clear();

	pixelFormat	= format;
	width 		= w;
	height 		= h;

	pixelsSize = newSize / sizeof(PixelType);

	pixels = new PixelType[newSize];
	bAllocated = true;
	pixelsOwner = true;
}

@arturoc
Copy link
Member

arturoc commented Nov 26, 2016

it should be new PixelType[pixelsSize] right? can you send a PR? otherwise i'll fix it later

@arturoc arturoc closed this as completed in 407ff95 Dec 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants