Open main menu

CDOT Wiki β

Changes

Savy Cat

153 bytes added, 17:47, 25 March 2018
Rotate90
// CImg[x][y][z]
inline int idx(int x, int y, int w, int h, int z) {
return x + y * w + w * h * z;
}
// 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;
}
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();
}
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;
}
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)]; } }
}
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>
;Rotate.cpp
int main(int argc, char** argv) {
// Allocate memory for 3 CImg structures, initializing colour values from speficied files. cimg_library::CImg<PX_TYPE> img_tiny("C:\\School\\DPS915\\Project\\CImg-Rotate\\Debug\\Tiny-Shay.jpg"); cimg_library::CImg<PX_TYPE> img_med("C:\\School\\DPS915\\Project\\CImg-Rotate\\Debug\\Medium-Shay.jpg"); cimg_library::CImg<PX_TYPE> img_large("C:\\School\\DPS915\\Project\\CImg-Rotate\\Debug\\Large-Shay.jpg");
// Allocate memory for rotated versions of above, initializing colour values to 0. cimg_library::CImg<PX_TYPE> img_tiny90(img_tiny.height(), img_tiny.width(), 1, 3, 0); cimg_library::CImg<PX_TYPE> img_med90(img_med.height(), img_med.width(), 1, 3, 0); cimg_library::CImg<PX_TYPE> img_large90(img_large.height(), img_large.width(), 1, 3, 0);
// Un-comment to print pixel values to console and display image for 4 rotations /* display(img_tiny); rotate90(img_tiny, img_tiny90); display(img_tiny90); rotate90(img_tiny90, img_tiny); display(img_tiny); rotate90(img_tiny, img_tiny90); display(img_tiny90); rotate90(img_tiny90, img_tiny); display(img_tiny); */
// Display image statistics and rotate 12 times each. imgStats("Tiny Shay", img_tiny); std::cout << "Rotating 4x..." << std::endl; rotate90x4(img_tiny, img_tiny90); std::cout << "Rotating 8x..." << std::endl; rotate90x4(img_tiny, img_tiny90); std::cout << "Rotating 12x..." << std::endl; rotate90x4(img_tiny, img_tiny90); std::cout << "Shay is dizzy!" << std::endl << std::endl;
imgStats std::cout << "Rotating 4x..." << std::endl; rotate90x4(img_tiny, img_tiny90); std::cout << "Rotating 8x..." << std::endl; rotate90x4(img_tiny, img_tiny90); std::cout << "Medium ShayRotating 12x..."<< std::endl; rotate90x4(img_tiny, img_medimg_tiny90); std::cout << "Shay is dizzy!" << std::endl << std::endl;
std::cout << "Rotating 4x..." << std::endl; rotate90x4 imgStats(img_med, img_med90); std::cout << "Rotating 8x...Medium Shay" << std::endl; rotate90x4(img_med, img_med90); std::cout << "Rotating 12x..." << std::endl; rotate90x4(img_med, img_med90); std::cout << "Shay is dizzy!" << std::endl << std::endl;
imgStats std::cout << "Rotating 4x..." << std::endl; rotate90x4(img_med, img_med90); std::cout << "Rotating 8x..." << std::endl; rotate90x4(img_med, img_med90); std::cout << "Large ShayRotating 12x..."<< std::endl; rotate90x4(img_med, img_largeimg_med90); std::cout << "Shay is dizzy!" << std::endl << std::endl;
std::cout << "Rotating 4x..." << std::endl; rotate90x4 imgStats(img_large, img_large90); std::cout << "Rotating 8x...Large Shay" << std::endl; rotate90x4(img_large, img_large90); std::cout << "Rotating 12x..." << std::endl; rotate90x4(img_large, img_large90); std::cout << "Shay is dizzy!" << std::endl << std::endl;
return 0 std::cout << "Rotating 4x..." << std::endl; rotate90x4(img_large, img_large90); std::cout << "Rotating 8x..." << std::endl; rotate90x4(img_large, img_large90); std::cout << "Rotating 12x..." << std::endl; rotate90x4(img_large, img_large90); std::cout << "Shay is dizzy!" << std::endl << std::endl;
return 0; }</nowiki>
==== Pre-profile ====
We can un-comment the "test" section to attempt reading a .jpg, verify colour channel values that were stored, and otherwise make sure everything is working as expected. Here is Tiny-Shay.jpg, 30 30px x 21 pixel 21px top-down imageof my cat laying on the floor. Mousing over a pixel will display the X and Y coordinates, along with the corresponding red, green, blue values.
[[File:Verify-1.png|800px]]
93
edits