unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob e8469b9c946609319aee9aa7d0006dbb762141c4 7682 bytes (raw)
name: src/w32image.c 	 # note: path name is non-authoritative(*)

  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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
 
/* Implementation of GUI terminal on the Microsoft Windows API.

Copyright (C) 1989, 1993-2020 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */

#include <config.h>
#include "lisp.h"
#include "dispextern.h"
#define COBJMACROS
#include <objidl.h>
#include <wtypes.h>
#include <gdiplus.h>
#include <shlwapi.h>
#include "w32term.h"
#include "frame.h"
#include "coding.h"


#if 0
/*
 * The following code is not really needed, but it provides an illustration on
 * how to probe the list of available codecs.
 */

static void
print_wchar(FILE *stream, WCHAR *string)
{
  for ( ; *string; ++string) {
    fputc (*string, stream);
  }
}

static void
w32_list_decoders (void)
{
  UINT ndecoders, size;
  ImageCodecInfo *pCodec;
  int i;

  GdipGetImageDecodersSize(&ndecoders, &size);
  pCodec = (ImageCodecInfo *)malloc(size);

  GdipGetImageDecoders(ndecoders, size, pCodec);
  for (i = 0; i < ndecoders; ++i)
    {
      fprintf(stderr, "\nCodec name: "); print_wchar (stderr, pCodec[i].CodecName);
      fprintf(stderr, "\n    format: "); print_wchar (stderr, pCodec[i].FormatDescription);
      fprintf(stderr, "\n       ext: "); print_wchar (stderr, pCodec[i].FilenameExtension);
      fprintf(stderr, "\n      mime: "); print_wchar (stderr, pCodec[i].MimeType);
    }
  fprintf(stderr, "\n");

  free(pCodec);
}
#endif /* Unused */

static int gdip_initialized = 0;
static ULONG_PTR token;
static GdiplusStartupInput input;
static GdiplusStartupOutput output;

bool
w32_gdiplus_startup (void)
{
  GpStatus status;

  if (gdip_initialized < 0)
      return 0;
  else if (gdip_initialized)
      return 1;
  else
    {
      input.GdiplusVersion = 1;
      input.DebugEventCallback = NULL;
      input.SuppressBackgroundThread = FALSE;
      input.SuppressExternalCodecs = FALSE;

      status = GdiplusStartup(&token, &input, &output);
      if (status == Ok)
        {
          gdip_initialized = 1;
          return 1;
        }
      else
        {
          gdip_initialized = -1;
          return 0;
        }
    }
}

void
w32_gdiplus_shutdown (void)
{
  GdiplusShutdown(token);
}


static double
w32_frame_delay (GpBitmap *pBitmap, int frame)
{
  UINT size;
  PropertyItem *propertyItem;
  double delay = 0.0;

  /* Assume that the image has a property item of type PropertyItemEquipMake.
     Get the size of that property item. */
  GdipGetPropertyItemSize(pBitmap, PropertyTagFrameDelay, &size);

  /* Allocate a buffer to receive the property item.  */
  propertyItem = (PropertyItem*)malloc(size);
  if (propertyItem != NULL)
    {
      /* Get the property item.  */
      GdipGetPropertyItem(pBitmap, PropertyTagFrameDelay, size, propertyItem);
      delay = ((double)propertyItem[frame].length) / 100;
      if (delay == 0)
        {
          /* In GIF files, unfortunately, delay is only specified for the first
             frame.  */
          delay = ((double)propertyItem[0].length) / 100;
        }
      free(propertyItem);
    }
  return delay;
}

static UINT
w32_select_active_frame(GpBitmap *pBitmap, int frame, int *nframes, double *delay)
{
  UINT count, frameCount;
  GUID pDimensionIDs[1];
  GpStatus status = Ok;

  status = GdipImageGetFrameDimensionsCount(pBitmap, &count);
  frameCount = *nframes = 0;
  *delay = 0.0;
  if (count)
    {
      status = GdipImageGetFrameDimensionsList (pBitmap, pDimensionIDs, 1);
      status = GdipImageGetFrameCount (pBitmap, &pDimensionIDs[0], &frameCount);
      if ((status == Ok) && (frameCount > 1))
        {
          if (frame < 0 || frame >= frameCount)
            {
              status = GenericError;
            }
          else
            {
              status = GdipImageSelectActiveFrame (pBitmap, &pDimensionIDs[0], frame);
              *delay = w32_frame_delay (pBitmap, frame);
              *nframes = frameCount;
            }
        }
    }
  return status;
}

static ARGB
w32_image_bg_color(struct frame *f, struct image *img)
{
  /* png_color_16 *image_bg; */
  Lisp_Object specified_bg
    = Fplist_get (XCDR (img->spec), QCbackground);
  Emacs_Color color;

  /* If the user specified a color, try to use it; if not, use the
     current frame background, ignoring any default background
     color set by the image.  */
  if (STRINGP (specified_bg)
      ? FRAME_TERMINAL (f)->defined_color_hook (f,
                                                SSDATA (specified_bg),
                                                &color,
                                                false,
                                                false)
      : (FRAME_TERMINAL (f)->query_frame_background_color (f, &color),
         true))
    /* The user specified `:background', use that.  */
    {
      DWORD red = (((DWORD) color.red) & 0xff00) << 8;
      DWORD green = ((DWORD) color.green) & 0xff00;
      DWORD blue = ((DWORD) color.blue) >> 8;
      return red | green | blue;
    }
  return ((DWORD) 0xff000000);
}

int
w32_load_image (struct frame *f, struct image *img,
                Lisp_Object spec_file, Lisp_Object spec_data)
{
  Emacs_Pixmap pixmap;
  GpStatus status = GenericError;
  GpBitmap *pBitmap;
  wchar_t filename[MAX_PATH];
  ARGB bg_color;
  Lisp_Object lisp_index, metadata;
  unsigned int index, nframes;
  double delay;

  eassert (valid_image_p (img->spec));

  /* This function only gets called if init_w32_gdiplus () was invoked. We have
     a valid token and GDI+ is active.  */
  if (STRINGP (spec_file))
    {
      filename_to_utf16 (SSDATA (spec_file) , filename);
      status = GdipCreateBitmapFromFile (filename, &pBitmap);
    }
  else if (STRINGP (spec_data))
    {
      IStream *pStream = SHCreateMemStream ((BYTE *) SSDATA (spec_data),
                                            SBYTES (spec_data));
      if (pStream != NULL)
        {
          status = GdipCreateBitmapFromStream (pStream, &pBitmap);
          IStream_Release (pStream);
        }
    }

  metadata = Qnil;
  if (status == Ok)
    {
      /* In multiframe pictures, select the first one */
      lisp_index = Fplist_get (XCDR (img->spec), QCindex);
      index = FIXNUMP (lisp_index) ? XFIXNAT (lisp_index) : 0;
      status = w32_select_active_frame (pBitmap, index, &nframes, &delay);
      if ((status == Ok))
        {
          if (nframes > 1)
            metadata = Fcons (Qcount, Fcons (make_fixnum (nframes), metadata));
          if (delay)
            metadata = Fcons (Qdelay, Fcons (make_float (delay), metadata));
        }
    }

  if (status == Ok)
    {
      bg_color = w32_image_bg_color(f, img);
      status = GdipCreateHBITMAPFromBitmap (pBitmap, &pixmap, bg_color);
      if (status == Ok)
        {
          UINT width, height;
          GdipGetImageWidth (pBitmap, &width);
          GdipGetImageHeight (pBitmap, &height);
          img->width = width;
          img->height = height;
          img->pixmap = pixmap;
          img->lisp_data = metadata;
        }

      GdipDisposeImage (pBitmap);
    }

  if (status != Ok)
    {
      add_to_log ("Unable to load image %s", img->spec);
      return 0;
    }
  return 1;
}

debug log:

solving e8469b9c94 ...
found e8469b9c94 in https://yhetil.org/emacs-devel/86lfnh8wzn.fsf@csic.es/

applying [1/1] https://yhetil.org/emacs-devel/86lfnh8wzn.fsf@csic.es/
diff --git a/src/w32image.c b/src/w32image.c
new file mode 100644
index 0000000000..e8469b9c94

Checking patch src/w32image.c...
Applied patch src/w32image.c cleanly.

index at:
100644 e8469b9c946609319aee9aa7d0006dbb762141c4	src/w32image.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).