Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126191 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 3B85F1A00BD for ; Tue, 31 Dec 2024 12:42:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1735648755; bh=Z9PlrTAccRU5DFLZE8MvFfvmHrUM1TvZmCe4uqMx4Jo=; h=Date:Subject:To:References:From:In-Reply-To:From; b=QTXMV717uTv8ICChmmK2H7nTcn1x9ImenMqwPmZ3un5scO2f21ufF9jZckG0fZWcX SZoUab+OPpXxW2kisoGgqNmmZ6NMeUHkgbIQlgZ39e4M5fG0wSdS7elItQ+ggE2M2+ vzFATUYjG6JGGCMFsovoAkgUoO3C1Hrwe5vScM+vtTLSZj7F6LYPadv6jaWHnYNQx0 XnrjvbTK5KqKGQTwqliBZYTtlKOSE3/TokLxn2da+BtQpCPBlyXnRc3WtdYGc8bIv8 QFP6ySYj2SN3bpSzSUKtbKJ1+QBHP76i1lLoAJiI5b8IAjCvBKPwY9f6fyUDDvt9/f qnlwaLpDMZ6OA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 85871180039 for ; Tue, 31 Dec 2024 12:39:14 +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.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mout.gmx.net (mout.gmx.net [212.227.15.19]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 31 Dec 2024 12:39:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de; s=s31663417; t=1735648930; x=1736253730; i=cmbecker69@gmx.de; bh=KIFHBVNp1FPGVMEXgvPkrqFJQmP5WmCIOlmkqsEBf2Y=; h=X-UI-Sender-Class:Message-ID:Date:MIME-Version:Subject:To: References:From:In-Reply-To:Content-Type: Content-Transfer-Encoding:cc:content-transfer-encoding: content-type:date:from:message-id:mime-version:reply-to:subject: to; b=lle9j5sQ9DtBN5BK8RjHGYSC24YL1m2iZIpBflZFKcp/FElhwMMvo81K2dYLKMDA aZQKHqLXnmwpfkT4XYAKSKgLWC3u5FA4rSpiYB63OS+2WkeiC8mqn3S+1SxVD+88s yrFuN0bYPUKNMMiEbHkjuIEqjWBu/Mo5RI5GR0gpAn+S8stpqm/I2eidBzDOlbFIv cwPsY5d7Q3pFSoxYUAYdbm0t7j/+2XTK/yM20gCpCjGdGr0ujPUul9p8bh69jTjWN c4FZE/j5tcta09S+gCDEiDrW4saMWKXLYXIeijM6f6Zl2XkWpn9PRie9/o7Kl3AFL bqQZZxguaH3zsxu35w== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [192.168.2.130] ([79.251.201.250]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1N2V4P-1tddq10gU3-0136fC; Tue, 31 Dec 2024 13:42:10 +0100 Message-ID: <631837de-c2a0-4bde-9400-5e8e5f2a0906@gmx.de> Date: Tue, 31 Dec 2024 13:42:09 +0100 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] ext/gd new imagematch function (was: adding imagecompare) Content-Language: de-DE To: Giovanni Giacobbi , PHP Internals List References: In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:tHanbymxPczMMzct0NxcwZc/RoQ6KA8/wRhy8btMXNr+UylbP5A M2yCkQlsWn2RcoaPJIyCbetfhKi+yh2OWn1otF+rJhTfON2569oTkZjwZyF2UowtoJD9jhN 1NbIbcJVN+oRsvKyD+YBEj1X5VdZ3Z5///9taO7ItEiD5Kauy6J2tKnizdzS+uLRE17zvmJ B/vIgvdvCC6ehGWVotUcw== UI-OutboundReport: notjunk:1;M01:P0:urRPys/2iFo=;x3vBUjpx6AOyTZ4lXTIu4lY8+Y+ 8PxrDmdLfuqx4jleAhjCXXBhkAekBdZJzrB4nO6vvDRJQQYLI+pd1OGaUwK1EMoHdxETp483l /syWNBlnXyCYJtTkwRSmvQecqU4eKNxQ5bgBbTlM9HZ9EV9u0Qi/T5tyK11sy6RMbgdkmyllY hNpEjI9kCkcdQ1UeCB7L3uBSjQJlepi9ZvHhRSxw+NCXXVnkaL9H22zL5ZniOKOzbRQRi0bZl 7dZccg2JWyaHD4GSCGfkvKB7He3TW83AhiLbmlhTb6m0Fu0f2Ca6YWAfOC26uXlovcJyPxqYY VDfrDfREDctW8gFnF+uykcA/bcwuemLyVjwoH77t3fnhawMQFAp6wc/4wx2N2qARsCAt4rfDJ H+GNo9IRwy9i0oQFTFR/ElvQ2x10ZK9PI8BDXL1HSmYkTrN2GSyCZq8BONTjv5Oolauvh9o3E LJpSu7qqgCL5gqzWbsupaMOb/cQz8EMjKNotEUjzA5EOPxwFDus7lb+Ov7TyUb/QpYTqs+kCi qG1YCnAtZOXsxDI7i6nZ+6YmNFVbnOfcQxj/T3owqUPvk/aO7D+UMGRF7V9e0E04S2eB8jPGr W4UjjG+j24T8T6c9JbJFwXaTZgPhzoR4e09ug2ocoQPFHwT2kPBgM3YwKX6xpgV1gYryBSVgB MGoKAKrYmy/OxRbdvz9UOBZcRFQLsowM/SnWcITC9lo+1o5N8asoMZKGYLfdULH9gUrPYcAxM lI+hmJeXbaARV3PaRn+3tpLME2LEWE7wruw+2N+qDkWQn0wUvCc0lnnV1uWutfI5JshC1DXBz EmrcsvJMmKxnLjvpua9QtSmvJ2WDCO0i/ZjcyqCvp/wuoDWeeO/zmMtVe5wPgBydxRBJaxZuu Tv/z/X+3J5KzQ61TNPXMlj5YbWZYU+K06yDaapbX8i77PVnnK+lirPTE0VWTr+sMVYKmJBeci ehX8R1ROjKYkr2Br5ecA25x/vKXlK3tjVzIW3Xrg/FeLU6nSmSH0xDbUBQnh8uK+x0J4ptTE2 yLEwazk33Knhw4x0PX0pEO1FDZLVMhiU7EBVzYs5QvAU+oH7v2RVdkUwzGJCqFi2+eIIPTREC mZmGTOQ3jqmuom8+QttsFN44MHxafiCOOJVZocupFC/8UIOE/StvMtEZgKCXQzycp9RjK0QlU = From: cmbecker69@gmx.de ("Christoph M. Becker") On 11.07.2024 at 11:35, Giovanni Giacobbi wrote: > The recent PR #14877 [1] proposes to add the imagecompare gd function th= at > mimics the gdImageCompare function from libgd. I always thought that a > pixel-by-pixel matching function for two images was a big missing featur= e > in PHP, as the corresponding userland implementation is really, REALLY s= low. > > 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 i= n > 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 pix= el > 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 i= n > 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 implement= ed > in userland with existing functions: imageinterlace, imagecolortranspare= nt, > imageistruecolor, imagesx, imagesy, imagecolorstotal. > > I believe PHP should only offer required building blocks and leave to th= e > libraries the implementation of more structured functionality. My propos= al > 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 signatur= e: > > 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 exten= ded > functionality of matching portions of the images it would be just 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/5586d0c7de00acbfb217e1c6321da918b96e= f960/ext/gd/libgd/gd.c#L2834 > [3] https://github.com/php/php-src/pull/14914 Difficult. It seems to me we first need to consider the use-case. Likely useful only for testing purposes, but I may miss some other use-cases. And now comes the really hard part: when do we consider images to be equal? You have mentioned that gdImageCompare() completely ignores the alpha channel, and that would be a mistake. I tend to agree, but if the images are stored via imagejpeg(), for instance, the alpha channel would be ignored as well, so the resulting images would be equal. Then there is the question whether two fully transparent pixels (alpha value 127) are different if their RGB channels have different values. In theory, sure, but it practice is may not really matter. Then we have the transparent pixel. Should equality comparison take that into account? Again, in theory, it is not the same as a pixel with alpha channel 127, but in practice it may very well be. Now you claim that doing pixel-to-pixel comparisons in PHP would be *really* slow. I'd argue that with modern PHP the actual looping over the pixels isn't that slow. Even getting the pixel colors (imagecolorat()) and checking them for equality shouldn't be that slow. It only gets slow if you want to do more elaborate comparisons; e.g. getting the actual values a palette entry refers to, but if you move that out of the loop, performance might not be that bad. Implementing that in C is certainly faster, but the difference should be measured, and I wonder how much could be gained using SIMD instructions (something libgd still ignores completely). Christoph