473,796 Members | 2,618 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 #1
49 1737
ok, throw the class away.

| // REMEMBER TO USE REFERENCE POINTER ONTO $image OBJECT TO ENSURE
| "static" CHANGE TO OBJECT AND NOT TO INSTANCE

this 'explanation' is totally off the charts as to being wrong. follow that
up with the magical 'global $sections' and you've got icing for the cake.
not to mention this class will leak like a fucking screen used to pan gold.
there is color allocation for every pixel, there is a count/sizeof operation
on this allocation inside a loop. next, it uses color averaging to derive
gray. last but certainly not least, this class totally ignores the fact that
images have alpha layers. do i dare mention that this class doesn't clean up
after itself? it relies on YOU to destroy the images IT creates among other
niceties.

for all the 'performance' reasons the brain-dead author gives for creating
this beast, he shows far more neglect in his code...which by default will
run more slowly that imagecopymergeg ray() since it isn't part of php native
code! plus, as you've already seen, it fucking blows up.

i'd look up imagecopymergeg ray() on php.net and see how the commenters there
are performing grayscale operations. they at least have the math
approximations for calculating gray a better match than ol' phil here.

btw imho, any class that simply has procedural code (simply a set of
similar/related functions) should be singletons. this is a perfect example.
i'd have named the class 'imaging' and given an interface called
getGrayscale...

$igg = imaging::getGra yscale($image);

all the height/width shit can be discovered via the one arg $image and
nothing else in that class needs to be stored by the object instance. that
may be just me, but as far as i am concerned, i'm already waaaay past strike
three here.
Apr 16 '07 #2
How does "for all the 'performance' reasons the brain-dead author
gives for creating
this beast" help anyone? Thank you for utterly useless ad-hominem
information!

On Apr 15, 11:18 pm, "Steve" <no....@example .comwrote:
ok, throw the class away.

| // REMEMBER TO USE REFERENCE POINTER ONTO $image OBJECT TO ENSURE
| "static" CHANGE TO OBJECT AND NOT TO INSTANCE

this 'explanation' is totally off the charts as to being wrong. follow that
up with the magical 'global $sections' and you've got icing for the cake.
not to mention this class will leak like a fucking screen used to pan gold.
there is color allocation for every pixel, there is a count/sizeof operation
on this allocation inside a loop. next, it uses color averaging to derive
gray. last but certainly not least, this class totally ignores the fact that
images have alpha layers. do i dare mention that this class doesn't clean up
after itself? it relies on YOU to destroy the images IT creates among other
niceties.

for all the 'performance' reasons the brain-dead author gives for creating
this beast, he shows far more neglect in his code...which by default will
run more slowly that imagecopymergeg ray() since it isn't part of php native
code! plus, as you've already seen, it fucking blows up.

i'd look up imagecopymergeg ray() on php.net and see how the commenters there
are performing grayscale operations. they at least have the math
approximations for calculating gray a better match than ol' phil here.

btw imho, any class that simply has procedural code (simply a set of
similar/related functions) should be singletons. this is a perfect example.
i'd have named the class 'imaging' and given an interface called
getGrayscale...

$igg = imaging::getGra yscale($image);

all the height/width shit can be discovered via the one arg $image and
nothing else in that class needs to be stored by the object instance. that
may be just me, but as far as i am concerned, i'm already waaaay past strike
three here.

Apr 16 '07 #3
On Apr 15, 11:33 pm, "comp.lang. php" <phillip.s.pow. ..@gmail.com>
wrote:
How does "for all the 'performance' reasons the brain-dead author
gives for creating
this beast" help anyone? Thank you for utterly useless ad-hominem
information!

On Apr 15, 11:18 pm, "Steve" <no....@example .comwrote:
ok, throw the class away.
| // REMEMBER TO USE REFERENCE POINTER ONTO $image OBJECT TO ENSURE
| "static" CHANGE TO OBJECT AND NOT TO INSTANCE
this 'explanation' is totally off the charts as to being wrong. follow that
up with the magical 'global $sections' and you've got icing for the cake.
not to mention this class will leak like a fucking screen used to pan gold.
there is color allocation for every pixel, there is a count/sizeof operation
on this allocation inside a loop. next, it uses color averaging to derive
gray. last but certainly not least, this class totally ignores the fact that
images have alpha layers. do i dare mention that this class doesn't clean up
after itself? it relies on YOU to destroy the images IT creates among other
niceties.
for all the 'performance' reasons the brain-dead author gives for creating
this beast, he shows far more neglect in his code...which by default will
run more slowly that imagecopymergeg ray() since it isn't part of php native
code! plus, as you've already seen, it fucking blows up.
i'd look up imagecopymergeg ray() on php.net and see how the commenters there
are performing grayscale operations. they at least have the math
approximations for calculating gray a better match than ol' phil here.
btw imho, any class that simply has procedural code (simply a set of
similar/related functions) should be singletons. this is a perfect example.
i'd have named the class 'imaging' and given an interface called
getGrayscale...
$igg = imaging::getGra yscale($image);
all the height/width shit can be discovered via the one arg $image and
nothing else in that class needs to be stored by the object instance. that
may be just me, but as far as i am concerned, i'm already waaaay past strike
three here.
You struck out. imagecopymergeg ray() fails as well, produces a total
non-image with segfault just as much as anything I have done. So much
for your brilliant cocky strategy. Try again.

Apr 16 '07 #4

"comp.lang. php" <ph************ **@gmail.comwro te in message
news:11******** **************@ q75g2000hsh.goo glegroups.com.. .
| How does "for all the 'performance' reasons the brain-dead author
| gives for creating
| this beast" help anyone? Thank you for utterly useless ad-hominem
| information!

well <as he scratches his head>, it kind of warns you NOT to use the code,
as in solving your current problem will not solve the others you will have
when you employ this class. but i though that was apparent in the examples i
gave to support my opinion.

i'm now assuming you are said brain-dead author since you glossed over the
litany of things-gone-wrong in the code that i pointed out and have
immediately championed a defensive attitude.
Apr 16 '07 #5
| You struck out. imagecopymergeg ray() fails as well, produces a total
| non-image with segfault just as much as anything I have done. So much
| for your brilliant cocky strategy. Try again.

well as EVERYTHING i've pointed out as WRONG with the class REMAINS VALID,
i'd say i've just stepped up to the plate. if you wanna play hard-ball, you
need to be a better pitcher and quit reff-ing since the strike-outs belong
to the class creator and NOT me.

you've merely pitched BALL ONE.

try again.
Apr 16 '07 #6
On Apr 15, 11:49 pm, "Steve" <no....@example .comwrote:
"comp.lang. php" <phillip.s.pow. ..@gmail.comwro te in message

news:11******** **************@ q75g2000hsh.goo glegroups.com.. .
| How does "for all the 'performance' reasons the brain-dead author
| gives for creating
| this beast" help anyone? Thank you for utterly useless ad-hominem
| information!

well <as he scratches his head>, it kind of warns you NOT to use the code,
as in solving your current problem will not solve the others you will have
when you employ this class. but i though that was apparent in the examples i
gave to support my opinion.

i'm now assuming you are said brain-dead author since you glossed over the
litany of things-gone-wrong in the code that i pointed out and have
immediately championed a defensive attitude.
So I assume you feel calling someone "brain-dead" is your way of
assuming they are receptive to your solutions, whatever they may be,
which, as I can see, do not yet exist by your means.

Apr 16 '07 #7
On Apr 15, 11:52 pm, "Steve" <no....@example .comwrote:
| You struck out. imagecopymergeg ray() fails as well, produces a total
| non-image with segfault just as much as anything I have done. So much
| for your brilliant cocky strategy. Try again.

well as EVERYTHING i've pointed out as WRONG with the class REMAINS VALID,
i'd say i've just stepped up to the plate. if you wanna play hard-ball, you
need to be a better pitcher and quit reff-ing since the strike-outs belong
to the class creator and NOT me.

you've merely pitched BALL ONE.

try again.
You haven't even tried in the first place, or, do you even want to? If
not, please do not waste bandwith by showing your technological
bravado and try to help someone out with a problem with grayscaling
images. How would YOU do it?

Apr 16 '07 #8
On Apr 16, 12:05 am, "comp.lang. php" <phillip.s.pow. ..@gmail.com>
wrote:
On Apr 15, 11:52 pm, "Steve" <no....@example .comwrote:
| You struck out. imagecopymergeg ray() fails as well, produces a total
| non-image with segfault just as much as anything I have done. So much
| for your brilliant cocky strategy. Try again.
well as EVERYTHING i've pointed out as WRONG with the class REMAINS VALID,
i'd say i've just stepped up to the plate. if you wanna play hard-ball, you
need to be a better pitcher and quit reff-ing since the strike-outs belong
to the class creator and NOT me.
you've merely pitched BALL ONE.
try again.
Original thread that provided solution:
http://coding.derkeiler.com/Archive/...4-02/0459.html
>
You haven't even tried in the first place, or, do you even want to? If
not, please do not waste bandwith by showing your technological
bravado and try to help someone out with a problem with grayscaling
images. How would YOU do it?

Apr 16 '07 #9

"comp.lang. php" <ph************ **@gmail.comwro te in message
news:11******** **************@ o5g2000hsb.goog legroups.com...
| On Apr 15, 11:52 pm, "Steve" <no....@example .comwrote:
| | You struck out. imagecopymergeg ray() fails as well, produces a total
| | non-image with segfault just as much as anything I have done. So much
| | for your brilliant cocky strategy. Try again.
| >
| well as EVERYTHING i've pointed out as WRONG with the class REMAINS
VALID,
| i'd say i've just stepped up to the plate. if you wanna play hard-ball,
you
| need to be a better pitcher and quit reff-ing since the strike-outs
belong
| to the class creator and NOT me.
| >
| you've merely pitched BALL ONE.
| >
| try again.
|
| You haven't even tried in the first place, or, do you even want to? If
| not, please do not waste bandwith by showing your technological
| bravado and try to help someone out with a problem with grayscaling
| images. How would YOU do it?

BALL TWO.

i said look at the commentor's code and see how they grayscale. i have an
imaging class. it is my code. i'm not giving it to anyone as it brings me a
great deal of consulting work. you present me with code, and i'll tell you
what's wrong with it. that's the way it goes here. and did you not catch
that that is EXACTLY what i did here.

try again.
Apr 16 '07 #10

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

Similar topics

8
2064
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
2333
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
2120
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
11065
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
2682
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
1769
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
2356
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
9685
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
10459
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
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7553
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
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
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2928
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.