no code formating so you'll have to do a copy paste to a compiler. Essentially a command line tool so just type the names of the files you want to average after the command line and it'll output a SAM_AVERG.RAW. Its hard coded to work with NX10 and NX100 Raws but if you find the raw size for the other camera's you can just update the len variable number. It'll work with as many files as you can fit to the command line but don't exceed 16 as you may overflow the shorts I've used in memory (you're welcome to change this).
-
include
-
include
int main (int argc,char
argv[]){
FILE
file_id=NULL;
unsigned int srw_start=0;
FILE
file_w;
unsigned char b1=0;
unsigned char b2=0;
unsigned char b3=0;
unsigned short s1=0;
unsigned short s2=0;
unsigned short sm=0;
unsigned int len=22028832;
unsigned int s_len=len*4/3;
unsigned int short_count=0;
unsigned int num_files=argc-1;
unsigned char
mem_b;
read buffer
unsigned char
mem_b2;
write buffer
unsigned char
mem_b3;
header buffer
unsigned short
mem_s;
pointer for short memory
mem_b=(unsigned char*)malloc(len);
allocate image to be loaded
mem_b2=(unsigned char*)calloc(len*2,1);
allocate short memory and set it to all zero
mem_s=(unsigned short*)mem_b2;
cast it as shorts
for (int n=1;n
if(file_id!=NULL){
fclose(file_id);
}
file_id=fopen(argv[n],"rb");
if (file_id==NULL){
printf("could not open file to read \n");
printf(argv[n]);
return 0;
}else{
printf(argv[n]);
printf("\n");
}
fseek(file_id,0,SEEK_END);
srw_start=ftell(file_id);
srw_start=srw_start-len;
fseek(file_id,srw_start,SEEK_SET);
fread(mem_b,1,len,file_id);
short_count=0;
for (unsigned int i=0;i
b1=mem_b[i];
b2=mem_b[i+1];
b3=mem_b[i+2];
s1=b1;
s1=s1
sm=b2>
>
4;
shift the next byte 4 bits to the left so you're only left with the bottom 4
s1=s1+sm;
add them together and you have your first short
s2=b3;
3 byte is the lower 8 bits of the next short
sm=b2&0x0F;
blanks out the top 4 bits of the middle byte (you could also try shifting it 4 left)
sm=sm
s2=s2+sm;
add the beginning and end together
mem_s[short_count]=mem_s[short_count]+s1;
write the first short to memory
mem_s[short_count+1]=mem_s[short_count+1]+s2;
write the second short to memory
short_count=short_count+2;
}
}
average
for(unsigned int n=0;n
mem_s[n]=mem_s[n]
num_files;
}
packing and endian convert. The intel stors the most signficant byte last while the file wants it first so bytes have to be moved around
short_count=0;
for (unsigned int i=0;i
s1=mem_s[short_count];
I've done this in steps to keep it simple to understand you can do it on three lines if you wish
s1=s1>
>
4;
shift the the top 8 bits of the 12 bit number to the bottom (one byte)
b1=s1;
fist byte now equal to first 8 bits
s2=mem_s[short_count];
still working off the first 12 bits now we need to put the last 4 bits in the second byte
s2=s2&0x000F;
clears the top 12 bits
s2=s2
sm=mem_s[short_count+1]&0x0FFF;
now we need the first four bits of the next 12bit number so blank out the top 4 just in case
sm=sm>
>
8;
shift the remaining bits down 8 and we should have the first 4 bits of the second 12 bits at the bottom of the short
s2=s2+sm;
add the top and bottom bits together
b2=s2;
put it in the second byte
s2=mem_s[short_count+1];
now we need the last 8 bits of the second short in the last byte
s2=s2&0x00FF;
clear everything except the last 8 bits
b3=s2;
put the last 8 bits in the last byte
mem_b[i]=b1;
write bytes to memory
mem_b[i+1]=b2;
mem_b[i+2]=b3;
short_count=short_count+2;
}
free(mem_b2);
mem_b3=(unsigned char*)malloc(srw_start);
fseek(file_id,0,SEEK_SET);
fread(mem_b3,1,srw_start,file_id);
fclose(file_id);
file_w=fopen("SAM_AVER.SRW","wb");
fwrite(mem_b3,1,srw_start,file_w);
fwrite(mem_b,1,len,file_w);
fclose(file_w);
free(mem_b);
free(mem_b3);
}