/[dtapublic]/projs/ets/trunk/src/c_tk_base_7_5_w_mods/tkwinimage.c
ViewVC logotype

Diff of /projs/ets/trunk/src/c_tk_base_7_5_w_mods/tkwinimage.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 70 by dashley, Sat Nov 5 10:54:17 2016 UTC revision 71 by dashley, Sat Nov 5 11:07:06 2016 UTC
# Line 1  Line 1 
1  /* $Header$ */  /* $Header$ */
2    
3  /*  /*
4   * tkWinImage.c --   * tkWinImage.c --
5   *   *
6   *      This file contains routines for manipulation full-color images.   *      This file contains routines for manipulation full-color images.
7   *   *
8   * Copyright (c) 1995 Sun Microsystems, Inc.   * Copyright (c) 1995 Sun Microsystems, Inc.
9   *   *
10   * See the file "license.terms" for information on usage and redistribution   * See the file "license.terms" for information on usage and redistribution
11   * of this file, and for a DISCLAIMER OF ALL WARRANTIES.   * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12   *   *
13   * RCS: @(#) $Id: tkwinimage.c,v 1.1.1.1 2001/06/13 05:13:34 dtashley Exp $   * RCS: @(#) $Id: tkwinimage.c,v 1.1.1.1 2001/06/13 05:13:34 dtashley Exp $
14   */   */
15    
16  #include "tkWinInt.h"  #include "tkWinInt.h"
17    
18  static int              DestroyImage _ANSI_ARGS_((XImage* data));  static int              DestroyImage _ANSI_ARGS_((XImage* data));
19  static unsigned long    ImageGetPixel _ANSI_ARGS_((XImage *image, int x, int y));  static unsigned long    ImageGetPixel _ANSI_ARGS_((XImage *image, int x, int y));
20  static int              PutPixel _ANSI_ARGS_((XImage *image, int x, int y,  static int              PutPixel _ANSI_ARGS_((XImage *image, int x, int y,
21                              unsigned long pixel));                              unsigned long pixel));
22    
23  /*  /*
24   *----------------------------------------------------------------------   *----------------------------------------------------------------------
25   *   *
26   * DestroyImage --   * DestroyImage --
27   *   *
28   *      This is a trivial wrapper around ckfree to make it possible to   *      This is a trivial wrapper around ckfree to make it possible to
29   *      pass ckfree as a pointer.   *      pass ckfree as a pointer.
30   *   *
31   * Results:   * Results:
32   *      None.   *      None.
33   *   *
34   * Side effects:   * Side effects:
35   *      Deallocates the image.   *      Deallocates the image.
36   *   *
37   *----------------------------------------------------------------------   *----------------------------------------------------------------------
38   */   */
39    
40  static int  static int
41  DestroyImage(imagePtr)  DestroyImage(imagePtr)
42       XImage *imagePtr;          /* image to free */       XImage *imagePtr;          /* image to free */
43  {  {
44      if (imagePtr) {      if (imagePtr) {
45          if (imagePtr->data) {          if (imagePtr->data) {
46              ckfree((char*)imagePtr->data);              ckfree((char*)imagePtr->data);
47          }          }
48          ckfree((char*)imagePtr);          ckfree((char*)imagePtr);
49      }      }
50      return 0;      return 0;
51  }  }
52    
53  /*  /*
54   *----------------------------------------------------------------------   *----------------------------------------------------------------------
55   *   *
56   * ImageGetPixel --   * ImageGetPixel --
57   *   *
58   *      Get a single pixel from an image.   *      Get a single pixel from an image.
59   *   *
60   * Results:   * Results:
61   *      Returns the 32 bit pixel value.   *      Returns the 32 bit pixel value.
62   *   *
63   * Side effects:   * Side effects:
64   *      None.   *      None.
65   *   *
66   *----------------------------------------------------------------------   *----------------------------------------------------------------------
67   */   */
68    
69  static unsigned long  static unsigned long
70  ImageGetPixel(image, x, y)  ImageGetPixel(image, x, y)
71      XImage *image;      XImage *image;
72      int x, y;      int x, y;
73  {  {
74      unsigned long pixel = 0;      unsigned long pixel = 0;
75      unsigned char *srcPtr = &(image->data[(y * image->bytes_per_line)      unsigned char *srcPtr = &(image->data[(y * image->bytes_per_line)
76              + ((x * image->bits_per_pixel) / NBBY)]);              + ((x * image->bits_per_pixel) / NBBY)]);
77    
78      switch (image->bits_per_pixel) {      switch (image->bits_per_pixel) {
79          case 32:          case 32:
80          case 24:          case 24:
81              pixel = RGB(srcPtr[2], srcPtr[1], srcPtr[0]);              pixel = RGB(srcPtr[2], srcPtr[1], srcPtr[0]);
82              break;              break;
83          case 16:          case 16:
84              pixel = RGB(((((WORD*)srcPtr)[0]) >> 7) & 0xf8,              pixel = RGB(((((WORD*)srcPtr)[0]) >> 7) & 0xf8,
85                      ((((WORD*)srcPtr)[0]) >> 2) & 0xf8,                      ((((WORD*)srcPtr)[0]) >> 2) & 0xf8,
86                      ((((WORD*)srcPtr)[0]) << 3) & 0xf8);                      ((((WORD*)srcPtr)[0]) << 3) & 0xf8);
87              break;              break;
88          case 8:          case 8:
89              pixel = srcPtr[0];              pixel = srcPtr[0];
90              break;              break;
91          case 4:          case 4:
92              pixel = ((x%2) ? (*srcPtr) : ((*srcPtr) >> 4)) & 0x0f;              pixel = ((x%2) ? (*srcPtr) : ((*srcPtr) >> 4)) & 0x0f;
93              break;              break;
94          case 1:          case 1:
95              pixel = ((*srcPtr) & (0x80 >> (x%8))) ? 1 : 0;              pixel = ((*srcPtr) & (0x80 >> (x%8))) ? 1 : 0;
96              break;              break;
97      }      }
98      return pixel;      return pixel;
99  }  }
100    
101  /*  /*
102   *----------------------------------------------------------------------   *----------------------------------------------------------------------
103   *   *
104   * PutPixel --   * PutPixel --
105   *   *
106   *      Set a single pixel in an image.   *      Set a single pixel in an image.
107   *   *
108   * Results:   * Results:
109   *      None.   *      None.
110   *   *
111   * Side effects:   * Side effects:
112   *      None.   *      None.
113   *   *
114   *----------------------------------------------------------------------   *----------------------------------------------------------------------
115   */   */
116    
117  static int  static int
118  PutPixel(image, x, y, pixel)  PutPixel(image, x, y, pixel)
119      XImage *image;      XImage *image;
120      int x, y;      int x, y;
121      unsigned long pixel;      unsigned long pixel;
122  {  {
123      unsigned char *destPtr = &(image->data[(y * image->bytes_per_line)      unsigned char *destPtr = &(image->data[(y * image->bytes_per_line)
124              + ((x * image->bits_per_pixel) / NBBY)]);              + ((x * image->bits_per_pixel) / NBBY)]);
125    
126      switch  (image->bits_per_pixel) {      switch  (image->bits_per_pixel) {
127          case 32:          case 32:
128              /*              /*
129               * Pixel is DWORD: 0x00BBGGRR               * Pixel is DWORD: 0x00BBGGRR
130               */               */
131    
132              destPtr[3] = 0;              destPtr[3] = 0;
133          case 24:          case 24:
134              /*              /*
135               * Pixel is triplet: 0xBBGGRR.               * Pixel is triplet: 0xBBGGRR.
136               */               */
137    
138              destPtr[0] = (unsigned char) GetBValue(pixel);              destPtr[0] = (unsigned char) GetBValue(pixel);
139              destPtr[1] = (unsigned char) GetGValue(pixel);              destPtr[1] = (unsigned char) GetGValue(pixel);
140              destPtr[2] = (unsigned char) GetRValue(pixel);              destPtr[2] = (unsigned char) GetRValue(pixel);
141              break;              break;
142          case 16:          case 16:
143              /*              /*
144               * Pixel is WORD: 5-5-5 (R-G-B)               * Pixel is WORD: 5-5-5 (R-G-B)
145               */               */
146    
147              (*(WORD*)destPtr) =              (*(WORD*)destPtr) =
148                  ((GetRValue(pixel) & 0xf8) << 7)                  ((GetRValue(pixel) & 0xf8) << 7)
149                  | ((GetGValue(pixel) & 0xf8) <<2)                  | ((GetGValue(pixel) & 0xf8) <<2)
150                  | ((GetBValue(pixel) & 0xf8) >> 3);                  | ((GetBValue(pixel) & 0xf8) >> 3);
151              break;              break;
152          case 8:          case 8:
153              /*              /*
154               * Pixel is 8-bit index into color table.               * Pixel is 8-bit index into color table.
155               */               */
156    
157              (*destPtr) = (unsigned char) pixel;              (*destPtr) = (unsigned char) pixel;
158              break;              break;
159          case 4:          case 4:
160              /*              /*
161               * Pixel is 4-bit index in MSBFirst order.               * Pixel is 4-bit index in MSBFirst order.
162               */               */
163              if (x%2) {              if (x%2) {
164                  (*destPtr) = (unsigned char) (((*destPtr) & 0xf0)                  (*destPtr) = (unsigned char) (((*destPtr) & 0xf0)
165                      | (pixel & 0x0f));                      | (pixel & 0x0f));
166              } else {              } else {
167                  (*destPtr) = (unsigned char) (((*destPtr) & 0x0f)                  (*destPtr) = (unsigned char) (((*destPtr) & 0x0f)
168                      | ((pixel << 4) & 0xf0));                      | ((pixel << 4) & 0xf0));
169              }              }
170              break;              break;
171          case 1: {          case 1: {
172              /*              /*
173               * Pixel is bit in MSBFirst order.               * Pixel is bit in MSBFirst order.
174               */               */
175    
176              int mask = (0x80 >> (x%8));              int mask = (0x80 >> (x%8));
177              if (pixel) {              if (pixel) {
178                  (*destPtr) |= mask;                  (*destPtr) |= mask;
179              } else {              } else {
180                  (*destPtr) &= ~mask;                  (*destPtr) &= ~mask;
181              }              }
182          }          }
183          break;          break;
184      }      }
185      return 0;      return 0;
186  }  }
187    
188  /*  /*
189   *----------------------------------------------------------------------   *----------------------------------------------------------------------
190   *   *
191   * XCreateImage --   * XCreateImage --
192   *   *
193   *      Allocates storage for a new XImage.   *      Allocates storage for a new XImage.
194   *   *
195   * Results:   * Results:
196   *      Returns a newly allocated XImage.   *      Returns a newly allocated XImage.
197   *   *
198   * Side effects:   * Side effects:
199   *      None.   *      None.
200   *   *
201   *----------------------------------------------------------------------   *----------------------------------------------------------------------
202   */   */
203    
204  XImage *  XImage *
205  XCreateImage(display, visual, depth, format, offset, data, width, height,  XCreateImage(display, visual, depth, format, offset, data, width, height,
206          bitmap_pad, bytes_per_line)          bitmap_pad, bytes_per_line)
207      Display* display;      Display* display;
208      Visual* visual;      Visual* visual;
209      unsigned int depth;      unsigned int depth;
210      int format;      int format;
211      int offset;      int offset;
212      char* data;      char* data;
213      unsigned int width;      unsigned int width;
214      unsigned int height;      unsigned int height;
215      int bitmap_pad;      int bitmap_pad;
216      int bytes_per_line;      int bytes_per_line;
217  {  {
218      XImage* imagePtr = (XImage *) ckalloc(sizeof(XImage));      XImage* imagePtr = (XImage *) ckalloc(sizeof(XImage));
219      imagePtr->width = width;      imagePtr->width = width;
220      imagePtr->height = height;      imagePtr->height = height;
221      imagePtr->xoffset = offset;      imagePtr->xoffset = offset;
222      imagePtr->format = format;      imagePtr->format = format;
223      imagePtr->data = data;      imagePtr->data = data;
224      imagePtr->byte_order = LSBFirst;      imagePtr->byte_order = LSBFirst;
225      imagePtr->bitmap_unit = 8;      imagePtr->bitmap_unit = 8;
226      imagePtr->bitmap_bit_order = MSBFirst;      imagePtr->bitmap_bit_order = MSBFirst;
227      imagePtr->bitmap_pad = bitmap_pad;      imagePtr->bitmap_pad = bitmap_pad;
228      imagePtr->bits_per_pixel = depth;      imagePtr->bits_per_pixel = depth;
229      imagePtr->depth = depth;      imagePtr->depth = depth;
230    
231      /*      /*
232       * Under Windows, bitmap_pad must be on an LONG data-type boundary.       * Under Windows, bitmap_pad must be on an LONG data-type boundary.
233       */       */
234    
235  #define LONGBITS    (sizeof(LONG) * 8)  #define LONGBITS    (sizeof(LONG) * 8)
236    
237      bitmap_pad = (bitmap_pad + LONGBITS - 1) / LONGBITS * LONGBITS;      bitmap_pad = (bitmap_pad + LONGBITS - 1) / LONGBITS * LONGBITS;
238    
239      /*      /*
240       * Round to the nearest bitmap_pad boundary.       * Round to the nearest bitmap_pad boundary.
241       */       */
242    
243      if (bytes_per_line) {      if (bytes_per_line) {
244          imagePtr->bytes_per_line = bytes_per_line;          imagePtr->bytes_per_line = bytes_per_line;
245      } else {      } else {
246          imagePtr->bytes_per_line = (((depth * width)          imagePtr->bytes_per_line = (((depth * width)
247                  + (bitmap_pad - 1)) >> 3) & ~((bitmap_pad >> 3) - 1);                  + (bitmap_pad - 1)) >> 3) & ~((bitmap_pad >> 3) - 1);
248      }      }
249    
250      imagePtr->red_mask = 0;      imagePtr->red_mask = 0;
251      imagePtr->green_mask = 0;      imagePtr->green_mask = 0;
252      imagePtr->blue_mask = 0;      imagePtr->blue_mask = 0;
253    
254      imagePtr->f.put_pixel = PutPixel;      imagePtr->f.put_pixel = PutPixel;
255      imagePtr->f.get_pixel = ImageGetPixel;      imagePtr->f.get_pixel = ImageGetPixel;
256      imagePtr->f.destroy_image = DestroyImage;      imagePtr->f.destroy_image = DestroyImage;
257      imagePtr->f.create_image = NULL;      imagePtr->f.create_image = NULL;
258      imagePtr->f.sub_image = NULL;      imagePtr->f.sub_image = NULL;
259      imagePtr->f.add_pixel = NULL;      imagePtr->f.add_pixel = NULL;
260            
261      return imagePtr;      return imagePtr;
262  }  }
263    
264  /*  /*
265   *----------------------------------------------------------------------   *----------------------------------------------------------------------
266   *   *
267   * XGetImage --   * XGetImage --
268   *   *
269   *      This function copies data from a pixmap or window into an   *      This function copies data from a pixmap or window into an
270   *      XImage.   *      XImage.
271   *   *
272   * Results:   * Results:
273   *      Returns a newly allocated image containing the data from the   *      Returns a newly allocated image containing the data from the
274   *      given rectangle of the given drawable.   *      given rectangle of the given drawable.
275   *   *
276   * Side effects:   * Side effects:
277   *      None.   *      None.
278   *   *
279   *----------------------------------------------------------------------   *----------------------------------------------------------------------
280   */   */
281    
282  XImage *  XImage *
283  XGetImage(display, d, x, y, width, height, plane_mask, format)  XGetImage(display, d, x, y, width, height, plane_mask, format)
284      Display* display;      Display* display;
285      Drawable d;      Drawable d;
286      int x;      int x;
287      int y;      int y;
288      unsigned int width;      unsigned int width;
289      unsigned int height;      unsigned int height;
290      unsigned long plane_mask;      unsigned long plane_mask;
291      int format;      int format;
292  {  {
293      TkWinDrawable *twdPtr = (TkWinDrawable *)d;      TkWinDrawable *twdPtr = (TkWinDrawable *)d;
294      XImage *imagePtr;      XImage *imagePtr;
295      HDC dc;      HDC dc;
296      char infoBuf[sizeof(BITMAPINFO) + sizeof(RGBQUAD)];      char infoBuf[sizeof(BITMAPINFO) + sizeof(RGBQUAD)];
297      BITMAPINFO *infoPtr = (BITMAPINFO*)infoBuf;      BITMAPINFO *infoPtr = (BITMAPINFO*)infoBuf;
298    
299      if ((twdPtr->type != TWD_BITMAP) || (twdPtr->bitmap.handle == NULL)      if ((twdPtr->type != TWD_BITMAP) || (twdPtr->bitmap.handle == NULL)
300              || (format != XYPixmap) || (plane_mask != 1)) {              || (format != XYPixmap) || (plane_mask != 1)) {
301          panic("XGetImage: not implemented");          panic("XGetImage: not implemented");
302      }      }
303    
304    
305      imagePtr = XCreateImage(display, NULL, 1, XYBitmap, 0, NULL,      imagePtr = XCreateImage(display, NULL, 1, XYBitmap, 0, NULL,
306              width, height, 32, 0);              width, height, 32, 0);
307      imagePtr->data = ckalloc(imagePtr->bytes_per_line * imagePtr->height);      imagePtr->data = ckalloc(imagePtr->bytes_per_line * imagePtr->height);
308    
309      dc = GetDC(NULL);      dc = GetDC(NULL);
310    
311      GetDIBits(dc, twdPtr->bitmap.handle, 0, height, NULL,      GetDIBits(dc, twdPtr->bitmap.handle, 0, height, NULL,
312              infoPtr, DIB_RGB_COLORS);              infoPtr, DIB_RGB_COLORS);
313    
314      infoPtr->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);      infoPtr->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
315      infoPtr->bmiHeader.biWidth = width;      infoPtr->bmiHeader.biWidth = width;
316      infoPtr->bmiHeader.biHeight = -(LONG)height;      infoPtr->bmiHeader.biHeight = -(LONG)height;
317      infoPtr->bmiHeader.biPlanes = 1;      infoPtr->bmiHeader.biPlanes = 1;
318      infoPtr->bmiHeader.biBitCount = 1;      infoPtr->bmiHeader.biBitCount = 1;
319      infoPtr->bmiHeader.biCompression = BI_RGB;      infoPtr->bmiHeader.biCompression = BI_RGB;
320      infoPtr->bmiHeader.biCompression = 0;      infoPtr->bmiHeader.biCompression = 0;
321      infoPtr->bmiHeader.biXPelsPerMeter = 0;      infoPtr->bmiHeader.biXPelsPerMeter = 0;
322      infoPtr->bmiHeader.biYPelsPerMeter = 0;      infoPtr->bmiHeader.biYPelsPerMeter = 0;
323      infoPtr->bmiHeader.biClrUsed = 0;      infoPtr->bmiHeader.biClrUsed = 0;
324      infoPtr->bmiHeader.biClrImportant = 0;      infoPtr->bmiHeader.biClrImportant = 0;
325    
326      GetDIBits(dc, twdPtr->bitmap.handle, 0, height, imagePtr->data,      GetDIBits(dc, twdPtr->bitmap.handle, 0, height, imagePtr->data,
327              infoPtr, DIB_RGB_COLORS);              infoPtr, DIB_RGB_COLORS);
328      ReleaseDC(NULL, dc);      ReleaseDC(NULL, dc);
329    
330      return imagePtr;      return imagePtr;
331  }  }
332    
333  /* End of tkwinimage.c */  /* End of tkwinimage.c */

Legend:
Removed from v.70  
changed lines
  Added in v.71

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25