LibGD library does not work: crash while saving image

I have been looking for a JPG library for saving in C ++ for a long time, but I cannot get anything to work. Now I am trying to use LibGD:

What am I doing wrong? It seems to work, but the savings are falling. The code:

... #pragma comment(lib, "bgd.lib") #include <gd/gd.h> ... void save_test(){ gdImagePtr im; FILE *jpegout; int black; int white; im = gdImageCreateTrueColor(64, 64); black = gdImageColorAllocate(im, 0, 0, 0); white = gdImageColorAllocate(im, 255, 255, 255); gdImageLine(im, 0, 0, 63, 63, white); if(jpegout = fopen("test.jpg", "wb")){ if(im){ gdImageJpeg(im, jpegout, -1); // crash here! } fclose(jpegout); } gdImageDestroy(im); } 

I downloaded the library from: http://www.libgd.org/releases/gd-latest-win32.zip

I have library / include / bgd.dll files in the correct directories, etc.

Edit: The answer below is this code that fixed my problem:

 int size; char* data = (char*)gdImagePngPtr(im, &size); fwrite(data, sizeof(char), size, out); gdFree(data); 
+1
source share
1 answer

Check im and jpegout before trying to use them to make sure that both of them are highlighted.

[Change] It would be better to separate the purpose of the file descriptor from the test by its reliability. Have you tried the libgd example ?

[Edit2] I downloaded the same source, etc., set up the project in VS2008 and got the same problem. You can try this offer .

one important thing in GD is to make sure it is built on the same CRT as the main project, because it uses structures like FILE, and if you call the GD DLL built with the same version of the compiler from the executable created with another version, you will encounter memory access violations.

There is a piece of code that fixes a crash on my machine:

 /* Bring in gd library functions */ #include "gd.h" /* Bring in standard I/O so we can output the PNG to a file */ #include <stdio.h> int main() { /* Declare the image */ gdImagePtr im; /* Declare output files */ FILE *pngout, *jpegout; /* Declare color indexes */ int black; int white; /* Allocate the image: 64 pixels across by 64 pixels tall */ im = gdImageCreate(64, 64); /* Allocate the color black (red, green and blue all minimum). Since this is the first color in a new image, it will be the background color. */ black = gdImageColorAllocate(im, 0, 0, 0); /* Allocate the color white (red, green and blue all maximum). */ white = gdImageColorAllocate(im, 255, 255, 255); /* Draw a line from the upper left to the lower right, using white color index. */ gdImageLine(im, 0, 0, 63, 63, white); /* Open a file for writing. "wb" means "write binary", important under MSDOS, harmless under Unix. */ errno_t result1 = fopen_s(&pngout, "C:\\Projects\\Experiments\\LibGD\\test.png", "wb"); /* Do the same for a JPEG-format file. */ errno_t result2 = fopen_s(&jpegout, "C:\\Projects\\Experiments\\LibGD\\test.jpg", "wb+"); /* Output the image to the disk file in PNG format. */ int size; char* data = (char*)gdImagePngPtr(im, &size); fwrite(data, sizeof(char), size, pngout); gdFree(data); data = (char*)gdImageJpegPtr(im, &size, -1); fwrite(data, sizeof(char), size, jpegout); gdFree(data); /* Close the files. */ fclose(pngout); fclose(jpegout); /* Destroy the image in memory. */ gdImageDestroy(im); } 
+2
source

Source: https://habr.com/ru/post/1301614/


All Articles