Load JPGE image files into the surface of DirectDraw
Loading Jpegs To DirectDraw Surfaces Loads the JpGE Image File to the Surface of DirectDraw: SEA_BUG Translator: This is an article about using Intel Jpges Library, while translators, the translator according to its own experience Adding the article to the article, I hope to be suitable for readers. In order to keep the size of this article down, I've decided to make a few assumptions. First of all, I assume somewhat familiar that you already know C / C and how to troubleshoot and debug code. I also assume that you are 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 at the end of this article provides conversions to 16bit and 32bit surfaces in order to maintain this. The order in the article, I will decide to arrange some assumptions. First of all, I assume that you have learned that C / C is debugging the code. I also assume that you have some understanding of DirectDraw with the library files you get DirectX 7.0, and work in 24bit. Note: The original code included in this article is provided to the operation of 16bit and 32 bit 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. To load the JPGE image file, you want to download Intel Jpeg Library on the website, this library contains development documents and routines, and what you are not interested. We really want ijl.h, ijl15.lib, and ijl15.dll files. Once you have these files, you contain ijl.h header to your code file, add ijl15.lib files to your project, and determine that ijl15.dll file is in a valid location, such as a C: / Windows / SysStem file. The clip, of course, can also be placed on the same folder with the program that we compile.
There Are A Few More Things We need to do before what we have a direct draw surface to work with: Some things need us ready before starting, we need to determine how we have workable DirectDraw surface : Lpdirectdrawsurface7 surface = null; We need to also be sure to setur display bit depth to 24 bit: We also need to set our video mode, depth is 24bit: 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: We are now ready to load the JPEG image to our surface, since we want to use intel JPEG Library, we need to establish a connection object: IJLERR jerr; JPEG_CORE_PROPERTIES jcprops; 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: IJLERR.. Save the returned termination or error attribute information. JPEG_CORE_PROPERTIES our JPEG object, once we have these two objects, we are ready to initialize: jerr = ijlInit (& jcprops); if (jerr = IJL_OK!) // report initialization errorThe 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. ijlInit function call to initialize JPEG_CORE_PROPERTIES object, we can detect whether the function call our IJLERR object initialization test whether the property is 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 available at the end of 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. at this point, we must decide if we are from The file loads our JPEG image or from the data buffer.
Because the steps need to be loaded from files, we will use this method. In any case, in the example of the article Example.zip, I will give two available methods. We load and convert our JPG file members to a file name from the file, and we call the IJLRead function to re-get the file parameters. jcprops.JPGFile = FileName; jerr = ijlRead (& jcprops, IJL_JFILE_READPARAMS); if (! jerr = IJL_OK) // report read errorThis 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. This initially specifies the file name of our JPGE object, we will load it according to this. We must now look for a conversion method for JPGE devices and Bitmap (BID), so we can bind it to our DirectDraw 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: We start building a buffer to save our bitmap data, after this buffer is established, we must adjust bitmap size to fit in a 24bit: // Prepare a 24Bit buffer to receive image dataBYTE * buffer24; // Determine the required sizelong szbuff24 = (jcprops.JPGWidth * 24 7) / 8 * jcprops.JPGHeight; // Resize the buffer and check for nullbuffer24 = new BYTE [szbuff24]; if (buffer24 == NULL) // Report memory allocation errorNow we need to fill in the DIB portion of the JPEG object to get ready for the conversion from JPEG to DIB us now. It is necessary to prepare the JPEG object to convert to the BID portion for filling.
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
That is the way DIBS actual storage, they are also inverted, you can flip it: // this is what you shouth do if you find you images are coming out Upside down.jcprops.dibHeight = - JCProps.jpGheight; Before We Read in The Image, We Have To Check One More: The JPG Color Space: Before we read the data, we have to detect other things: JPG color space // set the JPG Color Space ... this will always becomewhat of AN / / educated guess at best because JPEG is "color blind" (ie, // nothing in the bit stream tells you what color space the data was // encoded from.switch (jcprops.JPGChannels) {case 1: jcprops.JPGColor = IJL_G ; break; case 3: jcprops.JPGColor = IJL_YCBCR; break; default:. // 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_HER; Break;} we are finally ready. Thanks to Intel's Jpeg Library - this is a trivial task: We are ready to finally get JPEG image data, thank INTEL's JPEG library - this is a very simple Task: // Read in Image from Fil ejerr = ijlRead (& jcprops, IJL_JFILE_READWHOLEIMAGE);. if (! jerr = IJL_OK) // Report read errorThis 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: This function copy bitmap information to our buffer, in this place, if we are inserting a BitmapFileHeader or a BitmapInfoHeader to our buffer front, we can reverse into a binary,
This will create a valid BMP image file on the disk. In any case, we insert into the bitmap to rotate into a DIB and associate it on a surface of a DirectDraw. Therefore, we use the Windows API function createBitmap to build our DIB: hbitmap hbm; // create the bitmap and get a handle to ithbm = createbitmap (jcprops.jpgwidth, jcprops.jpgheight, 1, 24, buffer24); if (HBM == NULL) // Report failure to create bitmapThe CreateBitmap function takes the dimmensions 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. The task of createBitMap is to create a bitmap for us, including the number of channels, the number of pixels, the color bit information. On the basis of success, we get a new bitmap handle.
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: We need to confirm that we have one for us. we DirectDraw surface to copy the bitmap, and the establishment of a surface structure is provided DirectDraw surface: DDSURFACEDESC2 ddsd; ZeroMemory (& ddsd, sizeof (ddsd)); ddsd.dwSize = sizeof (ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; ddsd.dwWidth = jcprops.JPGWidth; ddsd.dwHeight = jcprops.JPGHeight; Result = DDObject-> CreateSurface (& ddsd, & Surface, NULL); if (! Result = DD_OK) // Report surface creation . errorNow, 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:. now, we copy bits Figure is on the surface of our DirectDraw, fortunately, if DirectDraw is just a function, it can be built in 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, let's test our bitmap, let We clean up something, it is we don't need: // We no lonffer NEED OUR image bufferdelete buffer24; // Release The Jpeg Objectijlfree (& jcprops); Finally, The Time Has comage to takeur iMage for a test drive: Eventually, We test our bitmap: Rect Image; // Reset Surface Descriptionzeromemory (& DDSD, SIZEOF (DDSD)); DDSD.DWSIZE = SizeOf (DDSD); // Get The Surface Description So That We can Dynamically // Find THE width and height of our surfaceResult = Surface-> GetSurfaceDesc (& ddsd); if (Result == DD_OK) {// Coordinates of image size Image.left = 0; Image.top = 0; Image.right = ddsd.dwWidth;