FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Help with PHP BD imaging functionality
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Help with PHP BD imaging functionality [message #182484] Wed, 07 August 2013 23:53 Go to next message
Ed Jay is currently offline  Ed Jay
Messages: 7
Registered: August 2013
Karma: 0
Junior Member
I'm trying to highlight a portion of a grayscale image, with the
highlighting color applied to pixels within a range of level only. I'm not
getting the result I seek.

While the grayscale values within the range possibly yield the highlight
color, the entire image is masked with the color from the last
imagecolorallocate.

My code follows, and the 'Process of interest' is the offending culprit.

How do I highlight the value-region of interest with the background of my
choosing?

$source_image = $_GET['source'];
$threshold = $_GET['threshold'];
$range = $_GET['range'];

$starting_img = imagecreatefromjpeg($source_image);
$img_data = getimagesize($source_image);
$width = $img_data[0];
$height = $img_data[1];
$final = imagecreatetruecolor($img_data[0],$img_data[1]);

$gray_threshold = 1*$threshold;
$gray_range = 1*$range;
$gray_top = ($gray_threshold + $gray_range);
if ($gray_top > 255) {$gray_top = 255;}
$highlight_color = imagecolorallocate($final,0,0,255);

//Process of interest:

for($x=0; $x<$width; $x++){
for($y=0; $y<$height; $y++){

$gray =(imagecolorat($final,$x,$y) >> 16) & 0xFF;
if (($gray > $gray_threshold) && ($gray < $gray_top)) {$gray =
$highlight_color;}
$gray2 = imagecolorallocate($final,$gray,$gray,$gray);
imagesetpixel($final, $x,$y,$gray2);

}
}

header('Content-Type: image/jpeg');
imagejpeg($final);
imagedestroy($starting_img);
imagedestroy($final);

Thanks in advance,
--
Ed Jay (remove 'M' to respond by email)
Re: Help with PHP BD imaging functionality [message #182486 is a reply to message #182484] Thu, 08 August 2013 07:58 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 08/08/13 00:53, Ed Jay wrote:
> I'm trying to highlight a portion of a grayscale image, with the
> highlighting color applied to pixels within a range of level only. I'm not
> getting the result I seek.
>
> While the grayscale values within the range possibly yield the highlight
> color, the entire image is masked with the color from the last
> imagecolorallocate.
>
> My code follows, and the 'Process of interest' is the offending culprit.
>
> How do I highlight the value-region of interest with the background of my
> choosing?

is the problem less that you don't know how to make GD do what you want,
then you don't actually know how to do what you want at all?

Is what you want covered by this?

http://www.php.net/manual/en/image.examples.merged-watermark.php

>
> $source_image = $_GET['source'];
> $threshold = $_GET['threshold'];
> $range = $_GET['range'];
>
> $starting_img = imagecreatefromjpeg($source_image);
> $img_data = getimagesize($source_image);
> $width = $img_data[0];
> $height = $img_data[1];
> $final = imagecreatetruecolor($img_data[0],$img_data[1]);
>
> $gray_threshold = 1*$threshold;
> $gray_range = 1*$range;
> $gray_top = ($gray_threshold + $gray_range);
> if ($gray_top > 255) {$gray_top = 255;}
> $highlight_color = imagecolorallocate($final,0,0,255);
>
> //Process of interest:
>
> for($x=0; $x<$width; $x++){
> for($y=0; $y<$height; $y++){
>
> $gray =(imagecolorat($final,$x,$y) >> 16) & 0xFF;
> if (($gray > $gray_threshold) && ($gray < $gray_top)) {$gray =
> $highlight_color;}
> $gray2 = imagecolorallocate($final,$gray,$gray,$gray);
> imagesetpixel($final, $x,$y,$gray2);
>
> }
> }
>
> header('Content-Type: image/jpeg');
> imagejpeg($final);
> imagedestroy($starting_img);
> imagedestroy($final);
>
> Thanks in advance,


--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.
Re: Help with PHP BD imaging functionality [message #182487 is a reply to message #182486] Thu, 08 August 2013 12:01 Go to previous messageGo to next message
Ed Jay is currently offline  Ed Jay
Messages: 7
Registered: August 2013
Karma: 0
Junior Member
The Natural Philosopher wrote:

> On 08/08/13 00:53, Ed Jay wrote:
>> I'm trying to highlight a portion of a grayscale image, with the
>> highlighting color applied to pixels within a range of level only. I'm not
>> getting the result I seek.
>>
>> While the grayscale values within the range possibly yield the highlight
>> color, the entire image is masked with the color from the last
>> imagecolorallocate.
>>
>> My code follows, and the 'Process of interest' is the offending culprit.
>>
>> How do I highlight the value-region of interest with the background of my
>> choosing?
>
> is the problem less that you don't know how to make GD do what you want,
> then you don't actually know how to do what you want at all?

A massive sprinkling of each...
>
> Is what you want covered by this?

I already considered it a possibility, but it intuitively didn't strike me
as the best method available. Is it the only method available to
accomplish my goal?
>
> http://www.php.net/manual/en/image.examples.merged-watermark.php
>
>> //Process of interest:
>>
>> for($x=0; $x<$width; $x++){
>> for($y=0; $y<$height; $y++){
>>
>> $gray =(imagecolorat($final,$x,$y) >> 16) & 0xFF;
>> if (($gray > $gray_threshold) && ($gray < $gray_top)) {$gray =
>> $highlight_color;}
>> $gray2 = imagecolorallocate($final,$gray,$gray,$gray);
>> imagesetpixel($final, $x,$y,$gray2);
>>
>> }
>> }
>>

--
Ed Jay (remove 'M' to reply by email)

Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
Re: Help with PHP BD imaging functionality [message #182488 is a reply to message #182484] Thu, 08 August 2013 12:55 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 8/7/2013 7:53 PM, Ed Jay wrote:
> I'm trying to highlight a portion of a grayscale image, with the
> highlighting color applied to pixels within a range of level only. I'm not
> getting the result I seek.
>
> While the grayscale values within the range possibly yield the highlight
> color, the entire image is masked with the color from the last
> imagecolorallocate.
>
> My code follows, and the 'Process of interest' is the offending culprit.
>
> How do I highlight the value-region of interest with the background of my
> choosing?
>
> $source_image = $_GET['source'];
> $threshold = $_GET['threshold'];
> $range = $_GET['range'];
>
> $starting_img = imagecreatefromjpeg($source_image);
> $img_data = getimagesize($source_image);
> $width = $img_data[0];
> $height = $img_data[1];
> $final = imagecreatetruecolor($img_data[0],$img_data[1]);
>
> $gray_threshold = 1*$threshold;
> $gray_range = 1*$range;
> $gray_top = ($gray_threshold + $gray_range);
> if ($gray_top > 255) {$gray_top = 255;}
> $highlight_color = imagecolorallocate($final,0,0,255);
>
> //Process of interest:
>
> for($x=0; $x<$width; $x++){
> for($y=0; $y<$height; $y++){
>
> $gray =(imagecolorat($final,$x,$y) >> 16) & 0xFF;
> if (($gray > $gray_threshold) && ($gray < $gray_top)) {$gray =
> $highlight_color;}
> $gray2 = imagecolorallocate($final,$gray,$gray,$gray);
> imagesetpixel($final, $x,$y,$gray2);
>
> }
> }
>
> header('Content-Type: image/jpeg');
> imagejpeg($final);
> imagedestroy($starting_img);
> imagedestroy($final);
>
> Thanks in advance,
>

Hi, Ed,

I think I understand what you're trying to do, but haven't actually run
the code (I don't happen to have any gray scale jpegs handy right now).

I see you create the $starting_image. However, I don't see where you
use it again. You don't copy the image into $final, so after

$final = imagecreatetruecolor($img_data[0],$img_data[1]);

$final is black.

I also don't see

Perhaps

$gray =(imagecolorat($final,$x,$y) >> 16) & 0xFF;

should be using $starting_image instead of $final?

Alternatively, why are you creating $final? Why not operate on
$starting_image directly?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Help with PHP BD imaging functionality [message #182489 is a reply to message #182487] Thu, 08 August 2013 13:12 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 08/08/13 13:01, Ed Jay wrote:
> The Natural Philosopher wrote:
>
>> is the problem less that you don't know how to make GD do what you
>> want, then you don't actually know how to do what you want at all?
> A massive sprinkling of each...
>> Is what you want covered by this?
> I already considered it a possibility, but it intuitively didn't strike me
> as the best method available. Is it the only method available to
> accomplish my goal?

I am still unclear as to what your goal actually is?
Beyond that you have an image that you want to partially modify.

"I'm trying to highlight a portion of a grayscale image, with the
highlighting color applied to pixels within a range of level only."

I am simply not clear as to what you MEAN by 'highlighting' or 'highlighting color' or 'range of level'
Are you essentially trying to slap a layer of virtual translucent ink over an area of a monochrome image?

so eg. blacker bits or whiter bits turn a 'whiter shade of pale'?


>> http://www.php.net/manual/en/image.examples.merged-watermark.php
>>
>>> //Process of interest:
>>>
>>> for($x=0; $x<$width; $x++){
>>> for($y=0; $y<$height; $y++){
>>>
>>> $gray =(imagecolorat($final,$x,$y) >> 16) & 0xFF;
That line at least is not extracting a gray value from an RGB pixmap.
Its extracting the red value component.
you need to extract R G and B separately - they SHOULD be the same, and
average them unless your are sure they image is truly monochrome.

eg
|$rgb = imagecolorat($im, 10, 15);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;|

$gray=$r+$b+$g)/3;

>>> if (($gray > $gray_threshold) && ($gray < $gray_top)) {$gray =
>>> $highlight_color;}
well that's weird. $highlight_colour is presumably not gray?
And not an R,G or B value either.
Its an index into a table..of RGB values.
>>> $gray2 = imagecolorallocate($final,$gray,$gray,$gray);
So this ^^^^ is a rubbish line
>>> imagesetpixel($final, $x,$y,$gray2);
And that wont do what you want either.
All you had to do was

imagesetpixel($final, $x,$y,$highlight_color);


As I understand it, the magical typecasting and loose typing of PHP has as usual made things harder, not easier.

The way GD works is that you allocate a color to a table of colors using RGB[alpha] values,. but the output of imagecolorallocate is not a 'color' in RGB terms, its a pointer to an _array_ of RGB[alpha] values! Worse still it only applies to a given IMAGE.


see http://www.php.net/manual/en/function.imagecolorsforindex.php

What that means is that if you e.g. want to apply any given arbitrary color value to an image, you need 2GB table color values!!!

>>> }
>>> }
>>>

I am not going to test it, but this is something like what I *THINK* you
need
Assuming the original image is actually monochrome, and you have used
imagecolorallocate($final, $r,$g,$b) to set up $higlight_color, and
$grey_threshold, and $grey_top are actually numbers set between 0 and 255..

for($x=0; $x<$width; $x++)
{
for($y=0; $y<$height; $y++)
{
$gray =(imagecolorat($final,$x,$y) & 0xFF); // why not use blue instead?
if (($gray > $gray_threshold) && ($gray < $gray_top)) // pixel in defined gresay scale range
imagesetpixel($final, $x,$y,$highlight_color); // simply set it to highlight color.

}
}
However this won't actually result in a translucent highlighting, it will simply be 'for a range of greyscale, replace with a single solid color'

One of the reasons I don't actually like GD that much is that easy things are made quite hard... and hard things are made almost impossible.

Which is why I use C and pngwriter to write graphs when its more than a simple task.

e.g. http://pngwriter.sourceforge.net/manual-en.php
---------------------------------------------------

*Plot Blend*

Plots the colour given by red, green blue, but blended with the existing
pixel value at that position. opacity is a double that goes from 0.0 to
1.0. For example, 0.0 will not change the pixel at all, and 1.0 will
plot the given colour.Anything in between will be a blend of both pixel
levels. *Please note*: This is neither alpha channel nor PNG
transparency chunk support. This merely blends the plotted pixels.

void plot_blend(int x, int y, double opacity, int red, int green, int blue);

void plot_blend(int x, int y, double opacity,double red, double green,
double blue);

------------------------------------------------------------------

which allows you to 'tint' a relevant pixel with a directly specified color.

And also direct access to RGB values of any pixel. In both directions.

GS's 'cleverness' in avoiding have to send RGB and alpha values every time, and makes it impossible to do so without 'allocating a color' to the image for every conscievable hue you need, makes it pants for difficult stuff.

I haven't ever looked at Imagick, beyond a moments heartstopping when I looked at all the stuff it did that I really didnt need.And ghow much I would need to learn to avoid using it.

so will pass comment on that.

Sigh.

--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.
Re: Help with PHP BD imaging functionality [message #182500 is a reply to message #182489] Thu, 08 August 2013 23:13 Go to previous message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
The Natural Philosopher wrote:

> What that means is that if you e.g. want to apply any given arbitrary
> color value to an image, you need 2GB table color values!!!

AFAIK that's not necessarily true. If the image is created by
imagecreatetruecolor() (or, assumably, if it is created from a
file/string which contains a true color image, opposed to a palletized
image) the "index" to the color table is actually the RGB value:

>>> $im = imagecreatetruecolor(100, 100)
>>> $c = imagecolorallocate($im, 128, 128, 128)
>>> dechex($c)
'808080'

This suggests the assumption that GD doesn't build a palette in this case.

--
Christoph M. Becker
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: GUI designer in html
Next Topic: Dictionary Words
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Dec 19 12:05:53 GMT 2024

Total time taken to generate the page: 0.02354 seconds