Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- void Inversion(unsigned char *arr, unsigned char *new_arr, int height, int width) {
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- new_arr[i * width + j] = 255 - arr[i * width + j];
- }
- }
- }
- void HorizontalMirrorImage(unsigned char *arr, unsigned char *new_arr, int height, int width) {
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- new_arr[i * width + width - j - 1] = arr[i * width + j];
- }
- }
- }
- void HorizontalMirrorImageColor(unsigned char *arr, unsigned char *new_arr, int height, int width) {
- for (int i = 0; i < height; i += 1) {
- for (int j = 0; j < width; j += 3) {
- new_arr[i * width + width - j - 3] = arr[i * width + j];
- new_arr[i * width + width - j - 2] = arr[i * width + j + 1];
- new_arr[i * width + width - j - 1] = arr[i * width + j + 2];
- }
- }
- }
- void VerticalMirrorImage(unsigned char *arr, unsigned char *new_arr, int height, int width) {
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- new_arr[(height - i - 1) * width + j] = arr[i * width + j];
- }
- }
- }
- void VerticalMirrorImageColor(unsigned char *arr, unsigned char *new_arr, int height, int width) {
- for (int i = 0; i < height; i += 1) {
- for (int j = 0; j < width; j += 3) {
- new_arr[(height - i - 1) * width + j + 0] = arr[i * width + j];
- new_arr[(height - i - 1) * width + j + 1] = arr[i * width + j + 1];
- new_arr[(height - i - 1) * width + j + 2] = arr[i * width + j + 2];
- }
- }
- }
- void RightTurn(unsigned char *arr, unsigned char *new_arr, int height, int width) {
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- new_arr[j * height + height - i - 1] = arr[i * width + j];
- }
- }
- }
- void RightTurnColor(unsigned char *arr, unsigned char *new_arr, int height, int width) {
- for (int i = 0; i < height; i += 3) {
- for (int j = 0; j < width; j += 1) {
- new_arr[j * height + height - i - 3] = arr[i * width + 3 * j];
- new_arr[j * height + height - i - 2] = arr[i * width + 3 * j + 1];
- new_arr[j * height + height - i - 1] = arr[i * width + 3 * j + 2];
- }
- }
- }
- void LeftTurn(unsigned char *arr, unsigned char *new_arr, int height, int width) {
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- new_arr[(width - j - 1) * height + i] = arr[i * width + j];
- }
- }
- }
- void LeftTurnColor(unsigned char *arr, unsigned char *new_arr, int height, int width) {
- for (int i = 0; i < height; i += 3) {
- for (int j = 0; j < width; j += 1) {
- new_arr[(width - j - 1) * height + i] = arr[i * width + 3 * j];
- new_arr[(width - j - 1) * height + i + 1] = arr[i * width + 3 * j + 1];
- new_arr[(width - j - 1) * height + i + 2] = arr[i * width + 3 * j + 2];
- }
- }
- }
- int main(int argc, char **argv) {
- int type, width, height, dummy, BytesInPixel, Check;
- FILE *File, *NewFile;
- File = fopen(argv[1], "rb");
- if (fscanf(File, "P%d", &type) == 0) {
- return -1;
- }; //&width, &height, &dummy);
- fscanf(File, "%d %d\n%d\n", &width, &height, &dummy);
- if ((width < 1) || (height < 1)) {
- return -1;
- }
- if (dummy != 255) {
- return -1;
- }
- if (type == 5) {
- BytesInPixel = 1;
- } else if (type == 6) {
- BytesInPixel = 3;
- } else return -1;
- unsigned char *arr = (unsigned char *) malloc((sizeof(unsigned char)) * BytesInPixel * width * height);
- if (arr == NULL) {
- return -1;
- }
- Check = fread(arr, sizeof(unsigned char), BytesInPixel * width * height, File);
- Check += fread(arr, sizeof(unsigned char), 1, File);
- if (Check != BytesInPixel * width * height) {
- return -1;
- }
- unsigned char *arr_new = (unsigned char *) malloc((sizeof(unsigned char)) * BytesInPixel * width * height);
- if (arr_new == NULL) {
- return -1;
- }
- if (atoi(argv[3]) == 0) {
- Inversion(arr, arr_new, height, width * BytesInPixel);
- }
- if (atoi(argv[3]) == 1) {
- if (type == 5) {
- HorizontalMirrorImage(arr, arr_new, height, width * BytesInPixel);
- } else {
- HorizontalMirrorImageColor(arr, arr_new, height, width * BytesInPixel);
- }
- }
- if (atoi(argv[3]) == 2) {
- if (type == 5) {
- VerticalMirrorImage(arr, arr_new, height, width * BytesInPixel);
- } else {
- VerticalMirrorImageColor(arr, arr_new, height, width * BytesInPixel);
- }
- }
- if (atoi(argv[3]) == 3) {
- if (type == 5) {
- RightTurn(arr, arr_new, height, width);
- } else {
- RightTurnColor(arr, arr_new, height * BytesInPixel, width);
- }
- }
- if (atoi(argv[3]) == 4) {
- if (type == 5) {
- LeftTurn(arr, arr_new, height, width);
- } else {
- LeftTurnColor(arr, arr_new, height * BytesInPixel, width);
- }
- }
- if ((atoi(argv[3]) != 0) && (atoi(argv[3]) != 1) && (atoi(argv[3]) != 2) && (atoi(argv[3]) != 3) &&
- (atoi(argv[3]) != 4)) {
- return -1;
- }
- NewFile = fopen(argv[2], "wb");
- fprintf(NewFile, "P%d\n", type);
- if (atoi(argv[3]) < 3) {
- fprintf(NewFile, "%d %d\n", width, height);
- } else {
- fprintf(NewFile, "%d %d\n", height, width);
- }
- fprintf(NewFile, "%d\n", dummy);
- fwrite(arr_new, sizeof(unsigned char), BytesInPixel * height * width, NewFile);
- fclose(File);
- fclose(NewFile);
- free(arr);
- free(arr_new);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement