Small Task: PHP convert Image to RGB565, compress, show stats then download

Forum to require and offer php jobs. Are you searching for php developers to make or complete your php script ? This place is for you.

Moderators: egami, macek, gesf

Post Reply
xxkeldecknightxx
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Apr 24, 2019 9:28 am

Wed Apr 24, 2019 9:32 am

Hi Everyone!!
I am working on logic for a Microcontroller that dynamically updates a screen via UART. The screen only accepts vector based commands to fill a coordinate with a square and a filled color. The command looks like so
fill x,y,h,w,<color>

The PHP code I wrote so far takes the image and converts each pixel to the following format, However, there are tons of room for optimization. An idea I had was to take the image and find the most common color then set the whole screen to this color. Then find all pixel's within a given shade and make it the same color than that beings the fill command to a single command because it can then cover two squares by using the command x,y,2,1,<color>.
The goal is to generate the picture with lossy compression but without extremely affecting the image and as few FILL commands as possible.
During the development of these algorithms, your PHP code will need to display the before and after image and the number of FILL lines required to draw the image.
The Binary data of the x,y, color will then be sent as a .bin file when requested from the .php script.

It can be your own or someone else's algorithm.. just make it possible to redraw with least amount of commands

Examples of algorithms are like this.
Cycle through each pixel and neighboring pixels and if they are similar shade make them the same. Then find the most common color and set the background image to that color. then cycle through each pixel if it's not that background color fill in that color.
The least amount of times you have to cast the fill command to drop a pixel the better the algorithm!

A sample image that will be used is here
https://www.faichi.com/sites/default/fi ... %202_0.png

In this case, the large portion of the map is gray, so its best to make that shade one distinct RGB565 color then set the image of the full background to that color using a single fill command.
Then cycle through each pixel and combine additional like colors to make the least amount of permutations to cast a fill command to fill a pixel or a subset of pixels.
When doing this without any compression you would have to cast nearly one fill command for each pixel and would take over 5 minutes to draw a screen which is not a doable solution however there should be a simple way to go upon this.

Sample PHP code to get started.

Code: Select all

<?php
$img = imagecreatefromjpeg("Path to Image that is 480x272");
$width = ImageSX($img);
$height = ImageSY($img);

for ($y = 0; $y < $height; $y++) {
    for ($x = 0; $x < $width; $x++) {
        $c = imageColorAt($img, $x, $y);
        $r = ($c >> 16) & 0xFF;
        $g = ($c >> 8) & 0xFF;
        $b = $c & 0xFF;

        $r = $r >> 3;
        $g = $g >> 2;
        $b = $b >> 3;

        $oc = ($r << 11) | ($g << 5) | $b;
 	echo ("fill $x,$y,1,1,$oc<br>");
    }
}
?>
I am willing to pay Hourly and/or a Flat rate.
Thanks everyone for your consideration!

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 834
Joined: Mon Feb 22, 2016 5:52 pm

Thu Apr 25, 2019 9:59 am

You're willing to pay someone to do your homework?
Hi Everyone!!
I am working on logic for a Microcontroller that dynamically updates a screen via UART. The screen only accepts vector based commands to fill a coordinate with a square and a filled color. The command looks like so
fill x,y,h,w,<color>

The PHP code I wrote [really?]so far takes the image and converts each pixel to the following format, However, there are tons of room for optimization. An idea I had [you can't half tell them!] was to take the image and find the most common color then set the whole screen to this color. Then find all pixel's within a given shade and make it the same color than that beings the fill command to a single command because it can then cover two squares by using the command x,y,2,1,<color>.
The goal is to generate the picture with lossy compression but without extremely affecting the image and as few FILL commands as possible.
During the development of these algorithms, your PHP code will need to display the before and after image and the number of FILL lines required to draw the image.
The Binary data of the x,y, color will then be sent as a .bin file when requested from the .php script.

It can be your own or someone else's algorithm.. just make it possible to redraw with least amount of commands

Examples of algorithms are like this.
Cycle through each pixel and neighboring pixels and if they are similar shade make them the same. Then find the most common color and set the background image to that color. then cycle through each pixel if it's not that background color fill in that color.
The least amount of times you have to cast the fill command to drop a pixel the better the algorithm!

A sample image that will be used is here
https://www.faichi.com/sites/default/fi ... %202_0.png

In this case, the large portion of the map is gray, so its best to make that shade one distinct RGB565 color then set the image of the full background to that color using a single fill command.
Then cycle through each pixel and combine additional like colors to make the least amount of permutations to cast a fill command to fill a pixel or a subset of pixels.
When doing this without any compression you would have to cast nearly one fill command for each pixel and would take over 5 minutes to draw a screen which is not a doable solution however there should be a simple way to go upon this.

Sample PHP code to get started.

Code: Select all

<?php
$img = imagecreatefromjpeg("Path to Image that is 480x272");
$width = ImageSX($img);
$height = ImageSY($img);

for ($y = 0; $y < $height; $y++) {
    for ($x = 0; $x < $width; $x++) {
        $c = imageColorAt($img, $x, $y);
        $r = ($c >> 16) & 0xFF;
        $g = ($c >> 8) & 0xFF;
        $b = $c & 0xFF;

        $r = $r >> 3;
        $g = $g >> 2;
        $b = $b >> 3;

        $oc = ($r << 11) | ($g << 5) | $b;
 	echo ("fill $x,$y,1,1,$oc<br>");
    }
}
?>
I am willing to pay Hourly and/or a Flat rate.
Thanks everyone for your consideration!
Are you also taking classes in fibbing? :D

xxkeldecknightxx
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Apr 24, 2019 9:28 am

Tue May 07, 2019 8:55 am

hyper, Wth are you talking about.. What about this is a dead giveaway that Im fibbing and trying to have someone else do my homework? No this is a serious task for anyone with a background of Algorithm Theory in conjunction with php. Image compression is something actully useful in the real world then to be used for just a student's homework assignment.

I assume you've never used 9600 baud before to know that its not the fastest protocol for sending pixel data so yes, compression is required! I hope your not into Law Enforcement Hyper.

Post Reply