I don't understand why when i export to jpeg i get vertical lines every 1080 pixels. what am i doing wrong?
full repo -> https://github.com/ElPettego/swg_bg
vertical lines example -> https://github.com/ElPettego/swg_bg/blob/master/swg_test.jpg
void export_jpg(std::vector<std::vector<int>> grid) {
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
FILE* outfile = fopen("swg_test.jpg", "wb");
if (!outfile) {
exit(1);
}
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, 100, TRUE);
jpeg_start_compress(&cinfo, TRUE);
while (cinfo.next_scanline < cinfo.image_height) {
JSAMPROW row_buffer = new JSAMPLE[cinfo.image_width * 3];
for (int x = 0; x < width; x++) {
row_buffer[x * 3] = 0;
row_buffer[x * 3 + 2] = 0;
row_buffer[x * 3 + 1] = grid[cinfo.next_scanline][x] ? 255 : 0;
}
jpeg_write_scanlines(&cinfo, &row_buffer, 1);
delete[] row_buffer;
}
jpeg_finish_compress(&cinfo);
fclose(outfile);
jpeg_destroy_compress(&cinfo);
}
i tried modifying the bounds of the for loops in new_generation function and quality of export but the error still remains(https://github.com/ElPettego/swg_bg/blob/master/main.c%2B%2B)
Simple undefined behavior.
But you'd never know it by looking at the code in the question here, you need to look at the code in the repo that wasn't included.
Look at how
gridis created:Notice that
widthis defining the size of the outer vector, whileheightis defining the size of each inner vector.Now look at how you're accessing the grid:
The first pair of brackets dereferences the outer vector, while the second dereferences the inner vector. This is backwards to the way the vector was defined. Since width is greater than height, you're accessing out-of-bounds elements of the vector.
P.S. there's other out-of-bounds accesses lurking in that code, you should give it a once-over.