Advertisement
Guest User

Untitled

a guest
Sep 15th, 2015
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.80 KB | None | 0 0
  1. #/bin/perl
  2. #
  3. # tst_bm16.pl - Pbairefvba ivqrb ra tvs 15 pbhyf 160k200. [ROT13]
  4. #
  5. # par Samuel DEVULDER, Sept 2015.
  6. #
  7.  
  8. if(0) {
  9.     # pour trouver les niveaux equi-repartis
  10.     &init_magick;
  11.  
  12.     my(@px);
  13.     for my $c (0, 100, 127, 142, 163, 179, 191, 203, 215, 223, 231, 239, 243, 247, 251, 255) {push(@px,$c,$c,$c);}
  14.     my $img=&px2img(16,1, @px);
  15.     my @p1 = $img->GetPixels(height=>1,normalize=>"True");
  16.     $img->Set(colorspace=>$LINEAR_SPACE);
  17.     my @p2 = $img->GetPixels(height=>1,normalize=>"True");
  18.     for my $i (0..15) {
  19.         print "$i ",$px[$i*3]," ",255*$p1[$i*3]," ",2*$p2[$i*3]," ",255*$p2[$i*3]," ", 2*$p2[$i*3],"\n";
  20.     }
  21.     for my $i (0..15) {
  22.         print "$i ",$px[$i*3]," ",255*$p1[$i*3]," ",3*$p2[$i*3]," ",255*$p2[$i*3]," ", 3*$p2[$i*3],"\n";
  23.     }
  24.    
  25.     # 0 163 215 255
  26.     # 0 191 255
  27.  
  28.     exit;
  29. }
  30.  
  31. # parametres
  32. ($w, $h) = (160, 100);
  33. $fps     = 10;
  34. $dither  = "vac8"; #"bayer4";
  35. $zigzag  = 0;
  36. $satur   = 140;
  37. $max     = 245;
  38.  
  39. # fichier entree
  40. $file    = $ARGV[0];
  41. $name    = $file;
  42. $file =~ s/\.[^\.]*$/.gif/;
  43. mkdir "out";
  44. $file =~ s/^.*[\/]/out\//;
  45. exit if -e $file;
  46. print "$name\n";
  47.  
  48. # dossier temporaire
  49. mkdir "tmp";
  50. unlink(<tmp/img*.bmp>);
  51.  
  52. open(OUT,"| ./ffmpeg -i - -v 0 -r $fps -s ${w}x${h} -an tmp/img%05d.bmp");
  53. open(IN, "<$name");
  54. binmode(OUT);
  55. binmode(IN);
  56.  
  57. open(OUT2,"| ./ffmpeg -i - -v 0 -r 1 -s ${w}x${h} -an tmp/img%05d.bmp");
  58. open(IN2, "<$name");
  59. binmode(OUT2);
  60. binmode(IN2);
  61.  
  62. &init_magick;
  63.  
  64. $gif = Image::Magick->new(size=>($w*2)."x".($h*2));
  65.  
  66. @mat = ([1]) if $dither eq "checker";
  67. @mat = ([1,3],
  68.         [4,2]) if $dither eq "bayer2";
  69. @mat = ([7,8,2],
  70.         [6,9,4],
  71.     [3,5,1]) if $dither eq "sam3";
  72. @mat = ([3,7,4],
  73.         [6,1,9],
  74.     [2,8,5]) if $dither eq "3x3";
  75. @mat = ([1,9,3,11],
  76.         [13,5,15,7],
  77.     [4,12,2,10],
  78.     [16,8,14,6]) if $dither eq "bayer4";
  79. @mat = ([21,2,16,11,6],
  80.     [9,23,5,18,14],
  81.         [19,12,7,24,1],
  82.         [22,3,17,13,8],
  83.         [15,10,25,4,20]) if $dither eq "vac5";
  84. @mat = ([35,57,19,55,7,51,4,21],
  85.     [29,6,41,27,37,17,59,45],
  86.     [61,15,53,12,62,25,33,9],
  87.     [23,39,31,49,2,47,13,43],
  88.     [3,52,8,22,36,58,20,56],
  89.     [38,18,60,46,30,5,42,28],
  90.     [63,26,34,11,64,16,54,10],
  91.     [14,48,1,44,24,40,32,50]) if $dither eq "vac8";
  92.  
  93. $mat_x = 1+$#mat;
  94. $mat_y = 1+$#{$mat[0]};
  95. for my $a (@mat) {for my $b (@$a) {$b /= $mat_x*$mat_y+1.0;}}
  96.  
  97. @teo_pal = (0,100,127,142,163,179,191,203,215,223,231,239,243,247,251,255);
  98. @lin_pal = (0, 33, 54, 69, 93,115,133,152,173,188,204,220,229,237,246,255);
  99. #($MID31, $MID21, $MID32) = (4,6,8);
  100. #($MID31, $MID21, $MID32) = (2,4,8);
  101. ($MID31, $MID21, $MID32) = (1,3,5);
  102.  
  103. if(1) {
  104.     my @pc2teo = ();
  105.     for my $i (1..$#teo_pal) {
  106.         my($a,$b,$c) = ($teo_pal[$i-1],($teo_pal[$i-1]+$teo_pal[$i])>>1,$teo_pal[$i]);
  107.         for my $j ($a..$b)   {$pc2teo[$j] = $i-1;}
  108.         for my $j ($b+1..$c) {$pc2teo[$j] = $i;}
  109.     }
  110.    
  111.     my @tab1 = (0)x16;
  112.     my @tab2 = (0)x16;
  113.     $time = 1;
  114.     my $run = 1;
  115.     while(1) {
  116.         my $name = sprintf("tmp/img%05d.bmp", $time);
  117.         $expected_size = $h*(($w*3 + 3)&~3) + 54 if !$expected_size;
  118.         if($expected_size != -s $name) {
  119.             last unless $run;
  120.             my $buf;
  121.             my $read = read(IN2,$buf,4096);
  122.             if($read) {
  123.                 syswrite OUT2, $buf, $read;
  124.             } else {
  125.                 $run = 0;
  126.                 close(OUT2);
  127.             }
  128.         } else  {
  129.             # image complete!
  130.             print STDERR $time++,"s\r";
  131.        
  132.             # lecture de l'image
  133.             my $img = Image::Magick->new();
  134.             $img->Read($name);
  135.             unlink $name;
  136.            
  137.             # on force la saturation (140%) pour avoir des couleurs plus franches
  138.             $img->Modulate(saturation=>$satur);
  139.             $img->Evaluate(operator=>'Multiply', value=>255/$max);
  140.        
  141.             # trammage
  142.             my @px = $img->GetPixels(height=>$h, normalize=>"True");
  143.             undef $img;
  144.             for(my $i=0; $i<$#px; $i+=3) {
  145.                 ++$tab1[$pc2teo[int($px[$i]*255)]];
  146.                 ++$tab1[$pc2teo[int($px[$i+1]*255)]];
  147.                 ++$tab2[$pc2teo[int($px[$i+2]*255)]];
  148.             }  
  149.         }
  150.     }
  151.     close(IN2);close(OUT2);
  152.    
  153.     # for my $i (0..15) {
  154.         # print $tab1[$i],"  ",$tab2[$i],"\n";
  155.     # }
  156.     # 0 a b 15
  157.     # 0  c  15
  158.     my($tot,$acc);
  159.     $tot = -$tab1[0]-$tab1[15];
  160.     $acc = 0;
  161.     for my $t (@tab1) {$tot += $t;}
  162.     for(my $i=1; $i<16; ++$i) {
  163.         $acc += $tab1[$i];
  164.         if($acc*3>=$tot) {
  165.             $MID31 = $i;
  166.             last;
  167.         }
  168.     }
  169.     for(my $i=$MID31+1; $i<16; ++$i) {
  170.         $acc += $tab1[$i];
  171.         if($acc*3>=$tot*2) {
  172.             $MID32 = $i;
  173.             last;
  174.         }
  175.     }
  176.     $tot = -$tab2[0]-$tab2[15];
  177.     $acc = 0;
  178.     for my $t (@tab2) {$tot += $t;}
  179.     for(my $i=1; $i<16; ++$i) {
  180.         $acc += $tab2[$i];
  181.         if($acc*2>=$tot) {
  182.             $MID21 = $i;
  183.             last;
  184.         }
  185.     }
  186.     print "$MID31 $MID21 $MID32\n";
  187.  
  188.     unlink(<tmp/img*.bmp>);
  189. }
  190.  
  191.  
  192. $d4 = sub {
  193.     my($v, $d) = @_;
  194.     my($a) = $lin_pal[$MID32];
  195.     return ($v-$a)/(255-$a)>=$d ? 255 : $teo_pal[$MID32] if $v>=$a;
  196.     my($b) = $lin_pal[$MID31];
  197.     return ($v-$b)/($a-$b)>=$d ? $teo_pal[$MID32] : $teo_pal[$MID31] if $v>=$b;
  198.     return $v/$b>=$d ? $teo_pal[$MID31] : 0;
  199. };
  200. $d3 = sub {
  201.     my($v, $d) = @_;
  202.     my($a) = $lin_pal[$MID21];
  203.     return ($v-$a)/(255-$a)>=$d ? 255 : $teo_pal[$MID21] if $v>=$a;
  204.     return $v/$a>=$d ? $teo_pal[$MID21] : 0;
  205. };     
  206.  
  207. $cpt = 1;$run = 1;
  208. while(1) {
  209.     $name = sprintf("tmp/img%05d.bmp", $cpt);
  210.    
  211.     $expected_size = $h*(($w*3 + 3)&~3) + 54 if !$expected_size;
  212.     if($expected_size != -s $name) {
  213.         last unless $run;
  214.         my $buf;
  215.         my $read = read(IN,$buf,4096);
  216.         if($read) {
  217.             syswrite OUT, $buf, $read;
  218.         } else {
  219.             $run = 0;
  220.             close(OUT);
  221.         }
  222.        
  223.     } else  {
  224.         # image complete!
  225.         print STDERR int($cpt++/$fps),"s (",int(100*($cpt-2)/($fps*$time)),"%)\r";
  226.         sleep(5) if ($cpt%100)==0; # on fait une pause régulière pour ne pas surchauffer le processeur
  227.        
  228.         # lecture de l'image
  229.         my $img = Image::Magick->new();
  230.         $img->Read($name);
  231.         unlink $name;
  232.         $img->Set(colorspace=>$LINEAR_SPACE);
  233.        
  234.         # on force la saturation (140%) pour avoir des couleurs plus franches
  235.         $img->Modulate(saturation=>$satur);
  236.         $img->Evaluate(operator=>'Multiply', value=>255/$max);
  237.        
  238.         # trammage
  239.         my @px = $img->GetPixels(height=>$h, normalize=>"True");
  240.         undef $img;
  241.         for my $c (@px) {$c = int($c*255);}
  242.        
  243.         # dither
  244.         my @im = (0,0,0)x($w*$h*4);
  245.         for my $y (0..$h-1) {
  246.             for my $x (0..$w-1) {
  247.                 my(@p) = splice(@px, 0, 3);
  248.                 my($d) = $mat[$x%$mat_x][$y%$mat_y];
  249.                 my($p) = ($y*$w*2 + $x)*6;
  250.                
  251.                 @p = ($d4->($p[0],$d),$d4->($p[1],$d),$d3->($p[2],$d));
  252.                
  253.                 if($zigzag & $x) {
  254.                     @im[($p,$p+2,$p+3,$p+5)] = @p[(0,2,0,2)];
  255.                     $p += $w*6;
  256.                     @im[($p+1,$p+4)]   = @p[(1,1)];
  257.                 } else {
  258.                     @im[($p+1,$p+4)]   = @p[(1,1)];
  259.                     $p += $w*6;
  260.                     @im[($p,$p+2,$p+3,$p+5)] = @p[(0,2,0,2)];
  261.                 }
  262.             }
  263.         }
  264.         $img = &px2img($w*2,$h*2, @im);
  265.         $img->Write("tmp/toto.png");
  266.        
  267.         # ajout de l'image au gif animé
  268.         $img->Set(dispose=>"None");
  269.         $img->Set(delay=>int(100/$fps));
  270.         push(@$gif, $img);
  271.         undef $img;
  272.        
  273.         # pas plus de 3000 imgs (5mins)
  274.         last if $cpt==3000 || !$run;
  275.     }
  276. }
  277. close(IN);
  278. close(OUT);
  279.  
  280. # ecriture du fichier gif
  281. $gif->Set(dispose=>"None");
  282. $gif->Set(Layers=>"optimize-trans");
  283. $gif->Set(delay=>int(100/$fps));
  284. $gif->Write($file);
  285.  
  286. sub init_magick {
  287.     eval 'use Image::Magick;';
  288.    
  289.     # determination de l'espace RGB lineaire
  290.     my $img = Image::Magick->new(size=>"256x1", depth=>16);
  291.     $img->Read('gradient:black-white');
  292.     $img->Set(colorspace=>'RGB');
  293.     #$img->Set(colorspace=>"Gray") unless $coul;
  294.     my @px1 = $img->GetPixel(x=>128, y=>0);
  295.     $img->Read('gradient:black-white');
  296.     $img->Set(colorspace=>'sRGB');
  297.     #$img->Set(colorspace=>"Gray") unless $coul;
  298.     my @px2 = $img->GetPixel(x=>128, y=>0);
  299.     my $d1 = $px1[0]-0.5; $d1=-$d1 if $d1<0;
  300.     my $d2 = $px2[0]-0.5; $d2=-$d2 if $d2<0;
  301.     $LINEAR_SPACE = $d1>=$d2 ? "RGB" : "sRGB";
  302.     #print $px1[0], "   ",$px2[0],"    $LINEAR_SPACE\n";
  303. }
  304.  
  305. sub px2img {
  306.     my($width,$height,@px) = @_;
  307.  
  308.     open(OUT2,">/tmp/.toto2.pnm");
  309.     print OUT2 "P6\n$width $height\n255\n",pack('C*', @px),"\n";
  310.     close(OUT2);
  311.     my $img2 = Image::Magick->new();
  312.     $img2->ReadImage("/tmp/.toto2.pnm");
  313.     unlink "/tmp/.toto2.pnm";
  314.  
  315.     return $img2;
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement