Test2
This commit is contained in:
parent
46183d084c
commit
17c546f4d5
157
imageInput.c
157
imageInput.c
@ -1,115 +1,146 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include "imageInput.h"
|
#include "imageInput.h"
|
||||||
|
|
||||||
#define BUFFER_SIZE 100
|
#define BUFFER_SIZE 128
|
||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
|
|
||||||
static void strip_newline(char *s)
|
/* Hilfsfunktionen (module-local) */
|
||||||
|
|
||||||
|
static int checkFileHeader(FILE *fp)
|
||||||
{
|
{
|
||||||
if (!s) return;
|
size_t headerLen = strlen(FILE_HEADER_STRING);
|
||||||
size_t len = strlen(s);
|
if (headerLen >= BUFFER_SIZE) return 0;
|
||||||
while (len > 0 && (s[len-1] == '\n' || s[len-1] == '\r')) {
|
char buf[BUFFER_SIZE] = {0};
|
||||||
s[len-1] = '\0';
|
if (fread(buf, sizeof(char), headerLen, fp) != headerLen) return 0;
|
||||||
len--;
|
return (memcmp(buf, FILE_HEADER_STRING, headerLen) == 0);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
static int readUShort(FILE *fp, unsigned short *out)
|
||||||
|
{
|
||||||
|
if (!fp || !out) return 0;
|
||||||
|
uint16_t tmp = 0;
|
||||||
|
if (fread(&tmp, sizeof(uint16_t), 1, fp) != 1) return 0;
|
||||||
|
*out = (unsigned short)tmp;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
static int readPixels(FILE *fp, unsigned char *buffer, size_t count)
|
||||||
|
{
|
||||||
|
if (count == 0) return 1;
|
||||||
|
if (fread(buffer, sizeof(unsigned char), count, fp) != count) return 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int readLabel(FILE *fp, unsigned char *outLabel)
|
||||||
|
{
|
||||||
|
if (!fp || !outLabel) return 0;
|
||||||
|
if (fread(outLabel, sizeof(unsigned char), 1, fp) != 1) return 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void freeImagesInternal(GrayScaleImage *images, unsigned int count)
|
||||||
|
{
|
||||||
|
if (!images) return;
|
||||||
|
for (unsigned int i = 0; i < count; ++i) {
|
||||||
|
free(images[i].buffer);
|
||||||
|
images[i].buffer = NULL;
|
||||||
|
}
|
||||||
|
free(images);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Hauptfunktion: readImages im binären Format */
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
{
|
{
|
||||||
if (!path) return NULL;
|
if (!path) return NULL;
|
||||||
|
|
||||||
FILE *fp = fopen(path, "r");
|
FILE *fp = fopen(path, "rb");
|
||||||
if (!fp) return NULL;
|
if (!fp) return NULL;
|
||||||
|
|
||||||
char header[BUFFER_SIZE];
|
if (!checkFileHeader(fp)) {
|
||||||
if (!fgets(header, sizeof(header), fp)) {
|
|
||||||
fclose(fp);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
strip_newline(header);
|
|
||||||
if (strcmp(header, FILE_HEADER_STRING) != 0) {
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int count = 0;
|
unsigned short count = 0, width = 0, height = 0;
|
||||||
if (fscanf(fp, "%u", &count) != 1) {
|
if (!readUShort(fp, &count) || !readUShort(fp, &width) || !readUShort(fp, &height)) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries));
|
||||||
if (!series) {
|
if (!series) { fclose(fp); return NULL; }
|
||||||
fclose(fp);
|
|
||||||
return NULL;
|
series->count = (unsigned int)count;
|
||||||
}
|
|
||||||
series->count = count;
|
|
||||||
series->images = NULL;
|
series->images = NULL;
|
||||||
series->labels = NULL;
|
series->labels = NULL;
|
||||||
|
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
series->images = NULL;
|
/* leere Serie */
|
||||||
series->labels = NULL;
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return series;
|
return series;
|
||||||
}
|
}
|
||||||
|
|
||||||
series->images = calloc(count, sizeof(GrayScaleImage));
|
series->images = (GrayScaleImage *)calloc((size_t)count, sizeof(GrayScaleImage));
|
||||||
series->labels = calloc(count, sizeof(unsigned char));
|
series->labels = (unsigned char *)calloc((size_t)count, sizeof(unsigned char));
|
||||||
if (!series->images || !series->labels) {
|
if (!series->images || !series->labels) {
|
||||||
clearSeries(series);
|
free(series->images);
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned int i = 0; i < count; ++i) {
|
size_t pixelCount = (size_t)width * (size_t)height;
|
||||||
unsigned int label = 0;
|
|
||||||
unsigned int width = 0, height = 0;
|
|
||||||
if (fscanf(fp, "%u %u %u", &label, &width, &height) != 3) {
|
|
||||||
clearSeries(series);
|
|
||||||
fclose(fp);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
series->labels[i] = (unsigned char)(label & 0xFF);
|
|
||||||
series->images[i].width = width;
|
|
||||||
series->images[i].height = height;
|
|
||||||
|
|
||||||
size_t pixels = (size_t)width * (size_t)height;
|
for (unsigned int i = 0; i < (unsigned int)count; ++i) {
|
||||||
if (pixels == 0) {
|
/* Pixeldaten zuerst (width*height bytes), dann Label (1 byte) */
|
||||||
series->images[i].buffer = NULL;
|
unsigned char *pixels = NULL;
|
||||||
continue;
|
if (pixelCount > 0) {
|
||||||
}
|
pixels = (unsigned char *)malloc(pixelCount * sizeof(unsigned char));
|
||||||
|
if (!pixels) {
|
||||||
series->images[i].buffer = malloc(pixels * sizeof(GrayScalePixelType));
|
/* cleanup */
|
||||||
if (!series->images[i].buffer) {
|
freeImagesInternal(series->images, i);
|
||||||
clearSeries(series);
|
free(series->labels);
|
||||||
fclose(fp);
|
free(series);
|
||||||
return NULL;
|
fclose(fp);
|
||||||
}
|
return NULL;
|
||||||
|
}
|
||||||
for (size_t p = 0; p < pixels; ++p) {
|
|
||||||
int pix = 0;
|
if (!readPixels(fp, pixels, pixelCount)) {
|
||||||
if (fscanf(fp, "%d", &pix) != 1) {
|
free(pixels);
|
||||||
clearSeries(series);
|
freeImagesInternal(series->images, i);
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (pix < 0) pix = 0;
|
|
||||||
if (pix > 255) pix = 255;
|
|
||||||
series->images[i].buffer[p] = (GrayScalePixelType)pix;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned char label = 0;
|
||||||
|
if (!readLabel(fp, &label)) {
|
||||||
|
free(pixels);
|
||||||
|
freeImagesInternal(series->images, i);
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
|
fclose(fp);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
series->images[i].buffer = pixels;
|
||||||
|
series->images[i].width = width;
|
||||||
|
series->images[i].height = height;
|
||||||
|
series->images[i].label = label;
|
||||||
|
series->labels[i] = label;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return series;
|
return series;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
/* Freigabe der Serie */
|
||||||
void clearSeries(GrayScaleImageSeries *series)
|
void clearSeries(GrayScaleImageSeries *series)
|
||||||
{
|
{
|
||||||
if (!series) return;
|
if (!series) return;
|
||||||
|
|||||||
11
imageInput.h
11
imageInput.h
@ -3,18 +3,17 @@
|
|||||||
|
|
||||||
typedef unsigned char GrayScalePixelType;
|
typedef unsigned char GrayScalePixelType;
|
||||||
|
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
unsigned char *buffer; // Pixeldaten (width * height)
|
||||||
GrayScalePixelType *buffer;
|
|
||||||
unsigned int width;
|
unsigned int width;
|
||||||
unsigned int height;
|
unsigned int height;
|
||||||
|
unsigned char label; // <- Label hinzufügen
|
||||||
} GrayScaleImage;
|
} GrayScaleImage;
|
||||||
|
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
|
||||||
GrayScaleImage *images;
|
GrayScaleImage *images;
|
||||||
unsigned char *labels;
|
|
||||||
unsigned int count;
|
unsigned int count;
|
||||||
|
unsigned char *labels;
|
||||||
} GrayScaleImageSeries;
|
} GrayScaleImageSeries;
|
||||||
|
|
||||||
GrayScaleImageSeries *readImages(const char *path);
|
GrayScaleImageSeries *readImages(const char *path);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user