載入JPGE圖象檔案到DirectDraw的表面中(轉)

post0發表於2007-08-12
載入JPGE圖象檔案到DirectDraw的表面中(轉)[@more@]

  譯者的話:這是一篇關於使用Intel JPGEs Library的文章,在翻譯的同時,譯者根據自己的使用經驗對文章進行適當的添減章節,希望適合各位讀者。

  

  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 at the end of this article provides conversions to 16bit and 32bit surfaces.

  為了保持這篇文章內的排列順序,我先決定安排一些假設。首先,我假設你已經瞭解C/C++與如何對程式碼進行除錯。我還假設你對DirectDraw有些瞭解與你拿到了DirectX 7.0的庫檔案,並且能夠在24bit的情況下工作。注意:本文章附帶的原始碼EXAMPLE.ZIP中提供轉換到16bit和32bit表面的操作。

  

  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:WINDOWSSYSTEM folder.

  要載入JPGE圖象檔案首先要Intel的網站上去下載Intel JPEG Library,這個庫包含了開發文件和例程,以及你不感興趣的東西。我們真正要的是IJL.H,IJL15.LIB,和IJL15.DLL檔案。一旦你擁有了這些檔案,包含IJL.H標頭檔案到你的程式碼檔案中,新增IJL15.LIB檔案到你的工程,並且確定IJL15.DLL檔案是在有效的位置,如C:WindowsSysstem資料夾,當然,也可以跟我們編譯出來的程式執行檔放置於同一資料夾。

  

  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:

  有些東西需要我們在開始之前先準備好,我們需要確定我們擁有可工作的DirectDraw表面:

  

  LPDIRECTDRAWSURFACE7 Surface = NULL;

  

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

  我們還需要設定我們的影片模式,深度為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:

  我們現在準備載入JPEG圖象到我們的表面,既然我們要使用Intel JPEG Library,我們需要建立一個連線物件:

  

  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儲存返回的終止或錯誤屬性資訊。

  JPEG_CORE_PROPERTIES是我們的JPEG物件,一旦我們有這兩個物件,我們準備對其進行初始化:

  

  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.

  ijlInit函式呼叫初始化JPEG_CORE_PROPERTIES物件,我們能檢測這個函式呼叫測試我們的IJLERR物件是否初始化屬性是否為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.

  在這點,我們必須決定我們是從檔案載入我們的JPEG圖象,還是從資料緩衝。因為從檔案載入所需的步驟較少,我們將用這方法。無論如何,在文章的例子EXAMPLE.ZIP中,我會給出兩種可用的的方法。我們從檔案中載入並轉換我們的JPEG物件的JPG檔案成員到一個檔名,我們當呼叫ijlRead函式可以重新獲得檔案引數。

  

  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.

  這初始指定我們的JPGE物件的檔名,我們將根據這個進行載入。我們現在必須尋找一個轉換的方式用於JPGE裝置與bitmap(BID),因此我們能繫結它到我們的DirectDraw表面。

  

  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:

  我們開始建立一個緩衝為儲存我們的點陣圖資料,在這個緩衝建立之後,我們必須調整大小以適合一個24bit的點陣圖:

  

  //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.

  現在我們需要為JPEG物件準備好轉換到BID的部分進行填充。

  

  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 (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:

  讓我們看看這一些結構,DIBBytes成員變數指向一個我們已經建立好的資料緩衝,當我們重新獲得JPEG資料,這些資訊將是我們用於儲存的緩衝;DIBWidth和DIBHeight成員指定DIB的大小;DIBColor成員指定我們要我們的點陣圖資料是倒序的蘭、綠、紅。那是DIBs實際儲存的方式,他們也是顛倒存放,你可以翻轉它:

  

  //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:

  在我們讀資料之前,我們還要檢測其它東西:JPG顏色空間

  

  //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.

  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_OTHER;

  break;

  }

  

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

  我們準備最終獲得JPEG圖象資料,感謝Intel的JPEG庫―這是一個十分簡單的任務:

  

  //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 BITM

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-952207/,如需轉載,請註明出處,否則將追究法律責任。

相關文章