php - 将 C 转换为 PHP

标签 php c qt gd

嗨,我有以下用于 CLI 工作的代码,用 C 语言完成。我现在想用 PHP 翻译它,但我陷入了颜色转换。有什么线索吗?

for (size_t y = 0; y < targetHeight; y++) {
    for (size_t x = 0; x < targetWidth; x++) {
        // Convertimos el color

        png::rgba_pixel_16 pixel = renderImage[y][x];

        uint alpha = pixel.alpha / 256;

        if (alpha > 0) {

            minX = minX < x ? minX : x;
            maxX = maxX > x ? maxX : x;
            minY = minY < y ? minY : y;
            maxY = maxY > y ? maxY : y;

            jpgImage->setPixel(x, y, (pixel.red/256 << 16) + (pixel.green/256 << 8) + pixel.blue/256 ) ;

            uint mP = mapImage->pixel(x, y);

            mapImage->setPixel(x, y, mP + (alpha<<16)  );

        }
    }
}

遇到的问题是 Pixel.red << 16 ,我无法从图像中获取正确的颜色并使用 PHP 正确转换它们。

这是到目前为止的 PHP 代码:

for ($y = 0; $y < HEIGHT; $y++) {
    for ($x = 0; $x < WIDTH; $x++) {
        // Convertimos el color

        $pixel = imagecolorat($render_img, $x, $y);
        //$pixel = imagecolorat($render_img, $y, $x);

        $rgba = imagecolorsforindex($render_img, $pixel);
        $currentAlpha = $rgba['alpha']/256;
        //$alpha = pixel.alpha / 256;

        if ($currentAlpha > 0) {

            $minX = $minX < $x ? $minX : $x;
            $maxX = $maxX > $x ? $maxX : $x;
            $minY = $minY < $y ? $minY : $y;
            $maxY = $maxY > $y ? $maxY : $y;

            imagesetpixel ($result_jpg_image, $x, $y, ($rgba['red']/256<<16) + ($rgba['green']/256<<8) + ($rgba['blue']/256));
            //imagesetpixel ($result_jpg_image, $x, $y, ($rgba['red'] << 16) + ($rgba['green'] << 8) + ($rgba['blue']));
            //jpgImage->setPixel(x, y, (pixel.red/256 << 16) + (pixel.green/256 << 8) + pixel.blue/256 ) ;

            $pixel = imagecolorat($zMap, $x, $y);
            imagesetpixel ($zMap, $x, $y, ($pixel + $currentAlpha<<16));
        }
    }
}

最佳答案

我不确定将 RGB 值除以 256 能否获得所需的效果。

尝试:

imagesetpixel($result_jpg_image, $x, $y,
              ($rgba['red']<<16) + ($rgba['green']<<8) + ($rgba['blue']));

我不确定您是否正在尝试应用某种颜色算法,但将 RGB 值除以 256,然后左移所得 float 会在 PHP 中得到 0

我给你的上面的代码行只是将像素设置回相同的颜色。

关于php - 将 C 转换为 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9425365/

相关文章:

php - 使用 loadHTML 和 DOMXPath 获取 td 值

php - 空查询附加到主 URL .htaccess 问题

php - 如何在 php 中获得尽可能早的日期时间?

编译错误

c - 如果两者在 C 中具有相同的名称而不使用第三个变量,如何将局部变量复制到全局变量?

Qt@5.5 有效,但 Qt 无效

javascript - 元素在移动设备上显示时不会居中,背景不会固定

c - 在C中将数学方程插入二叉树

c++ - 调整 Qt 窗口大小时出现闪烁的白色区域

c++ - QString字符删除函数