MATLAB/Octave code for KFB reading

  • 1 Replies
  • 294 Views

0 Members and 1 Guest are viewing this topic.

Offline gerrit

  • *
  • 3f
  • ******
  • Posts: 1779
« on: September 24, 2017, 09:50:52 PM »
I've had some fun reading in KFB files in MATLAB and trying various processing on it.
Attached a MATLAB/Octave function to read a KFB file. Hope it is useful for someone.

Oh, can't attached text... below then:

Code: [Select]
function [width,height,counts,trans,maxiter,parts,keys] = readKFB(fn)

% counts are iterations
% don't know what trans is, but counts+1-trans is some form of smoothed
% iteration map.
% parts is number of colors, keys are the colors.
% to make a colormap(cmap):
% > cmap = reshape(keys,3,parts)'/255.;
% > cmap = flipud(fliplr(cmap));

fid = fopen(fn);
d = fread(fid); %unsigned 8 bit numbers
szint = 4;
%d(1:3) == 'KFB' test skipped
k = 4;
nBytes = 4;
width = typecast(uint8(d(k:k+nBytes-1)),'uint32');
k = k + nBytes;
height = typecast(uint8(d(k:k+nBytes-1)),'uint32');
k = k + nBytes;
nCounts = width*height; %32 bit ints
nBytes = 4 * nCounts;
counts = typecast(uint8(d(k:k+nBytes-1)),'uint32');
counts = reshape(counts,height,width);
counts = double(counts);
k = k + nBytes;
nBytes = 4;
iterdiv = typecast(uint8(d(k:k+nBytes-1)),'uint32');
k = k + nBytes;
parts = typecast(uint8(d(k:k+nBytes-1)),'uint32');
k = k + nBytes;
nBytes = parts * 3;
% r,g,b,... parts triples
keys = typecast(uint8(d(k:k+nBytes-1)),'uint8');
keys = double(keys);
k = k + nBytes;
nBytes = 4;
maxiter = typecast(uint8(d(k:k+nBytes-1)),'uint32');
maxiter = double(maxiter);
k = k + nBytes;
floatSize = 4;
nBytes = width*height * floatSize;
trans = typecast(uint8(d(k:k+nBytes-1)),'single');
trans = reshape(trans,height,width);
trans = double(trans);

fclose(fid);

Offline gerrit

  • *
  • 3f
  • ******
  • Posts: 1779
« Reply #1 on: September 26, 2017, 06:10:10 AM »
Here's a better version, minimizing memory. I've been able to load a 32400X21600 map into it (32G of RAM) with only minor swapping.
Code: [Select]
function [width,height,counts,trans,maxiter,parts,keys] = readKFB(fn)

% counts are iterations
% trans is the correction to smooth iteration count based on escape time
% (see wiki)
% parts is number of colors, keys are the colors.
% to make a colormap(cmap):
% > cmap = reshape(keys,3,parts)'/255.;
% > cmap = flipud(fliplr(cmap));

info = dir(fn);
fid = fopen(fn,'r');
d = fread(fid, [1, info.bytes], '*uint8');
fclose(fid);
%d(1:3) == 'KFB' test skipped
k = 4;
nBytes = 4;
width = typecast(uint8(d(k:k+nBytes-1)),'uint32');
width = double(width);
k = k + nBytes;
height = typecast(uint8(d(k:k+nBytes-1)),'uint32');
height = double(height);
k = k + nBytes;
nCounts = width*height; %32 bit ints
nBytes = 4 * nCounts;
counts = typecast(uint8(d(k:k+nBytes-1)),'uint32');
counts = reshape(counts,height,width);
counts = single(counts);
k = k + nBytes;
nBytes = 4;
iterdiv = typecast(uint8(d(k:k+nBytes-1)),'uint32');
k = k + nBytes;
parts = typecast(uint8(d(k:k+nBytes-1)),'uint32');
parts = double(parts);
k = k + nBytes;
nBytes = parts * 3;
% r,g,b,... parts triples
keys = typecast(uint8(d(k:k+nBytes-1)),'uint8');
keys = double(keys);
k = k + nBytes;
nBytes = 4;
maxiter = typecast(uint8(d(k:k+nBytes-1)),'uint32');
maxiter = double(maxiter);
k = k + nBytes;
nBytes = 4 * nCounts;
trans = typecast(uint8(d(k:k+nBytes-1)),'single');
trans = reshape(trans,height,width);


xx
Octave/MATLAB Mandelbrot code

Started by gerrit on Programming

11 Replies
975 Views
Last post October 19, 2017, 05:45:09 AM
by gerrit
xx
If you see someone reading a tome about fractals on the beach...

Started by greentexas on Fractal Humor

1 Replies
283 Views
Last post September 24, 2018, 09:36:34 PM
by gforce
clip
reading off external angles from binary decomposition images

Started by claude on Fractal Mathematics And New Theories

4 Replies
161 Views
Last post May 12, 2019, 06:18:19 PM
by Adam Majewski
xx
Code: (Select) not working?

Started by Anon on Forum Help And Support

3 Replies
165 Views
Last post February 19, 2018, 03:02:31 PM
by 3DickUlus
xx
Code Snippets for Fragmentarium

Started by Sabine62 on Code Snippets (fragments)

0 Replies
124 Views
Last post November 02, 2018, 01:23:11 PM
by Sabine62