Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adamltaylor committed Mar 12, 2019
0 parents commit 898f6c3
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text
*.m text
*.sh text

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
*.mj2 binary
*.tif binary
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.mj2
*.tif
aside
mj2tif-test-input-folder
mj2tif-test-output-folder
repo
9 changes: 9 additions & 0 deletions is_mj2_same_as_tif.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function result = is_mj2_same_as_tif(mj2_file_name, tif_file_name)
% Converts a single .mj2 file at input_file_name to a multi-image .tif
% at output_file_name. Will overwrite pre-existing file at
% output_file_name, if present.

mj2_stack = read_16bit_grayscale_mj2(mj2_file_name) ;
tif_stack = read_16bit_grayscale_tif(tif_file_name) ;
result = isequal(class(mj2_stack), class(tif_stack)) && isequal(size(mj2_stack), size(tif_stack)) && all(all(all(mj2_stack==tif_stack))) ;
end
43 changes: 43 additions & 0 deletions mj2tif.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function mj2tif(input_folder_name, output_folder_name, do_verify)
% Converts each .mj2 file in input_folder_name to a multi-image .tif in
% output_folder_name. Will overwrite pre-existing files in
% output_folder_name, if present.

if nargin<3 ,
do_verify = false ;
end

if ~exist(output_folder_name, 'dir') ,
mkdir(output_folder_name) ;
end
entity_names = setdiff(simple_dir(input_folder_name), {'.' '..'}) ;
entity_count = length(entity_names) ;
for i = 1 : entity_count ,
entity_name = entity_names{i} ;
entity_path = fullfile(input_folder_name, entity_name) ;
if exist(entity_path, 'dir') ,
% if a folder, recurse
output_entity_path = fullfile(output_folder_name, entity_name) ;
mj2tif(entity_path, output_entity_path) ;
else
% if a normal file, convert to .tif if it's a .mj2, or just
% copy otherwise
[~,~,ext] = fileparts(entity_path) ;
if isequal(ext, '.mj2') ,
output_entity_path = fullfile(output_folder_name, replace_extension(entity_name, '.tif')) ;
if ~exist(output_entity_path, 'file') ,
fprintf('About to convert %s...', entity_path);
tic_id = tic() ;
mj2tif_single(entity_path, output_entity_path, do_verify) ;
duration = toc(tic_id) ;
fprintf(' took %g seconds\n', duration) ;
end
else
output_entity_path = fullfile(output_folder_name, entity_name) ;
if ~exist(output_entity_path, 'file') ,
copyfile(entity_path, output_entity_path) ;
end
end
end
end
end
27 changes: 27 additions & 0 deletions mj2tif_single.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function mj2tif_single(input_file_name, output_file_name, do_verify)
% Converts a single .mj2 file at input_file_name to a multi-image .tif
% at output_file_name. Will overwrite pre-existing file at
% output_file_name, if present.

if nargin<3 ,
do_verify = false ;
end

input_file = VideoReader(input_file_name) ;
frame_index = 0 ;
while input_file.hasFrame() ,
frame_index = frame_index + 1 ;
frame = input_file.readFrame() ;
if frame_index == 1 ,
imwrite(frame, output_file_name, 'WriteMode', 'overwrite', 'Compression', 'none') ;
else
imwrite(frame, output_file_name, 'WriteMode', 'append', 'Compression', 'none') ;
end
end

if do_verify ,
if ~is_mj2_same_as_tif(input_file_name, output_file_name) ,
error('%s is not the same as %s\n', output_file_name, input_file_name) ;
end
end
end
15 changes: 15 additions & 0 deletions read_16bit_grayscale_mj2.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function stack = read_16bit_grayscale_mj2(file_name)
input_file = VideoReader(file_name) ;
n_rows = input_file.Height ;
n_cols = input_file.Width ;
n_pages_approx = ceil(input_file.FrameRate * input_file.Duration) ; % hopefully big enough
stack = zeros([n_rows n_cols n_pages_approx], 'uint16') ;
frame_index = 0 ;
while input_file.hasFrame() ,
frame_index = frame_index + 1 ;
frame = input_file.readFrame() ;
stack(:,:,frame_index) = frame ;
end
n_pages = frame_index ;
stack = stack(:,:,1:n_pages) ;
end
16 changes: 16 additions & 0 deletions read_16bit_grayscale_tif.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function stack = read_16bit_grayscale_tif(file_name)
info = imfinfo(file_name, 'tif') ;
n_pages = length(info) ;
if n_pages>0 ,
first_frame_info = info(1) ;
n_cols = first_frame_info.Width;
n_rows = first_frame_info.Height;
else
n_cols = 0 ;
n_rows = 0 ;
end
stack = zeros([n_rows n_cols n_pages], 'uint16');
for i = 1:n_pages ,
stack(:,:,i) = imread(file_name, 'Index', i, 'Info', info) ;
end
end
5 changes: 5 additions & 0 deletions replace_extension.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function result = replace_extension(file_name, new_extension)
% Replace the extension of the given file_name with the new_extension
[path_to_parent, base_name, ~] = fileparts(file_name) ;
result = fullfile(path_to_parent, horzcat(base_name, new_extension)) ;
end
6 changes: 6 additions & 0 deletions simple_dir.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function file_names = simple_dir(folder_name)
% Does what I wish dir() did: just return a cell array of the directory
% entries, without all the other stuff.
s = dir(folder_name) ;
file_names = {s.name} ;
end

0 comments on commit 898f6c3

Please sign in to comment.