function [] = pic2laser(inputname, outputname, command, resolution, travel_speed, laser_speed, power, x_lim, y_lim)

clc
disp('Processing Image...     ');
    
x_max = floor(x_lim / 10 * resolution);
y_max = floor(y_lim / 10 * resolution);
original = imread(inputname);
[y_pixels, x_pixels, z_colors] = size(original);
x_scale = x_max / x_pixels;
y_scale = y_max / y_pixels;
scalar = min([x_scale y_scale]);
if scalar > 1;
    scalar = 1;
end
scaled = imresize(original, scalar);
gray = 255 - rgb2gray(scaled);
gray = floor(power/100 .* gray);
render = 255 - gray;
[y_dim, x_dim] = size(gray);

output = fopen(outputname, 'w');
fprintf(output, '%s\r\n', [command ' S0']);
fprintf(output, '%s\r\n', 'G28 X0 Y0');
fprintf(output, '%s\r\n', 'G90');
fprintf(output, '%s\r\n', 'G21');
fprintf(output, '%s\r\n', ['G0 F' num2str(travel_speed)]);
laser_threshold = 25;
laser_enabled = 0;

for y = 1:y_dim
    clc
    disp(['Exporting GCODE...     ' num2str(round(y/y_dim*100)) '%']);
    for x = 1:x_dim
        if (mod(y,2) == 0) && (gray(y_dim - y + 1, x_dim - x + 1) > laser_threshold)
            if laser_enabled == 0
                laser_enabled = 1;
                fprintf(output, '%s\r\n', ['G0 X' num2str(x_lim - (x-1)/x_dim*x_lim) ' Y' num2str((y-1)/y_dim*y_lim)]);
                fprintf(output, '%s\r\n', 'M400');
                fprintf(output, '%s\r\n', [command ' S' num2str(gray(y_dim - y + 1, x_dim - x + 1))]);
            else
                fprintf(output, '%s\r\n', [command ' S' num2str(gray(y_dim - y + 1, x_dim - x + 1))]);
                fprintf(output, '%s\r\n', ['G0 X' num2str(x_lim - (x-1)/x_dim*x_lim) ' Y' num2str((y-1)/y_dim*y_lim) ' F' num2str(laser_speed)]);
                fprintf(output, '%s\r\n', ['G0 F' num2str(laser_speed)]);
            end
        elseif (mod(y,2) ~= 0) && (gray(y_dim - y + 1, x) > laser_threshold)
            if laser_enabled == 0
                laser_enabled = 1;
                fprintf(output, '%s\r\n', ['G0 X' num2str((x-1)/x_dim*x_lim) ' Y' num2str((y-1)/y_dim*y_lim)]); 
                fprintf(output, '%s\r\n', 'M400');
                fprintf(output, '%s\r\n', [command ' S' num2str(gray(y_dim - y + 1, x))]);
            else
                laser_enabled = 1;
                fprintf(output, '%s\r\n', [command ' S' num2str(gray(y_dim - y + 1, x))]);
                fprintf(output, '%s\r\n', ['G0 X' num2str((x-1)/x_dim*x_lim) ' Y' num2str((y-1)/y_dim*y_lim) ' F' num2str(laser_speed)]);            
                fprintf(output, '%s\r\n', ['G0 F' num2str(laser_speed)]);
            end
        elseif laser_enabled == 1
            fprintf(output, '%s\r\n', [command ' S0']);
            fprintf(output, '%s\r\n', 'M400');
            fprintf(output, '%s\r\n', ['G0 F' num2str(travel_speed)]);
            laser_enabled = 0;
        end
    end
end        

fprintf(output, '%s\r\n', [command ' S0']);
fprintf(output, '%s\r\n', 'G28 X0 Y0');
fprintf(output, '%s\r\n', 'M84');

clc
disp('Process Complete');
imshow(render);
fclose('all');

end

