These pages are written by only Japanese.

Welcom to My Diary.com
最新の日記タイトル一覧カテゴリ別タイトル一覧トップへ戻る〜

おはようございます♪ 現在は4月20日(土)6時20分。 鳥がさえずり始める頃です。


hns - 日記自動生成システム - Version 2.19.5 (色々 Fixed)

先月 2008年01月 来月
01 02 03 04 05
06 07 08 09 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Namazu for hns による簡易全文検索です。
詳細は 詳細指定/ヘルプをご参照下さい。
検索式:

2008年01月26日(土)

Flash SWF 画像入れ替え(PHP 編)

前回の課題のうち、ファイルサイズの更新処理と CWS の圧縮処理を入れて、 JPEG画像入れ替えまで実装してみた。

CWS の圧縮で問題発生:

PHP Warning:  gzuncompress(): buffer error in /home/yoya/prog/flash/FlashSWF.php on line 42
google で探しても解決した例が見つからないので、php のソースを読んでみた。
(yum で PHP 5.1.6 (cli) を使っているが、)とりあえず最新版の 5.2.5 を取得。
php の中にはメッセージがない
% pwd
/home/yoya/src/php-5.2.5
% grep "buffer error" * */* */*/*
%
zlib の中を探す
% grep "buffer error" *.c
zutil.c:"buffer error",        /* Z_BUF_ERROR     (-5) */
zutil.c (メッセージを定義してる場所)
const char * const z_errmsg[10] = {
"need dictionary",     /* Z_NEED_DICT       2  */
"stream end",          /* Z_STREAM_END      1  */
"",                    /* Z_OK              0  */
"file error",          /* Z_ERRNO         (-1) */
"stream error",        /* Z_STREAM_ERROR  (-2) */
"data error",          /* Z_DATA_ERROR    (-3) */
"insufficient memory", /* Z_MEM_ERROR     (-4) */
"buffer error",        /* Z_BUF_ERROR     (-5) */
"incompatible version",/* Z_VERSION_ERROR (-6) */
""};

#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
compress.c (実際に出力する場所)
     compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
   memory, Z_BUF_ERROR if there was not enough room in the output buffer,
   Z_STREAM_ERROR if the level parameter is invalid.
	<略>
   if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
    err = deflate(&stream, Z_FINISH);
    if (err != Z_STREAM_END) {
        deflateEnd(&stream);
        return err == Z_OK ? Z_BUF_ERROR : err;
伸張する時に長さを指定するけど、 実際に伸張したらその長さを超えちゃったってだけか…
つまり、ファイル長フィールドの更新に失敗してると… ○rz
chara.swf と(それを read して write した) output.swf はファイルサイズが同じなんだけど。(そうなるように圧縮率を調整したし)
* chara.swf

  addr :  0 1  2 3  4 5  6 7  8 9  A B  C D  E F  0123456789ABCDEF
0x00000: 4357 5306 0048 2c00 789c 9cb9 6b5c 5259  CWS  H, x   k\RY
                   ~~~~~~~~
* output.swf addr : 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF 0x00000: 4357 5306 a86f 2800 789c 9cb9 6b5c 5259 CWS o( x k\RY ~~~~~~~~
確かに駄目だ

圧縮失敗の原因:

if ($magic == 'CWS') {
	movie_and_body = gzcompress($movie_and_body, 6);
}
<略>
$this->_header['filelength'] = strlen($header_without_length) + 4 + strlen($movie_and_body);
えーっと… ファイル長フィールドには 伸張した時のファイルサイズを入れないと駄目なのに、 圧縮後のファイルサイズを入れてました…
そりゃ、伸張した時にバッファも足りなくなるさ… ○rz
$this->_header['filelength'] = strlen($header_without_length) + 4 + strlen($movie_and_body);
<略>
if ($magic == 'CWS') {
	movie_and_body = gzcompress($movie_and_body, 6);
}
こうしないと。

問題 (chara.swf から吸い出した画像が表示できない):

  • saitama.swf の画像吸出し成功、表示も出来る。
  • chara.swf の画像吸出せたが、ビューアで表示できない。(;_;
  • saitama.swf の画像入れ替え成功
  • chara.swf の画像入れ替えも成功 (DefineBitsJPEG3 は未対応 alpha フィールドがよく分からないので…)
先頭 0x10 分を見る限り問題ない JPEG 画像に見えるので長さが足りないか、 途中のどこかを壊してるかってとこだけど、調べるの面倒そう。

課題:

  • chara.swf の画像抽出処理を見直す
  • JPEG 画像の SWF 仕様への変換を考える。
  • もう少しブラッシュアップ
  • 異常系を考える (とりあえず FlashSWFException 実装するか…)
  • デバッグ機能を入れる (動作確認をしやすいように)

追記 (2009/01/18):

追記 (2009/09/09):

ming 導入

まずはコードを取得。current で。
cvs -z3 -d:pserver:anonymous@ming.cvs.sourceforge.net:/cvsroot/ming co -P ming
↑こちらを参考にインストール。

jpeg patch:

まずは、JPEG パッチ *1 を当てる。(flash lite 実行環境は画像フォーマットに好き嫌いがあるので、その対策らしい)
$ wget http://auicon.freeownhost.com/pc/dev/ming/jpeg.c.diff
$ patch -b jpeg.c jpeg.c.diff
patching file jpeg.c
Hunk #1 succeeded at 129 (offset 6 lines).
Hunk #3 succeeded at 310 (offset 11 lines).
ちなみに -b は backup オプション。
(2008/07/25 追記) パッチを公開していたサーバがアクセスできなくなっているので、↑代わり
ついでに JPEG に関するまとめ。↓

configure:

%  ./autogen.sh
Running aclocal -I macros
Running libtoolize --automake
./autogen.sh: line 15: libtoolize: command not found

  Something went wrong, bailing out!
むー。libtool 入ってなかったか…
yum install libtool
./configure
<略>
Config summary:
  C++ enabled
  Perl extension disabled
  PHP extension disabled
  Python extension disabled
  Tcl extension disabled
  ZLIB enabled (-lz)
  Freetype library enabled (/usr/bin/freetype-config)
  GIF library disabled
  PNG library enabled (-lpng)
色々負けてる…
# yum install giflib-devel
# exit
% ./configure --enable-perl --enable-php
<略>
Config summary:
  C++ enabled
  Perl extension enabled
  PHP extension enabled
  Python extension disabled
  Tcl extension disabled
  ZLIB enabled (-lz)
  Freetype library enabled (/usr/bin/freetype-config)
  GIF library enabled (-lungif)
  PNG library enabled (-lpng)
よし…
% make
	<略>
% su
Password:
# make install
	<略>

さて php で…:

によるとコンパイル手順は、
    mkdir <phpdir>/ext/ming
    cp php_ext/* <phpdir>/ext/ming
    cd <phpdir>
    ./buildconf 
    ./configure --with-ming <other config options>
なんだけど、php を yum で入れてるのでどうしよう…
php 自体もいい加減(5.1.6)古いので、これを機会に入れなおそうかとも思うけど、 yum upgrade 対象外になるのは後々、面倒そう。う~む…

php-5.1.6 ツリー上でコンパイル:

yum で入れた php に config オプションはあわせて、 php-5.1.6 のソースをコンパイルしてみた。
php -i | grep Configure
Configure Command =>  './configure' '--build=i686-redhat-linux-gnu'
<以下略>
php -i | grep Configure > ../php-config-option.txt
vi ../php-config-option.txt
sh ../php-config-option.txt
<略>

環境整備:

色々とヘッダが足りなくてエラーが…
checking for PCRE support... yes
configure: error: Could not find pcre.h in /usr
checking for BZip2 in default path... not found
configure: error: Please reinstall the BZip2 distribution
checking for db4 major version... configure: error: Header contains different version
checking for GNU MP support... yes
configure: error: Unable to locate gmp.h
configure: error: Cannot find rfc822.h. Please check your c-client installation.
configure: error: Cannot find ldap.h
configure: error: Cannot find MySQL header files under /usr.
Note that the MySQL client library is not bundled anymore!
checking for ncurses support... yes, shared
checking for unixODBC support... configure: error: ODBC header file '/usr/include/sqlext.h' not found!
not found
configure: error: Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path
checking for sqlite3 files in default path... not found
configure: error: Please reinstall the sqlite3 distribution
checking for PSPELL support... yes
configure: error: Cannot find pspell
configure: error: SNMP sanity check failed. Please check config.log for more information.
必要に応じて -devel パッケージをインストール
# yum install pcre-devel
# yum install bzip2-devel
# yum install db4-devel
# yum install gmp-devel
# yum install libc-client-devel
# yum install openldap-devel
# yum install mysql-devel
# yum install ncurses-devel
# yum install unixODBC-devel
# yum install postgresql-devel
# yum install sqlite-devel
# yum install aspell-devel
# yum install net-snmp-devel

configure (--with-ming で失敗):

さて、php-config.txt に --with-ming を付け足してと…
% sh ../php-config-option.txt
make
ming は php に入ってるのでそのまま。
/home/yoya/src/php-5.1.6/ext/ming/ming.c:2104: error: too many arguments to function 'newSWFMovie'
/home/yoya/src/php-5.1.6/ext/ming/ming.c:2106: error: too many arguments to function 'newSWFMovie'
/usr/local/ に current の ming が入っているので微妙か…
% cd ext/ming/ ext/ming.orig
% cp -r ~/src/flash/ming/php_ext/* ext/ming/.
% sh ../php-config-option.txt
% make
<略>
ext/ming/.libs/ming.o: In function `zif_ming_useSWFVersion':
/home/yoya/src/php-5.1.6/ext/ming/ming.c:121: multiple definition of `zif_ming_useSWFVersion'
ext/ming/.libs/ming.o:/home/yoya/src/php-5.1.6/ext/ming/ming.c:121: first defined here
ext/ming/.libs/ming.o:(.data.rel+0x0): multiple definition of `ming_module_entry'
ext/ming/.libs/ming.o:(.data.rel+0x0): first defined here
collect2: ld returned 1 exit status
駄目か…

configure (--enable-ming で成功):

sh ../php-config-option.txt (--with-ming を --enable-ming に変更)
make
<略>
Build complete.
(It is safe to ignore warnings about tempnam and tmpnam).
やはり…

インストール:

% cd ext/ming;
% sh make_module.sh
% mv ext/ming/tmp/modules/ming.so /usr/lib/php/modules/.

動作確認:

<?php
dl('ming.so');
ming_useswfversion(4);
$swf = new SWFMovie();
$swf->setDimension(640, 480);
$swf->add(new SWFBitmap(fopen('/home/yoya/prog/flash/image1.jpg', 'rb')));
$swf->save('sample.swf');
?>
成功。
% cat > /etc/php.d/ming.ini
; Enable ming extension module
extension=ming.so
^d

。。。:

↓こっち参考にして作業すればよかった… (⊃д⊂)

*1: 詳細や JPEG パッチの cache はこちら。
http://pwiki.awm.jp/~yoya/?Flash/JPEG

スパムメールを送って得られる月収は77万円-108万円

スパマー全員シネバイイノニ
背後に怖い人達がいるんでしょうけど…

これで、1 日分だよ〜。

タイトル一覧
カテゴリ分類
Database
JXTA
Java
XML
awm
bookmark
keyword
memo
news
research
Powered by hns-2.19.5, HyperNikkiSystem Project