Skip to content

Instantly share code, notes, and snippets.

@jeesay
Created September 14, 2011 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeesay/1215945 to your computer and use it in GitHub Desktop.
Save jeesay/1215945 to your computer and use it in GitHub Desktop.
image_dump.ijm
// Image dump
// Jean-Christophe Taveau
// http://crazybiocomputing.blogspot.com
setBatchMode(true);
// M A I N
// 1- Get information from current active image
in=getTitle();
w=getWidth();
h=getHeight();
bypp=bitDepth()/8; // bytes per pixel
size=w*h;
fmt="char";
little=false;
// 2- Display dialog box
modes=newArray("ROI","dump");
words=newArray("byte","2-byte","3-byte","4-byte");
display=newArray("numeric","hexadecimal","alphanumeric");
Dialog.create("Image Dump");
Dialog.addChoice("Mode:",modes);
Dialog.addChoice("Display:",display);
Dialog.addChoice("Word:",words);
Dialog.addNumber("Offset:",0);
Dialog.addNumber("Size:",size);
Dialog.addCheckbox("Little-endian",false);
Dialog.show();
mode=Dialog.getChoice();
tmp=Dialog.getChoice();
options=0;
if (tmp=="hexadecimal")
options=options + 2;
else if (tmp=="alphanumeric")
options = options + 4;
fmt = Dialog.getChoice();
offset = Dialog.getNumber();
size = Dialog.getNumber();
roi=(mode=="ROI");
options=options + Dialog.getCheckbox()*8; //endianness
print(options);
// check the options
if (roi == 1)
if ( (options & 4) == 4 && bypp != 1)
{
showMessage("Error","The 'alphanumeric' display is only available for 8-bit image in ROI mode");
exit();
}
if (roi == 1)
{
getSelectionBounds(x, y, wROI, hROI);
offset=x+y*w;
length=wROI;
skip=w-wROI;
word=1;
size=w * hROI + wROI;
}
else {
skip=0;
length=40;
}
word=0;
if (fmt=="byte")
word=1;
else if (fmt=="2-byte")
word=2;
else if (fmt=="3-byte")
word=3;
else if (fmt=="4-byte")
word=4;
// 3- Create table
if (options & 4 == 4)
table="dump_" + fmt +"_little-endian";
else
table="dump_" + fmt +"_big-endian";
f="["+table+"]";
run("Table...", "name=" + f + " width=900 height=450");
// 1- Create the columns by defining the headings
heads="index";
for (i=0;i<length;i++) {
heads= heads + '\t' ;
if (floor(i/26)!=0)
heads= heads + fromCharCode(floor(i/26)-1+65);
heads= heads + fromCharCode((i%26)+65);
}
print(f,"\\Headings:"+heads);
// 4- Create dump
createContents(word,offset,length*word,options);
setBatchMode(false);
exit();
// F U N C T I O N S
function createContents(num,off,len,opts)
{
hexa=(opts & 2 == 2);
little=(opts & 8 == 8);
print(hexa,little);
start=off;
count=0;str=toString(count);
shift=0;
if (little)
shift=num-1;
print(start, size, num, len, skip);
for (i=0;i<size;i+=num)
{
selectWindow(in);
if (roi)
str=str + "\t" + getPix(hexa,start+i,num,little);
else
str=str + "\t" + getWord(hexa,start+i,num,little);
count++;
x=i%w;
if (i!=0 && (x % len)==0)
{
print(f,str);
str=toString(count);
i+=skip;
}
}
print(f,str);
}
function getWord(disp,index,bytes,lendian)
{
value=0;
str="";
for (j=0;j<num;j++)
{
if (lendian)
k=num-1-j;
else
k=j;
x=(index+j)%w;y=floor((index+j)/w);
value=value | (getPixel(x,y)&0xff << (8*k));
}
if (disp==0) // ASCII
{
if (value<32)
str="\\" + value;
else if (value>=32 && value <127)
str=fromCharCode(pix) ;
else
str=pix;
}
else if (disp==1) // Hexa
{
zeros="";
for (m=0;m<(num*2-lengthOf(toHex(value)));m++)
zeros=zeros+"0";
str=zeros + toHex(value);
}
else
str=toString(value);
return str;
}
function getPix(disp,index,bytes,lendian)
{
x=index%w;y=floor(index/w);
value=getPixel(x,y);
str="";
if (disp==0) // ASCII
{
if (value<32)
str="\\" + value;
else if (value>=32 && value <127)
str=fromCharCode(pix) ;
else
str=toString(value);
}
else if (disp==1) // Hexa
{
zeros="";
for (m=0;m<(num*2-lengthOf(toHex(value)));m++)
zeros=zeros+"0";
str=zeros + toHex(value);
}
else
{
if (bypp==3)
str= toString(value&0xff0000>>16) + "," + toString(value&0xff00>>8) + "," + toString(value&0xff);
else
str=toString(value);
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment