Skip to content

Commit

Permalink
Merge pull request #60 from noodlebox/cleanup_build_image
Browse files Browse the repository at this point in the history
Speed up build_image.php
  • Loading branch information
iamcal authored Sep 12, 2016
2 parents bfdc824 + 99d78fc commit 19517a4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 81 deletions.
2 changes: 1 addition & 1 deletion build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ You can rebuild by following these steps:

# Rebuild positions and make the master spritesheets
# (This step requires ImageMagick or GraphicsMagick)
php build_image.php # this is slow!
php build_image.php
php build_sheets.php

# create quantized sheets and optimize them all (_very_ slow)
Expand Down
118 changes: 38 additions & 80 deletions build/build_image.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?php
$dir = dirname(__FILE__);

#
# load master catalog
#

$in = file_get_contents('../emoji.json');
$in = file_get_contents(__DIR__.'/../emoji.json');
$catalog = json_decode($in, true);


Expand All @@ -29,6 +27,8 @@
}
}

$size = $max + 1;


#
# bake sheets
Expand All @@ -46,7 +46,7 @@ function create_sheet($type){

echo "Creating $type : \n";

global $catalog, $max;
global $catalog, $size;


#
Expand All @@ -65,10 +65,11 @@ function create_sheet($type){
# first, build out the compositing list
#

$comp = array();
$comp = array_fill(0, $size*$size, null);

foreach ($catalog as $row){

$index = $size*$row['sheet_y'] + $row['sheet_x'];

#
# do we have the image in this set?
Expand All @@ -84,8 +85,7 @@ function create_sheet($type){

if ($row["has_img_{$type}"]){

$main_img = "img-{$type}-64/{$row['image']}";
$comp[] = array($row['sheet_x'], $row['sheet_y'], $main_img);
$comp[$index] = "img-{$type}-64/{$row['image']}";
break;
}

Expand All @@ -101,8 +101,7 @@ function create_sheet($type){

if ($row["has_img_{$try_type}"]){

$main_img = "img-{$try_type}-64/{$row['image']}";
$comp[] = array($row['sheet_x'], $row['sheet_y'], $main_img);
$comp[$index] = "img-{$try_type}-64/{$row['image']}";
break 2;
}
}
Expand All @@ -112,8 +111,7 @@ function create_sheet($type){
# it's missing - try the fallback (2753)
#

$main_img = $replacement;
$comp[] = array($row['sheet_x'], $row['sheet_y'], $main_img);
$comp[$index] = $replacement;
echo "Unable to find any images for U+{$row['unified']}\n";
break;
}
Expand All @@ -126,6 +124,7 @@ function create_sheet($type){
if (isset($row['skin_variations'])){
foreach ($row['skin_variations'] as $row2){

$index = $size*$row2['sheet_y'] + $row2['sheet_x'];
$vari_img = $row2["has_img_{$type}"];

# uncomment this line if you want each variations position to
Expand All @@ -135,92 +134,51 @@ function create_sheet($type){

if ($vari_img){

$comp[] = array($row2['sheet_x'], $row2['sheet_y'], "img-{$type}-64/{$row2['image']}");
$comp[$index] = "img-{$type}-64/{$row2['image']}";
}
}
}

}

$geom = escapeshellarg("{$img_w}x{$img_w}");
$tile = escapeshellarg("{$size}x{$size}");
$dst = escapeshellarg("sheet_{$type}_{$img_w}.png");
$cmd = "montage @- -geometry {$geom} -tile {$tile} -background none png32:{$dst}";

#
# next, build the strips one by one. we do this instead of doing it all
# in one go so that we canm load/save the intermediate image much faster.
#

$mp = $max + 1;

$pw = $mp * $img_w;
$ph = $mp * $img_w;

for ($i=0; $i<$mp; $i++){
$ip = $i + 1;
echo "col $ip/$mp : ";
# Read filenames on stdin
$fd_spec = array(
0 => array("pipe", "r")
);

$dst = $GLOBALS['dir']."/../sheet_{$type}_{$img_w}_col{$i}.png";
$pipes = array();

echo shell_exec("convert -size {$img_w}x{$ph} xc:none {$dst}");
# chdir into parent directory first
$cwd = __DIR__.'/..';

foreach ($comp as $row){
if ($row[0] != $i) continue;
$res = proc_open($cmd, $fd_spec, $pipes, $cwd);

$px = 0;
$py = $row[1] * $img_w;

$path = $GLOBALS['dir'].'/../'.$row[2];
if (file_exists($path)){

echo shell_exec("composite -geometry +{$px}+{$py} {$path} {$dst} {$dst}");
echo '.';
}else{
echo "(not found: $src)";
}
# Write out each filename
foreach ($comp as $index => $file){
if ($file !== null){
fwrite($pipes[0], "{$file}\n");
echo '.';
}else{
fwrite($pipes[0], "null:\n");
echo ' ';
}

echo " OK\n";
if ($index % $size == $size - 1){
echo "\n";
}
}

fclose($pipes[0]);

#
# merge the strips
#

echo "merging ... ";

$dst = $GLOBALS['dir']."/../sheet_{$type}_{$img_w}.png";

echo shell_exec("convert -size {$pw}x{$ph} xc:none {$dst}");

for ($i=0; $i<$mp; $i++){
$src = $GLOBALS['dir']."/../sheet_{$type}_{$img_w}_col{$i}.png";

$px = $i * $img_w;
$py = 0;

echo shell_exec("composite -geometry +{$px}+{$py} {$src} {$dst} {$dst}");
echo '.';

unlink($src);
if (proc_close($res) > 0) {
echo "Something went wrong\n\n";
return;
}

echo " OK\n";

echo "Optimizing sheet ... ";
echo shell_exec("convert {$dst} png32:{$dst}");
echo "DONE\n\n";
}

function composite($sheet_x, $sheet_y, $img_w, $src, $dst){

$px = $sheet_x * $img_w;
$py = $sheet_y * $img_w;

$path = $GLOBALS['dir'].'/../'.$src;
if (file_exists($path)){

echo shell_exec("composite -geometry +{$px}+{$py} {$path} {$dst} {$dst}");
echo '.';
}else{
echo "(not found: $src)";
}
}

0 comments on commit 19517a4

Please sign in to comment.