2011年2月13日日曜日

SWF Shape reducer

* SWF Shape reducer


** code
** size compare

- before
magic=CWS  version=8  file_length=3166474
 rect=(0, 0)-(550, 300) (f_size=15)

- after
magic=CWS  version=8  file_length=3166366
 rect=(0, 0)-(550, 300) (f_size=15)

** detail


swf defineshape vector data has length field at first, and following data has  concrete value.


- sample

+------------------------------+
| numBits |  deltaX  | deltaY  |
+------------------------------+
<4or5 bits><-numBits-><-numBits->


i can reduce size for rebuild swf binary.


** notes

- numBits 0 is OK
- styleChangeRecord x, y is not delta value.
- new style not add table but replace it.

** TODO

- deforme vector.

SWF DefineShape parser

SWF DefineShape parser in PHP.

** essence




  • http://openpear.org/package/IO_SWF/src/trunk/IO/SWF/Shape.php






  • $this->_shapeBounds = IO_SWF_Type::parseRECT($reader);
    $this->_parseFILLSTYLEARRAY($reader);
    $this->_parseLINESTYLEARRAY($reader);
    $reader->byteAlign();
    $numFillBits = $reader->getUIBits(4);
    $numLineBits = $reader->getUIBits(4);
    while ($done === false) {
        $typeFlag = $reader->getUIBit();
        if ($typeFlag == 0) {
            $endOfShape = $reader->getUIBits(5);
            if ($endOfShape == 0) {
                $done = true;
            } else {
                // StyleChangeRecord
                ...
            }
        } else {
            $straightFlag = $reader->getUIBit();
            if ($straightFlag) {
                 // StraightEdgeRecord
                 ...
                $stateNewStyles = $reader->getUIBit();
                if ($stateNewStyles) {
                    $this->_parseFILLSTYLEARRAY($reader);
                    $this->_parseLINESTYLEARRAY($reader);
                    $reader->byteAlign();
    $numFillBits = $reader->getUIBits(4);
                    $numLineBits = $reader->getUIBits(4);
                }
            } else {
                 // CurvedEdgeRecord
                 ...
            }
        }
    }
    
    ** ready

    pear channel-discover openpear.org
    pear install openpear/IO_Bit
    pear install openpear/IO_SWF
    

    ** go

    ShapeId: 1
    ShapeBounds:
            (-7.75, -7.75) - (7.75, 7.75)
    FillStyles:
            solid fill: #0066ff
    LineStyles:
    ShapeRecords:
            ChangeStyle: MoveTo: (-7.75, -7.75)  FillStyle: 0|1  LineStyle: 0
            StraightEdge: MoveTo: (7.75, -7.75)
            StraightEdge: MoveTo: (7.75, 7.75)
            StraightEdge: MoveTo: (-7.75, 7.75)
            StraightEdge: MoveTo: (-7.75, -7.75)
    

    ** TODO

    i want convert to SVG for shape display.




    2011年2月12日土曜日

    Challenge to FizzBuzz

    I challenged to fizzbuzz because i had watched "fizzbuzz" in Twitter timeline.
    It takes 5 minutes for take1,2,3. so good.

    - reference
    --    http://www.aoky.net/articles/jeff_atwood/why_cant_programmers_program.htm

    * subject

    Let's programming 1 to 100 number print out.
    but if number is multiple of three then print "Fizz",
    ,multiple of five then print "Buzz", and
    multiple of three and file then print "FizzBuzz".

    * take1

    simple and  stupidly honest.

    <?php
    foreach (range(1, 100) as $n) {
        if ($n % 3 == 0)  {
            if ($n % 5 == 0)  {
              echo "FizzBuzz\n";
            } else {
              echo "Fizz\n";
            }
        } elseif ($n % 5 == 0)  {
              echo "Buzz\n";
        } else {
            echo "$n\n";
        }
    }
    

    * take2

    aggregate %5 routine.

    <?php
    foreach (range(1, 100) as $n) {
        $d = '';
        if ($n % 3 == 0)  {
            $d .= "Fizz";
        }
        if ($n % 5 == 0)  {
            $d .= "Buzz";
        }
        if ($d === '') {
            $d = $n;
        }
        echo "$d\n";
    }

    * take2'

    ternary operand

    <?php
    foreach (range(1, 100) as $n) {
        $d = ($n % 3)?'':"Fizz";
        $d .= ($n % 5)?'':"Buzz";
        echo ($d?$d:$n)."\n";
    }
    

    i don't want to use ternary operand nesting.

    * take3

    flag programming. clearly but long code.

    <?php
    foreach (range(1, 100) as $n) {
        $b3 = ($n % 3)?0:1;
        $b5 = ($n % 5)?0:2;
        switch ($b3 | $b5) {
            case 0:
                 echo "$n\n";
                 break;
            case 1:
                 echo "Fizz\n";
                 break;
            case 2:
                 echo "Buzz\n";
                 break;
            case 3:
                 echo "FizzBuzz\n";
                 break;
        }
    }
    

    * take3

    printf programming.

    $fmt = array('%d', 'Fizz', 'Buzz', 'FizzBuzz');
    foreach (range(1, 100) as $n) {
        $b3 = ($n % 3)?0:1;
        $b5 = ($n % 5)?0:2;
        printf($fmt[$b3 | $b5]."\n", $n);
    }
    
    * perl

    foreach (1 .. 100) {
        $_ = ($_, 'Fizz', 'Buzz', 'FizzBuzz')[2 * !($_ % 3) + !($_ % 5)]."\n"; print
    }
    

    very simple....