Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #pragma warning(disable:4996)
- #define _CRT_SECURE_NO_DEPRECATE
- using namespace std;
- typedef unsigned char uchar;
- struct RGB {
- uchar R;
- uchar G;
- uchar B;
- };
- struct Image {
- char format[2];
- size_t width;
- size_t height;
- size_t grade;
- size_t size;
- vector<RGB> imageP6;
- vector<uchar> imageP5;
- };
- enum Command {
- REVERS = 0,
- HORIZONTAL_MIRROR = 1,
- VERTICAL_MIRROR = 2,
- LEFT_ROTATE = 3,
- RIGHT_ROTATE = 4
- };
- void ReadImage(char* file, Image& image) {
- FILE* f = fopen(file, "rb");
- if (f == nullptr)
- throw exception("ERROR. File not opend");
- fscanf(f, "%c%c\n%lu %lu\n%lu\n", &image.format[0], &image.format[1], &image.width, &image.height, &image.grade);
- if (!strcmp(image.format, "P5") || !strcmp(image.format, "P6"))
- throw exception("ERROR. Unknow format");
- if (image.width < 0 || image.height < 0 || image.grade != 255)
- throw exception("ERROR. Bad header");
- image.size = image.width * image.height;
- uchar* buffer = new uchar[image.size * 3];
- fread(buffer, sizeof(uchar), image.size * 3, f);
- fclose(f);
- image.imageP6.resize(image.size);
- for (size_t i = 0; i < image.size; i++) {
- image.imageP6[i].R = buffer[i * 3 + 0];
- image.imageP6[i].G = buffer[i * 3 + 1];
- image.imageP6[i].B = buffer[i * 3 + 2];
- }
- delete[] buffer;
- }
- void WriteImage(char* file, Image& image) {
- FILE* f = fopen(file, "wb");
- if (strcmp(image.format, "P6")) {
- if (strcmp(file, ".pgm")) {
- throw exception("ERROR. Formats not converge");
- }
- }
- else {
- if (strcmp(file, ".pnm")) {
- throw exception("ERROR. Formats not converge");
- }
- }
- if (f == nullptr)
- throw exception("ERROR. File not opend");
- fprintf(f, "%c%c\n%lu %lu\n%d\n", image.format[0], image.format[1], image.width, image.height, image.grade);
- uchar* buffer = new uchar[image.size * 3];
- for (size_t i = 0; i < image.size; i++) {
- buffer[i * 3 + 0] = image.imageP6[i].R;
- buffer[i * 3 + 1] = image.imageP6[i].G;
- buffer[i * 3 + 2] = image.imageP6[i].B;
- }
- fwrite(buffer, sizeof(uchar), image.size * 3, f);
- delete[] buffer;
- fclose(f);
- }
- void ReversImage(Image& image) {
- for (size_t y = 0; y < image.height; y++) {
- for (size_t x = 0; x < image.width; x++) {
- image.imageP6[y * image.width + x].R = -image.imageP6[y * image.width + x].R;
- image.imageP6[y * image.width + x].G = -image.imageP6[y * image.width + x].G;
- image.imageP6[y * image.width + x].B = -image.imageP6[y * image.width + x].B;
- }
- }
- }
- void HorisontalMirrorImage(Image& image) {
- for (size_t y = 0; y < image.height / 2; y++) {
- for (size_t x = 0; x < image.width; x++) {
- swap(image.imageP6[y * image.width + x].R, image.imageP6[image.size - image.width - y * image.width + x].R);
- swap(image.imageP6[y * image.width + x].G, image.imageP6[image.size - image.width - y * image.width + x].G);
- swap(image.imageP6[y * image.width + x].B, image.imageP6[image.size - image.width - y * image.width + x].B);
- }
- }
- }
- void VerticalMirrorImage(Image& image) {
- for (size_t y = 0; y < image.height; y++) {
- for (size_t x = 0; x < image.width / 2; x++) {
- swap(image.imageP6[y * image.width + x].R, image.imageP6[y * image.width + image.width - 1 - x].R);
- swap(image.imageP6[y * image.width + x].G, image.imageP6[y * image.width + image.width - 1 - x].G);
- swap(image.imageP6[y * image.width + x].B, image.imageP6[y * image.width + image.width - 1 - x].B);
- }
- }
- }
- void LeftRotateImage(Image& image) {
- }
- void RightRotateImage(Image& image) {
- }
- int main(int argc, char** argv) {
- try {
- if (argc > 4)
- throw exception("ERROR. Many argumets");
- if (argc < 4)
- throw exception("ERROR. Few argumets");
- Image image;
- ReadImage(argv[1], image);
- switch (atoi(argv[3]))
- {
- case REVERS:
- ReversImage(image);
- break;
- case HORIZONTAL_MIRROR:
- HorisontalMirrorImage(image);
- break;
- case VERTICAL_MIRROR:
- VerticalMirrorImage(image);
- break;
- case LEFT_ROTATE:
- LeftRotateImage(image);
- break;
- case RIGHT_ROTATE:
- RightRotateImage(image);
- break;
- default:
- throw exception("ERROR. Command not found");
- break;
- }
- WriteImage(argv[2], image);
- cout << "Success!" << endl;
- }
- catch (const exception& error) {
- cerr << error.what() << endl;
- return -1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement