Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <stdlib.h>
- #include <algorithm>
- using namespace std;
- int a, widht, height, maxcol, num;
- int main() {
- FILE * pFile;
- FILE * copyFile;
- pFile = fopen("pict.ppm", "rb");
- if (pFile == NULL) {
- pFile = fopen("pict.pgm", "rb");
- }
- if (pFile == NULL) {
- printf("%c%c%c %c%c%c%c %c%c%c %c%c%c %c%c%c%c%c \n", 't','h','e','f','i','l','e','w','a','s','n','o','t','f','o','u','n','d');
- return 0;
- }
- fscanf(pFile, "P%i%i%i%i\n", &a, &widht, &height, &maxcol);
- printf("%c%i \n", 'P', a);
- printf("%i %i \n", height, widht);
- if (a == 5) {
- unsigned char * k;
- k = (unsigned char*) malloc (sizeof(unsigned char) * height * widht);
- if (k != NULL) {
- fread(k, sizeof(unsigned char), widht * height, pFile);
- copyFile = fopen("pictureconverted.pgm", "wb");
- cout << "enter number of action" << '\n';
- cin >> num;
- switch (num) {
- case 0:
- for (int i = 0 ; i < (widht * height); ++i) {
- k[i] = 255 - k[i];
- }
- fprintf(copyFile, "P%i\n%i %i\n%i\n", a, widht, height, maxcol);
- fwrite(k, sizeof(unsigned char), widht * height, copyFile);
- break;
- case 1:
- for (int i = 0; i < height / 2; ++i) {
- for (int j = 0; j < widht; ++j) {
- swap(k[i * widht + j ], k[(height - i - 1) * widht + j]);
- }
- }
- fprintf(copyFile, "P%i\n%i %i\n%i\n", a, widht, height, maxcol);
- fwrite(k, sizeof(unsigned char), widht * height, copyFile);
- break;
- case 2:
- for (int i = 0; i < height; ++i) {
- for (int j = 0; j < widht / 2; ++j) {
- swap(k[ i * widht + j], k[(i + 1) * widht - j]);
- }
- }
- fprintf(copyFile, "P%i\n%i %i\n%i\n", a, widht, height, maxcol);
- fwrite(k, sizeof(unsigned char), widht * height, copyFile);
- break;
- case 3:
- unsigned char * k1;
- k1 = (unsigned char*) malloc (sizeof(unsigned char) * widht * height);
- for ( int i = 0; i < height; ++i) {
- for (int j = 0; j < widht; ++j) {
- k1[j * height + height - i - 1] = k[i * widht + j];
- }
- }
- fprintf(copyFile, "P%i\n%i %i\n%i\n", a, height, widht, maxcol);
- fwrite(k1, sizeof(unsigned char), height * widht, copyFile);
- free(k1);
- break;
- case 4:
- unsigned char * k2;
- k2 = (unsigned char*) malloc (sizeof(unsigned char) * widht * height);
- for (int i = 0; i < height; ++i) {
- for (int j = 0; j < widht; ++j) {
- k2[(widht - j + 1) * height + i] = k[i * widht + j];
- }
- }
- fprintf(copyFile, "P%i\n%i %i\n%i\n", a, height, widht, maxcol);
- fwrite(k2, sizeof(unsigned char), height * widht, copyFile);
- free(k1);
- break;
- default:
- cout << "unavailable action" << '\n';
- }
- free(k);
- } else {
- printf( "Allocation memory failed");
- }
- } else {
- unsigned char *k;
- k = (unsigned char *) malloc(sizeof(unsigned char) * 3 * height * widht);
- if (k != NULL) {
- fread(k, sizeof(unsigned char), 3 * widht * height, pFile);
- copyFile = fopen("pictureconverted.ppm", "wb");
- cout << "enter number of action" << '\n';
- cin >> num;
- switch (num) {
- case 0:
- fprintf(copyFile, "P%i\n%i %i\n%i\n", a, widht, height, maxcol);
- for (int i = 0; i < (3 *widht * height); ++i) {
- k[i] = 255 - k[i];
- }
- fwrite(k, sizeof(unsigned char), 3 * widht * height, copyFile);
- break;
- case 1:
- for (int i = 0; i < height / 2; ++i) {
- for (int j = 0; j < widht; j += 3) {
- swap(k[i * widht + j ], k[(height - i - 1) * widht + j]);
- swap(k[(i + 1) * widht + j + 1], k[(height - (i + 1) - 1) * widht + j ]);
- swap(k[(i + 2) * widht + j + 2], k[(height - (i + 2) - 1) * widht + j ]);
- }
- }
- fprintf(copyFile, "P%i\n%i %i\n%i\n", a, widht, height, maxcol);
- fwrite(k, sizeof(unsigned char), 3 * widht * height, copyFile);
- break;
- case 2:
- for (int i = 0; i < height; ++i) {
- for (int j = 0; j < widht / 2; j += 3) {
- swap(k[ i * widht + j], k[(i + 1) * widht - j]);
- swap(k[ (i) * widht + j ], k[(i + 1) * widht - j ]);
- swap(k[ i * widht + j ], k[(i + 1) * widht - j ]);
- }
- }
- fprintf(copyFile, "P%i\n%i %i\n%i\n", a, widht, height, maxcol);
- fwrite(k, sizeof(unsigned char), 3 * widht * height, copyFile);
- break;
- case 3:
- unsigned char * k1;
- k1 = (unsigned char*) malloc (sizeof(unsigned char) * widht * height * 3);
- for ( int i = 0; i < height; ++i) {
- for (int j = 0; j < widht; j += 3) {
- k1[j * height + height - i - 1] = k[i * widht + j];
- k1[(j + 1) * height + height - i - 1] = k[i * widht + (j + 1)];
- k1[(j + 2) * height + height - i - 1] = k[i * widht + (j + 2)];
- }
- }
- fprintf(copyFile, "P%i\n%i %i\n%i\n", a, height, widht, maxcol);
- fwrite(k1, sizeof(unsigned char), 3 * height * widht, copyFile);
- free(k1);
- break;
- case 4:
- unsigned char * k2;
- k2 = (unsigned char*) malloc (sizeof(unsigned char) * widht * height * 3);
- for (int i = 0; i < height; ++i) {
- for (int j = 0; j < widht; j += 3) {
- k2[(widht - j + 1) * height + i] = k[i * widht + j];
- k2[(widht - (j + 1) + 1) * height + i] = k[i * widht + (j + 1)];
- k2[(widht - (j + 2) + 1) * height + i] = k[i * widht + (j + 2)];
- }
- }
- fprintf(copyFile, "P%i\n%i %i\n%i\n", a, height, widht, maxcol);
- fwrite(k2, sizeof(unsigned char), 3 * height * widht, copyFile);
- free(k1);
- break;
- default:
- cout << "unavailable action" << '\n';
- }
- free(k);
- }
- else {
- printf("Allocation memory failed");
- }
- }
- fclose(pFile);
- fclose(copyFile);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement