473,805 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Grayscaling images causes mangled image + segfault

[php]
/**
* Class for grayscaling image
*
* @author Phil Powell
* @version 1.2.1
* @package IMAGE_CATALOG:: IMAGE
*/
class ImageGrayscaleG enerator extends ImageResizeComp onents {

/
*----------------------------------------------------------------------------------------------------------------------------------------------------------
This class exists due to the rather poor performance of the
imagecopymergeg ray() command
Borrowing code snippet from http://us3.php.net/manual/en/functio...ymergegray.php
first line comment
Will perform actual grayscaling of image one pixel at a time.
-----------------------------------------------------------------------------------------------------------------------------------------------------------
*/
// REMEMBER TO USE REFERENCE POINTER ONTO $image OBJECT TO ENSURE
"static" CHANGE TO OBJECT AND NOT TO INSTANCE

/**
* Constructor. Set all properties dynamically
*
* @access public
* @param resource $image (reference)
* @param resource $newImage (reference)
* @param int $image_width
* @param int $image_height
*/
function ImageGrayscaleG enerator(&$imag e, &$newImage, $image_width,
$image_height) { // CONSTRUCTOR
global $section;
foreach (array($section , 'newImage', "${section}_wid th", "$
{section}_heigh t") as $val) $this->$val =& ${$val};
}

/**
* Make the image grayscale
*
* @access protected
*/
function makeGray() { // VOID METHOD
global $section;
for ($i = 0; $i <= 255; $i++) $colorNDX[$i] =
@imagecolorallo cate($this->newImage, $i, $i, $i);

if (is_array($colo rNDX) && @sizeof($colorN DX) 0) {
for ($y = 0; $y < $this->{$section . '_height'}; $y++) {
for ($x = 0; $x < $this->{$section . '_width'}; $x++) {
$ndx = @imagecolorat($ this->image, $x, $y);
$ndxColorArray = @imagecolorsfor index($this->image, $ndx);
$avg = floor(($ndxColo rArray['red'] + $ndxColorArray['green'] +
$ndxColorArray['blue']) / 3);
@imagesetpixel( $this->newImage, $x, $y, $colorNDX[$avg]);
}
}
}
}
}

if ($this->isSuccessful && $image && $willGrayscale) { // GRAYSCALE
IMAGE
$igg =& new ImageGrayscaleG enerator($image , $newImage,
$image_width, $image_height);
$igg->makeGray();
$igg = null;
}
[/php]

Whenever I run this class to grayscale an image, I wind up with the
resulting image being horribly mangled to the point of its original
format completely irreparable, furthermore, I wind up with an Apache
segfault and having to reboot the server to correct the problem.

Um, why?

Phil

Apr 14 '07
49 1738
comp.lang.php wrote:
<snip>
Thank you, Jerry, very nice to hear this! Unfortunately no I never
did solve the problem; I am still not capable of grayscaling an image:

/**
* Make the image grayscale
*
* @access protected
*/
function makeGray() { // VOID METHOD
global $section;
for ($i = 0; $i <= 255; $i++) $colorNDX[$i] =
@imagecolorallo cate($this->$section, $i, $i, $i);

for ($y = 0; $y < $this->{$section . '_height'}; $y++) {
for ($x = 0; $x < $this->{$section . '_width'}; $x++) {
$ndx = @imagecolorat($ this->$section, $x, $y);
$red = ($ndx >16) & 0xFF;
$green = ($ndx >8) & 0xFF;
$blue = $ndx & 0xFF;
//$ndxColorArray = @imagecolorsfor index($this->$section, $ndx);
//$avg = floor(($ndxColo rArray['red'] + $ndxColorArray['green'] +
$ndxColorArray['blue']) / 3);
$col = $red * 0.299 + $green * 0.587 + $blue * 0.114;
@imagesetpixel( $this->$section, $x, $y, $colorNDX[$col]);
}
}

//@imagecopy($thi s->newImage, $this->$section, 0, 0, 0, 0,
// $this->{$section . '_width'}, $this->{$section .
'_height'});

}

No image is ever produced in spite of the code produced.
>For your next post try alt.anal.orific es. It matches you perfectly.

--
============== ====
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attg lobal.net
============== ====

I'm sorry, I thought you found a fix for it.

I looked at the code, but unfortunately I'm that great with the image
functions.

A couple of things I do see. First of all, you shouldn't use a global
($section). Anything you need you should pass to the function or the
class itself.

Also, in your constructor you're saving things in $this->$val, which is
incorrect in two counts. It should be $this->val, and you never defined
$val in your class. No idea what the code would do in this case.

And I'm not sure just what you're trying to accomplish in the foreach()
loop in your constructor. If you're trying to use this to create
variables in your class, that's the hard way to go about it, and the
values you're defining are temporary and will disappear at the end of
the function. Rather, you should just define them as members of the
class (probably private).

Also, I'm not sure what's in $section, but I think you're trying to get
the width and height of the image.

As for your makeGray() function -

You should first define $colorNDX as an array, i.e.

$colorNDX = array();

Your if statement following the for loop is unnecessary - you already
know it's an array of 256 elements because you just created it. And the
following nested for statements are referencing variables which don't
exist any more.

These are just off the top of my head, and may not be correct. But I
did find our class to be pretty hard to understand.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 16 '07 #21
| Also, in your constructor you're saving things in $this->$val, which is
| incorrect in two counts. It should be $this->val, and you never defined
| $val in your class. No idea what the code would do in this case.

as i pointed out, it seems an attempt to dynamically create the class
interface names. nothing else makes sense.

| And I'm not sure just what you're trying to accomplish in the foreach()
| loop in your constructor. If you're trying to use this to create
| variables in your class, that's the hard way to go about it, and the
| values you're defining are temporary and will disappear at the end of
| the function. Rather, you should just define them as members of the
| class (probably private).

hmmm...sounds familiar. :)

| Also, I'm not sure what's in $section, but I think you're trying to get
| the width and height of the image.

so we agree that global $section is magic. i suppose you didn't read my
initial response.

| As for your makeGray() function -
|
| You should first define $colorNDX as an array, i.e.
|
| $colorNDX = array();

that only matters if he's displaying notices. otherwise he is dynamically
filling $colorNDX as an array type with allocated colors - he just doesn't
check to see if the color has *already* been allocated and does this in a
loop. but then again, i already pointed out these two flaws.

| Your if statement following the for loop is unnecessary - you already
| know it's an array of 256 elements because you just created it. And the
| following nested for statements are referencing variables which don't
| exist any more.

another reason why i said the author was 'brain-dead' thinking that this
version of code has better performance potential than the built-in php
function he was aiming to supplant!

| These are just off the top of my head, and may not be correct. But I
| did find our class to be pretty hard to understand.

so jerry, you found that my initial response to phil did not add anything to
the post. i find that interesting since you restated exactly 3 things there
i've already covered. and, off-the-top-of-MY-head i stated even more
potential problems with the op's eye-sore coding.

if it's the language i used that was offensive to you, then damn the
language...else , why nullify your opinion by restating what i've already
said!

as it is jerry, i did not know the op was in fact the author of this class.
perhaps i would have been more gentil and guarded with my flurry of insults
had i known before hand. i just find it hard for you to agree that i've not
added anything to resolving the problem at hand when you are mirroring my
findings...simp ly because you don't like the tone of my delivery.

oh well.

later.
Apr 16 '07 #22
Steve wrote:
<snip garbage>

I found your tone to be arrogant and condescending, whether he was the
author of the class or not.

And as I said before - alt.anal.orific es is thataway ===>
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 16 '07 #23

"Jerry Stuckle" <js*******@attg lobal.netwrote in message
news:sd******** *************** *******@comcast .com...
| Steve wrote:
| <snip garbage>
|
| I found your tone to be arrogant and condescending, whether he was the
| author of the class or not.
|
| And as I said before - alt.anal.orific es is thataway ===>

i couldn't care less how you find my tone. you have no response to justify
'i have to agree with...added nothing to...' whilst you're almost
word-for-wording my review of the code...other than, i didn't like your
tone. i can then duely note that you are fully qualified to tell me
directions to alt.anal.orific es since your head is apparently buried up
yours.
Apr 16 '07 #24
| And as I said before - alt.anal.orific es is thataway ===>

btw, since that's where your cranium resides, do i take this as an
invitation to come over and visit?
Apr 16 '07 #25

"Steve" <no****@example .comwrote in message
news:1k******** ******@newsfe02 .lga...
>
"comp.lang. php" <ph************ **@gmail.comwro te in message
news:11******** *************@l 77g2000hsb.goog legroups.com...
| On Apr 16, 1:47 am, "Steve" <no....@example .comwrote:
| "Steve" <no....@example .comwrote in message
| >
| news:m_******** *******@newsfe0 2.lga...
| | hey genious, here's an RBI:
| |
| | foreach (
| | array(
| | $section ,
| | 'newImage' ,
| | "${section}_wid th" ,
| | "${section}_hei ght"
| | )
| | as $val
| | ){ $this->$val =& ${$val}; }
| |
| | what do you intend to do with this lil' gem, eh? you do realize
you'll
get
| | the same results by just:
| |
| | $this->val =& "${section}_hei ght";
| >
| make that:
| >
| $this->$val =& ${"${section}_h eight"};
| >
| but you get my point. if you want to make the values into variables,
do
| it...but don't confuse the issue of what $this->$val is. this is right
up
| there with magic numbers! gotta love coders like you. ;^)
|
| Coders like me can spell "genius".

double-entandre dimwit
[snip]

Methinks you mean "double-entendre" (sp. again - ROFL)
Apr 16 '07 #26
On Apr 16, 4:37 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
Steve wrote:

<snip garbage>

I found your tone to be arrogant and condescending, whether he was the
author of the class or not.

And as I said before - alt.anal.orific es is thataway ===>
Thanx Jerry for your help! Turns out that the simple process of having
class properties that I forgot to include fixed everything (*ahem*)

Phil
>
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attgl obal.net
=============== ===

Apr 17 '07 #27

"comp.lang. php" <ph************ **@gmail.comwro te in message
news:11******** *************@y 80g2000hsf.goog legroups.com...
| On Apr 16, 4:37 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
| Steve wrote:
| >
| <snip garbage>
| >
| I found your tone to be arrogant and condescending, whether he was the
| author of the class or not.
| >
| And as I said before - alt.anal.orific es is thataway ===>
|
| Thanx Jerry for your help! Turns out that the simple process of having
| class properties that I forgot to include fixed everything (*ahem*)

now on to fix the other problems noted, right? of course not. have you
benchmarked your performance against imagecopygraysc ale? of course not. will
you? probably. will you post the dire results back here...of course not.

keep slinging that code phil.
Apr 17 '07 #28
On Apr 17, 1:26 am, "Steve" <no....@example .comwrote:
"comp.lang. php" <phillip.s.pow. ..@gmail.comwro te in message

news:11******** *************@y 80g2000hsf.goog legroups.com...
| On Apr 16, 4:37 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote: | Steve wrote:

| >
| <snip garbage>
| >
| I found your tone to be arrogant and condescending, whether he was the
| author of the class or not.
| >
| And as I said before - alt.anal.orific es is thataway ===>
|
| Thanx Jerry for your help! Turns out that the simple process of having
| class properties that I forgot to include fixed everything (*ahem*)

now on to fix the other problems noted, right? of course not. have you
benchmarked your performance against imagecopygraysc ale? of course not. will
you? probably. will you post the dire results back here...of course not.

keep slinging that code phil.
Thanx, I will! And I appreciate all of your help!
Apr 17 '07 #29
"comp.lang. php" <ph************ **@gmail.comwro te in message
news:11******** **************@ n59g2000hsh.goo glegroups.com.. .
| On Apr 17, 1:26 am, "Steve" <no....@example .comwrote:
| "comp.lang. php" <phillip.s.pow. ..@gmail.comwro te in message
| >
| news:11******** *************@y 80g2000hsf.goog legroups.com...
| | On Apr 16, 4:37 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote: | >
Steve wrote:
| >
| | >
| | <snip garbage>
| | >
| | I found your tone to be arrogant and condescending, whether he was
the
| | author of the class or not.
| | >
| | And as I said before - alt.anal.orific es is thataway ===>
| |
| | Thanx Jerry for your help! Turns out that the simple process of having
| | class properties that I forgot to include fixed everything (*ahem*)
| >
| now on to fix the other problems noted, right? of course not. have you
| benchmarked your performance against imagecopygraysc ale? of course not.
will
| you? probably. will you post the dire results back here...of course not.
| >
| keep slinging that code phil.
|
| Thanx, I will! And I appreciate all of your help!

hey, no problem. you are single-handedly increasing my paygrade. companies
that have a go with you and those of your prowess soon learn that quality
has a much higher price tag. so, i thank you very much. :)
Apr 17 '07 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2066
by: Phil Powell | last post by:
I borrowed this code from a source: for($a=0;$a<imagecolorstotal ($image_id);$a++) { $color = imageColorsForIndex($image_id,$i); $R=.299 * ($color)+ .587 * ($color)+ .114 * ($color); $G=.299 * ($color)+ .587 * ($color)+ .114 *
22
3140
by: Fabian | last post by:
var preload1 = new Image(); preload1.src = "/pic/yay.gif"; var preload2 = new Image(); preload2.src = "/pic/nay.gif"; The above is meant to preload image files, yes? Problem is, it doesnt seem to be doing so in practice. Any idea where Im going wrong? Could it be that things work differnetly when in an attached .js file? -- --
0
2335
by: styler | last post by:
I am having difficulty with the page located here: http://tinyurl.com/2zwa9 I am creating a number of image sets (some left-, some right-aligned; an example of the right-aligned is shown the first figure below) in which the text accompanying the image sets flows down around them. The image sets have a single image on top and two immediately below. To create the effect, I contain the image on top in a DIV and float it, and the bottom...
2
2122
by: billrdio | last post by:
I am trying to make a JavaScript animation of real-time images - i.e. images that will periodically change on the server. The problem I am having is that the JavaScript animation I have created is always using the images from the cache, even though I have set the HTTP response header on the server (via .htaccess) so that the browser should validate the image in the cache against the image on the server for freshness. I have tested the...
7
11066
by: bbxrider | last post by:
there is some property that i think is mostly used for images used as backrounds that fades them, so you can get the idea of the image but not have it obscuring the text thats on it, its not trasparency i have been going nuts trying to find it, i'm sure i've seen it described somewhere on the w3c site but embarrassed to say can't find it again hopefully somebody knows this or maybe i'm going crazy
3
2683
by: yawnmoth | last post by:
http://us2.php.net/manual/en/function.imagegif.php#20425 The above URL suggests that it's possible to sorta embed images within an HTML document so that they don't have to be loaded via a seperate HTTP request. The idea intrigues me, although I can't seem to get it working. Is the idea sound or is that link just kinda bogus? Here's my (failed) attempt: http://www.geocities.com/terra1024/inline_gif.html
12
2849
by: comp.lang.php | last post by:
index.php: // STUFF // STEP 1: imagecreatetruecolor ONLY IF GD 2.0+ SUPPORTED AND FOUND if ($this->isSuccessful && !$hasMogrified && $image && !$newImage && function_exists('imagecreatetruecolor') && preg_match('/2\.0/i', $this->gd_info_array)) { $newImage = @imagecreatetruecolor($configArray, $configArray);
2
1772
by: utahwrx | last post by:
I currently have a Javascript application that randomizes about 200 images. The problem is that the images preload, which causes the entire site to not come up until all the images are loaded. I'd like to find a Javascript application that can load images as they are randomly chosen. In addition, I'm trying to figure out how to not display the same image more than once; or at least until after the 200 have been displayed, then create a new...
11
2358
by: eholz1 | last post by:
Hello PHP group, I am using some php code to check the size of images, and then resize or determine new dimension for the image. GD seems quite slow. It takes about 5 seconds (plus or minus) to calulate dimension for 7 jpeg images. I have a 700mhz processor (Pentium III, remember those??)! with almost a gb of memory. Is that the way GD is??? Here is a snippet of the dode I use: I pass
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10103
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.