
//#define WIDTHBYTES(bits)    (((bits) + 31) / 32 * 4)


/*************************************************************************

  Function:  ReadBMPFile (CFile&)

   Purpose:  Reads in the specified BMP file into a global chunk of
			 memory.

   Returns:  A handle to a bmp (hBmp) if successful.
			 NULL if an error occurs.

  Comments:  BITMAPFILEHEADER is stripped off of the BMP.  Everything
			 from the end of the BITMAPFILEHEADER structure on is
			 returned in the global memory handle.

*************************************************************************/
/*
bool WINAPI ReadBMPFile(CFile& file, HBITMAP &hBmp, bool &IsWin30, CSize &size, int &NrOfColors, int &PaletteSize, int &planes, int &BitsPixel)
{
	BITMAPFILEHEADER bmfHeader;
	DWORD dwBitsSize, OldSize;
	unsigned char * pBMP;
	size.cx = size.cy =0;

	 //get length of BMP in bytes for use when reading

	dwBitsSize = file.GetLength();

	// Go read the BMP file header and check if it's valid.
	if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader)) return false;

	if (bmfHeader.bfType != BMP_HEADER_MARKER) return false;

	// Allocate memory for BMP
	if (hBmp !=NULL) {
		pBMP = (unsigned char *) ::GlobalLock((HGLOBAL) hBmp);
		OldSize = ::GlobalSize( (HGLOBAL) hBmp);
		if (OldSize<dwBitsSize) {
			::GlobalUnlock((HGLOBAL) hBmp);
			::GlobalFree((HGLOBAL) hBmp);
			hBmp = NULL;
		}
	}
	if (hBmp==NULL) {
		hBmp = (HBITMAP) ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwBitsSize);
		if (hBmp == 0) return false;
		pBMP = (unsigned char *) ::GlobalLock((HGLOBAL) hBmp);
	}

	// Go read the bits.
	if (file.ReadHuge(pBMP, dwBitsSize - sizeof(BITMAPFILEHEADER)) != dwBitsSize - sizeof(BITMAPFILEHEADER) )	{
		::GlobalUnlock((HGLOBAL) hBmp);
		::GlobalFree((HGLOBAL) hBmp);
		hBmp =NULL;
		return false;
	}
	unsigned char * PalettePtr;
	GetBmpDocProp( pBMP, IsWin30, size, NrOfColors, PaletteSize, planes, BitsPixel, PalettePtr);
	::GlobalUnlock((HGLOBAL) hBmp);
	return true;
}
*/




/*************************************************************************
 *
 * SaveBMP()
 *
 * Saves the specified BMP into the specified CFile.  The CFile
 * is opened and closed by the caller.
 *
 * Parameters:
 *
 * HBITMAP hBmp - Handle to the bmp to save
 *
 * CFile& file - open CFile used to save BMP
 *
 * Return value: 0 if successful, else error code or CFileException
 *
 *************************************************************************/

/*
bool WINAPI SaveBMP(HBITMAP hBmp, CFile& file, int PaletteSize)
{
	BITMAPFILEHEADER bmfHdr; // Header for Bitmap file
	LPBITMAPINFOHEADER lpBI;   // Pointer to BMP info structure
	DWORD dwBMPSize;

	if (hBmp == NULL) return false;

	// Get a pointer to the BMP memory, the first of which contains a BITMAPINFO structure
	lpBI = (LPBITMAPINFOHEADER) ::GlobalLock((HGLOBAL) hBmp);
	if (lpBI == NULL) return false;

	if (!IS_WIN30_BMP(lpBI))
	{
		::GlobalUnlock((HGLOBAL) hBmp);
		return false;       // It's an other-style BMP (save not supported)
	}

	// Fill in the fields of the file header

	// Fill in file type (first 2 bytes must be "BM" for a bitmap)
	bmfHdr.bfType = BMP_HEADER_MARKER;  // "BM"

	// Calculating the size of the BMP is a bit tricky (if we want to
	// do it right).  The easiest way to do this is to call GlobalSize()
	// on our global handle, but since the size of our global memory may have
	// been padded a few bytes, we may end up writing out a few too
	// many bytes to the file (which may cause problems with some apps).
	//
	// So, instead let's calculate the size manually (if we can)
	//
	// First, find size of header plus size of color table.  Since the
	// first DWORD in both BITMAPINFOHEADER and BITMAPCOREHEADER conains
	// the size of the structure, let's use this.

	dwBMPSize = *(LPDWORD)lpBI + PaletteSize;  // Partial Calculation

	// Now calculate the size of the image

	if ((lpBI->biCompression == BI_RLE8) || (lpBI->biCompression == BI_RLE4))
	{
		// It's an RLE bitmap, we can't calculate size, so trust the
		// biSizeImage field
		dwBMPSize += lpBI->biSizeImage;
	}
	else
	{
		DWORD dwBmBitsSize;  // Size of Bitmap Bits only
		// It's not RLE, so size is Width (DWORD aligned) * Height
		dwBmBitsSize = WIDTHBYTES((lpBI->biWidth)*((DWORD)lpBI->biBitCount)) * lpBI->biHeight;
		dwBMPSize += dwBmBitsSize;
		// Now, since we have calculated the correct size, why don't we
		// fill in the biSizeImage field (this will fix any .BMP files which
		// have this field incorrect).
		lpBI->biSizeImage = dwBmBitsSize;
	}


	// Calculate the file size by adding the BMP size to sizeof(BITMAPFILEHEADER)

	bmfHdr.bfSize = dwBMPSize + sizeof(BITMAPFILEHEADER);
	bmfHdr.bfReserved1 = 0;
	bmfHdr.bfReserved2 = 0;

	
	// Now, calculate the offset the actual bitmap bits will be in
	// the file -- It's the Bitmap file header plus the BMP header,
	// plus the size of the color table.
	
	bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + lpBI->biSize + PaletteSize;
	TRY
	{
		// Write the file header
		file.Write((LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER));
		//
		// Write the BMP header and the bits
		//
		file.WriteHuge(lpBI, dwBMPSize);
	}
	CATCH (CFileException, e)
	{
		::GlobalUnlock((HGLOBAL) hBmp);
		THROW_LAST();
	}
	END_CATCH

	::GlobalUnlock((HGLOBAL) hBmp);
	return true;
}
*/

from Doc
/*
	if (::CreateBMPPalette(m_hBMP, m_palBMP, m_IsWin30) == NULL)
	{
		// BMP may not have a palette
		delete m_palBMP;
		m_palBMP = NULL;
		return;
	}
*/












/*
 * Sample routine for JPEG decompression.  We assume that the source file name
 * is passed in.  We want to return 1 on success, 0 on error.
 */

/*
 * Sample routine for JPEG decompression.  We assume that the source file name
 * is passed in.  We want to return 1 on success, 0 on error.
 */
// Compression quality (0..100; 5-95 is useful range)\n");
int write_JPEG_file (const char * filename, int quality)
{
  /* This struct contains the JPEG compression parameters and pointers to
   * working space (which is allocated as needed by the JPEG library).
   * It is possible to have several such structures, representing multiple
   * compression/decompression processes, in existence at once.  We refer
   * to any one struct (and its associated working data) as a "JPEG object".
   */
  struct jpeg_compress_struct cinfo;
  /* This struct represents a JPEG error handler.  It is declared separately
   * because applications often want to supply a specialized error handler
   * (see the second half of this file for an example).  But here we just
   * take the easy way out and use the standard error handler, which will
   * print a message on stderr and call exit() if compression fails.
   * Note that this struct must live as long as the main JPEG parameter
   * struct, to avoid dangling-pointer problems.
   */
  struct jpeg_error_mgr jerr;
  /* More stuff */
  FILE * outfile;		/* target file */
//  JSAMPROW row_pointer[1];	/* pointer to JSAMPLE row[s] */
  int row_stride;		/* physical row width in image buffer */

  /* Step 1: allocate and initialize JPEG compression object */

  /* We have to set up the error handler first, in case the initialization
   * step fails.  (Unlikely, but it could happen if you are out of memory.)
   * This routine fills in the contents of struct jerr, and returns jerr's
   * address which we place into the link field in cinfo.
   */
  cinfo.err = jpeg_std_error(&jerr);
  /* Now we can initialize the JPEG compression object. */
  jpeg_create_compress(&cinfo);

  /* Step 2: specify data destination (eg, a file) */
  /* Note: steps 2 and 3 can be done in either order. */

  /* Here we use the library-supplied code to send compressed data to a
   * stdio stream.  You can also write your own code to do something else.
   * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
   * requires it in order to write binary files.
   */
  if ((outfile = fopen(filename, "wb")) == NULL) {
    fprintf(stderr, "can't open %s\n", filename);
    exit(1);
  }
  jpeg_stdio_dest(&cinfo, outfile);

  /* Step 3: set parameters for compression */

  /* First we supply a description of the input image.
   * Four fields of the cinfo struct must be filled in:
   */
  cinfo.image_width = m_sizeDoc.cx; 	/* image width and height, in pixels */
  cinfo.image_height = m_sizeDoc.cy; // image_height;
  cinfo.input_components = 3;		/* # of color components per pixel */
  cinfo.in_color_space = JCS_RGB; 	/* colorspace of input image */
  /* Now use the library's routine to set default compression parameters.
   * (You must set at least cinfo.in_color_space before calling this,
   * since the defaults depend on the source color space.)
   */
  jpeg_set_defaults(&cinfo);
  /* Now you can set any non-default parameters you wish to.
   * Here we just illustrate the use of quality (quantization table) scaling:
   */
  jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);

  /* Step 4: Start compressor */

  /* TRUE ensures that we will write a complete interchange-JPEG file.
   * Pass TRUE unless you are very sure of what you're doing.
   */
  jpeg_start_compress(&cinfo, TRUE);

  /* Step 5: while (scan lines remain to be written) */
  /*           jpeg_write_scanlines(...); */

  /* Here we use the library's state variable cinfo.next_scanline as the
   * loop counter, so that we don't have to keep track ourselves.
   * To keep things simple, we pass one scanline per call; you can pass
   * more if you wish, though.
   */
  row_stride = m_sizeDoc.cx * 3;	/* JSAMPLEs per row in image_buffer */
	unsigned char * InputPtr;
	int lineLength = WidthBytes( cinfo.input_components*8* m_sizeDoc.cx);
	InputPtr = (unsigned char *) ::GlobalLock( m_hBMP);
	InputPtr += sizeof( tagBITMAPINFOHEADER)+lineLength* (m_sizeDoc.cy-1);
//	char str[ 200];
//	sprintf( str, "width %i, height %i, lineLength %i, cinfo.next_scanline %i", cinfo.image_width , cinfo.image_height, lineLength, cinfo.next_scanline);
//	AfxMessageBox( str);


	
  while (cinfo.next_scanline < cinfo.image_height) {
    /* jpeg_write_scanlines expects an array of pointers to scanlines.
     * Here the array is only one element long, but you could pass
     * more than one scanline at a time if that's more convenient.
     */
//	CopyRGBSwitch( cp, InputPtr, m_sizeDoc.cx);
//	(void) jpeg_write_scanlines(&cinfo, &cp, 1);
//	InputPtr -= lineLength;
	RGBSwitch( InputPtr, m_sizeDoc.cx);
	(void) jpeg_write_scanlines(&cinfo, &InputPtr, 1);
	RGBSwitch( InputPtr, m_sizeDoc.cx);
	InputPtr -= lineLength;
  }

  /* Step 6: Finish compression */
	::GlobalUnlock( m_hBMP);
  jpeg_finish_compress(&cinfo);
  /* After finish_compress, we can close the output file. */
  fclose(outfile);

  /* Step 7: release JPEG compression object */

  /* This is an important step since it will release a good deal of memory. */
  jpeg_destroy_compress(&cinfo);

  /* And we're done! */
  return 1;
}


/*
 * SOME FINE POINTS:
 *
 * In the above loop, we ignored the return value of jpeg_write_scanlines,
 * which is the number of scanlines actually written.  We could get away
 * with this because we were only relying on the value of cinfo.next_scanline,
 * which will be incremented correctly.  If you maintain additional loop
 * variables then you should be careful to increment them properly.
 * Actually, for output to a stdio stream you needn't worry, because
 * then jpeg_write_scanlines will write all the lines passed (or else exit
 * with a fatal error).  Partial writes can only occur if you use a data
 * destination module that can demand suspension of the compressor.
 * (If you don't know what that's for, you don't need it.)
 *
 * If the compressor requires full-image buffers (for entropy-coding
 * optimization or a multi-scan JPEG file), it will create temporary
 * files for anything that doesn't fit within the maximum-memory setting.
 * (Note that temp files are NOT needed if you use the default parameters.)
 * On some systems you may need to set up a signal handler to ensure that
 * temporary files are deleted if the program is interrupted.  See libjpeg.txt.
 *
 * Scanlines MUST be supplied in top-to-bottom order if you want your JPEG
 * files to be compatible with everyone else's.  If you cannot readily read
 * your data in that order, you'll need an intermediate array to hold the
 * image.  See rdtarga.c or rdbmp.c for examples of handling bottom-to-top
 * source data using the JPEG code's internal virtual-array mechanisms.
 */


int CJpegTestDoc::read_JPEG_file1 (const char * filename)
{
	char str[ 200];
  /* This struct contains the JPEG decompression parameters and pointers to
   * working space (which is allocated as needed by the JPEG library).
   */
  struct jpeg_decompress_struct cinfo;
  /* We use our private extension JPEG error handler.
   * Note that this struct must live as long as the main JPEG parameter
   * struct, to avoid dangling-pointer problems.
   */
  struct my_error_mgr jerr;
  /* More stuff */
  FILE * infile;		/* source file */
//  JSAMPARRAY buffer;		/* Output row buffer */
  int row_stride;		/* physical row width in output buffer */

  /* In this example we want to open the input file before doing anything else,
   * so that the setjmp() error recovery below can assume the file is open.
   * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
   * requires it in order to read binary files.
   */

  if ((infile = fopen(filename, "rb")) == NULL) {
    sprintf( str, "can't open %s\n", filename);
	AfxMessageBox( str);
    return 0;
  }

  /* Step 1: allocate and initialize JPEG decompression object */
  /* We set up the normal JPEG error routines, then override error_exit. */
  cinfo.err = jpeg_std_error(&jerr.pub);
  jerr.pub.error_exit = my_error_exit;
  /* Establish the setjmp return context for my_error_exit to use. */
  if (setjmp(jerr.setjmp_buffer)) {
    /* If we get here, the JPEG code has signaled an error.
     * We need to clean up the JPEG object, close the input file, and return.
     */
    jpeg_destroy_decompress(&cinfo);
    fclose(infile);
    return 0;
  }

  /* Now we can initialize the JPEG decompression object. */
  jpeg_create_decompress(&cinfo);

  /* Step 2: specify data source (eg, a file) */
  jpeg_stdio_src(&cinfo, infile);

  /* Step 3: read file parameters with jpeg_read_header() */
  (void) jpeg_read_header(&cinfo, TRUE);
  /* We can ignore the return value from jpeg_read_header since
   *   (a) suspension is not possible with the stdio data source, and
   *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
   * See libjpeg.txt for more info.
   */

  /* Step 4: set parameters for decompression */
  /* In this example, we don't need to change any of the defaults set by
   * jpeg_read_header(), so we do nothing here.
   */

  /* Step 5: Start decompressor */

  (void) jpeg_start_decompress(&cinfo);
  /* We can ignore the return value since suspension is not possible
   * with the stdio data source.
   */

  /* We may need to do some setup of our own at this point before reading
   * the data.  After jpeg_start_decompress() we have the correct scaled
   * output image dimensions available, as well as the output colormap
   * if we asked for color quantization.
   * In this example, we need to make an output work buffer of the right size.
   */ 
  /* JSAMPLEs per row in output buffer */
  row_stride = cinfo.output_width * cinfo.output_components;
	// BMP prepare
	if (!MakeBitmapColors0( cinfo.output_components, cinfo.output_width, cinfo.output_height)) {
	  AfxMessageBox( "No mem allocated");
	  jpeg_destroy_decompress(&cinfo);
	  fclose(infile);
	  return 0;
	}

	unsigned char * pBMP;
	int lineLength = WidthBytes( cinfo.output_components*8* cinfo.output_width);
	pBMP = (unsigned char *) ::GlobalLock((HGLOBAL) m_hBMP);
	pBMP += sizeof( tagBITMAPINFOHEADER)+lineLength* (cinfo.output_height-1);
  /* Make a one-row-high sample array that will go away when done with image */
//  buffer = (*cinfo.mem->alloc_sarray)
//		((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);

  /* Step 6: while (scan lines remain to be read) */
  /*           jpeg_read_scanlines(...); */
  /* Here we use the library's state variable cinfo.output_scanline as the
   * loop counter, so that we don't have to keep track ourselves.
   */

  int LoopCounter =0;
  while (cinfo.output_scanline < cinfo.output_height) {
    /* jpeg_read_scanlines expects an array of pointers to scanlines.
     * Here the array is only one element long, but you could ask for
     * more than one scanline at a time if that's more convenient.
     */
//    (void) jpeg_read_scanlines(&cinfo, buffer, 1);
    /* Assume put_scanline_someplace wants a pointer and sample count. */
//	CopyRGBSwitch( pBMP, buffer[ 0], cinfo.output_width);
//	pBMP -= lineLength;
	(void) jpeg_read_scanlines(&cinfo, &pBMP, 1);
	RGBSwitch( pBMP, cinfo.output_width);
	pBMP -= lineLength;
	LoopCounter++;
  }
//	sprintf( str, "LoopCounter %i, outputWidth %i, outputHeight %i, outputComponents %i, lineLength %i", LoopCounter, cinfo.output_width, cinfo.output_height, cinfo.output_components, lineLength);
//	AfxMessageBox( str);
	::GlobalUnlock((HGLOBAL) m_hBMP);
  /* Step 7: Finish decompression */

  (void) jpeg_finish_decompress(&cinfo);
  /* We can ignore the return value since suspension is not possible
   * with the stdio data source.
   */

  /* Step 8: Release JPEG decompression object */
  /* This is an important step since it will release a good deal of memory. */
  jpeg_destroy_decompress(&cinfo);

  /* After finish_decompress, we can close the input file.
   * Here we postpone it until after no more JPEG errors are possible,
   * so as to simplify the setjmp error logic above.  (Actually, I don't
   * think that jpeg_destroy can do an error exit, but why assume anything...)
   */
  fclose(infile);

  /* At this point you may want to check to see whether any corrupt-data
   * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
   */

  /* And we're done! */
  return 1;
}

/*
 * SOME FINE POINTS:
 *
 * In the above code, we ignored the return value of jpeg_read_scanlines,
 * which is the number of scanlines actually read.  We could get away with
 * this because we asked for only one line at a time and we weren't using
 * a suspending data source.  See libjpeg.txt for more info.
 *
 * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
 * we should have done it beforehand to ensure that the space would be
 * counted against the JPEG max_memory setting.  In some systems the above
 * code would risk an out-of-memory error.  However, in general we don't
 * know the output image dimensions before jpeg_start_decompress(), unless we
 * call jpeg_calc_output_dimensions().  See libjpeg.txt for more about this.
 *
 * Scanlines are returned in the same order as they appear in the JPEG file,
 * which is standardly top-to-bottom.  If you must emit data bottom-to-top,
 * you can use one of the virtual arrays provided by the JPEG memory manager
 * to invert the data.  See wrbmp.c for an example.
 *
 * As with compression, some operating modes may require temporary files.
 * On some systems you may need to set up a signal handler to ensure that
 * temporary files are deleted if the program is interrupted.  See libjpeg.txt.
 */




struct my_error_mgr {
  struct jpeg_error_mgr pub;	/* "public" fields */

  jmp_buf setjmp_buffer;	/* for return to caller */
};

typedef struct my_error_mgr * my_error_ptr;

/*
 * Here's the routine that will replace the standard error_exit method:
 */

METHODDEF(void)
my_error_exit (j_common_ptr cinfo)
{
  /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
  my_error_ptr myerr = (my_error_ptr) cinfo->err;

  /* Always display the message. */
  /* We could postpone this until after returning, if we chose. */
  (*cinfo->err->output_message) (cinfo);

  /* Return control to the setjmp point */
  longjmp(myerr->setjmp_buffer, 1);
}


/*
 * Sample routine for JPEG decompression.  We assume that the source file name
 * is passed in.  We want to return 1 on success, 0 on error.
 */

/*
 * Sample routine for JPEG decompression.  We assume that the source file name
 * is passed in.  We want to return 1 on success, 0 on error.
 */
// Compression quality (0..100; 5-95 is useful range)\n");
int CJpegTestDoc::write_JPEG_file (const char * filename, int quality)
{
	if (m_BitsPixel !=24) {
		AfxMessageBox( "Sorry only for 24 bits per pixel.");
		return 0;
	}
  /* This struct contains the JPEG compression parameters and pointers to
   * working space (which is allocated as needed by the JPEG library).
   * It is possible to have several such structures, representing multiple
   * compression/decompression processes, in existence at once.  We refer
   * to any one struct (and its associated working data) as a "JPEG object".
   */
  struct jpeg_compress_struct cinfo;
  /* This struct represents a JPEG error handler.  It is declared separately
   * because applications often want to supply a specialized error handler
   * (see the second half of this file for an example).  But here we just
   * take the easy way out and use the standard error handler, which will
   * print a message on stderr and call exit() if compression fails.
   * Note that this struct must live as long as the main JPEG parameter
   * struct, to avoid dangling-pointer problems.
   */
  struct jpeg_error_mgr jerr;
  /* More stuff */
  FILE * outfile;		/* target file */
//  JSAMPROW row_pointer[1];	/* pointer to JSAMPLE row[s] */
  int row_stride;		/* physical row width in image buffer */

  /* Step 1: allocate and initialize JPEG compression object */

  /* We have to set up the error handler first, in case the initialization
   * step fails.  (Unlikely, but it could happen if you are out of memory.)
   * This routine fills in the contents of struct jerr, and returns jerr's
   * address which we place into the link field in cinfo.
   */
  cinfo.err = jpeg_std_error(&jerr);
  /* Now we can initialize the JPEG compression object. */
  jpeg_create_compress(&cinfo);

  /* Step 2: specify data destination (eg, a file) */
  /* Note: steps 2 and 3 can be done in either order. */

  /* Here we use the library-supplied code to send compressed data to a
   * stdio stream.  You can also write your own code to do something else.
   * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
   * requires it in order to write binary files.
   */
  if ((outfile = fopen(filename, "wb")) == NULL) {
    fprintf(stderr, "can't open %s\n", filename);
    exit(1);
  }
  jpeg_stdio_dest(&cinfo, outfile);

  /* Step 3: set parameters for compression */

  /* First we supply a description of the input image.
   * Four fields of the cinfo struct must be filled in:
   */
  cinfo.image_width = m_sizeDoc.cx; 	/* image width and height, in pixels */
  cinfo.image_height = m_sizeDoc.cy; // image_height;
  cinfo.input_components = 3;		/* # of color components per pixel */
  cinfo.in_color_space = JCS_RGB; 	/* colorspace of input image */
  /* Now use the library's routine to set default compression parameters.
   * (You must set at least cinfo.in_color_space before calling this,
   * since the defaults depend on the source color space.)
   */
  jpeg_set_defaults(&cinfo);
  /* Now you can set any non-default parameters you wish to.
   * Here we just illustrate the use of quality (quantization table) scaling:
   */
  jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);

  /* Step 4: Start compressor */

  /* TRUE ensures that we will write a complete interchange-JPEG file.
   * Pass TRUE unless you are very sure of what you're doing.
   */
  jpeg_start_compress(&cinfo, TRUE);

  /* Step 5: while (scan lines remain to be written) */
  /*           jpeg_write_scanlines(...); */

  /* Here we use the library's state variable cinfo.next_scanline as the
   * loop counter, so that we don't have to keep track ourselves.
   * To keep things simple, we pass one scanline per call; you can pass
   * more if you wish, though.
   */
  row_stride = m_sizeDoc.cx * 3;	/* JSAMPLEs per row in image_buffer */
	unsigned char * InputPtr;
	int lineLength = WidthBytes( cinfo.input_components*8* m_sizeDoc.cx);
	InputPtr = (unsigned char *) ::GlobalLock( m_hBMP);
	InputPtr += sizeof( tagBITMAPINFOHEADER)+lineLength* (m_sizeDoc.cy-1);
//	char str[ 200];
//	sprintf( str, "width %i, height %i, lineLength %i, cinfo.next_scanline %i", cinfo.image_width , cinfo.image_height, lineLength, cinfo.next_scanline);
//	AfxMessageBox( str);


	
  while (cinfo.next_scanline < cinfo.image_height) {
    /* jpeg_write_scanlines expects an array of pointers to scanlines.
     * Here the array is only one element long, but you could pass
     * more than one scanline at a time if that's more convenient.
     */
//	CopyRGBSwitch( cp, InputPtr, m_sizeDoc.cx);
//	(void) jpeg_write_scanlines(&cinfo, &cp, 1);
//	InputPtr -= lineLength;
	RGBSwitch( InputPtr, m_sizeDoc.cx);
	(void) jpeg_write_scanlines(&cinfo, &InputPtr, 1);
	RGBSwitch( InputPtr, m_sizeDoc.cx);
	InputPtr -= lineLength;
  }

  /* Step 6: Finish compression */
	::GlobalUnlock( m_hBMP);
  jpeg_finish_compress(&cinfo);
  /* After finish_compress, we can close the output file. */
  fclose(outfile);

  /* Step 7: release JPEG compression object */

  /* This is an important step since it will release a good deal of memory. */
  jpeg_destroy_compress(&cinfo);

  /* And we're done! */
  return 1;
}


/*
 * SOME FINE POINTS:
 *
 * In the above loop, we ignored the return value of jpeg_write_scanlines,
 * which is the number of scanlines actually written.  We could get away
 * with this because we were only relying on the value of cinfo.next_scanline,
 * which will be incremented correctly.  If you maintain additional loop
 * variables then you should be careful to increment them properly.
 * Actually, for output to a stdio stream you needn't worry, because
 * then jpeg_write_scanlines will write all the lines passed (or else exit
 * with a fatal error).  Partial writes can only occur if you use a data
 * destination module that can demand suspension of the compressor.
 * (If you don't know what that's for, you don't need it.)
 *
 * If the compressor requires full-image buffers (for entropy-coding
 * optimization or a multi-scan JPEG file), it will create temporary
 * files for anything that doesn't fit within the maximum-memory setting.
 * (Note that temp files are NOT needed if you use the default parameters.)
 * On some systems you may need to set up a signal handler to ensure that
 * temporary files are deleted if the program is interrupted.  See libjpeg.txt.
 *
 * Scanlines MUST be supplied in top-to-bottom order if you want your JPEG
 * files to be compatible with everyone else's.  If you cannot readily read
 * your data in that order, you'll need an intermediate array to hold the
 * image.  See rdtarga.c or rdbmp.c for examples of handling bottom-to-top
 * source data using the JPEG code's internal virtual-array mechanisms.
 */


bool CJpegTestDoc::MakeBitmapColors0( int Components, int width, int height)
{
	DWORD dwBitsSize, OldSize;
	if (Components!=3) {
		AfxMessageBox( "MakeBitmapColors0: Components need to be 3");
		return false;
	}
	 //get length of BMP in bytes for use when reading

	dwBitsSize = WidthBytes( Components*8*width) * height+sizeof( tagBITMAPINFOHEADER);
	if (m_hBMP !=NULL) {
		/*pBMP = (LPSTR)*/ ::GlobalLock((HGLOBAL) m_hBMP);
		OldSize = ::GlobalSize( (HGLOBAL) m_hBMP);
		::GlobalUnlock((HGLOBAL) m_hBMP);
		if (OldSize<dwBitsSize) {
			::GlobalFree((HGLOBAL) m_hBMP);
			m_hBMP = NULL;
		} 
	}

	if (m_hBMP==NULL) {
		m_hBMP = (HBITMAP) ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwBitsSize);
		if (m_hBMP == 0) return false;
	}
	
	LPBITMAPINFO lpbmi;      // pointer to BITMAPINFO structure (Win3.0)
	/* get pointer to BITMAPINFO (Win 3.0) */
//	lpbmi = (LPBITMAPINFO) Ptr;
//
	lpbmi = (LPBITMAPINFO) ::GlobalLock((HGLOBAL) m_hBMP);
	lpbmi->bmiHeader.biSize = 40;
	lpbmi->bmiHeader.biWidth = width;
	lpbmi->bmiHeader.biHeight = height;
	lpbmi->bmiHeader.biPlanes = 1;
	lpbmi->bmiHeader.biBitCount = 8*Components;
	lpbmi->bmiHeader.biCompression = 0;
	lpbmi->bmiHeader.biSizeImage = width * height;
	lpbmi->bmiHeader.biXPelsPerMeter = 0;
	lpbmi->bmiHeader.biYPelsPerMeter = 0;
	lpbmi->bmiHeader.biClrUsed = 0;
	lpbmi->bmiHeader.biClrImportant = 0;
	::GlobalUnlock((HGLOBAL) m_hBMP);

	m_sizeDoc = CSize( width, height);
	m_PaletteSize =0;
	m_NrOfColors =0;
	m_Planes = 1;
	m_BitsPixel = 8*Components;

	return true;
}

