rewrote read image data programm now sometimes works and sometime dosnt, because of faulty memory allocation
This commit is contained in:
parent
d59ffa6888
commit
0a909936ed
135
imageInput.c
135
imageInput.c
@ -32,16 +32,24 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
|
||||
if (readSource != NULL)
|
||||
{
|
||||
series = calloc(amountOfStatusInfoToRead, (3 * sizeof(unsigned int) + 3 * sizeof(headerString)));
|
||||
series->images = calloc(amountOfStatusInfoToRead, (2 * sizeof(unsigned int) + sizeof(headerString)));
|
||||
|
||||
//Speicheralloziierung komplett umschreichen!!!!!
|
||||
// Series reservieren mit größe einer grayscaleImageSeries
|
||||
// ein series->count reservieren mit größe unsigned int
|
||||
// series->labels reservieren mit zahl count und größe int
|
||||
// series->images[i].buffer mit zahl series->count *numberOfBytesToRead und size of unsigned Char
|
||||
|
||||
series = calloc(amountOfStatusInfoToRead, (3 * sizeof(unsigned int) + 3 * sizeof(headerString)));
|
||||
series->images = calloc(amountOfStatusInfoToRead, (2 * sizeof(unsigned int) + sizeof(headerString)));
|
||||
|
||||
numberOfBytesToRead = readStatusInfo(readSource, series, headerString, sizeOfStausInfoElementsInBytes, amountOfStatusInfoToRead);
|
||||
numberOfBytesToRead = readStatusInfo(readSource, series, headerString, sizeOfStausInfoElementsInBytes, amountOfStatusInfoToRead);
|
||||
|
||||
expectedHeader = checkHeaderString(headerString);
|
||||
expectedHeader = checkHeaderString(headerString);
|
||||
|
||||
// printf("llu %llu\n", series->count * numberOfBytesToRead * sizeof(GrayScalePixelType));
|
||||
series->images->buffer = calloc(((series->count) * numberOfBytesToRead), sizeof(GrayScalePixelType));
|
||||
series->labels = calloc((series->count), sizeof(&(series->labels)));
|
||||
// printf("count %d\n", series->count);
|
||||
// series->images[0].buffer = calloc(((series->count) * numberOfBytesToRead), sizeof(GrayScalePixelType));
|
||||
series->labels = calloc((series->count), sizeof(&(series->labels)));
|
||||
|
||||
if (expectedHeader)
|
||||
{
|
||||
@ -49,8 +57,13 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
//heightbuffer = series->images->height;
|
||||
|
||||
// reallocate memory so that each image width can be saved seperately
|
||||
series->images = realloc(series->images, (series->count * (2 * sizeof(unsigned int) + sizeof(headerString))));
|
||||
//series->images = realloc(series->images, (series->count * (2 * sizeof(unsigned int) + sizeof(headerString))));
|
||||
for (int i = 1; i < series->count; i++)
|
||||
{
|
||||
series->images[i].buffer = calloc(numberOfBytesToRead, sizeof(unsigned char));
|
||||
}
|
||||
|
||||
|
||||
//series->images->width = widthbuffer;
|
||||
//series->images->height = heightbuffer;
|
||||
|
||||
@ -63,11 +76,15 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
|
||||
for (int i = 0; i < series->count; i++)
|
||||
{
|
||||
// printf("hier1! \n");
|
||||
series->images[i].width = series->images->width;
|
||||
// printf("hier2! \n");
|
||||
series->images[i].height = series->images->height;
|
||||
// printf("hier2! \n");
|
||||
}
|
||||
|
||||
readImagedata(readSource, series, numberOfBytesToRead);
|
||||
// printf("hier4! \n");
|
||||
|
||||
}
|
||||
else
|
||||
@ -87,13 +104,54 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
void clearSeries(GrayScaleImageSeries * series)
|
||||
{
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
|
||||
|
||||
// Write NULL into all memory spaces
|
||||
|
||||
for (i = 0; i < series->count; i++)
|
||||
{
|
||||
for (j = 0; j < series->images[i].width * series->images[i].height; j++)
|
||||
{
|
||||
series->images[i].buffer[j] = 0;
|
||||
}
|
||||
|
||||
series->labels[i] = 0;
|
||||
series->images[i].width = 0;
|
||||
series->images[i].height = 0;
|
||||
}
|
||||
|
||||
// free all pointer
|
||||
for (i = 0; i < series->count; i++)
|
||||
{
|
||||
free(series->images[i].buffer);
|
||||
series->images[i].buffer = NULL;
|
||||
}
|
||||
|
||||
series->count = 0;
|
||||
|
||||
free(series->images);
|
||||
series->images = NULL;
|
||||
free(series->labels);
|
||||
series->labels = NULL;
|
||||
free(series);
|
||||
series = NULL;
|
||||
|
||||
printf("cleared\n");
|
||||
|
||||
|
||||
/*
|
||||
// Write NULL into all memory spaces
|
||||
for (i = 0; i < ((series->count) * (series->images->width) * (series->images->height) / 4); i++)
|
||||
{
|
||||
*(series->images->buffer + i * 4 * (series->images->width) * (series->images->height)) = '\0';
|
||||
}
|
||||
|
||||
for (i = 1; i < series->count; i++)
|
||||
{
|
||||
free(series->images[i].buffer);
|
||||
series->images[i].buffer = NULL;
|
||||
}
|
||||
|
||||
for (i = 0; i < (series->count); i++)
|
||||
{
|
||||
@ -103,10 +161,12 @@ void clearSeries(GrayScaleImageSeries * series)
|
||||
series->count = 0;
|
||||
series->images->width = 0;
|
||||
series->images->height = 0;
|
||||
series->images = 0;
|
||||
|
||||
// Closse all allocated memory
|
||||
// AND write NULL into every pointer
|
||||
// so they can't be accessed
|
||||
|
||||
free(series->images->buffer);
|
||||
series->images->buffer = NULL;
|
||||
free(series->labels);
|
||||
@ -115,7 +175,7 @@ void clearSeries(GrayScaleImageSeries * series)
|
||||
series->images = NULL;
|
||||
free(series);
|
||||
series = NULL;
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
@ -140,18 +200,67 @@ unsigned int readStatusInfo(FILE *const source, GrayScaleImageSeries *const seri
|
||||
// reads the imagebytes and the label of all images
|
||||
void readImagedata(FILE *const source, GrayScaleImageSeries *const series, int const amountToRead)
|
||||
{
|
||||
int i = 0;
|
||||
int i = 0;
|
||||
|
||||
for (i = 0; i < series->count ; i++)
|
||||
{
|
||||
{
|
||||
// int numbreadbuffer = 0;
|
||||
// int numbreadlabel = 0;
|
||||
// labelbuffer = 0;
|
||||
// printf("amount11 %d\n", amountToRead);
|
||||
// gedanke: &(series->images[i].buffer)
|
||||
// geht: &(series->images->buffer[0]) +i*36)
|
||||
// geht nicht: &series->images[i].buffer
|
||||
fread(&series->images[i].buffer[0], 1, amountToRead, source);
|
||||
fread(&(series->labels[i]), 1, 1, source);
|
||||
// printf("label: %d\n", series->labels[i]);
|
||||
// printf("amountToRead: %d\n", amountToRead);
|
||||
/* numbreadbuffer =*/ fread(&series->images[i].buffer[0], 1, amountToRead, source);
|
||||
|
||||
/*
|
||||
if (feof(source))
|
||||
{
|
||||
printf("EOF!! buffer\n");
|
||||
}
|
||||
if (ferror(source))
|
||||
{
|
||||
printf("ERROR!!! buffer\n");
|
||||
}
|
||||
// */
|
||||
|
||||
/*numbreadlabel =*/ fread(&series->labels[i], sizeof(*series->images->buffer), sizeof(*series->labels), source);
|
||||
/*
|
||||
if (feof(source))
|
||||
{
|
||||
printf("EOF!!\n label");
|
||||
}
|
||||
if (ferror(source))
|
||||
{
|
||||
printf("ERROR!!! label\n");
|
||||
}
|
||||
|
||||
|
||||
printf("numbreadbuffer: %d\n", numbreadbuffer);
|
||||
printf("numbreadlabel: %d\n", numbreadlabel);
|
||||
|
||||
printf("label %d: %d\n", i, series->labels[i]);
|
||||
|
||||
// fread(&(series->labels[i]), sizeof(*series->images->buffer), sizeof(*series->labels), source);
|
||||
|
||||
|
||||
// print complete imagebuffer
|
||||
int k = 0;
|
||||
for (int j = 0; j < amountToRead; j++)
|
||||
{
|
||||
printf("%x", series->images[i].buffer[j]);
|
||||
k++;
|
||||
if (k >= 9)
|
||||
{
|
||||
printf("\n");
|
||||
k = 0;
|
||||
}
|
||||
|
||||
}
|
||||
printf("\n");
|
||||
// */
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user