summaryrefslogtreecommitdiffhomepage
path: root/MagicEx/MagicLoad.cpp
blob: 2b13a93cf9eb6d52a1ff0619e2e5e775ec0dd5c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo
*/

#include "MagicLoad.h"

#include "Bitmap.h"
#include "Color.h"
#include "D3DXImage.h"
#include "Geometry.h"
#include "Pcx.h"


int LoadBuffer(const char* filename, BYTE*& buf, bool null_terminate)
{
   buf = 0;

   FILE* f = ::fopen(filename, "rb");

   if (f) {
      ::fseek(f, 0, SEEK_END);
      int len = ftell(f);
      ::fseek(f, 0, SEEK_SET);

      if (null_terminate) {
         buf = new BYTE[len+1];
         if (buf)
            buf[len] = 0;
      }

      else {
         buf = new BYTE[len];
      }

      if (buf)
         ::fread(buf, len, 1, f);

      ::fclose(f);

      return len;
   }

   return 0;
}


int LoadTexture(const char* fname, Bitmap*& bitmap, int type)
{
   int   result = 0;

   if (!fname || !*fname)
      return result;

   bitmap = Bitmap::CheckCache(fname);

   if (!bitmap) {
      bool pcx_file = strstr(fname, ".pcx") || strstr(fname, ".PCX");

      // handle PCX formats:
      if (pcx_file) {
         PcxImage pcx;

         if (pcx.Load((char*) fname) == PCX_OK) {
            bitmap = new Bitmap;

            // 32-bit image
            if (pcx.himap) {
               bitmap->CopyHighColorImage(pcx.width, pcx.height, pcx.himap);
            }

            // 8-bit image, check for 32-bit image as well
            else if (pcx.bitmap) {
               bitmap->CopyImage(pcx.width, pcx.height, pcx.bitmap);

               char tmp[256];
               int  len = strlen(fname);
               bool found = false;

               ZeroMemory(tmp, sizeof(tmp));

               for (int i = 0; i < len && !found; i++) {
                  if (strstr(fname + i, ".pcx") == (fname+i)) {
                     found = true;
                  }
                  else {
                     tmp[i] = fname[i];
                  }
               }

               if (found) {
                  strcat_s(tmp, "+.pcx");
                  if (pcx.Load(tmp) == PCX_OK && pcx.himap != 0) {
                     bitmap->CopyHighColorImage(pcx.width, pcx.height, pcx.himap);
                  }
               }
            }
         }
      }

      // for all other formats, use D3DX:
      else {
         D3DXImage d3dx;
         if (d3dx.Load((char*) fname)) {
            bitmap = new Bitmap;
            bitmap->CopyHighColorImage(d3dx.width, d3dx.height, d3dx.image);
         }
      }

      if (bitmap) {
         LoadAlpha(fname, *bitmap, type);

         bitmap->SetFilename(fname);
         bitmap->SetType(type);
         bitmap->MakeTexture();

         Bitmap::AddToCache(bitmap);
      }
   }

   return result;
}

int LoadAlpha(const char* name, Bitmap& bitmap, int type)
{
   PcxImage    pcx;
   D3DXImage   d3dx;
   bool        pcx_file = strstr(name, ".pcx") || strstr(name, ".PCX");
   bool        bmp_file = strstr(name, ".bmp") || strstr(name, ".BMP");
   bool        jpg_file = strstr(name, ".jpg") || strstr(name, ".JPG");
   bool        png_file = strstr(name, ".png") || strstr(name, ".PNG");
   bool        tga_file = strstr(name, ".tga") || strstr(name, ".TGA");

   // check for an associated alpha-only (grayscale) bitmap:
   char filename[256];
   strcpy_s(filename, name);

   char* dot = strrchr(filename, '.');
   if (dot && pcx_file)
      strcpy(dot, "@.pcx");
   else if (dot && bmp_file)
      strcpy(dot, "@.bmp");
   else if (dot && jpg_file)
      strcpy(dot, "@.jpg");
   else if (dot && png_file)
      strcpy(dot, "@.png");
   else if (dot && tga_file)
      strcpy(dot, "@.tga");
   else
      return 0;

   // first try to load from current directory:
   bool loaded = false;

   if (pcx_file)
      loaded = pcx.Load(filename) == PCX_OK;

   else
      loaded = d3dx.Load(filename);

   // now copy the alpha values into the bitmap:
   if (loaded) {
      if (pcx_file && pcx.bitmap) {
         bitmap.CopyAlphaImage(pcx.width, pcx.height, pcx.bitmap);
      }
      else if (pcx_file && pcx.himap) {
         bitmap.CopyAlphaRedChannel(pcx.width, pcx.height, pcx.himap);
      }
      else if (d3dx.image) {
         bitmap.CopyAlphaRedChannel(d3dx.width, d3dx.height, d3dx.image);
      }
   }

   return 0;
}