function varargout = Frontend(varargin)
%FRONTEND M-file for Frontend.fig
%      FRONTEND, by itself, creates a new FRONTEND or raises the existing
%      singleton*.
%
%      H = FRONTEND returns the handle to a new FRONTEND or the handle to
%      the existing singleton*.
%
%      FRONTEND('Property','Value',...) creates a new FRONTEND using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to Frontend_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      FRONTEND('CALLBACK') and FRONTEND('CALLBACK',hObject,...) call the
%      local function named CALLBACK in FRONTEND.M with the given input
%      arguments.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Frontend

% Last Modified by GUIDE v2.5 06-Aug-2014 13:53:40

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Frontend_OpeningFcn, ...
                   'gui_OutputFcn',  @Frontend_OutputFcn, ...
                   'gui_LayoutFcn',  [], ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
   gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Frontend is made visible.
function Frontend_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

% Choose default command line output for Frontend
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Frontend wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Frontend_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in dir.
function dir_Callback(hObject, eventdata, handles)
    [FileName, PathName] = uigetfile('*.*', 'Choose Picture'); 
    FilePath = strcat(PathName, FileName);
    set(hObject, 'String', FilePath);
    makeStream(hObject, FilePath);
    
    

% hObject    handle to dir (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

function makeStream(hObject, dir)
    spiel = 12;  %Spiel in Schritten, die beim Richtungswechsel verloren gehen
    IMG = imread(dir);  %Layout einladen
    IMG = imcomplement(IMG);  %Layot Invertieren
    [y, x] = find(IMG);  %x- und y-Koordinaten aller Punkte ermitteln
    x_min = min(x);  %Kleinste x-Stelle finden
    x_max = max(x);  %...
    y_min = min(y);
    y_max = max(y);
    IMG2 = IMG(y_min:y_max, x_min:x_max);  %Neues Layout mit kleinstmöglichen Maßen erstellen
    [y_range, x_range] = size(IMG2);  %Größe des neuen Layouts ermitteln
    for i = 1:2:x_range-1  %Jede zweite Zeile umdrehen
        IMG2(:,i) = IMG2(end:-1:1,i);  
    end
    IMG = (ones(y_range + spiel, x_range)==1);  %neues leeres Bild erstellen, das um das Spiel größer ist
    IMG(1:y_range,1:x_range) = IMG2; %Layout in neues Bild einfügen
    imshow(IMG); %Bild darstellen
    [y_range, x_range] = size(IMG);  %Größe des neuen Bildes berechnen
    stream = IMG(1,:);  %erste Zeile des Bildes in Stream einfügen
    for i = 2:y_range %Restliche Zeilen in Stream einfügen
        stream = horzcat(stream, IMG(i,:));
    end
    
