画像のWAVファイル化 誤差拡散2値化付
画像をオシロスコープ用の音声信号に変換します。
解像度大きめの画像用に飛ばして走査
使用例 http://www.youtube.com/watch?v=1h8QF0g4kPw
/**
* Copyright TheCoolMuseum ( http://wonderfl.net/user/TheCoolMuseum )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/jMQQ
*/
// forked from TheCoolMuseum's forked from: 画像のWAVファイル化
// forked from TheCoolMuseum's 画像のWAVファイル化
// forked from TheCoolMuseum's ローカルファイルの読み込み・保存
// 画像をオシロスコープ用の音声信号に変換します。
// 解像度大きめの画像用に飛ばして走査
// 使用例 http://www.youtube.com/watch?v=1h8QF0g4kPw
package {
import flash.utils.IDataInput;
import flash.display.InterpolationMethod;
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.ByteArray;
import flash.utils.Endian;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.net.FileReference;
import flash.net.FileFilter;
public class FlashTest extends Sprite {
private var fileLoader:FileReference;
private var fileSaver:FileReference;
private var imageLoader:Loader;
private var wavBuffer:ByteArray;
private var preview:Bitmap;
private var uIx:Array = [0,2,1,3,0,2,1,3,0,2,1,3,0,2,1,3];
private var vIx:Array = [0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3];
public function makeBtn(name:String):TextField{
var btn:TextField = new TextField();
btn.text = name;
btn.autoSize = TextFieldAutoSize.LEFT;
btn.border = true;
btn.selectable = false;
return btn;
}
public function FlashTest() {
preview = new Bitmap();
preview.y = 60;
addChild(preview);
var loadBtn:TextField = makeBtn("LOAD");
addChild(loadBtn);
loadBtn.addEventListener(MouseEvent.CLICK, onClickLoadBtn);
var saveBtn:TextField = makeBtn("SAVE");
saveBtn.y = 32;
addChild(saveBtn);
saveBtn.addEventListener(MouseEvent.CLICK, onClickSaveBtn);
}
private function onClickLoadBtn(e:Event):void{
load();
}
private function load():void{
var fileFilter:FileFilter = new FileFilter("Images (.png .jpg .gif)", "*.png;*.jpg;*.gif");
fileLoader = new FileReference();
fileLoader.addEventListener(Event.SELECT, onSelectLoadFile);
fileLoader.addEventListener(Event.COMPLETE, onCompleteLoadFile);
fileLoader.browse([fileFilter]);
}
private function onSelectLoadFile(e:Event):void{
fileLoader.load();
}
private function onCompleteLoadFile(e:Event):void{
imageLoader = new Loader();
imageLoader.loadBytes(fileLoader.data);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteLoadImage);
}
private function onCompleteLoadImage(e:Event):void{
var width:int = imageLoader.width;
var height:int = imageLoader.height;
var bitmapData:BitmapData = new BitmapData(width, height);
bitmapData.draw(imageLoader);
errorDiffusion(bitmapData);
preview.bitmapData = bitmapData;
var vNum:int =Math.floor(height/4);
var uNum:int =Math.floor(width/4);
var dataBuffer:ByteArray = new ByteArray();
dataBuffer.endian = Endian.LITTLE_ENDIAN;
for(var i:int=0; i<16; i++){
for(var v:int=0; v<vNum; v++){
for(var u:int=0; u<uNum; u++){
var pV:int = v*4 + vIx[i];
var pU:int = u*4 + uIx[i];
if(pU<=width && pV<=height){
var color:uint = bitmapData.getPixel(pU, pV);
var c:Number = (color&0xff) + ((color>>8)&0xff) + ((color>>16)&0xff);
if(c>384){
var l:int = 32768-int(pV/height*65535);
var r:int = int(pU/width*65535)-32768;
dataBuffer.writeShort(l);
dataBuffer.writeShort(r);
}
}
}
}
}
var fmtBuffer:ByteArray = new ByteArray();
fmtBuffer.endian = Endian.LITTLE_ENDIAN
fmtBuffer.writeShort(1); //PCM
fmtBuffer.writeShort(2); //Stereo
fmtBuffer.writeUnsignedInt(44100); //SampleRate
fmtBuffer.writeUnsignedInt(44100*4); //DataRate
fmtBuffer.writeShort(4); //4Byte = 16bit*Stereo
fmtBuffer.writeShort(16); //16bit
var loopBuffer:ByteArray = new ByteArray();
loopBuffer.endian = Endian.LITTLE_ENDIAN
loopBuffer.writeUnsignedInt(0); //Manufacturer
loopBuffer.writeUnsignedInt(0); //Product
loopBuffer.writeUnsignedInt(0); //Sample Period
loopBuffer.writeUnsignedInt(0x3C); //MIDI Unity Note
loopBuffer.writeUnsignedInt(0); //MIDI Pitch Fraction
loopBuffer.writeUnsignedInt(0); //SMPTE Format
loopBuffer.writeUnsignedInt(0); //SMPTE Offset
loopBuffer.writeUnsignedInt(1); //Num Sample Loops
loopBuffer.writeUnsignedInt(0); //Sampler Data
loopBuffer.writeUnsignedInt(0); //Cue Point ID
loopBuffer.writeUnsignedInt(0); //Type
loopBuffer.writeUnsignedInt(0); //Start
loopBuffer.writeUnsignedInt(dataBuffer.length/4); //End
loopBuffer.writeUnsignedInt(0); //Fraction
loopBuffer.writeUnsignedInt(0); //Play Count
wavBuffer = new ByteArray();
wavBuffer.endian = Endian.LITTLE_ENDIAN
wavBuffer.writeUTFBytes("RIFF") ;
wavBuffer.writeUnsignedInt(36 + fmtBuffer.length + dataBuffer.length + loopBuffer.length);
wavBuffer.writeUTFBytes("WAVE") ;
wavBuffer.writeUTFBytes("fmt ") ;
wavBuffer.writeUnsignedInt(fmtBuffer.length);
wavBuffer.writeBytes(fmtBuffer);
wavBuffer.writeUTFBytes("data") ;
wavBuffer.writeUnsignedInt(dataBuffer.length);
wavBuffer.writeBytes(dataBuffer);
wavBuffer.writeUTFBytes("smpl") ;
wavBuffer.writeUnsignedInt(loopBuffer.length);
wavBuffer.writeBytes(loopBuffer);
}
private function onClickSaveBtn(e:Event):void{
save();
}
private function save():void{
fileSaver = new FileReference();
fileSaver.save(wavBuffer,fileLoader.name.split(".")[0]+".wav");
}
private function errorDiffusion(img:BitmapData):BitmapData{
var width:int = img.width;
var height:int = img.height;
var array:ByteArray = img.getPixels(img.rect);
var l:int=array.length;
for(var y:int=0; y<height; y++){
var pos:int = y*width*4;
for(var x:int=0; x<width; x++){
var c:int = array[pos+1]+array[pos+2]+array[pos+3];
c /= 3;
var dst:Number = c>127 ? 255 : 0;
var err:Number = c>127 ? c-255 : c;
var c1:int = err*5/16;
var c2:int = err*3/16;
array[pos] = 0xff;
array[pos+1] = dst;
array[pos+2] = dst;
array[pos+3] = dst;
if(y<height-1){
array[pos+width*4+1] = clip8bit((array[pos+width*4+1]+array[pos+width*4+2]+array[pos+width*4+3])/3 + c1);
array[pos+width*4+2] = array[pos+width*4+1];
array[pos+width*4+3] = array[pos+width*4+1];
if(x<width-1){
array[pos+5] = clip8bit((array[pos+5] +array[pos+6] +array[pos+7])/3 + c1);
array[pos+6] = array[pos+5];
array[pos+7] = array[pos+5];
array[pos+width*4+5] = clip8bit((array[pos+width*4+5] + array[pos+width*4+6] + array[pos+width*4+7])/3 + c2);
array[pos+width*4+6] = array[pos+width*4+5];
array[pos+width*4+7] = array[pos+width*4+5];
}
if(x>0){
array[pos+width*4-3] = clip8bit((array[pos+width*4-3] + array[pos+width*4-2] + array[pos+width*4-1])/3 + c2);
array[pos+width*4-2] = array[pos+width*4-3];
array[pos+width*4-1] = array[pos+width*4-3];
}
}else{
if(x<width-1){
array[pos+5] = clip8bit((array[pos+5] + array[pos+6] + array[pos+7])/3 + c1);
array[pos+6] = array[pos+5];
array[pos+7] = array[pos+5];
}
}
pos+=4;
}
}
array.position=0;
img.setPixels(img.rect, array);
return img;
}
private function clip8bit(n:int):int{
return Math.min(255,Math.max(0,n));
}
}
}