Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- struct pgm_file {
- unsigned int format; //структура файла
- unsigned int width;
- unsigned int height;
- unsigned int colours;
- };
- void inverse(unsigned char *data, unsigned char *dataNew, int height, int width) {
- for (int i = 0; i < height; ++i) {
- for (int j = 0; j < width; ++j) {
- dataNew[i * width + j] = 255 - data[i * width + j];
- }
- }
- }
- void vertical(unsigned char *data, unsigned char *dataNew, int height, int width) {
- for (int i = 0; i < height; ++i) {
- for (int j = 0; j < width; ++j) {
- dataNew[(height - i - 1) * width + j] = data[i * width + j];
- }
- }
- }
- void horizontalColoured(unsigned char *data, unsigned char *dataNew, int height, int width) {
- for (int i = 0; i < height; ++i) {
- for (int j = 0; j < width; j += 3) {
- dataNew[i * width + width - j - 3] = data[i * width + j + 0];
- dataNew[i * width + width - j - 2] = data[i * width + j + 1];
- dataNew[i * width + width - j - 1] = data[i * width + j + 2];
- }
- }
- }
- void horizontalBW(unsigned char *data, unsigned char *dataNew, int height, int width) {
- for (int i = 0; i < height; ++i) {
- for (int j = 0; j < width; ++j) {
- dataNew[i * width + width - j - 1] = data[i * width + j];
- }
- }
- }
- void rightBW(unsigned char *data, unsigned char *dataNew, int height, int width) {
- for (int i = 0; i < height; ++i) {
- for (int j = 0; j < width; ++j) {
- dataNew[height * j + height - i - 1] = data[i * width + j];
- }
- }
- }
- void rightColour(unsigned char *data, unsigned char *dataNew, int height, int width) {
- for (int i = 0; i < height; ++i) {
- for (int j = 0; j < width; ++j) {
- dataNew[(height * j + height - i) * 3 - 3] = data[(i * width + j) * 3 + 0];
- dataNew[(height * j + height - i) * 3 - 2] = data[(i * width + j) * 3 + 1];
- dataNew[(height * j + height - i) * 3 - 1] = data[(i * width + j) * 3 + 2];
- }
- }
- }
- void leftBW(unsigned char *data, unsigned char *dataNew, int height, int width) {
- for (int i = 0; i < height; ++i) {
- for (int j = 0; j < width; ++j) {
- dataNew[(width - j - 1) * height + i] = data[i * width + j];
- }
- }
- }
- void leftColour(unsigned char *data, unsigned char *dataNew, int height, int width) {
- for (int i = 0; i < height; ++i) {
- for (int j = 0; j < width; ++j) {
- dataNew[((width - j - 1) * height + i) * 3 + 0] = data[(i * width + j) * 3 + 0];
- dataNew[((width - j - 1) * height + i) * 3 + 1] = data[(i * width + j) * 3 + 1];
- dataNew[((width - j - 1) * height + i) * 3 + 2] = data[(i * width + j) * 3 + 2];
- }
- }
- }
- int main(int argc, char **argv) {
- unsigned int i, j;
- int numberofcoloures;
- if (argc != 4) {
- printf("Incorrect format of the input data\n");
- return -1;
- }
- struct pgm_file pgm;
- FILE *file = fopen(argv[1], "rb");
- if (file == nullptr) {
- printf("Can`t open input file\n");
- return -1;
- }
- FILE *file_new = fopen(argv[2], "rb");
- if (file_new == nullptr) {
- printf("Can`t open output file\n");
- return -1;
- }
- if (fscanf(file, "P%d", &pgm.format) == 0) {
- printf("Incorrect format of the file\n");
- return -1;
- }
- fscanf(file, "%d %d", &pgm.width, &pgm.height);
- fscanf(file, "%d\n", &pgm.colours);
- if ((pgm.height < 1) || (pgm.width < 1)) {
- printf("Incorrect height or width in the FrameHeader\n");
- return -1;
- }
- if (pgm.colours != 255) {
- printf("Incorrect colours in the FrameHeader\n");
- return -1;
- }
- if (pgm.format==5){
- numberofcoloures = 1;
- } else {
- if (pgm.format==6){
- numberofcoloures = 3;
- } else {
- printf("Incorrect format of the file\n");
- }
- }
- unsigned char *data = (unsigned char *) malloc(sizeof(unsigned char) * numberofcoloures * pgm.height * pgm.width);
- if (data == NULL) {
- printf("Error\n");
- return -1;
- }
- int troubleLess = fread(data, sizeof(char), numberofcoloures * (pgm.height * pgm.width), file);
- if (troubleLess < numberofcoloures* (pgm.height * pgm.width)) {
- printf("Incorrect number of pixels\n");
- return -1;
- }
- int troubleMore = fread(data, sizeof(char), 1, file);
- if (troubleMore > 0) {
- printf("Incorrect number of pixels\n");
- return -1;
- }
- unsigned char *dataNew = (unsigned char *) malloc(sizeof(unsigned char) * numberofcoloures * pgm.height * pgm.width);
- if (dataNew == NULL) {
- printf("Error\n");
- return -1;
- }
- switch (atoi(argv[3])) {
- case 0:
- inverse(data, dataNew, pgm.height, pgm.width * numberofcoloures);
- break;
- case 1:
- if (pgm.format == 5) {
- horizontalBW(data, dataNew, pgm.height, pgm.width * numberofcoloures);
- } else {
- horizontalColour(data, dataNew, pgm.height, pgm.width * numberofcoloures);
- }
- break;
- case 2:
- vertical(data, dataNew, pgm.height, pgm.width * numberofcoloures);
- break;
- case 3:
- if (pgm.format == 5) {
- rightBW(data, dataNew, pgm.height, pgm.width);
- } else {
- rightColour(data, dataNew, pgm.height, pgm.width);
- }
- break;
- case 4:
- if (pgm.format == 5) {
- leftBW(data, dataNew, pgm.height, pgm.width);
- } else {
- leftColour(data, dataNew, pgm.height, pgm.width);
- }
- break;
- default:
- printf("Error\n");
- return -1;
- }
- file_new = fopen(argv[2], "wb");
- fprintf(file_new, "P%d\n", pgm.format);
- if (atoi(argv[3]) < 3) {
- fprintf(file_new, "%d %d\n", pgm.width, pgm.height);
- } else {
- fprintf(file_new, "%d %d\n", pgm.height, pgm.width);
- }
- fprintf(file_new, "%d\n", pgm.colours);
- fwrite(dataNew, sizeof(char), (numberofcoloures * pgm.height * pgm.width), file_new);
- fclose(file);
- fclose(file_new);
- free(data);
- free(dataNew);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement