Skip to content
Snippets Groups Projects
Commit 48aac244 authored by vojtatom's avatar vojtatom
Browse files

Adding basic functionality

parent 7bbc0cef
No related branches found
No related tags found
No related merge requests found
{
"files.associations": {
"iostream": "cpp",
"iosfwd": "cpp"
}
}
\ No newline at end of file
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image.h"
#include "stb/stb_image_write.h"
#include "include/editor.hpp"
#include <iostream>
Editor::Editor()
: image_stack_filled(0)
{
for (size_t i = 0; i < STACKSIZE; i++)
images[i].image = nullptr;
}
Editor::~Editor()
{
for (size_t i = 0; i < STACKSIZE; i++)
if (images[i].image != nullptr)
stbi_image_free(images[i].image);
}
int Editor::load_image(const string & filename)
{
if (image_stack_filled == STACKSIZE - 1)
return NOIMAGESPACE;
Image & image = images[image_stack_filled];
//hardcoded BW COLOR
image.filename = filename;
image.image = stbi_load(filename.c_str(),
&image.width, &image.height,
&image.channels, STBI_grey);
cout << &(image.image) << endl;
return OPOK;
}
int Editor::drop_image(string filename){
size_t index;
for (index = 0; index < STACKSIZE; index++)
if (images[index].filename == filename)
break;
if (index >= STACKSIZE || images[index].image == nullptr)
return EMPTYIMAGESLOT;
stbi_image_free(images[index].image);
images[index].image = nullptr;
images[index].filename.clear();
return OPOK;
}
const Image * Editor::get_image(size_t index) const
{
if (index >= STACKSIZE || images[index].image == nullptr)
return nullptr;
return images + index;
}
\ No newline at end of file
#pragma once
#include <cstdlib>
#include <string>
#define STACKSIZE 16
#define NOIMAGESPACE -1
#define EMPTYIMAGESLOT -1
#define OPOK 0
using namespace std;
struct Image {
string filename;
unsigned char * image;
int width;
int height;
int channels;
};
class Editor {
public:
Editor();
~Editor();
int load_image(const string & filename);
int drop_image(string filename);
const Image * get_image(size_t index) const;
protected:
Image images[STACKSIZE];
size_t image_stack_filled;
};
\ No newline at end of file
#pragma once
#include "editor.hpp"
class Interface {
public:
Interface();
void run();
protected:
void load_image(istringstream & iss);
void drop_image(istringstream & iss);
void print_help() const;
void print_image_stack() const;
Editor editor;
};
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include "include/interface.hpp"
#include "include/editor.hpp"
#define PROMPT ">>>"
using namespace std;
Interface::Interface()
{
cout << "Initializing the application..." << endl;
}
void Interface::run()
{
cout << PROMPT << flush;
string line, input;
//main loop until exit
while (getline(cin, line))
{
if (!line.empty())
{
istringstream iss(line);
iss >> input;
if (input == "open")
load_image(iss);
if (input == "drop")
drop_image(iss);
if (input == "help")
print_help();
if (input == "images")
print_image_stack();
if (input == "exit")
{
iss.clear();
return;
}
}
if (cin.eof())
return;
cout << PROMPT << flush;
}
}
void Interface::load_image(istringstream &iss)
{
string filename;
iss >> filename;
if (filename.empty())
{
cout << "command: open <filename>" << endl;
return;
}
cout << "loading " << filename << endl;
int status = editor.load_image(filename);
if (status == OPOK)
cout << "image " << filename << " loaded" << endl;
else
cout << "there was some error loading the image" << endl;
}
void Interface::drop_image(istringstream &iss)
{
string filename;
iss >> filename;
if (filename.empty())
{
cout << "command: drop <filename>" << endl;
return;
}
cout << "dropping " << filename << endl;
int status = editor.drop_image(filename);
if (status == OPOK)
cout << "image " << filename << " dropped" << endl;
else
cout << "there was some error loading the image" << endl;
}
void Interface::print_help() const
{
cout << "Overview of available commands:\n\n"
" open <filename> loads image from file onto image stack\n\n"
" drop <filename> drop first image with corresponding\n"
" filename from image stack\n\n"
" exit exit the application\n\n";
}
void Interface::print_image_stack() const{
for (size_t i = 0; i < STACKSIZE; i++)
{
const Image * image = editor.get_image(i);
cout << i;
if (image == nullptr)
{
cout << ": no entry" << endl;
continue;
}
cout << ": " << image->filename << " : " << image->width << "x" << image->height << endl;
}
}
\ No newline at end of file
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image.h"
#include "stb/stb_image_write.h"
#include "include/interface.hpp"
int main(int argc, char const *argv[])
{
//init application interface
Interface interface;
//run the interface
interface.run();
int width, height, channels;
//stbi_set_flip_vertically_on_load(true);
/*int width, height, channels;
unsigned char *image = stbi_load("cicabw.png", &width, &height, &channels, STBI_grey);
stbi_write_png("output.png", width, height, 1, image, 0);
stbi_image_free(image);
stbi_image_free(image); */
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment