%Michael Koehle
%2015
% This code is provided as is with no guaranty of any sort

%user variables
dir = 'C:\Users\mkoehle\Documents\ART\Instructables\Painted Relief\OctaveCode\'; %path to your image
fileName = 'trish.jpg'; %image name
width = 15.2; % desired output in inches this is actually the width
height = 13.52; % desired height output in inches 
zDepth = 1.48; % desired depth of relief
%end user variables


rawImage = imread([dir fileName]);

if length(size(rawImage))==3  %full color
    intensityIm = sum(rawImage,3)/3/255;
else %b&w
    intensityIm = double(rawImage)/255;
end
intensityIm = fliplr(intensityIm);
% intensityIm = imrotate(intensityIm,180);
sizeIm = size(intensityIm);

horzScale = max([sizeIm(1)/height, sizeIm(2)/width]);
vertScale = 1/zDepth;

[meshX,meshY] = meshgrid(1:length(intensityIm(1,:)), 1:length(intensityIm(:,1)));

meshX = meshX/horzScale;
meshY = meshY/horzScale;
Z =intensityIm/vertScale;

compression = 'bin';
surf2stl([fileName(1:length(fileName)-4), '_Surface.stl',],meshX, meshY, Z,compression);

%
