Skip to content

Conversation

@dimitre
Copy link
Member

@dimitre dimitre commented Dec 8, 2024

This one will need the tests updated to reflect what pixelSize represents now.
closes #8225

@dimitre dimitre mentioned this pull request Dec 8, 2024
@dimitre dimitre marked this pull request as ready for review December 15, 2024 01:11
@dimitre
Copy link
Member Author

dimitre commented Jan 10, 2025

This one will need tests updated because pixelSize represented a different thing in the past.
I think we are going to a more complete and correct way of treating pixels
ideas? suggestions?

@ofTheo
Copy link
Member

ofTheo commented Feb 28, 2025

Don't we want a simple revert of:
f7522b9#diff-7d727932e9b85218273507defcbf4b9e9c9a300c10d62867e1459b2f167b45f8L399

Looking at this PR I am not sure if it's actually reverting and might be changing behavior even more.

I'll note in the lines what I mean.

cc @prisonerjohn

height = h;

pixelsSize = bytesFromPixelFormat(w,h,_pixelFormat);
pixelsSize = w * h;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pixels size used to be:
pixelsSize = bytesFromPixelFormat(w,h,_pixelFormat) / sizeof(PixelType);

bytesFromPixelFormat is effectively: w * h * 24 / 8 ( for RGB )
so: newSize = w * h * 3; for RGB

size_t ofPixels_<PixelType>::bytesFromPixelFormat(size_t w, size_t h, ofPixelFormat format){
	return w * h * pixelBitsFromPixelFormat(format) / 8;  <-- pixelBitsFromPixelFormat for rgb is 24 
}

a simpler fix would be:

pixelsSize = w*h * getNumChannels()

But maybe safer to first revert the commit where the regression was introduced and then do a PR with the bit depth support after?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of the problem (historical ofPixels problem) is we have two different units
one is PixelType (it can be char, float, etc)

and the other are formats that doesn't have an uniform number of bytes per channel like RGB565 (5 + 6 + 5 total 16 bits) or NV12 (12 bits per channel)

but the array that stores pixel information will be always a PixelType (so we consider an "uncompressed" pixel type, to be worked on shaders).

we can go one way or another.

if we support all the images format I suggest changing the Pixels array to uint8_t instead of PixelType and allocate everything following "bytesPerPixel"

Or keep everything as it is, and comment out all image types that doesn't follow PixelType bit depth.
makes sense?

height = h;

pixelsSize = newSize;
pixelsSize = w * h;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again good to note that pixelsSize is not w*h but w*h * getNumChannels();
pixelsSize is what ofPixels::size() returns and if you look here, it needs to represent the total number of bytes not just w*h.

void ofPixels_<PixelType>::copyFrom(const ofPixels_<SrcType> & mom){
	if(mom.isAllocated()){
		allocate(mom.getWidth(),mom.getHeight(),mom.getNumChannels());

		const float srcMax = ( (sizeof(SrcType) == sizeof(float) ) ? 1.f : std::numeric_limits<SrcType>::max() );
		const float dstMax = ( (sizeof(PixelType) == sizeof(float) ) ? 1.f : std::numeric_limits<PixelType>::max() );
		const float factor = dstMax / srcMax;

		if(sizeof(SrcType) == sizeof(float)) {
			// coming from float we need a special case to clamp the values
			for(size_t i = 0; i < mom.size(); i++){
				pixels[i] = ofClamp(mom[i], 0, 1) * factor;
			}
		} else{
			// everything else is a straight scaling
			for(size_t i = 0; i < mom.size(); i++){
				pixels[i] = mom[i] * factor;
			}
		}
	```

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose we gave more thought and try to solve incongruences that always existed in ofPixels instead of reverting last PR. When you say the total number of bytes it is not true, it is the total number of channels, each of them can be one byte (as it always supported) or more.

I'll take a look soon at this PR. so size() is back to original behavior

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look again and see the difference. it is now not based in bytes for pixels allocation.

@dimitre dimitre changed the title ofPixels pixelSize reflection number of pixels instead of bytes ofPixels memory allocation size fix Mar 1, 2025
@dimitre
Copy link
Member Author

dimitre commented Mar 1, 2025

I reverted size to the behavior it always was, and changed the PR title.
this now handles well new Pixels allocation that is based on "total number of channels" instead of total number of bytes, which is only true if we have a 8 bit PixelType (ofPixels)

@ofTheo ofTheo merged commit 984c9c6 into openframeworks:master Mar 1, 2025
15 checks passed
@ofTheo
Copy link
Member

ofTheo commented Mar 1, 2025

Awesome thanks @dimitre !

@dimitre dimitre deleted the px2 branch March 1, 2025 17:45
danoli3 added a commit that referenced this pull request Mar 13, 2025
* commit '5630d2034d835e81225a225355e97f4144b21179': (446 commits)
  Restore target build dir behavior for Xcode. Closes #8321 (#8340)
  of script updates (#8339)
  _OBJC_CLASS_$_AVFoundationVideoPlayer fix (#8337)
  poco mk (#8336)
  relative locations rabbit (#8335)
  fix(ofParameter): better default init() handling of non-comparable/non-copyable objects and containers (#8325)
  path to string conversion on Windows fix. (#8333)
  iOS template add ofxiOS group ref (#8330)
  Xcode iOS Target Project Fixes (#8328)
  chore(iOS): remove iosNativeARCExample (#8324)
  ofxGui fix for apple targets (#8320)
  assimp ios addon_config (#8319)
  chore(iOS): remove GLKit example (#8317)
  fix(iOS): adjustments to layout of Location example + deprecations updates (#8311)
  fix(iOS): update to CoreLocation authorization request (#8309)
  remove boost from android. (#8307)
  Remove boost from linux scripts, download_libs (#8306)
  ofPixels memory allocation size fix (#8226)
  remove boost references from CoreOF.xcconfig and config.*.default.mk for macOS and osx. (#8304)
  Fix to json scripts (#8301)
  ...

# Conflicts:
#	.gitignore
#	libs/openFrameworks/gl/ofGLProgrammableRenderer.cpp
#	libs/openFrameworks/gl/ofGLUtils.h
#	libs/openFrameworks/gl/ofShader.cpp
#	libs/openFrameworks/gl/ofTexture.h
#	libs/openFrameworks/graphics/ofTrueTypeFont.cpp
#	libs/openFrameworks/graphics/ofTrueTypeFont.h
#	libs/openFrameworks/sound/ofAVEngineSoundPlayer.h
#	libs/openFrameworks/sound/ofSoundBaseTypes.cpp
#	libs/openFrameworks/sound/ofSoundBaseTypes.h
#	libs/openFrameworks/sound/ofSoundStream.cpp
#	libs/openFrameworks/utils/ofConstants.h
#	libs/openFrameworks/utils/ofThread.h
#	libs/openFrameworks/utils/ofURLFileLoader.cpp
#	libs/openFrameworks/utils/ofUtils.h
#	libs/openFrameworks/utils/ofXml.h
#	libs/openFrameworksCompiled/project/android/build.gradle
#	libs/openFrameworksCompiled/project/android/common-functions.gradle
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

Successfully merging this pull request may close these issues.

ofPixels copy broken

2 participants