Click here to Skip to main content
15,916,463 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: switch to existing instance Pin
led mike4-Mar-09 8:36
led mike4-Mar-09 8:36 
GeneralRe: switch to existing instance Pin
negativeG4-Mar-09 8:51
negativeG4-Mar-09 8:51 
GeneralRe: switch to existing instance Pin
led mike4-Mar-09 9:04
led mike4-Mar-09 9:04 
AnswerRe: switch to existing instance Pin
David Crow4-Mar-09 9:38
David Crow4-Mar-09 9:38 
GeneralRe: switch to existing instance Pin
negativeG5-Mar-09 7:58
negativeG5-Mar-09 7:58 
QuestionClipboard to jpeg using jpeglib Pin
alexhalt4-Mar-09 5:39
alexhalt4-Mar-09 5:39 
AnswerRe: Clipboard to jpeg using jpeglib Pin
Stuart Dootson4-Mar-09 6:12
professionalStuart Dootson4-Mar-09 6:12 
GeneralRe: Clipboard to jpeg using jpeglib Pin
alexhalt10-Mar-09 1:45
alexhalt10-Mar-09 1:45 
Stuart Dootson wrote:
If you're using Visual Studio 2003 or later, it might be easier to use CImage[^], as that has built-in 'save as jpeg' functionality?


I use C, not C++.


Stuart Dootson wrote:

Looking at the code for libjpeg, 100 is the 'initialisation' state. This error would be raised by jpeg_write_scanlines if you hadn't called jpeg_start_compress.


Thanks! Now I get a better error Smile | :)

bRet = OpenClipboard( hwndWindow );
	
	// ERROR_SUCCESS
	


	uFormat = CountClipboardFormats();
	
	
	hClipboardData = GetClipboardData( CF_BITMAP );


	hDib = DDBToDIB( ( HBITMAP )hClipboardData, BI_RGB, NULL );
	lpbi = ( LPBITMAPINFOHEADER )hDib;

	
		
	{
		struct jpeg_compress_struct cinfo;
		struct jpeg_error_mgr jerr;
		FILE * outfile;
		char filename[] = "out.jpg";

		cinfo.err = jpeg_std_error(&jerr);
		jpeg_create_compress(&cinfo);


		
		//! destination
		if ((outfile = fopen(filename, "wb")) == NULL) {
			fprintf(stderr, "can't open %s\n", filename);
			exit(1);
		}
		jpeg_stdio_dest(&cinfo, outfile);


		cinfo.image_width = lpbi->biWidth; 	/* image width and height, in pixels */
		cinfo.image_height = lpbi->biHeight;
		
		cinfo.input_components = 3;// lpbi->biBitCount/8;	/* # of color components per pixel */
		
		cinfo.in_color_space = JCS_RGB; /* colorspace of input image */

		jpeg_set_defaults(&cinfo);
		
		
		/* Make optional parameter settings here */
		{
			
			JSAMPARRAY jsmpArray;    //Pixel RGB buffer for JPEG file
			
			
			
			jsmpArray = (*cinfo.mem->alloc_sarray)
                ((j_common_ptr) &cinfo,
                 JPOOL_IMAGE,
                 cinfo.image_width * cinfo.input_components,
                 cinfo.image_height);

			
			DibToSamps(	hDib, cinfo.image_width * cinfo.input_components, cinfo, jsmpArray );

			jpeg_start_compress(&cinfo,TRUE);


			(void)jpeg_write_scanlines( &cinfo,
                                       jsmpArray,
                                       cinfo.image_height );

Where DibToSamps() function is like here http://www.codeguru.com/cpp/g-m/bitmap/otherformats/print.php/c1731[^]
and DDBToDIB is like follows

HANDLE DDBToDIB( HBITMAP bitmap, DWORD dwCompression, HPALETTE pPal ) 
{
	BITMAP			bm;
	BITMAPINFOHEADER	bi;
	LPBITMAPINFOHEADER 	lpbi;
	DWORD			dwLen;
	HANDLE			hDIB;
	HANDLE			handle;
	HDC 			hDC;
	HPALETTE		hPal;
	int nColors = 0;
	BOOL bGotBits;


	// The function has no arg for bitfields
	if( dwCompression == BI_BITFIELDS )
		return NULL;

	// If a palette has not been supplied use defaul palette
	hPal = (HPALETTE) pPal;
	if (hPal==NULL)
		hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);

	// Get bitmap information
	GetObject(bitmap,sizeof(bm),(LPSTR)&bm);

	// Initialize the bitmapinfoheader
	bi.biSize		= sizeof(BITMAPINFOHEADER);
	bi.biWidth		= bm.bmWidth;
	bi.biHeight 		= bm.bmHeight;
	bi.biPlanes 		= 1;
	bi.biBitCount		= bm.bmPlanes * bm.bmBitsPixel;
	bi.biCompression	= dwCompression;
	bi.biSizeImage		= 0;
	bi.biXPelsPerMeter	= 0;
	bi.biYPelsPerMeter	= 0;
	bi.biClrUsed		= 0;
	bi.biClrImportant	= 0;

	// Compute the size of the  infoheader and the color table
	nColors = (1 << bi.biBitCount);
	if( nColors > 256 ) 
		nColors = 0;
	dwLen  = bi.biSize + nColors * sizeof(RGBQUAD);

	// We need a device context to get the DIB from
	hDC = GetDC(NULL);
	hPal = SelectPalette(hDC,hPal,FALSE);
	RealizePalette(hDC);

	// Allocate enough memory to hold bitmapinfoheader and color table
	hDIB = GlobalAlloc(GMEM_FIXED,dwLen);

	if (!hDIB)
	{
		SelectPalette(hDC,hPal,FALSE);
		ReleaseDC(NULL,hDC);
		return NULL;
	}

	lpbi = (LPBITMAPINFOHEADER)hDIB;

	*lpbi = bi;

	// Call GetDIBits with a NULL lpBits param, so the device driver 
	// will calculate the biSizeImage field 
	GetDIBits(hDC, (HBITMAP)bitmap, 0L, (DWORD)bi.biHeight,
			(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);

	bi = *lpbi;

	// If the driver did not fill in the biSizeImage field, then compute it
	// Each scan line of the image is aligned on a DWORD (32bit) boundary
	if (bi.biSizeImage == 0)
	{
		bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8) 
						* bi.biHeight;

		// If a compression scheme is used the result may infact be larger
		// Increase the size to account for this.
		if (dwCompression != BI_RGB)
			bi.biSizeImage = (bi.biSizeImage * 3) / 2;
	}

	// Realloc the buffer so that it can hold all the bits
	dwLen += bi.biSizeImage;
	if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
	{
		hDIB = handle;
	}
	else
	{
		GlobalFree(hDIB);
		
		// Reselect the original palette
		SelectPalette(hDC,hPal,FALSE);
		ReleaseDC(NULL,hDC);
		return NULL;
	}
	
	// Get the bitmap bits
	lpbi = (LPBITMAPINFOHEADER)hDIB;
	
	// FINALLY get the DIB
	bGotBits = GetDIBits( hDC, bitmap,
		0L,				// Start scan line
		(DWORD)bi.biHeight,		// # of scan lines
		(LPBYTE)lpbi 			// address for bitmap bits
		+ (bi.biSize + nColors * sizeof(RGBQUAD)),
		(LPBITMAPINFO)lpbi,		// address of bitmapinfo
		(DWORD)DIB_RGB_COLORS);		// Use RGB for color table
	
	if( !bGotBits )
	{
		GlobalFree(hDIB);
		
		SelectPalette(hDC,hPal,FALSE);
		ReleaseDC(NULL,hDC);
		return NULL;
	}
	
	SelectPalette(hDC,hPal,FALSE);
	ReleaseDC(NULL,hDC);
	return hDIB;
}


At the bottom line of the code :
(void)jpeg_write_scanlines( &cinfo,
                                       jsmpArray,
                                       cinfo.image_height );

appears some ACCESS VIOLATION at address 0x10.

In the code I followed the manual, which says
Allocate and initialize a JPEG compression object
Specify the destination for the compressed data (eg, a file)
Set parameters for compression, including image size & colorspace
jpeg_start_compress(...);
while (scan lines remain to be written)
jpeg_write_scanlines(...);

But obviously it looks like I haven't preallocated something.

Please advise


GeneralRe: Clipboard to jpeg using jpeglib Pin
Stuart Dootson10-Mar-09 2:02
professionalStuart Dootson10-Mar-09 2:02 
QuestionPing Using UDP Pin
tiger08064-Mar-09 5:00
tiger08064-Mar-09 5:00 
AnswerRe: Ping Using UDP [modified] Pin
Stuart Dootson4-Mar-09 5:52
professionalStuart Dootson4-Mar-09 5:52 
Question[Message Deleted] Pin
FISH7864-Mar-09 4:38
FISH7864-Mar-09 4:38 
AnswerRe: Powerbuilder to visual c++. Pin
Stuart Dootson4-Mar-09 5:49
professionalStuart Dootson4-Mar-09 5:49 
GeneralRe: Powerbuilder to visual c++. Pin
FISH7864-Mar-09 7:00
FISH7864-Mar-09 7:00 
GeneralRe: Powerbuilder to visual c++. Pin
Stuart Dootson4-Mar-09 7:06
professionalStuart Dootson4-Mar-09 7:06 
GeneralRe: Powerbuilder to visual c++. Pin
FISH7864-Mar-09 10:11
FISH7864-Mar-09 10:11 
QuestionCreating header - CListCtrl Pin
grassrootkit4-Mar-09 3:59
grassrootkit4-Mar-09 3:59 
AnswerRe: Creating header - CListCtrl Pin
Michael Schubert4-Mar-09 5:40
Michael Schubert4-Mar-09 5:40 
QuestionRe: Creating header - CListCtrl Pin
grassrootkit4-Mar-09 6:20
grassrootkit4-Mar-09 6:20 
AnswerRe: Creating header - CListCtrl Pin
Stuart Dootson4-Mar-09 6:25
professionalStuart Dootson4-Mar-09 6:25 
GeneralRe: Creating header - CListCtrl Pin
grassrootkit4-Mar-09 6:31
grassrootkit4-Mar-09 6:31 
GeneralRe: Creating header - CListCtrl Pin
grassrootkit4-Mar-09 6:33
grassrootkit4-Mar-09 6:33 
GeneralRe: Creating header - CListCtrl Pin
grassrootkit4-Mar-09 6:25
grassrootkit4-Mar-09 6:25 
QuestionFrom A FrameWindow Pin
grassrootkit4-Mar-09 3:37
grassrootkit4-Mar-09 3:37 
AnswerRe: From A FrameWindow Pin
Stuart Dootson4-Mar-09 5:45
professionalStuart Dootson4-Mar-09 5:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.