Grayscaling images causes mangled image + segfault

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    #1

    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

  • Steve

    #2
    Re: Grayscaling images causes mangled image + segfault

    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.


    Comment

    • comp.lang.php

      #3
      Re: Grayscaling images causes mangled image + segfault

      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.

      Comment

      • comp.lang.php

        #4
        Re: Grayscaling images causes mangled image + segfault

        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.

        Comment

        • Steve

          #5
          Re: Grayscaling images causes mangled image + segfault


          "comp.lang. php" <phillip.s.powe ll@gmail.comwro te in message
          news:1176694395 .805515.273040@ 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.


          Comment

          • Steve

            #6
            Re: Grayscaling images causes mangled image + segfault

            | 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.


            Comment

            • comp.lang.php

              #7
              Re: Grayscaling images causes mangled image + segfault

              On Apr 15, 11:49 pm, "Steve" <no....@example .comwrote:
              "comp.lang. php" <phillip.s.pow. ..@gmail.comwro te in message
              >
              news:1176694395 .805515.273040@ 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.

              Comment

              • comp.lang.php

                #8
                Re: Grayscaling images causes mangled image + segfault

                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?

                Comment

                • comp.lang.php

                  #9
                  Re: Grayscaling images causes mangled image + segfault

                  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:

                  >
                  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?

                  Comment

                  • Steve

                    #10
                    Re: Grayscaling images causes mangled image + segfault


                    "comp.lang. php" <phillip.s.powe ll@gmail.comwro te in message
                    news:1176696319 .043792.127810@ 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.


                    Comment

                    • Steve

                      #11
                      Re: Grayscaling images causes mangled image + segfault


                      "comp.lang. php" <phillip.s.powe ll@gmail.comwro te in message
                      news:1176698279 .300873.186820@ o5g2000hsb.goog legroups.com...
                      | 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

                      i don't care if gawd herself wrote it, it is STILL shit. glad to see though
                      that PHIL makes an EXACT COPY of SOMEONE ELSE'S code, then puts HIMSELF AS
                      THE AUTHOR when he (YOU) present it here. no wonder the commented
                      explanation shows complete lack of understanding of how references work. you
                      don't understand the code you copied.

                      BALL THREE.

                      | >
                      | 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?

                      btw, it's MY FUCKING BANDWIDTH...go buy your own. until you pay for MINE,
                      YOU HAVE NO SAY IN HOW I USE IT.


                      Comment

                      • Steve

                        #12
                        Re: Grayscaling images causes mangled image + segfault


                        "comp.lang. php" <phillip.s.powe ll@gmail.comwro te in message
                        news:1176696259 .542761.158820@ n76g2000hsh.goo glegroups.com.. .
                        | On Apr 15, 11:49 pm, "Steve" <no....@example .comwrote:
                        | "comp.lang. php" <phillip.s.pow. ..@gmail.comwro te in message
                        | >
                        | news:1176694395 .805515.273040@ 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.

                        you post as 'comp.lang.php' not as PHIL (author of the shitty class).
                        knowing this know, both of us, does the code change somehow such that it
                        isn't shitty...enough where i should justifiably refrain from calling it
                        shitty? having seen that this is NOT PHIL'S code in the first place (thanks
                        for outting yourself with the link), i can only be MORE correct in the use
                        of "brain-dead" since you try to pass it off as your own.

                        BALL FOUR...i can take my base now.

                        keep racking 'em up, PHIL.


                        Comment

                        • Steve

                          #13
                          Re: Grayscaling images causes mangled image + segfault

                          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";

                          ahhh, so when you plagerized the functionality, you copied it
                          incorrectly...i see.

                          ROFLMFAO !!!


                          Comment

                          • Steve

                            #14
                            Re: Grayscaling images causes mangled image + segfault


                            "Steve" <no.one@example .comwrote in message
                            news:m_DUh.171$ fK7.124@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. ;^)


                            Comment

                            • comp.lang.php

                              #15
                              Re: Grayscaling images causes mangled image + segfault

                              On Apr 16, 1:41 am, "Steve" <no....@example .comwrote:
                              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";
                              >
                              ahhh, so when you plagerized the functionality, you copied it
                              incorrectly...i see.
                              >
                              ROFLMFAO !!!
                              It's "genius", BTW.

                              Comment

                              Working...