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
121
imageInput.c
121
imageInput.c
@ -32,6 +32,13 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
|
|
||||||
if (readSource != NULL)
|
if (readSource != NULL)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
//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 = calloc(amountOfStatusInfoToRead, (3 * sizeof(unsigned int) + 3 * sizeof(headerString)));
|
||||||
series->images = calloc(amountOfStatusInfoToRead, (2 * sizeof(unsigned int) + sizeof(headerString)));
|
series->images = calloc(amountOfStatusInfoToRead, (2 * sizeof(unsigned int) + sizeof(headerString)));
|
||||||
|
|
||||||
@ -40,7 +47,8 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
expectedHeader = checkHeaderString(headerString);
|
expectedHeader = checkHeaderString(headerString);
|
||||||
|
|
||||||
// printf("llu %llu\n", series->count * numberOfBytesToRead * sizeof(GrayScalePixelType));
|
// printf("llu %llu\n", series->count * numberOfBytesToRead * sizeof(GrayScalePixelType));
|
||||||
series->images->buffer = calloc(((series->count) * numberOfBytesToRead), sizeof(GrayScalePixelType));
|
// printf("count %d\n", series->count);
|
||||||
|
// series->images[0].buffer = calloc(((series->count) * numberOfBytesToRead), sizeof(GrayScalePixelType));
|
||||||
series->labels = calloc((series->count), sizeof(&(series->labels)));
|
series->labels = calloc((series->count), sizeof(&(series->labels)));
|
||||||
|
|
||||||
if (expectedHeader)
|
if (expectedHeader)
|
||||||
@ -49,7 +57,12 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
//heightbuffer = series->images->height;
|
//heightbuffer = series->images->height;
|
||||||
|
|
||||||
// reallocate memory so that each image width can be saved seperately
|
// 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->width = widthbuffer;
|
||||||
//series->images->height = heightbuffer;
|
//series->images->height = heightbuffer;
|
||||||
@ -63,11 +76,15 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
|
|
||||||
for (int i = 0; i < series->count; i++)
|
for (int i = 0; i < series->count; i++)
|
||||||
{
|
{
|
||||||
|
// printf("hier1! \n");
|
||||||
series->images[i].width = series->images->width;
|
series->images[i].width = series->images->width;
|
||||||
|
// printf("hier2! \n");
|
||||||
series->images[i].height = series->images->height;
|
series->images[i].height = series->images->height;
|
||||||
|
// printf("hier2! \n");
|
||||||
}
|
}
|
||||||
|
|
||||||
readImagedata(readSource, series, numberOfBytesToRead);
|
readImagedata(readSource, series, numberOfBytesToRead);
|
||||||
|
// printf("hier4! \n");
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -87,14 +104,55 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
void clearSeries(GrayScaleImageSeries * series)
|
void clearSeries(GrayScaleImageSeries * series)
|
||||||
{
|
{
|
||||||
int i = 0;
|
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
|
// Write NULL into all memory spaces
|
||||||
for (i = 0; i < ((series->count) * (series->images->width) * (series->images->height) / 4); i++)
|
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';
|
*(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++)
|
for (i = 0; i < (series->count); i++)
|
||||||
{
|
{
|
||||||
*(series->labels + i) = '\0';
|
*(series->labels + i) = '\0';
|
||||||
@ -103,10 +161,12 @@ void clearSeries(GrayScaleImageSeries * series)
|
|||||||
series->count = 0;
|
series->count = 0;
|
||||||
series->images->width = 0;
|
series->images->width = 0;
|
||||||
series->images->height = 0;
|
series->images->height = 0;
|
||||||
|
series->images = 0;
|
||||||
|
|
||||||
// Closse all allocated memory
|
// Closse all allocated memory
|
||||||
// AND write NULL into every pointer
|
// AND write NULL into every pointer
|
||||||
// so they can't be accessed
|
// so they can't be accessed
|
||||||
|
|
||||||
free(series->images->buffer);
|
free(series->images->buffer);
|
||||||
series->images->buffer = NULL;
|
series->images->buffer = NULL;
|
||||||
free(series->labels);
|
free(series->labels);
|
||||||
@ -115,7 +175,7 @@ void clearSeries(GrayScaleImageSeries * series)
|
|||||||
series->images = NULL;
|
series->images = NULL;
|
||||||
free(series);
|
free(series);
|
||||||
series = NULL;
|
series = NULL;
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -144,14 +204,63 @@ void readImagedata(FILE *const source, GrayScaleImageSeries *const series, int c
|
|||||||
|
|
||||||
for (i = 0; i < series->count ; i++)
|
for (i = 0; i < series->count ; i++)
|
||||||
{
|
{
|
||||||
|
// int numbreadbuffer = 0;
|
||||||
|
// int numbreadlabel = 0;
|
||||||
|
// labelbuffer = 0;
|
||||||
// printf("amount11 %d\n", amountToRead);
|
// printf("amount11 %d\n", amountToRead);
|
||||||
// gedanke: &(series->images[i].buffer)
|
// gedanke: &(series->images[i].buffer)
|
||||||
// geht: &(series->images->buffer[0]) +i*36)
|
// geht: &(series->images->buffer[0]) +i*36)
|
||||||
// geht nicht: &series->images[i].buffer
|
// geht nicht: &series->images[i].buffer
|
||||||
fread(&series->images[i].buffer[0], 1, amountToRead, source);
|
// printf("amountToRead: %d\n", amountToRead);
|
||||||
fread(&(series->labels[i]), 1, 1, source);
|
/* numbreadbuffer =*/ fread(&series->images[i].buffer[0], 1, amountToRead, source);
|
||||||
// printf("label: %d\n", series->labels[i]);
|
|
||||||
|
/*
|
||||||
|
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);
|
// 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