Skip to content

Commit

Permalink
Update Main.cpp
Browse files Browse the repository at this point in the history
Add support for grid splitting, and clean up the code a bit.
  • Loading branch information
minelolpride authored May 19, 2020
1 parent c4c9310 commit 09193c6
Showing 1 changed file with 93 additions and 34 deletions.
127 changes: 93 additions & 34 deletions Main.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
#include <iostream>
#include "bitmap.h"

// one limitation of working with this header for processing BMP files
// is that it only works with 24bpp images, meaning we cannot utilize
// the alpha channel when splitting. it would be useful to have that
// functionality but i currently will work with what i have right now.
// this also means that some splits will have the default white
// canvas peek through, and that is because the image is not just not
// the right size to split evenly into chunks of whatever size you
// desire. so for now this is how it will be.

using namespace std;

int main(void) {
string inputName; // input filename
string modeIn; // the cutting orientation
string modeIn; // the cutting orientation input
unsigned int inImgCurY = 0; // where are we on the original image
unsigned int inImgCurX = 0; //
unsigned int outImgCurY = 0; // where are we on the output canvas
unsigned int outImgCurX = 0; //
unsigned int splitSize = 0; // how big each split should be
bool splitVert; // if true, the cuts are vertical, otherwise they are horizontal
double splitSize = 0.0; // how big each split should be
double splitSize2 = 0.0; // this one is used in the grid
bool splitSet = false; // a failsafe for later
int splitMode; // how should we split the image?
// 0 = vertically
// 1 = horizontally
// 2 = grid

// take in the input filename
cout << "What is your input image filename? (Excluding extension) (Must be BMP!)" << endl;
Expand All @@ -28,41 +41,37 @@ int main(void) {
cout << "Image is " << inputImage.width() << "x" << inputImage.height() << endl << endl;

// get the cutting orientation
cout << "How would you like this image to be split? ( H / V )" << endl;
getline(cin, modeIn);
cout << "How would you like this image to be split?" << endl << "[H]orizontal, [V]ertical, [G]rid" << endl;
cin >> modeIn;
if (modeIn == "V" || modeIn == "v") {
splitSet = true;
splitMode = 0;
}
if (modeIn == "H" || modeIn == "h") {
splitSet = true;
splitVert = false;
splitMode = 1;
}
if (modeIn == "V" || modeIn == "v") {
splitSet = true;
splitVert = true;
if (modeIn == "G" || modeIn == "g") {
splitSet = true;
splitMode = 2;
}
if (!splitSet) return 1; // there it is

// get the desired split size
cout << "How ";
if (splitVert) cout << "wide "; else cout << "tall ";
cout << "should each split be? (in pixels)" << endl;
cin >> splitSize;

// just say something so we know its working
cout << "Processing image...";

// work the magic
if (splitVert) {
unsigned int splitCount = inputImage.width() / splitSize;
splitCount++;
cout << splitCount << endl;
// enter here if we are cutting vertically
if (splitMode == 0) {
cout << "How wide should each split be? (in pixels) ";
cin >> splitSize;
cout << "Processing image...";
unsigned int splitCount = ceil(inputImage.width() / splitSize);
unsigned int i = 1;
while (i <= splitCount) {
bitmap_image outputSplit(splitSize, inputImage.height());
outputSplit.clear(0xFF);
while (outImgCurX < splitSize) {
while (outImgCurY <= inputImage.height() - 1) {
// following line existed to help debugging, uncomment if you want to see the program work (warning: a bit slow)
//cout << "Pixel at " << outImgCurX << " " << outImgCurY << " will be " << (int)inputImage.get_pixel(inImgCurX, inImgCurY).red << " " << (int)inputImage.get_pixel(inImgCurX, inImgCurY).green << " " << (int)inputImage.get_pixel(inImgCurX, inImgCurY).blue << endl;
outputSplit.set_pixel(outImgCurX, outImgCurY, inputImage.get_pixel(inImgCurX, inImgCurY).red, inputImage.get_pixel(inImgCurX, inImgCurY).green, inputImage.get_pixel(inImgCurX, inImgCurY).blue);
if (inImgCurX < inputImage.width() && inImgCurY < inputImage.height()) {
outputSplit.set_pixel(outImgCurX, outImgCurY, inputImage.get_pixel(inImgCurX, inImgCurY).red, inputImage.get_pixel(inImgCurX, inImgCurY).green, inputImage.get_pixel(inImgCurX, inImgCurY).blue);
}
inImgCurY++;
outImgCurY++;
}
Expand All @@ -72,21 +81,27 @@ int main(void) {
outImgCurX++;
}
outputSplit.save_image(inputName + "_" + to_string(i) + ".bmp");
outImgCurX = 0; outImgCurY = 0;
outImgCurX = 0;
i++;
}
cout << "done." << endl;
} else {
unsigned int splitCount = inputImage.height() / splitSize; splitCount++;
}

// enter here if we are cutting horizontally
if (splitMode == 1) {
cout << "How tall should each split be? (in pixels) ";
cin >> splitSize;
cout << "Processing image...";
unsigned int splitCount = ceil(inputImage.height() / splitSize);
unsigned int i = 1;
while (i <= splitCount) {
bitmap_image outputSplit(inputImage.width(), splitSize);
outputSplit.clear(0xFF);
while (outImgCurY < splitSize) {
while (outImgCurX <= inputImage.width() - 1) {
// following line existed to help debugging, uncomment if you want to see the program work (warning: a bit slow)
//cout << "Pixel at " << outImgCurX << " " << outImgCurY << " will be " << (int)inputImage.get_pixel(inImgCurX, inImgCurY).red << " " << (int)inputImage.get_pixel(inImgCurX, inImgCurY).green << " " << (int)inputImage.get_pixel(inImgCurX, inImgCurY).blue << endl;
outputSplit.set_pixel(outImgCurX, outImgCurY, inputImage.get_pixel(inImgCurX, inImgCurY).red, inputImage.get_pixel(inImgCurX, inImgCurY).green, inputImage.get_pixel(inImgCurX, inImgCurY).blue);
if (inImgCurX < inputImage.width() && inImgCurY < inputImage.height()) {
outputSplit.set_pixel(outImgCurX, outImgCurY, inputImage.get_pixel(inImgCurX, inImgCurY).red, inputImage.get_pixel(inImgCurX, inImgCurY).green, inputImage.get_pixel(inImgCurX, inImgCurY).blue);
}
inImgCurX++;
outImgCurX++;
}
Expand All @@ -96,10 +111,54 @@ int main(void) {
outImgCurY++;
}
outputSplit.save_image(inputName + "_" + to_string(i) + ".bmp");
outImgCurX = 0; outImgCurY = 0;
outImgCurY = 0;
i++;
}
cout << "done." << endl;
}

// enter here if we are cutting into grid pieces
if (splitMode == 2) {
cout << "How wide should each split be? (in pixels) ";
cin >> splitSize;
cout << "How tall should each split be? (in pixels) ";
cin >> splitSize2;

unsigned int splitsX = ceil(inputImage.width() / splitSize);
unsigned int splitsY = ceil(inputImage.height() / splitSize2);
unsigned int splitCount = splitsX + splitsY;

unsigned int x = 1; // keep track of what split we are on
unsigned int y = 1;

while (y <= splitsY) {
while (x <= splitsX) {
bitmap_image outputSplit(splitSize, splitSize2);
outputSplit.clear(0xFF);
while (outImgCurY < splitSize2) {
while (outImgCurX < splitSize) {
//cout << inImgCurX << " " << inImgCurY << endl;
if (inImgCurX < inputImage.width() && inImgCurY < inputImage.height()) {
outputSplit.set_pixel(outImgCurX, outImgCurY, inputImage.get_pixel(inImgCurX, inImgCurY).red, inputImage.get_pixel(inImgCurX, inImgCurY).green, inputImage.get_pixel(inImgCurX, inImgCurY).blue);
}
outImgCurX++;
inImgCurX++;
}
outImgCurY++;
outImgCurX = 0;
inImgCurX = (splitSize * x) - splitSize;
inImgCurY++;
}
outImgCurY = 0;
inImgCurY = (splitSize2 * y) - splitSize2;
outputSplit.save_image(inputName + "_" + to_string(x) + "_" + to_string(y) + ".bmp");
x++;
}
x = 1;
y++;
inImgCurX = 0;
inImgCurY = (splitSize2 * y) - splitSize2;
}
}
return 0;
}
}

0 comments on commit 09193c6

Please sign in to comment.