I have a cool problem. I'm trying to highlight exactly 50% of this turkey's mass. In practice it will not be 50%, but if I can get 50%, I can figure the rest out.
My current idea is this: Scan in every pixel to find out the mass of the turkey, in pixels. Then, when I want 50% of the turkey, highlight pixels from left to right until I've highlighted 50% of the pixels.
Does anyone have any better ideas? This is the slowest, brute-force idea that requires looping over every pixel once to count the pixels and again to highlight 50% of them.
Note that dividing the picture vertically would not leave 50% of the turkey, as the turkey's body is a lot meatier than it's neck.
roasted spatchcock turkey on the roasting rack. Credit: Kelly Cline. To "spatchcock" means to remove a bird's backbone so that it will lay flat while cooking. This method (also known as butterflying) ensures even cooking, juicier meat, and a quicker cooking time.
You say the percentage will change, but I'm going to go on a limb here and assume the turkey itself won't.
So you don't need to loop twice. You only need to do it once, map the data into some form of structure that you can reference, then only refer to it each time you need to highlight to figure out how far you need to go.
For instance, make an associative array of <percentage of mass (integer out of a hundred) => vertical column number (int)>
That way, when you need to shade x percent, you only need to loop over cached_data[percent_to_highlight] columns of pixels, shading them all, and then break out of the loop entirely once you reach it.
For even more performance, have two pictures - one fully shaded plus your normal one. The associative array should map percentage => byte_offset
For each image you wish to generate, simply memcpy or whatever PHP's equivalent of a fast, ranged copy from 0 -> byte_offset
of the shaded picture over the non-shaded and save. No more looping. You can't get faster than this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With