Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124384 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id C187D1A00B7 for ; Thu, 11 Jul 2024 09:48:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1720691382; bh=TzbEYIHvZeUqp6dmfoDnqrbOrWgi1mL+vODXul3Mt7Q=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=nBp8ftXMrqmzPm5kmrqZ8zJUGO6mbV1QE9Y3yAAKeF6NIU5QrnyYPXK9lOyZLZW4y rO3uCTMqBCiDz5Eo6iKZCssxZ+jW2QcboyUaFvqGKj471vc4Q/V7gM591k/h3ddDe9 sB47q+D1xJozRcCyEPSFO6IJ1fHNunnIE5j9RQftxE1FmkPeqTQjRNM4UKjd3QU/WP JD3jlQxBuGwNybxP+5KXxITJTFZiONrHTca7HNXTDbPX5NOUjZOWjaw5ye+F7oR8FW PemQmndzUf0+uE0FGhtcPWWuyB0dmnpV1Foh4mGeKTqyguXwC6ZbTS1l1vsEjOLWYM XL785VVzmeRGg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E2714180061 for ; Thu, 11 Jul 2024 09:49:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 11 Jul 2024 09:49:38 +0000 (UTC) Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2c8517aab46so579835a91.1 for ; Thu, 11 Jul 2024 02:48:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720691291; x=1721296091; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=TzbEYIHvZeUqp6dmfoDnqrbOrWgi1mL+vODXul3Mt7Q=; b=MjPDZxkDJMObP8eyPtTEuPxFfD3k3GGg5ufQg9v2s3XDksMTgeNIyxMOQgNnL7btw4 CqdLOH+rmNP/epGnEeOPU4kyqhBggbMVTXQdb68pJkvW0mhP9pwcqfuMDhXp8Pl6+aIM Qraps4XuQBIPMxnrrysp+W0Uy5AoOgoSh/RKQ2FTZeDa5z6oqDN+mdeqQQvUX3NlKldi KC93oPAUiunO/I5kqIobz5F0N1BEJh/4eux/yftlgvxl/GU0QUSZi1+lAk2d8QKB6nky 99kLVLbw6LkuOyeQof/CT8AU2NLQH0+9QxVHl2gsGU7e0Lo0VLKzyXcYe1H22kxODGDb Z4Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720691291; x=1721296091; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TzbEYIHvZeUqp6dmfoDnqrbOrWgi1mL+vODXul3Mt7Q=; b=gmgXmrtg4QXW0OGuNYL91SbV3U6AKoyYaj1ZbXxg7xFbtUjqlaK77slpZ92ugPsQVE Z3IRJHFJijAKvPSV3U/JCPWHvLXwWS35IFPh3qO7XZV/yo10fpjmda9pP+gWaayEeqy5 fipTIW+rzRqF8qjBsU6a+JWaf3WevRl2WHVuTcfJwCizySOK8jviFp3Dg30t2AGugudC yo2MLOZufP8uXX7Y7GXlmhdFRgthQ8p/Bhbn5+x6Z6QazO5t/Otk0Ddctz3jSgVa8i+P wLeD9QbJOaupm9tPFFGtBjGACnaqkkNFndTp/xpKsjQOLP5YEitHZkSKkYOpp3arN6v1 UJrw== X-Gm-Message-State: AOJu0YwPOZt9VN6d+comILC/aNeS7znwcsCRlbfCIkrE1d3kXx8W7m4W xsYtXA4y988ywVxCJ0bOGRm9rLVhQ5BsZFVxIDWVS+CP4MYXmG2o4Fv3f0xglY8trWT/c5YmvYm G0CBhew1o2CuEz3ew0St4GF2ttaYi6+TN X-Google-Smtp-Source: AGHT+IFsFZysDnV1xUQPpwbej+oslvJZ0ssgIJYEH0qbhENJgeylEIN3ZckXJerupVC4NpPgYVlK0dR/6fYAbfwdMnw= X-Received: by 2002:a17:90a:bf0f:b0:2c9:84f9:a321 with SMTP id 98e67ed59e1d1-2ca35c6894amr7606569a91.23.1720691290761; Thu, 11 Jul 2024 02:48:10 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 11 Jul 2024 16:47:58 +0700 Message-ID: Subject: Re: [PHP-DEV] ext/gd new imagematch function (was: adding imagecompare) To: giovanni@giacobbi.net Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="00000000000027753d061cf5a900" From: pierre.php@gmail.com (Pierre Joye) --00000000000027753d061cf5a900 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable hi, as I mentioned in the PR, this is an old function which we did not touch for bc reasons. I also linked a better image comparison function used for the gd tests suite. It covers exaxf match which is what it seems you are aiming to get. Additionally that function also return an image with the difference (the diff images contains the channels differences). A key, more tricky, but nevertheless important compared is a perceptual difference, but that can be done additionally. best, Pierre On Thu, Jul 11, 2024, 4:39=E2=80=AFPM Giovanni Giacobbi wrote: > The recent PR #14877 [1] proposes to add the imagecompare gd function tha= t > mimics the gdImageCompare function from libgd. I always thought that a > pixel-by-pixel matching function for two images was a big missing feature > in PHP, as the corresponding userland implementation is really, REALLY sl= ow. > > The problem with gdImageCompare is that it checks several aspects of the > two images, and fails on its core purpose. The behavior is better seen in > its source code [2] rather than explained. I see the following problems: > - The gdImageCompare is kind of buggy, as it completely disregards the > alpha channel. As a user, I would expect that two images that have a pixe= l > rgba(255, 0, 0, 100%) and one rgba(255, 0, 0, 50%) are considered > different, but the current implementation reports them as identical. > - Checking for bitmasks is cumbersome, and it requires 9 new constants in > the global namespace (the IMG_CMP_*), which are nearly useless. Most use > cases will just want to check for this: imagecompare($im1, $im2) & > IMG_CMP_IMAGE. > - The checks in gdImageCompare are also a bit arbitrary, why compare the > interlace flag but not the palette order? Why check the number of colors = in > the palette if they are not even used in the image? > - If a user is interested in the other checks, they can all be implemente= d > in userland with existing functions: imageinterlace, imagecolortransparen= t, > imageistruecolor, imagesx, imagesy, imagecolorstotal. > > I believe PHP should only offer required building blocks and leave to the > libraries the implementation of more structured functionality. My proposa= l > is a completely different function called imagematch() that solves the > exact problem of the pixel-by-pixel matching, with the added value of > optionally matching portions of the images, using the following signature= : > > function imagematch(GdImage $image1, GdImage $image2, int $x1 =3D 0, int = $y1 > =3D 0, int $x2 =3D 0, int $y2 =3D 0, ?int $width =3D null, ?int $height = =3D null): > bool {} > > I already drafted PR #14914 [3] with the first two parameters. There is > not an equivalent libgd implementation, so the implementation is entirely > on the php side, but as you can see it's quite trivial. Even with the > extended functionality of matching portions of the images it would be jus= t > a few extra lines of code, so not a big maintenance burden. > > If an RFC is required, I'm happy to redact it after the initial feedback > round. > > [1] https://github.com/php/php-src/pull/14877 > [2] > https://github.com/php/php-src/blob/5586d0c7de00acbfb217e1c6321da918b96ef= 960/ext/gd/libgd/gd.c#L2834 > [3] https://github.com/php/php-src/pull/14914 > > --00000000000027753d061cf5a900 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

hi,

as I mentioned in the PR, this is an old function which we d= id not touch for bc reasons.

I also linked a better image comparison function used for th= e gd tests suite. It covers exaxf match which is what it seems you are aimi= ng to get.

Additionally that function also return an image with the dif= ference (the diff images contains the channels differences).

A key, more tricky, but nevertheless important compared is a= perceptual difference, but that can be done additionally.

best,
Pierre


On Thu= , Jul 11, 2024, 4:39=E2=80=AFPM Giovanni Giacobbi <giovanni@giacobbi.net> wrote:
The recent PR #14877 [1] proposes to= add the imagecompare=C2=A0gd function that mimics the gdImageCompare funct= ion from libgd. I always thought that a pixel-by-pixel matching function fo= r two images was a big missing feature in PHP, as the corresponding userlan= d implementation is really, REALLY slow.

The problem wit= h gdImageCompare is that it checks several aspects of the two images, and f= ails on its core purpose. The behavior is better seen in its source code [2= ] rather than explained. I see the following problems:
- The gdIm= ageCompare is kind of buggy, as it completely disregards the alpha channel.= As a user, I would expect that two images that have a pixel rgba(255, 0, 0= , 100%) and one rgba(255, 0, 0, 50%) are considered different, but the curr= ent implementation reports them as identical.
- Checking for bitm= asks is cumbersome, and it requires 9 new constants in the global namespace= (the IMG_CMP_*), which are nearly useless. Most use cases will just want t= o check for this: imagecompare($im1, $im2) & IMG_CMP_IMAGE.
-= The checks in gdImageCompare are also a bit arbitrary, why compare the int= erlace flag but not the palette order? Why check the number of colors in th= e palette if they are not even used in the image?
- If a user is = interested in the=C2=A0other checks, they can all be implemented in userlan= d with existing functions: imageinterlace, imagecolortransparent, imageistr= uecolor, imagesx, imagesy, imagecolorstotal.

= I believe PHP should only offer required building blocks and leave to the l= ibraries the implementation of more structured functionality. My proposal i= s a completely different function called imagematch() that solves the exact= problem of the pixel-by-pixel matching, with the added value of optionally= matching portions of the images, using the following signature:
=
function imagematch(GdImage $image1, GdImage $image2, int $x= 1 =3D 0, int $y1 =3D 0, int $x2 =3D 0, int $y2 =3D 0, ?int $width =3D null,= ?int $height =3D null): bool {}

I already drafted= PR #14914 [3] with the first two parameters. There is not an equivalent li= bgd implementation, so the implementation is entirely on the php side, but = as you can see it's quite trivial. Even with the extended functionality= of matching portions of the images it would be just a few extra lines of c= ode, so not a big maintenance burden.

If an RFC is= required, I'm happy to redact it after the initial feedback round.

--00000000000027753d061cf5a900--