Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dds_api.cpp « dds « intern « imbuf « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 213e10cf744859a749c578b8b2dc035ded259a43 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup imbdds
 */

#include "BLI_utildefines.h"

#include <DirectDrawSurface.h>
#include <FlipDXT.h>
#include <Stream.h>
#include <cstddef>
#include <cstdio> /* printf */
#include <dds_api.h>
#include <fstream>

#if defined(WIN32)
#  include "utfconv.h"
#endif

#include "IMB_allocimbuf.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
#include "imbuf.h"

#include "IMB_colormanagement.h"
#include "IMB_colormanagement_intern.h"

extern "C" {

bool imb_save_dds(struct ImBuf *ibuf, const char *name, int /*flags*/)
{
  return false; /* TODO: finish this function. */

  /* check image buffer */
  if (ibuf == nullptr) {
    return false;
  }
  if (ibuf->rect == nullptr) {
    return false;
  }

  /* open file for writing */
  std::ofstream fildes;

#if defined(WIN32)
  wchar_t *wname = alloc_utf16_from_8(name, 0);
  fildes.open(wname);
  free(wname);
#else
  fildes.open(name);
#endif

  /* write header */
  fildes << "DDS ";
  fildes.close();

  return true;
}

bool imb_is_a_dds(const uchar *mem, const size_t size)
{
  if (size < 8) {
    return false;
  }
  /* heuristic check to see if mem contains a DDS file */
  /* header.fourcc == FOURCC_DDS */
  if ((mem[0] != 'D') || (mem[1] != 'D') || (mem[2] != 'S') || (mem[3] != ' ')) {
    return false;
  }
  /* header.size == 124 */
  if ((mem[4] != 124) || mem[5] || mem[6] || mem[7]) {
    return false;
  }
  return true;
}

struct ImBuf *imb_load_dds(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
{
  struct ImBuf *ibuf = nullptr;
  DirectDrawSurface dds((uchar *)mem, size); /* reads header */
  uchar bits_per_pixel;
  uint *rect;
  Image img;
  uint numpixels = 0;
  int col;
  uchar *cp = (uchar *)&col;
  Color32 pixel;
  Color32 *pixels = nullptr;

  /* OCIO_TODO: never was able to save DDS, so can't test loading
   *            but profile used to be set to sRGB and can't see rect_float here, so
   *            default byte space should work fine
   */
  colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE);

  if (!imb_is_a_dds(mem, size)) {
    return nullptr;
  }

  /* check if DDS is valid and supported */
  if (!dds.isValid()) {
    /* no need to print error here, just testing if it is a DDS */
    if (flags & IB_test) {
      return nullptr;
    }

    printf("DDS: not valid; header follows\n");
    dds.printInfo();
    return nullptr;
  }
  if (!dds.isSupported()) {
    printf("DDS: format not supported\n");
    return nullptr;
  }
  if ((dds.width() > 65535) || (dds.height() > 65535)) {
    printf("DDS: dimensions too large\n");
    return nullptr;
  }

  /* convert DDS into ImBuf */
  dds.mipmap(&img, 0, 0); /* load first face, first mipmap */
  pixels = img.pixels();
  numpixels = dds.width() * dds.height();
  bits_per_pixel = 24;
  if (img.format() == Image::Format_ARGB) {
    /* check that there is effectively an alpha channel */
    for (uint i = 0; i < numpixels; i++) {
      pixel = pixels[i];
      if (pixel.a != 255) {
        bits_per_pixel = 32;
        break;
      }
    }
  }
  ibuf = IMB_allocImBuf(dds.width(), dds.height(), bits_per_pixel, 0);
  if (ibuf == nullptr) {
    return nullptr; /* memory allocation failed */
  }

  ibuf->ftype = IMB_FTYPE_DDS;
  ibuf->dds_data.fourcc = dds.fourCC();
  ibuf->dds_data.nummipmaps = dds.mipmapCount();

  if ((flags & IB_test) == 0) {
    if (!imb_addrectImBuf(ibuf)) {
      return ibuf;
    }
    if (ibuf->rect == nullptr) {
      return ibuf;
    }

    rect = ibuf->rect;
    cp[3] = 0xff; /* default alpha if alpha channel is not present */

    for (uint i = 0; i < numpixels; i++) {
      pixel = pixels[i];
      cp[0] = pixel.r; /* set R component of col */
      cp[1] = pixel.g; /* set G component of col */
      cp[2] = pixel.b; /* set B component of col */
      if (dds.hasAlpha()) {
        cp[3] = pixel.a; /* set A component of col */
      }
      rect[i] = col;
    }

    if (ibuf->dds_data.fourcc != FOURCC_DDS) {
      ibuf->dds_data.data = (uchar *)dds.readData(ibuf->dds_data.size);

      /* flip compressed texture */
      if (ibuf->dds_data.data) {
        FlipDXTCImage(dds.width(),
                      dds.height(),
                      ibuf->dds_data.nummipmaps,
                      dds.fourCC(),
                      ibuf->dds_data.data,
                      ibuf->dds_data.size,
                      &ibuf->dds_data.nummipmaps);
      }
    }
    else {
      ibuf->dds_data.data = nullptr;
      ibuf->dds_data.size = 0;
    }

    /* flip uncompressed texture */
    IMB_flipy(ibuf);
  }

  return ibuf;
}

} /* extern "C" */