Changes

Jump to: navigation, search

Savy Cat

3,034 bytes added, 05:33, 25 March 2018
Initial Code
==== Initial Code ====
;Rotate.h
 
<nowiki>
// Evan Marinzel - DPS915 Project
// Rotate.h
 
#pragma once
 
#define cimg_use_jpeg
#define PX_TYPE unsigned char
 
#include <CImg.h>
#include <iostream>
#include <iomanip>
 
 
// Indexing function for CImg object.
// CImg[x][y][z]
inline int idx(int x, int y, int w, int h, int z) {
return x + y * w + w * h * z;
}
 
// Indexing function for accessing pixel location rotated 90 degrees relative to current location
// CImg[h - 1 - y][x][z]
inline int idx90(int x, int y, int w, int h, int z) {
return (h - 1 - y) + x * h + w * h * z;
}
 
// Prints colour channel values of img to console.
// Opens image, mouse-over pixels to verify indexing is correct.
// Uses 40 x 40 pixel sample from the top left corner if img is larger than 40 x 40
void display(const cimg_library::CImg<PX_TYPE> img) {
 
int height = img.height() > 40 ? 40 : img.height();
int width = img.width() > 40 ? 40 : img.width();
 
for (int i = 0; i < img.spectrum(); i++) {
if (i == 0)
std::cout << "Red:" << std::endl;
else if (i == 1)
std::cout << "Green:" << std::endl;
else if (i == 2)
std::cout << "Blue:" << std::endl;
for (int j = 0; j < height; j++) {
for (int k = 0; k < width; k++) {
std::cout << std::setw(4) << (int)img[idx(k, j, img.width(), img.height(), i)];
}
std::cout << std::endl;
}
std::cout << std::endl;
}
 
cimg_library::CImg<PX_TYPE> imgCropped(img);
imgCropped.crop(0, 0, width - 1, height - 1, 0);
imgCropped.display();
 
}
 
// Print image dimensions and size to console.
void imgStats(const char* title, cimg_library::CImg<PX_TYPE> img) {
 
std::cout << title << " Image Data" << std::endl;
std::cout << std::setfill('=') << std::setw(strlen(title) + 11) << "=" << std::setfill(' ') << std::endl;
std::cout << std::setw(17) << std::right << "Width: " << img.width() << "px" << std::endl;
std::cout << std::setw(17) << std::right << "Height: " << img.height() << "px" << std::endl;
std::cout << std::setw(17) << std::right << "Depth: " << img.depth() << std::endl;
std::cout << std::setw(17) << std::right << "Colour Channels: " << img.spectrum() << std::endl;
std::cout << std::setw(17) << std::right << "Pixel Size: " << sizeof(PX_TYPE) << " bytes" << std::endl;
std::cout << std::setw(17) << std::right << "Total Size: " << img.size() << " bytes" << std::endl;
std::cout << std::endl;
 
}
 
// Rotate src image 90 degrees clockwise.
// Works by assigning pixel values from src to dst.
// - dst must be allocated as valid size
void rotate90(cimg_library::CImg<PX_TYPE> src, cimg_library::CImg<PX_TYPE> &dst) {
 
for (int i = 0; i < src.spectrum(); i++) {
for (int j = 0; j < src.height(); j++) {
for (int k = 0; k < src.width(); k++)
dst[idx90(k, j, src.width(), src.height(), i)] = src[idx(k, j, src.width(), src.height(), i)];
}
}
 
}
 
// Rotate image 360 degrees by calling rotate90 4 times.
void rotate90x4(cimg_library::CImg<PX_TYPE> src, cimg_library::CImg<PX_TYPE> dst) {
 
rotate90(src, dst);
rotate90(dst, src);
rotate90(src, dst);
rotate90(dst, src);
 
}</nowiki>
93
edits

Navigation menu