Skip to content

Commit

Permalink
declare loop variables outside of for(). free resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
7890 committed May 13, 2021
1 parent 49c0e02 commit 01efa54
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,23 @@ main(int argc, char ** argv)

/* dump palette if verbose option was set */
if (verbose)
for (int i = 0; i < palette->size; i++)
{
int i;
for (i = 0; i < palette->size; i++)
printf("%d %d %d\n",
palette->colors[i].r,
palette->colors[i].g,
palette->colors[i].b
);

}
if (dither) {
ApplyFloydSteinbergDither(input, palette);
} else {
/* closest color only */
DTPixel *pixel;
for (int i = 0; i < input->height; i++) {
for (int j = 0; j < input->width; j++) {
int i, j;
for (i = 0; i < input->height; i++) {
for (j = 0; j < input->width; j++) {
pixel = &input->pixels[i*input->width + j];
*pixel = FindClosestColorFromPalette(*pixel, palette);
}
Expand All @@ -89,6 +92,11 @@ main(int argc, char ** argv)

WriteImageToFile(input, outputFile);

free(input->pixels);
free(input);
free(palette->colors);
free(palette);

return 0;
}

Expand Down Expand Up @@ -161,12 +169,13 @@ ReadPaletteFromStdin(int size)
palette->size = size;
palette->colors = malloc(sizeof(DTPixel) * size);

int r, g, b;
for (int i = 0; i < size; i++) {
scanf(" %d %d %d", &r, &g, &b);
int r, g, b, i, ret;
for (i = 0; i < size; i++) {
ret=scanf(" %d %d %d", &r, &g, &b);
palette->colors[i].r = r;
palette->colors[i].g = g;
palette->colors[i].b = b;
(void) ret;
}

return palette;
Expand All @@ -179,7 +188,8 @@ QuantizedPaletteForImage(DTImage *image, int size)
MCTriplet *colors;
DTPalette *palette;

for (int i = 0; i < image->resolution; i++)
int i;
for (i = 0; i < image->resolution; i++)
data[i] = MCTripletMake(
image->pixels[i].r,
image->pixels[i].g,
Expand All @@ -192,7 +202,7 @@ QuantizedPaletteForImage(DTImage *image, int size)
palette->size = size;
palette->colors = malloc(sizeof(DTPixel) * size);

for (int i = 0; i < size; i++) {
for (i = 0; i < size; i++) {
palette->colors[i].r = colors[i].value[0];
palette->colors[i].g = colors[i].value[1];
palette->colors[i].b = colors[i].value[2];
Expand Down

0 comments on commit 01efa54

Please sign in to comment.