generated from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
1 Commits
main
...
copilot_ru
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2b388ab748 |
176
imageInput.c
176
imageInput.c
@ -7,94 +7,131 @@
|
|||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
|
|
||||||
|
|
||||||
/// @brief Gets the next char value from specified and opened file
|
/// @brief Reads a value in little-endian format from file
|
||||||
/// @param openedFile stream FILE, from which to read
|
/// @param openedFile stream FILE, from which to read
|
||||||
/// @param iterations how many chars are being taken from (1 char equals 2 Hexdecimals equals 8bit)
|
/// @param bytes how many bytes to read
|
||||||
static int getCharValueFromFile(FILE* openedFile, int iterations) {
|
static unsigned int readLittleEndian(FILE* openedFile, int bytes) {
|
||||||
int addToFile = 0;
|
unsigned int value = 0;
|
||||||
|
|
||||||
if (openedFile == NULL) return 0;
|
if (openedFile == NULL) return 0;
|
||||||
|
|
||||||
for (int i = 0; i < iterations; i++) {
|
for (int i = 0; i < bytes; i++) {
|
||||||
int tmp = fgetc(openedFile);
|
int tmp = fgetc(openedFile);
|
||||||
//If the File ends, the method returns a '0'
|
if (tmp == EOF) return 0;
|
||||||
if (openedFile == EOF) return addToFile;
|
|
||||||
|
value |= ((unsigned int)tmp) << (i * 8);
|
||||||
addToFile += tmp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return addToFile;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
{
|
{
|
||||||
GrayScaleImageSeries *series = NULL;
|
FILE* openFile = fopen(path, "rb");
|
||||||
|
|
||||||
FILE* openFile;
|
|
||||||
|
|
||||||
openFile = fopen(path, "rb");
|
|
||||||
|
|
||||||
// file Could not be opened/does not exist
|
// file Could not be opened/does not exist
|
||||||
if (openFile != NULL) {
|
if (openFile == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
char* actualFileTag = malloc(strlen(FILE_HEADER_STRING) * sizeof(char));
|
char actualFileTag[strlen(FILE_HEADER_STRING) + 1];
|
||||||
int numberOfImages = 0;
|
|
||||||
int width = 0;
|
size_t tagLength = strlen(FILE_HEADER_STRING);
|
||||||
int height = 0;
|
if (fread(actualFileTag, 1, tagLength, openFile) != tagLength) {
|
||||||
|
fclose(openFile);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
actualFileTag[tagLength] = '\0';
|
||||||
|
|
||||||
for (int i = 0; i < strlen(FILE_HEADER_STRING); i++) {
|
// checks if the files are equal
|
||||||
actualFileTag += fgetc(openFile);
|
if (strcmp(actualFileTag, FILE_HEADER_STRING) != 0) {
|
||||||
|
fclose(openFile);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int numberOfImages = readLittleEndian(openFile, 2);
|
||||||
|
|
||||||
|
// no Images in series -> No image-series
|
||||||
|
if (numberOfImages == 0) {
|
||||||
|
fclose(openFile);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int width = readLittleEndian(openFile, 2);
|
||||||
|
unsigned int height = readLittleEndian(openFile, 2);
|
||||||
|
|
||||||
|
// no height/width --> impossible file
|
||||||
|
if(height == 0 || width == 0) {
|
||||||
|
fclose(openFile);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//all the starting parameters are set --> the images can be read and stored
|
||||||
|
GrayScaleImageSeries* series = malloc(sizeof(GrayScaleImageSeries));
|
||||||
|
if (series == NULL) {
|
||||||
|
fclose(openFile);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
GrayScaleImage* images = malloc(numberOfImages * sizeof(GrayScaleImage));
|
||||||
|
unsigned char* labels = malloc(numberOfImages * sizeof(unsigned char));
|
||||||
|
|
||||||
|
if (images == NULL || labels == NULL) {
|
||||||
|
free(images);
|
||||||
|
free(labels);
|
||||||
|
free(series);
|
||||||
|
fclose(openFile);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
series->count = 0;
|
||||||
|
series->images = images;
|
||||||
|
series->labels = labels;
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < numberOfImages; i++) {
|
||||||
|
// allocating the actual matrix image for each image
|
||||||
|
images[i].buffer = malloc(width * height);
|
||||||
|
|
||||||
|
if (images[i].buffer == NULL) {
|
||||||
|
for (unsigned int k = 0; k < i; k++) {
|
||||||
|
free(images[k].buffer);
|
||||||
|
}
|
||||||
|
free(images);
|
||||||
|
free(labels);
|
||||||
|
free(series);
|
||||||
|
fclose(openFile);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// checks if the files are equal: strcmp should return '0' --> convert it to '1' for 'true'
|
images[i].height = height;
|
||||||
int fileTagEqual = !strcmp(actualFileTag, FILE_HEADER_STRING);
|
images[i].width = width;
|
||||||
|
|
||||||
//we only need the fileTag to verify its an image for our series.
|
if (fread(images[i].buffer, 1, width * height, openFile) != width * height) {
|
||||||
free(actualFileTag);
|
for (unsigned int k = 0; k <= i; k++) {
|
||||||
actualFileTag = NULL;
|
free(images[k].buffer);
|
||||||
|
|
||||||
|
|
||||||
if (fileTagEqual) {
|
|
||||||
numberOfImages = getCharValueFromFile(openFile, 2);
|
|
||||||
|
|
||||||
// no Images in series -> No image-series
|
|
||||||
if (numberOfImages == 0) {
|
|
||||||
fclose(openFile);
|
|
||||||
return series;
|
|
||||||
}
|
|
||||||
|
|
||||||
width = getCharValueFromFile(openFile, 2);
|
|
||||||
height = getCharValueFromFile(openFile, 2);
|
|
||||||
|
|
||||||
// no height/width --> impossible file
|
|
||||||
if(height == 0 || width == 0) {
|
|
||||||
fclose(openFile);
|
|
||||||
return series;
|
|
||||||
}
|
|
||||||
|
|
||||||
//all the starting parameters are set --> the images can be read and stored
|
|
||||||
GrayScaleImage* images = malloc(numberOfImages * sizeof(GrayScaleImage));
|
|
||||||
unsigned char* labels = malloc(numberOfImages * sizeof(unsigned char));
|
|
||||||
|
|
||||||
series->count = 0;
|
|
||||||
series->images = images;
|
|
||||||
series->labels = labels;
|
|
||||||
|
|
||||||
for (int i = 0; i < numberOfImages; i++) {
|
|
||||||
for (int j = 0; j < width * height; j++) {
|
|
||||||
// allocating the actual matrix image for image
|
|
||||||
images[i].buffer = malloc(width * height);
|
|
||||||
|
|
||||||
images[i].height = height;
|
|
||||||
images[i].width = width;
|
|
||||||
images[i].buffer[j] = fgetc(openFile);
|
|
||||||
}
|
|
||||||
//rest of the values that only affect the image itself
|
|
||||||
series->labels[i] = fgetc(openFile);
|
|
||||||
series->count++;
|
|
||||||
}
|
}
|
||||||
|
free(images);
|
||||||
|
free(labels);
|
||||||
|
free(series);
|
||||||
|
fclose(openFile);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//rest of the values that only affect the image itself
|
||||||
|
int label = fgetc(openFile);
|
||||||
|
if (label == EOF) {
|
||||||
|
for (unsigned int k = 0; k <= i; k++) {
|
||||||
|
free(images[k].buffer);
|
||||||
|
}
|
||||||
|
free(images);
|
||||||
|
free(labels);
|
||||||
|
free(series);
|
||||||
|
fclose(openFile);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
series->labels[i] = (unsigned char)label;
|
||||||
|
series->count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(openFile);
|
fclose(openFile);
|
||||||
@ -104,12 +141,13 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||||
void clearSeries(GrayScaleImageSeries *series)
|
void clearSeries(GrayScaleImageSeries *series)
|
||||||
{
|
{
|
||||||
|
if (series == NULL) return;
|
||||||
|
|
||||||
for (int i = 0; i < series->count; i++) {
|
for (int i = 0; i < series->count; i++) {
|
||||||
free(series->images[i].buffer);
|
free(series->images[i].buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(series->images);
|
free(series->images);
|
||||||
series->images = NULL;
|
|
||||||
free(series->labels);
|
free(series->labels);
|
||||||
series->labels = NULL;
|
free(series);
|
||||||
}
|
}
|
||||||
115
imageInput_old.c
Normal file
115
imageInput_old.c
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "imageInput.h"
|
||||||
|
|
||||||
|
#define BUFFER_SIZE 100
|
||||||
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
|
|
||||||
|
|
||||||
|
/// @brief Gets the next char value from specified and opened file
|
||||||
|
/// @param openedFile stream FILE, from which to read
|
||||||
|
/// @param iterations how many chars are being taken from (1 char equals 2 Hexdecimals equals 8bit)
|
||||||
|
static int getCharValueFromFile(FILE* openedFile, int iterations) {
|
||||||
|
int addToFile = 0;
|
||||||
|
|
||||||
|
if (openedFile == NULL) return 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < iterations; i++) {
|
||||||
|
int tmp = fgetc(openedFile);
|
||||||
|
//If the File ends, the method returns a '0'
|
||||||
|
if (openedFile == EOF) return addToFile;
|
||||||
|
|
||||||
|
addToFile += tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
return addToFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||||
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
|
{
|
||||||
|
GrayScaleImageSeries *series = NULL;
|
||||||
|
|
||||||
|
FILE* openFile;
|
||||||
|
|
||||||
|
openFile = fopen(path, "rb");
|
||||||
|
|
||||||
|
// file Could not be opened/does not exist
|
||||||
|
if (openFile != NULL) {
|
||||||
|
|
||||||
|
char* actualFileTag = malloc(strlen(FILE_HEADER_STRING) * sizeof(char));
|
||||||
|
int numberOfImages = 0;
|
||||||
|
int width = 0;
|
||||||
|
int height = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < strlen(FILE_HEADER_STRING); i++) {
|
||||||
|
actualFileTag += fgetc(openFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
// checks if the files are equal: strcmp should return '0' --> convert it to '1' for 'true'
|
||||||
|
int fileTagEqual = !strcmp(actualFileTag, FILE_HEADER_STRING);
|
||||||
|
|
||||||
|
//we only need the fileTag to verify its an image for our series.
|
||||||
|
free(actualFileTag);
|
||||||
|
actualFileTag = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
if (fileTagEqual) {
|
||||||
|
numberOfImages = getCharValueFromFile(openFile, 2);
|
||||||
|
|
||||||
|
// no Images in series -> No image-series
|
||||||
|
if (numberOfImages == 0) {
|
||||||
|
fclose(openFile);
|
||||||
|
return series;
|
||||||
|
}
|
||||||
|
|
||||||
|
width = getCharValueFromFile(openFile, 2);
|
||||||
|
height = getCharValueFromFile(openFile, 2);
|
||||||
|
|
||||||
|
// no height/width --> impossible file
|
||||||
|
if(height == 0 || width == 0) {
|
||||||
|
fclose(openFile);
|
||||||
|
return series;
|
||||||
|
}
|
||||||
|
|
||||||
|
//all the starting parameters are set --> the images can be read and stored
|
||||||
|
GrayScaleImage* images = malloc(numberOfImages * sizeof(GrayScaleImage));
|
||||||
|
unsigned char* labels = malloc(numberOfImages * sizeof(unsigned char));
|
||||||
|
|
||||||
|
series->count = 0;
|
||||||
|
series->images = images;
|
||||||
|
series->labels = labels;
|
||||||
|
|
||||||
|
for (int i = 0; i < numberOfImages; i++) {
|
||||||
|
for (int j = 0; j < width * height; j++) {
|
||||||
|
// allocating the actual matrix image for image
|
||||||
|
images[i].buffer = malloc(width * height);
|
||||||
|
|
||||||
|
images[i].height = height;
|
||||||
|
images[i].width = width;
|
||||||
|
images[i].buffer[j] = fgetc(openFile);
|
||||||
|
}
|
||||||
|
//rest of the values that only affect the image itself
|
||||||
|
series->labels[i] = fgetc(openFile);
|
||||||
|
series->count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(openFile);
|
||||||
|
return series;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||||
|
void clearSeries(GrayScaleImageSeries *series)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < series->count; i++) {
|
||||||
|
free(series->images[i].buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(series->images);
|
||||||
|
series->images = NULL;
|
||||||
|
free(series->labels);
|
||||||
|
series->labels = NULL;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user