Skip to content

Instantly share code, notes, and snippets.

@crazymonkyyy
Created May 2, 2023 16:11
Show Gist options
  • Save crazymonkyyy/0f0a74408269c6b15508a4fb26175f84 to your computer and use it in GitHub Desktop.
Save crazymonkyyy/0f0a74408269c6b15508a4fb26175f84 to your computer and use it in GitHub Desktop.
#!/usr/bin/env -S sh -c 'dmd -run "$0" "$1" "$2" "$3" | ffmpeg -f rawvideo -pixel_format rgb24 -video_size $1x$2 -i pipe: -preset ultrafast $(basename "$0" .d).mp4'
import std;
enum pixels=32;
ubyte[3*pixels] pixels_;
int sentpixels;
pragma(inline):
//void pix(T,S,U)(T r,S b,U g){
void pix(int r,int g,int b){
sentpixels++;
static int i=0;
pixels_[i*3+0]=cast(ubyte)r;
pixels_[i*3+1]=cast(ubyte)g;
pixels_[i*3+2]=cast(ubyte)b;
i++;
if(i==pixels){
stdout.rawWrite(pixels_);
i=0;
}
}
void pix(ubyte[] a){
//pix(a[0]); //works when pix(int)
//pix(a[0],a[0],a[0]);//doesnt work
pix(a[0],a[1],a[2]);//doesnt work
//pix!(ubyte,ubyte,ubyte)(a[0],a[1],a[2]);//doesnt work
//pix!(int,int,int)(a[0],a[1],a[2]); //works
}
void pix(int a){
pix(a,a,a);
}
void main(string[] s){
int width=s[1].to!int;
int h=s[2].to!int;
int frames=s[3].to!int;
int w=executeShell("convert lain.jpg -format \"%w\" info:").output.to!int;
ubyte[] data=cast(ubyte[])executeShell("convert lain.jpg rgb:-").output;
int x=0;
int y=0;
int xv=1;
int yv=1;
int imageh=cast(int)data.length/(w*3);
//assert(imageh==648);
//imageh--;
foreach(i;0..frames){
sentpixels=0;
auto datai=0;
foreach(y_;0..y){
sentpixels=0;
foreach(zzz;0..width){
pix(0,0,0);
}
assert(sentpixels==width);
}
//assert(sentpixels==y*width,sentpixels.to!string~":"~(y*width).to!string);
foreach(y_;0..imageh){
sentpixels=0;
foreach(x_;0..x){
pix(0,0,0);
}
foreach(x_;0..w){
//pix(datai);
pix(data[datai*3..(datai+1)*3]);
//pix(datai,datai,datai);
datai++;
}
foreach(x_;0..width-x-w){
pix(0,0,0);
}
assert(sentpixels==width);
}
foreach(y_;0..h-imageh-y){
foreach(zzz;0..width){
pix(0,0,0);
}
}
//assert(sentpixels==width*h,sentpixels.to!string);
x+=xv;
y+=yv;
if(x==0){xv*=-1;}
if(y==0){yv*=-1;}
if(x==width-w){xv*=-1;}
if(y==h-imageh){yv*=-1;}
if(x==0&&y==0){return;}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment