Jump to content

  • Log In with Google      Sign In   
  • Create Account


Loading JPEGs to DirectDraw Surfaces

By Johnny Wood | Published Feb 13 2001 06:44 AM in DirectX and XNA

image jcprops surface jpeg buffer file ddsd bitmap
If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource

In order to keep the size of this article down, I've decided to make a few assumptions. First of all, I assume that you already know C/C++ and how to troubleshoot and debug code. I also assume that you are somewhat familiar with DirectDraw and that you have as a minimum the DirectX 7.0 libraries and the ability to work in 24 bit. Note: the source code in EXAMPLE.ZIP available with this article provides conversions to 16bit and 32bit surfaces.

The first step to loading JPEGs is to download the Intel JPEG Library from Intel's website. The Library is loaded with documentation and examples, all of which we're really not interested in. What we really want are the IJL.H, IJL15.LIB, and IJL15.DLL files that come with the package. Once you have those files, include the IJL.H header to your source file, add the IJL15.LIB file to your project, and make sure the IJL15.DLL is in a valid location such as the C:\WINDOWS\SYSTEM folder.

There are a few more things we need to do before beginning. We need to make sure that we have a Direct Draw Surface to work with:


We need to also be sure to set our display bit depth to 24 bit:

DDObject->SetDisplayMode(640, 480, 24, 0, 0);

We're now ready to load a JPEG to our surface. Since we're using the Intel JPEG Library, we need to create a couple of objects:

IJLERR jerr;

IJLERR holds return information for determining a pass or fail status. JPEG_CORE_PROPERTIES is our JPEG object. Once we have these two objects, we are ready to initialize them:

jerr = ijlInit(&jcprops);
if (jerr != IJL_OK)
   //report initialization error

The ijlInit function call initializes the JPEG_CORE_PROPERTIES object. We can check the status of this function call by testing whether or not our IJLERR object was initialized with the value IJL_OK.

At this point, we must decide if we are going to load our JPEG image from a file or from a buffer. Because loading from a file takes fewer steps, we will do that here. However, I give an example of loading from both in the EXAMPLE.ZIP file included with this article. We load from a file by changing our JPEG object's JPGFile member to a file name. We then call ijlRead to retrieve the file parameters.

jcprops.JPGFile = FileName;
jerr = ijlRead(&jcprops, IJL_JFILE_READPARAMS);
if (jerr != IJL_OK)
   //report read error

This initial read fills our JPEG object with information about the file we are going to load. What we must now do is find a way of converting the JPEG to a device independent bitmap (DIB) so that we can attach it to our Direct Draw surface.

We start by creating a buffer to hold our image data. After the buffer is created, we must resize it to fit a 24Bit image:

//Prepare a 24Bit buffer to receive image data
BYTE *buffer24;
//Determine the required size
long szbuff24 = (jcprops.JPGWidth * 24 + 7) / 8
    * jcprops.JPGHeight;

//Resize the buffer and check for null
buffer24 = new BYTE [szbuff24];
if (buffer24 == NULL)
    //Report memory allocation error

Now we need to fill in the DIB portion of the JPEG object to get ready for the conversion from JPEG to DIB.

jcprops.DIBWidth	= jcprops.JPGWidth;
jcprops.DIBHeight   = jcprops.JPGHeight; //Implies a bottom-up DIB.
jcprops.DIBChannels = 3;
jcprops.DIBColor	= IJL_BGR;
jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jcprops.JPGWidth, 3);
jcprops.DIBBytes	= reinterpret_cast <BYTE*> (buffer24);

Let's look at some of these a little closer. The DIBBytes member points to the buffer that we created. When we retrieve the JPEG data, the information we get will be stored in this buffer. The DIBWidth and DIBHeight members specify the size of the DIB. The DIBColor member specifies that we want our image data in reverse order Blue Green Red. That's the way that DIBs are actually stored. They are also stored upside down. You can flip the retrieved image by negating the DIBHeight member:

//This is what you should do if you find your images are coming out upside down.
jcprops.DIBHeight = - jcprops.JPGHeight;

Before we read in the image, we have to check one more thing: the JPG color space:

//Set the JPG color space ... this will always be somewhat of an
//educated guess at best because JPEG is "color blind" (i.e.,
//nothing in the bit stream tells you what color space the data was
//encoded from.
  case 1: jcprops.JPGColor = IJL_G;
  case 3: jcprops.JPGColor = IJL_YCBCR;
	//This catches everything else, but no color twist will be
	//performed by the IJL.
	jcprops.DIBColor = (IJL_COLOR)IJL_OTHER;
	jcprops.JPGColor = (IJL_COLOR)IJL_OTHER;

We are finally ready to retrieve the actual JPEG image. Thanks to Intel's JPEG Library - this is a trivial task:

//Read in image from file
jerr = ijlRead(&jcprops, IJL_JFILE_READWHOLEIMAGE);
if (jerr != IJL_OK)
   //Report read error

This function copies the image information into our buffer. At this point, if we were to insert a BITMAPFILEHEADER and a BITMAPINFOHEADER at the front of our buffer, we could dump the buffer to a binary file. This would effectively create a bitmap file saved to disk. However, we instead want to turn our image into a DIB and attach it to a Direct Draw surface. Therefore, we use the Windows API function CreateBitmap to build our DIB:


//Create the bitmap and get a handle to it
hbm = CreateBitmap (jcprops.JPGWidth, jcprops.JPGHeight, 1, 24, buffer24);
if(hbm == NULL)
   //Report failure to create bitmap

The CreateBitmap function takes the dimensions of the image, the number of channels, the number of bits per pixel, and the color bit information from our bitmap buffer and creates a bitmap for us. Upon success, we are given a handle to the newly created bitmap.

Before we go any further, we need to make sure that we have a Direct Draw surface to copy our bitmap to. Set up the Direct Draw surface description and create the surface:


ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize   = sizeof(ddsd);
ddsd.dwWidth  = jcprops.JPGWidth;
ddsd.dwHeight = jcprops.JPGHeight;

Result = DDObject->CreateSurface(&ddsd, &Surface, NULL);
if (Result != DD_OK)
   //Report surface creation error

Now, all that is left is to copy our bitmap over to our Direct Draw surface. Fortunately, there is a function provided by Direct Draw that does just that. It can be found in the DDUTILS.CPP file:

DDCopyBitmap(Surface, hbm, 0, 0, 0, 0);

Before we test our image out, let's clean up some things that we don't need any more:

//We no longer need our image buffer
delete buffer24;

//Release the JPEG object

Finally, the time has come to take our image for a test drive:

RECT Image;

//Reset surface description
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof( ddsd );

//Get the surface description so that we can dynamically
//find the width and height of our surface
Result = Surface->GetSurfaceDesc(&ddsd);
if (Result == DD_OK)
  //Coordinates of image size
  Image.left   = 0;
  Image.top	= 0;
  Image.right  = ddsd.dwWidth;
  Image.bottom = ddsd.dwHeight;
  //Blit image to back buffer
  while (true)
	Result = BackBuffer->BltFast (0, 0, Surface, &Image,
                              	DDBLTFAST_WAIT | DDBLTFAST_NOCOLORKEY);
	if( Result == DD_OK )  break;
	if( Result == DDERR_SURFACELOST )
	{  Result = RestoreAll();
   	if( Result != DD_OK ) break; }
	if( Result != DDERR_WASSTILLDRAWING ) break;

If everything goes smoothly, you should see your image pop up on the screen. Keep in mind that you still have to release your surface when you no longer need it, and that you may have to restore it as a result of ALT+TAB. You can restore the surface by following these exact steps, however, you will not need to create the surface again.

Good luck, and have fun with JPEGs!

Johnny Wood

michael kors handbags jordan retro 11 ugg boots lebron james shoes 2015 michael kors handbags uggs outlet ugg boots for women coach outlet store online fitflop shoes michael kors outlet online louis vuitton outlet fitflop shoes abercrombie and fitch cheap jordans canada goose outlet stores gucci belts canada goose sale coach outlet store burberry scarf canada goose sale ugg slippers chanel outlet soccer jerseys ray ban sunglasses chirstian louboutin shoes kevin durant shoes 2014 burberry scarf burberry outlet michael kors watches christian louboutin shoes replica rolex watches tory burch outlet jordan 3 abercrombie hermes belts christian louboutin shoes burberry scarf adidas wings canada goose jackets uggs on sale abercrombie & fitch burberry scarf abercrombie kids michael kors imitation watches the north face discount rayban sunglasses louis vuitton outlet mont blanc pens louis vuitton outlet michael kors kate spade outlet louis vuitton outlet true religion outlet michael kors outlet online ray ban glasses louis vuitton handbags dior outlet lululemon cheap beats by dre christian louboutin oakley vault coach factory outlet ugg boots jordan 11s tory burch handbags ugg boots outlet burberry outlet cheap toms coach factory outlet online coach outlet michael kors outlet uggs for kids marc jacobs bags polo ralph lauren online michael kors handbags abercrombie michael kors uk bottega veneta outlet online beats headphones coach outlet lululemon headbands michael kors outlet coach factory outlet tory burch outlet online montblanc pens timberland boots for men michael kors outlet sale burberry bags coach outlet uggs on sale moncler jackets tods sale hollister clothing michael kors uk michael kors uk coach handbags louis vuitton handbags timberland canada oakley sunglasses michael kors outlet online canada goose outlet ugg on sale guess outlet timberland canada louis vuitton purses louis vuitton nike nfl jerseys red bottoms michael kors outlet concords 11 hollister kids canada goose abercrombie coach factory christian louboutin outlet ugg boots jordan 3 retro burberry outlet celine bags louis vuitton purses ray ban aviators polo ralph lauren home polo shoes coach outlet store online rayban sunglasses gucci outlet timberland uk tod's shoes chaenl bags true religion outlet oakley sunglasses oakley sunglasses cheap ray ban aviators tory burch sale louis vuitton outlet michael kors outlet online tods outlet running warehouse michael kors michael kors outlet online canada goose sale ugg boots ugg shoes true religion ray ban wayfarer burberry handbags christian louboutin shoes ugg boots outlet hollister chanel handbags fendi handbags polo ralph lauren online louis vuitton handbags concord 11 retro jordans 13 ugg australia coach factory outlet hollister clothing store louis vuitton outlet stores jordan 11 low coach purses canada goose outlet michael kors outlet online sale true religion kids michael kors watches coach factory store hogan outlet abercrombie outlet gucci outlet online polo ralph lauren ugg boots lv bags abercrombie store rayban sunglasses polo ralph lauren cheap oakleys toms promo code oakley sunglasses cheap nfl jerseys toms canada michael kors watches jordan retro 8 michael kors outlet moncler jackets coach outlet store online michael kors outlet online canada goose jackets hollister jeans coach outlet store online hollister clothing store cheap uggs canada goose outlet abercrombie store michael kors handbags kors outlet coach outlet hollister kids abercrombie and fitch replica rolex watches celine outlet online coach outlet store online ray ban eyeglasses retro 11 ray ban eyeglasses ray ban clubmaster jordan concords louis vuitton outlet louis vuitton outlet michael kors uk ugg outlet michael kors outlet michael kors outlet online sale jordan 11 cheap uggs louis vuitton copy watches ugg outlet ray ban glasses michael kors uk sale ralph lauren bedding burberry outlet christian louboutin outlet oakley sunglasses outlet michael kors canada ray ban canada louis vuitton outlet timberland uk mont blanc air jordans coach factory outlet louis vuitton handbags abercrombie & fitch michael kors coach purses oakley sunglasses wholesale fitflop shoes canada goose sale hollister clothing store timberland pro michael kors watches ugg boots for women coach outlet coach factory outlet louis vuitton bags canada goose outlet online michael kors coach factory online wholesale nfl jerseys adidas shoes jeremy scott adidas canada goose louis vuitton outlet insanity workout louis vuitton louis vuitton handbags ralph lauren air jordan 11 canada goose jacket ugg boots michael kors louis vuitton purses michael kors purses coach factory outlet online coach factory online michael kors north face jackets tory burch outlet online chanel online shop michael kors outlet adidas running shoes oakley sunglasses ray ban sunglass abercrombie christian louboutin outlet christian louboutin shoes louis vuitton coach factory outlet timberland shoes michael kors handbags cheap uggs louis vuitton outlet michael kors canada coach outlet cheap toms cheap oakleys gucci handbags ray ban watyfarer p90x workouts coach outlet michael kors handbags hollister clothing michael kors outlet ugg australia jordan 11 michael kors outlet online michael kors handbags gucci handbags nike free run gucci outlet canada goose outlet coach outlet online ugg boots michael kors outlet online sale true religion outlet online tory burch outlet ugg outlet michael kors outlet online michael kors handbags ugg boots uk coach outlet store online toms wedges ugg boots toms wedges jordan shoes uggs outlet tod's outlet gucci factory outlet ralph lauren sale louis vuitton handbags prada handbags micahel kors outlet rayban sunglasses kobe bryant shoes for kids burberry scarf ray ban outlet discount oakley sunglasses christian louboutin michael kors outlet michael kors outlet online ugg outlet online store michael kors handbags jordan retro 11 cheap nfl jerseys chi hair strighteners michael kors coach outlet store online ray ban sunglasses sale fitflop outlet moncler uggs outlet marc jacobs outlet michael kors uk marc jacobs outlet louis vuitton burberry sale mont blanc legend true religion sale michael kors outlet michael kors outlet online coach factory outlet michael kors outlet tory burch handbags hollister clothing store cheap toms ugg australia moncler jacket coach outlet store michael kors watches jordan 4s fitflops outlet ralph lauren home cheap toms uggs for women ugg outlet timberlands abercrombie kids louis vuitton handbags jordan 6 cigar ugg boots abercrombie outlet coach outlet store online adidas shoes true religion outlet coach outlet michael kors outlet burberry scarf ray ban outlet louis vuitton outlet ray ban outlet replica rolex watches ralph lauren outlet coco chanel jeremy scott adidas christian louboutin sale uggs for women michael kors handbags abercrombie & fitch michael kors coach outlet store online retro 11 jordan 6 rings canada goose sale toms shoes nike outlet coach factory outlet louis vuitton belstaff outlet hollister clothing store coach outlet store online discount oakley sunglasses ray ban clubmaster uggs outlet hollister clothing store louis vuitton handbags gucci handbags replica watches christian louboutin shoes michael kors outlet online michael kors uk abercrombie & fitchfitch louis vuitton tods sale www.louisvuitton.com new jordans kate spade gucci outlet online timberland outlet michael kors outlet online ray ban aviator christian louboutin outlet ugg boots outlet christian louboutin outlet canada goose oakley sunglasses louis vuitton handbags jordan 11 concord low ray ban outlet ugg boots michael kors outlet online sale burberry bags chanel bags mont blanc pen jordan retro louis vuitton outlet canada goose jackets ugg outlet tory burch outlet michael kors outlet michael kors outlet online coach outlet online louis vuitton canada goose jackets michael kors outlet online michael kors outlet jerseys louis vuitton louis vuitton handbags ray ban outlet mont blanc mountain hollister jeans toms outlet jordans 11 michael kors canada goose outlet louis vuitton belstaff jackets true religion michael kors outlet michael kors handbags abercrombie & fitchfitch burberry outlet store cheap jerseys wholesale kobe bryant shoes 2015 oakley sunglasses tory burch handbags ghd flat iron oakley vault burberry bags coach outlet ray ban aviators ugg boots red bottom shoes knockoff watches coach factory ugg australia coach factory outlet fitflop footwear michael kors handbags timberland boots mont blanc legend coach outlet coach outlet red shoes oakley sunglasses wholesale louis vuitton louis vuitton outlet michael kors bags abercrombie polo ralph lauren online supra outlet louis vuitton coach factory louis vuitton canada goose outlet online lebron james shoes for sale coach outlet stores online ugg boots sale timberland canada michael kors canada oakley outlet michael kors outlet ray ban eyeglasses hollister louis vuitton handbags coach factory outlet hollister co coach factory red christian louboutin abercrombie and fitch coach outlet online michael kors outlet oakley sunglasses wholesale toms.com tory burch flats christian louboutin outlet

Correction: nike roshe run Seattle north face Minimum nike roshe Wage vans story lululemon outlet online Associated herve leger Press


Seattle wedding dresses begins ray ban sunglasses to nike trainers phase louboutin in hollister $15 montre homme minimum coach factory outlet wage lululemon outlet Associated coach outlet store online Press


What air jordan we can converse learn oakley pas cher from sac hermes the mont blanc CEO rolex watches who babyliss pro took louis vuitton a abercrombie and fitch 93% chi flat iron pay links of london cut nike blazer to reebok outlet give swarovski his soccer shoes team vanessa bruno a valentino shoes raise jordan shoes Vox.com


Seattle's louboutin shoes Wage birkin bag Hike tory burch outlet Reminds michael kors pas cher Us wedding dresses A instyler $9 toms shoes Minimum nike free Wage vans shoes Isn't sac burberry Enough prada handbags Huffington oakley sunglasses Post


US nike shoes CEO louboutin cuts hollister salary oakley sunglasses to converse pas cher boost converse shoes employee prada outlet pay soccer jerseys to true religion outlet $70,000 sac louis vuitton AFP


He's giuseppe zanotti already michael kors gained longchamp new louis vuitton outlet customers, air force too.



"We've pandora charms definitely coach outlet store online gained p90x a new balance shoes handful air max of gucci customers hollister clothing store in beats by dre the louis vuitton last true religion jeans day louis vuitton uk or nfl jerseys two," kate spade handbags said ray ban sunglasses Stefan polo ralph lauren Bennett, nike tn a sac guess customer ray ban relations hollister manager michael kors outlet at hogan outlet Gravity timberland boots Payments, thomas sabo a air max credit north face card pandora charms payment ghd processing air max firm. "We're bottega veneta showing oakley sunglasses cheap people lancel you north face jackets can ray ban sunglasses run louis vuitton a polo ralph lauren outlet good longchamp company, ralph lauren and air max you gucci outlet can michael kors outlet pay longchamp handbags people air max fairly, hollister and nike free pas cher it mac cosmetics can new balance pas cher be vans pas cher profitable."



Dan Price, christian louboutin chief insanity workout executive ralph lauren of michael kors outlet the nike roshe run company, air max pas cher stunned pandora jewelry his michael kors outlet online sale 100-plus kate spade outlet workers michael kors outlet on rolex watches Monday baseball bats when louboutin he north face outlet told hollister them abercrombie and fitch he nike huarache was swarovski crystal cutting longchamp handbags his coach purses roughly supra shoes $1 louis vuitton handbags million tiffany and co salary karen millen to chanel handbags $70,000 timberland and true religion outlet using mcm handbags company air max profits asics running shoes to ray ban pas cher ensure oakley that sac louis vuitton everyone coach outlet there ferragamo shoes would celine handbags earn michael kors outlet at least michael kors that much sac longchamp within longchamp outlet three juicy couture outlet years.



For juicy couture outlet some mulberry workers, tiffany and co the jimmy choo outlet increase iphone 6 cases will louis vuitton outlet stores more polo ralph lauren th

Related burberry outlet Stories



Correction: nike free Seattle louis vuitton outlet Minimum nike air max Wage burberry outlet story polo lacoste Associated louboutin outlet Press


true religion jeans

Note: Please offer only positive, constructive comments - we are looking to promote a positive atmosphere where collaboration is valued above all else.