13 Commits
3 changed files with 53 additions and 55 deletions
+31 -30
View File
@@ -29,23 +29,22 @@ GrayScaleImageSeries *readImages(const char *path)
if (readSource != NULL)
{
series = calloc(3 * sizeof(unsigned int) + 3*sizeof(headerString), amountOfStatusInfoToRead);
series->images = calloc(2 * sizeof(unsigned int) + sizeof(headerString), amountOfStatusInfoToRead);
series = calloc (amountOfStatusInfoToRead, sizeof(GrayScaleImageSeries));
series->images = calloc (amountOfStatusInfoToRead, sizeof(GrayScaleImage));
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)));
series->images = realloc (series->images, series->count * sizeof(GrayScaleImage));
series->labels = calloc (series->count, sizeof(&(series->labels)));
if (expectedHeader)
{
// reallocate memory so that each image width can be saved seperately
series->images = realloc(series->images, series->count * (2 * sizeof(unsigned int) + sizeof(headerString)));
for (int i = 0; i < series->count; i++)
{
series->images[i].buffer = calloc(numberOfBytesToRead, sizeof(unsigned char));
}
// for loop was previously running with i < series->count, wich caused programm to crash.
for (int i = 0; i < series->count; i++)
{
series->images[i].width = series->images->width;
@@ -56,7 +55,8 @@ GrayScaleImageSeries *readImages(const char *path)
}
else
{
series = NULL;
fclose(readSource);
return NULL;
}
fclose(readSource);
@@ -71,35 +71,36 @@ 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) * (series->images->width) * (series->images->height) / 4); i++)
for (i = 0; i < series->count; i++)
{
*(series->images->buffer + i * 4 * (series->images->width) * (series->images->height)) = '\0';
if (i >= 0)
{
for (j = 0; j < series->images[i].width * series->images[i].height; j++)
{
series->images[i].buffer[j] = 0;
}
}
for (i = 0; i < (series->count); i++)
{
*(series->labels + i) = '\0';
series->labels[i] = 0;
series->images[i].width = 0;
series->images[i].height = 0;
}
series->count = 0;
series->images->width = 0;
series->images->height = 0;
for (i = 0; i < series->count; i++)
{
free(series->images[i].buffer);
series->images[i].buffer = NULL;
}
// 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);
series->labels = NULL;
free(series->images);
series->images = NULL;
free(series);
series = NULL;
}
@@ -122,15 +123,15 @@ 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 amountOfBytes)
void readImagedata(FILE *const source, GrayScaleImageSeries *const series, int const amountToRead)
{
int i = 0;
for (i = 0; i < series->count ; i++)
{
fread(&(series->images->buffer[i]), sizeof(*series->images->buffer), amountOfBytes, source);
fread(&(series->labels[i]), sizeof(*series->images->buffer), sizeof(*series->labels), source);
fread(&series->images[i].buffer[0], sizeof(*series->images->buffer), amountToRead, source);
fread(&series->labels[i], sizeof(*series->images->buffer), sizeof(*series->labels), source);
}
}
+6 -10
View File
@@ -41,25 +41,21 @@ void clearMatrix(Matrix *matrix)
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
// printf("hier0\n");
if(matrix.buffer == NULL)
{
//printf("Fehler beim Setzen! Matrix nicht initialisiert");
// printf("Fehler beim Setzen! Matrix nicht initialisiert");
return;
}
// printf("rowIdx %d\n", rowIdx);
// printf("mat.row %d\n", matrix.rows);
// printf("colIdx %d\n", colIdx);
// printf("mat.cols %d\n", matrix.cols);
if(rowIdx >= matrix.rows || colIdx >= matrix.cols)
{
//printf("Ungueltige Indizes beim Setzen!\n");
// printf("Ungueltige Indizes beim Setzen!\n");
return;
}
// printf("%d \n", rowIdx + matrix.cols * colIdx);
matrix.buffer[rowIdx + matrix.cols * colIdx] = value;
// printf("value %f\n", value);
// printf("hier1\n");
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
// printf("hier2\n");
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
+1
View File
@@ -182,6 +182,7 @@ static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], un
{
for(int j = 0; j < images[i].width * images[i].height; j++)
{
// printf("i %d, j %d \n", i,j);
setMatrixAt((MatrixType)images[i].buffer[j], matrix, j, i);
}
}