Skip to content

Commit 96dba8a

Browse files
committed
[FIX]- BMP-JPEG conversion
In the following Code there was an issue that the converted image's colour was changed and the original format of the image was refused applying a blue filter on it. Which was due to the BMP colour pallet being different to the one of JPEG, which requires the use of a converter. Which was inproper.
1 parent 338251c commit 96dba8a

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/assets/images/testjpg.jpg

-20.4 KB
Binary file not shown.

src/converters/bmp_to_jpeg.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,35 @@ int bmp_to_jpeg(const char *input_filename, const char *output_filename)
8181
jpeg_stdio_dest(&cinfo, output_file);
8282
// start JPEG compressor
8383
jpeg_start_compress(&cinfo, TRUE);
84+
8485
// write JPEG image data
8586
while (cinfo.next_scanline < cinfo.image_height)
8687
{
87-
JSAMPROW row_pointer = &bmp_data[(cinfo.image_height - cinfo.next_scanline - 1) * (width * 3 + padding)];
88+
// Get a pointer to the current row of BMP data
89+
unsigned char *bmp_row = &bmp_data[(cinfo.image_height - cinfo.next_scanline - 1) * (width * 3 + padding)];
90+
91+
// Allocate an array to hold a row of RGB data in the correct order (RGB)
92+
unsigned char *rgb_row = (unsigned char *)malloc(width * 3);
93+
94+
// Extract RGB data from BGR order and store it in rgb_row
95+
for (int i = 0; i < width; i++)
96+
{
97+
rgb_row[i * 3 + 0] = bmp_row[i * 3 + 2]; // Blue
98+
rgb_row[i * 3 + 1] = bmp_row[i * 3 + 1]; // Green
99+
rgb_row[i * 3 + 2] = bmp_row[i * 3 + 0]; // Red
100+
}
101+
102+
// Write the corrected RGB row to the JPEG compressor
103+
JSAMPROW row_pointer = rgb_row;
88104
jpeg_write_scanlines(&cinfo, &row_pointer, 1);
105+
106+
// Free the allocated memory for the RGB row
107+
free(rgb_row);
89108
}
90-
// finish JPEG compressor
91109
jpeg_finish_compress(&cinfo);
92-
// get size of JPEG image data
93110
jpeg_size = ftell(output_file);
94-
// close output file
95111
fclose(output_file);
96-
// destroy JPEG compressor object
97112
jpeg_destroy_compress(&cinfo);
98-
// free BMP image data
99113
free(bmp_data);
100114
printf("Converted %s to %s (%lu bytes)\n", input_filename, output_filename, jpeg_size);
101115

0 commit comments

Comments
 (0)