2011-01-26

PHPでビット演算、シフト演算

ビットをフラグ代りに使うことが増えてきたので。。。
1ビット1アイテムって思うと、最大で63個まで持ってるか分かるようになる的な。
なんで63個かっていうと64個だとMySQLにしまえない可能性があるみたい。
(使ってるVerが古いってのもあるし、そもそも勘違いかもだけどー。)
とりあえず、PHPで書いたさんぷる。

さんぷるクリプト



// 色情報
$items=array('red'=>1, 'blue'=>2, 'green'=>3, 'white'=>4, 'black'=>5);
// 人情報
$peoples = array('LILY'=>0, 'LOGAN'=>0, 'OLIVIA'=>0);
/*
* 選択情報を作る
*/
$peoples['LILY'] =
(1 << $items['red'])
| (1 << $items['green'])
| (1 << $items['black']) ; // red, green, black を選ぶ

printf("name:% 7s int:% 4d bit:%08b\n",
'LILY', $peoples['LILY'], $peoples['LILY']);

$peoples['LOGAN'] =
(1 << $items['blue'])
| (1 << $items['green'])
| (1 << $items['black']) ; // blue, green, black を選ぶ

printf("name:% 7s int:% 4d bit:%08b\n",
'LOGAN', $peoples['LOGAN'], $peoples['LOGAN']);

$peoples['OLIVIA'] =
(1 << $items['red'])
| (1 << $items['white'])
| (1 << $items['black']) ; // red, white, black を選ぶ

printf("name:% 7s int:% 4d bit:%08b\n",
'OLIVIA', $peoples['OLIVIA'], $peoples['OLIVIA']);

print "======================\n";

/*
* 皆が選択している色だけを選らぶ
* (論理積)
*/
$common_col=0;
$common_col=
$peoples['LILY'] & $peoples['LOGAN'] & $peoples['OLIVIA'] ;
printf("common colors int:% 4d bit:%08b\n", $common_col, $common_col);
chkSelections($common_col);

print "======================\n";

/*
* 選択されている全ての色を選ぶ
* (論理和)
*/
$common_col=0;
$common_col=
$peoples['LILY'] | $peoples['LOGAN'] | $peoples['OLIVIA'] ;
printf("common colors int:% 4d bit:%08b\n", $common_col, $common_col);
chkSelections($common_col);

exit;

/*
* ビットが立っているものを探す
*/
function chkSelections($selections)
{
global $items;

foreach ( $items as $col_name => $col_num )
{
$chk_bit=( 1 << $col_num );
if( $selections & $chk_bit ) // ビットが立ってる
{
printf("Select color:% 6s\n", $col_name);
}
}
}

シフト演算でビットが立っているところを探していく方法もあるけど。

$col_num=0;
while( $selections )
{
if( $selections & 1 )
{
printf("Select color number:%d bit:%08b\n", $col_num, $selections);
}
$selections = $selections >> 1;
$col_num++;
}


MySQL


選択情報をMySQLに格納して、クエリで取得することもできる。

SELECT * hoge WHERE select_bit & ${col_num} ;

これでその色を選択している情報が引っ張れるです。

2010-12-21

PerlのImage::Magick

画像合成をする場合に使うライブラリのさんぷるクリプト

ImageMagick

さんぷるクリプト



#!/usr/bin/perl

use strict;
use warnings;

use Image::Magick;

my $dir = 'images';
my $width = 192;
my $height= 53;

my $i3 = Image::Magick->new(width=>$width, height=>$height, magick=>'GIF');
$i3->Read("$dir/layer3.gif");

my $i2 = Image::Magick->new(width=>$width, height=>$height, magick=>'GIF');
$i2->Read("$dir/layer2.gif");
$i3->Composite(image=>$i2, compose=>'Over', x=>0, y=>0);

my $i1 = Image::Magick->new(width=>$width, height=>$height, magick=>'GIF');
$i1->Read("$dir/layer1.gif");
$i3->Composite(image=>$i1, compose=>'Over', x=>0, y=>0);

$i3->Write("$dir/mix_i.gif");

#binmode STDOUT;
#$i3->Write('gif:-');

exit;

PerlのGD

画像合成をする場合に使うライブラリのさんぷるクリプト

GD

さんぷるクリプト



#!/usr/bin/perl

use strict;
use warnings;

use GD;

my $dir = 'images';
my $width = 192;
my $height= 53;

my $image = new GD::Image($width, $height);

my $i3 = new GD::Image("$dir/layer3.gif");
$image->copy($i3, 0, 0, 0, 0, $width, $height);

my $i2 = new GD::Image("$dir/layer2.gif");
my $i1 = new GD::Image("$dir/layer1.gif");
$i2->copy($i1, 0, 0, 0, 0, $width, $height);
$image->copy($i2, 0, 0, 0, 0, $width, $height);

open(OUT, ">$dir/mix.gif");
binmode OUT;
print OUT $image->gif;
close(OUT);

exit;