您现在的位置: 中国IT实验室 >> 游戏开发 >> 初学入门 >> 文章正文
载入JPGE图象文件到DirectDraw的表面中
来源:ChinaITLab 收集整理 作者: 时间:2004-12-26


  译者的话:这是一篇关于使用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:\WINDOWS\SYSTEM folder.
  要载入JPGE图象文件首先要Intel的网站上去下载Intel JPEG Library,这个库包含了开发文档和例程,以及你不感兴趣的东西。我们真正要的是IJL.H,IJL15.LIB,和IJL15.DLL文件。一旦你拥有了这些文件,包含IJL.H头文件到你的代码文件中,添加IJL15.LIB文件到你的工程,并且确定IJL15.DLL文件是在有效的位置,如C:\Windows\Sysstem文件夹,当然,也可以跟我们编译出来的程式执行档放置于同一文件夹。
  
  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 BITMA
收藏本文 责编:admin 


相关文章
游戏开发新手入门之位图化图形
给希望成为游戏美术设计师的朋友
Ogre游戏引擎鼠标选取物体演示  
所有绘画的核心灵魂——素描知识
DirectDraw与DirectInput游戏编程体验
推荐文章


 精彩友情推荐
·神州数码交换机
·神州数码交换机价格
·神州数码网络交换机
·netgear交换机
·网件交换机
·IDC资讯大全
·机房品质万里行
·IDC托管必备知识
·全国IDC报价
·网站推广优化
 基础入门  开发文档
 最新推荐
  多数的Windows程序都需要Windows.h和Windowsx.h这两个头文件,要确保使用它们。当然,你还需要其它......
游戏引擎演化史
在Windows上安装OGRE的方法
关于滤镜遮罩概念,Sobel 遮罩
游戏开发新手入门之Windows编程
游戏开发新手入门之位图化图形
教你实现卡通渲染的另类勾边方法
游戏设计大师谈如何成为一名游戏设
Visual C#编写 3D游戏框架示例
真正的 Java 学习从入门到精通
游戏开发经验——游戏开发的基本常
  针对于移动新出台的政策,需要尽快地把我们公司的游戏对应到不同的手机平台,这是针对市场策略的有利调整............
Quake-III代码里神奇的浮点开方函数
基于Dialogue的MFC程序调用DirectX
关于Kjava手机平台移植可行性报告
网络游戏的数据传输处理和防火墙穿
浅谈网游的数据传输处理和防火墙穿
向量几何在游戏编程中的使用(六)
向量几何在游戏编程中的使用(五)
向量几何在游戏编程中的使用(四)
向量几何在游戏编程中的使用(三)
向量几何在游戏编程中的使用(二)
  培训中心