74 lines
1.9 KiB
C
74 lines
1.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
int main()
|
|
{
|
|
|
|
FILE *inputFile, *outputFile;
|
|
char inputFileName[260], outputFileName[11];
|
|
char format[3];
|
|
unsigned int width, height, maxValue;
|
|
unsigned int rgbRed, rgbGreen, rgbBlue;
|
|
float *gray;
|
|
|
|
printf("Input file name: ");
|
|
scanf("%s", inputFileName);
|
|
|
|
// FILE pointer auf Datei zum einlesen und ausgeben zeigen lassen
|
|
inputFile = fopen(inputFileName, "r");
|
|
outputFile = fopen("output.ppm", "w");
|
|
|
|
// Abbruch, falls Input Datei nicht gefunden wird
|
|
if (!inputFile)
|
|
{
|
|
|
|
printf("Input file not found/corrupted!\n");
|
|
return 1;
|
|
}
|
|
|
|
// Magic Number auslesen und in format speichern
|
|
fgets(format, sizeof(format), inputFile);
|
|
|
|
// Abbruch, falls falsches Magic Number Format verwendet wird
|
|
if (format[1] != '3')
|
|
{
|
|
|
|
printf("Magic Number P3 expected!\n");
|
|
return 1;
|
|
}
|
|
|
|
// Einlesen der Maße und des Farbbereiches von Input Datei
|
|
fscanf(inputFile, "%d %d\n%d\n", &width, &height, &maxValue);
|
|
|
|
// Dynamische Speicherreservierung für gray
|
|
gray = malloc(width * height * sizeof(float));
|
|
|
|
// Ausrechnen der Graustufen für jeweilige RGB Werte mit Formel
|
|
for (int i = 0; i < width * height; i++)
|
|
{
|
|
fscanf(inputFile, "%d %d %d ", &rgbRed, &rgbGreen, &rgbBlue);
|
|
gray[i] = rgbRed * 0.299 + rgbGreen * 0.587 + rgbBlue * 0.114;
|
|
}
|
|
|
|
// Header in Output Datei schreiben
|
|
fprintf(outputFile, "P2\n%d %d\n%d", width, height, maxValue);
|
|
|
|
// Graustufenwerte in Output Datei schreiben
|
|
for (int i = 0; i < width * height; i++)
|
|
{
|
|
if (i % width == 0)
|
|
{
|
|
fprintf(outputFile, "\n");
|
|
}
|
|
fprintf(outputFile, "%d ", (int)gray[i]);
|
|
}
|
|
|
|
printf("Output file generated: output.ppm");
|
|
|
|
// Freigabe der FILE Pointer und des float Pointer
|
|
fclose(inputFile);
|
|
fclose(outputFile);
|
|
free(gray);
|
|
|
|
return 0;
|
|
} |