Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
D3DXImage.cpp
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: nGenEx.lib
6  FILE: D3DXImage.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  D3DX image file loader
13 */
14 
15 
16 #include "MemDebug.h"
17 #include "D3DXImage.h"
18 #include "VideoDX9.h"
19 
20 // +--------------------------------------------------------------------+
21 
23 : width(0), height(0), format(0), image(0)
24 { }
25 
26 D3DXImage::D3DXImage(WORD w, WORD h, DWORD* img)
27 {
28  ZeroMemory(this, sizeof(D3DXImage));
29 
30  width = w;
31  height = h;
32 
33  int pixels = width * height;
34 
35  image = new(__FILE__,__LINE__) DWORD [pixels];
36 
37  if (image && pixels) {
38  for (int i = 0; i < pixels; i++)
39  image[i] = img[i];
40  }
41 }
42 
44 {
45  delete [] image;
46 }
47 
48 // +--------------------------------------------------------------------+
49 
50 bool D3DXImage::Load(char *filename)
51 {
52  bool success = false;
53  FILE* f;
54 
55  fopen_s(&f, filename,"rb");
56  if (f == NULL)
57  return success;
58 
59  int len = 0;
60  BYTE* buf = 0;
61 
62  fseek(f, 0, SEEK_END);
63  len = ftell(f);
64  fseek(f, 0, SEEK_SET);
65 
66  buf = new(__FILE__,__LINE__) BYTE[len];
67 
68  if (buf) {
69  fread(buf, len, 1, f);
70  fclose(f);
71 
72  success = LoadBuffer(buf, len);
73  }
74 
75  return success;
76 }
77 
78 // +--------------------------------------------------------------------+
79 
80 bool D3DXImage::LoadBuffer(BYTE* buf, int len)
81 {
82  bool success = false;
83  HRESULT hr = E_FAIL;
84  D3DXIMAGE_INFO info;
85 
86  if (buf == NULL)
87  return success;
88 
89  hr = D3DXGetImageInfoFromFileInMemory(buf, len, &info);
90 
91  if (FAILED(hr))
92  return success;
93 
94  width = info.Width;
95  height = info.Height;
96  format = info.Format;
97 
98  if (image) {
99  delete [] image;
100  image = 0;
101  }
102 
103  IDirect3DSurface9* surf = 0;
104  IDirect3DDevice9* dev = VideoDX9::GetD3DDevice9();
105 
106 
107  hr = dev->CreateOffscreenPlainSurface( width,
108  height,
109  D3DFMT_A8R8G8B8,
110  D3DPOOL_SYSTEMMEM,
111  &surf,
112  NULL);
113 
114  if (FAILED(hr))
115  return success;
116 
117  hr = D3DXLoadSurfaceFromFileInMemory( surf, // dest surface
118  NULL, // dest palette (none)
119  NULL, // dest rect (entire image)
120  buf, // memory file
121  len, // size of data
122  NULL, // source rect (entire image)
123  D3DX_DEFAULT, // filter operation
124  0, // color key (none)
125  NULL); // image info
126 
127  if (SUCCEEDED(hr)) {
128  D3DLOCKED_RECT locked_rect;
129  hr = surf->LockRect(&locked_rect, NULL, D3DLOCK_READONLY);
130 
131  if (SUCCEEDED(hr)) {
132  // copy surface into image
133  image = new(__FILE__,__LINE__) DWORD[width*height];
134  if (image) {
135  for (DWORD i = 0; i < height; i++) {
136  BYTE* dst = (BYTE*) (image + i * width);
137  BYTE* src = (BYTE*) locked_rect.pBits + i * locked_rect.Pitch;
138 
139  CopyMemory(dst, src, width * sizeof(DWORD));
140  }
141 
142  success = true;
143  }
144 
145  surf->UnlockRect();
146  }
147  }
148 
149  surf->Release();
150  surf = 0;
151 
152  return success;
153 }
154 
155 // +--------------------------------------------------------------------+
156 
157 bool D3DXImage::Save(char *filename)
158 {
159  bool success = false;
160  HRESULT hr = E_FAIL;
161 
162  if (!image || !width || !height)
163  return success;
164 
165  FILE* f;
166  fopen_s(&f, filename,"wb");
167  if (f == NULL)
168  return success;
169 
170  IDirect3DSurface9* surf = 0;
171  IDirect3DDevice9* dev = VideoDX9::GetD3DDevice9();
172 
173  hr = dev->CreateOffscreenPlainSurface( width,
174  height,
175  D3DFMT_A8R8G8B8,
176  D3DPOOL_SYSTEMMEM,
177  &surf,
178  NULL);
179 
180 
181  if (SUCCEEDED(hr)) {
182  D3DLOCKED_RECT locked_rect;
183  hr = surf->LockRect(&locked_rect, NULL, 0);
184 
185  if (SUCCEEDED(hr)) {
186  // copy image into surface
187  for (DWORD i = 0; i < height; i++) {
188  BYTE* src = (BYTE*) (image + i * width);
189  BYTE* dst = (BYTE*) locked_rect.pBits + i * locked_rect.Pitch;
190 
191  CopyMemory(dst, src, width * sizeof(DWORD));
192  }
193 
194  surf->UnlockRect();
195 
196  ID3DXBuffer* buffer = 0;
197  D3DXIMAGE_FILEFORMAT imgfmt = D3DXIFF_PNG;
198 
199  if (strstr(filename, ".jpg") || strstr(filename, ".JPG"))
200  imgfmt = D3DXIFF_JPG;
201 
202  else if (strstr(filename, ".bmp") || strstr(filename, ".BMP"))
203  imgfmt = D3DXIFF_BMP;
204 
205  hr = D3DXSaveSurfaceToFileInMemory(&buffer, // destination
206  imgfmt, // type of file
207  surf, // image to save
208  NULL, // palette
209  NULL); // source rect (entire image)
210 
211  if (SUCCEEDED(hr)) {
212  fwrite(buffer->GetBufferPointer(), buffer->GetBufferSize(), 1, f);
213  success = true;
214  }
215  }
216  }
217 
218  surf->Release();
219  surf = 0;
220  fclose(f);
221  return success;
222 }
223