| 1 |
/* loupe.cxx |
| 2 |
$Id: loupe.cxx,v 1.7 1998/10/15 07:12:41 elf Exp $ |
| 3 |
|
| 4 |
written by Marc Singer |
| 5 |
30 May 1997 |
| 6 |
|
| 7 |
This file is part of the project XO. See the file README for |
| 8 |
more information. |
| 9 |
|
| 10 |
Copyright (C) 1997 Marc Singer |
| 11 |
|
| 12 |
This program is free software; you can redistribute it and/or |
| 13 |
modify it under the terms of the GNU General Public License as |
| 14 |
published by the Free Software Foundation; either version 2 of the |
| 15 |
License, or (at your option) any later version. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but |
| 18 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 |
General Public License for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License |
| 23 |
in a file called COPYING along with this program; if not, write to |
| 24 |
the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA |
| 25 |
02139, USA. |
| 26 |
|
| 27 |
----------- |
| 28 |
DESCRIPTION |
| 29 |
----------- |
| 30 |
|
| 31 |
*/ |
| 32 |
|
| 33 |
|
| 34 |
#include "standard.h" |
| 35 |
|
| 36 |
#include "ldisplay.h" |
| 37 |
#include "lwindow.h" |
| 38 |
#include "wdialog.h" |
| 39 |
#include "stats.h" |
| 40 |
#include "dither.h" |
| 41 |
#include "lpicture.h" |
| 42 |
|
| 43 |
#include <sys/shm.h> |
| 44 |
#include <sys/ipc.h> |
| 45 |
#include <X11/extensions/XShm.h> |
| 46 |
|
| 47 |
#define ROWWIDTH(width,depth) ((((width)*(depth))/8 + 3)& ~0x3) |
| 48 |
#define SHM(v,s,m) ((s < 8) ? (((v) << (8 - (s)))&(m)) \ |
| 49 |
: (((v) >> ((s) - 8))&(m))) |
| 50 |
|
| 51 |
#define USE_SHM // Enable XShmImage's |
| 52 |
#define USE_DITHER |
| 53 |
|
| 54 |
char** g_argv; |
| 55 |
bool g_fQuit; |
| 56 |
|
| 57 |
void exit_error (const char* sz, ...) |
| 58 |
{ |
| 59 |
fprintf (stderr, "%s: ", g_argv[0]); |
| 60 |
|
| 61 |
va_list ap; |
| 62 |
va_start (ap, sz); |
| 63 |
vfprintf (stderr, sz, ap); |
| 64 |
va_end (ap); |
| 65 |
|
| 66 |
fprintf (stderr, "\n"); |
| 67 |
|
| 68 |
exit (1); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
class WTopLevel : public LWindow { |
| 73 |
protected: |
| 74 |
bool m_fSharedMemory; |
| 75 |
XShmSegmentInfo m_shmInfo; |
| 76 |
|
| 77 |
public: |
| 78 |
XImage* m_pImage; |
| 79 |
|
| 80 |
public: |
| 81 |
WTopLevel (LDisplay* pDisplay) : LWindow (pDisplay) { zero (); } |
| 82 |
WTopLevel (LWindow* pWindow) : LWindow (pWindow) { zero (); } |
| 83 |
void zero (void) { |
| 84 |
memset (((LWindow*) this) + 1, 0, sizeof (*this) - sizeof (LWindow)); } |
| 85 |
void buttondown (XButtonEvent* pEvent); |
| 86 |
void expose (XExposeEvent* pEvent); |
| 87 |
|
| 88 |
void load_image (const char* szFile); |
| 89 |
|
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
EventMap g_rgEMTop[] = { |
| 94 |
{ ButtonPress, (PFNEvent) &WTopLevel::buttondown }, |
| 95 |
{ Expose, (PFNEvent) &WTopLevel::expose }, |
| 96 |
{ 0, NULL }, |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
void WTopLevel::buttondown (XButtonEvent* pEvent) |
| 101 |
{ |
| 102 |
XUnmapWindow (pEvent->display, pEvent->window); |
| 103 |
g_fQuit = true; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
void WTopLevel::expose (XExposeEvent* pEvent) |
| 108 |
{ |
| 109 |
GC gc = display ()->gc (); |
| 110 |
|
| 111 |
LTime time; |
| 112 |
time.reset (); |
| 113 |
long overhead = time.delta (); |
| 114 |
{ |
| 115 |
double r = (overhead*1e-6)/1; |
| 116 |
printf ("(overhead %d.%07d s/fetch) ", int (r), int ((r - int (r))*1e7)); |
| 117 |
} |
| 118 |
time.reset (); |
| 119 |
|
| 120 |
int cBlt = 1; |
| 121 |
|
| 122 |
if (m_fSharedMemory) { |
| 123 |
for (int i = cBlt; i--;) { |
| 124 |
XShmPutImage (pEvent->display, pEvent->window, gc, m_pImage, |
| 125 |
0, 0, 0, 0, m_pImage->width, m_pImage->height, False); |
| 126 |
m_pDisplay->flush (); |
| 127 |
} |
| 128 |
} |
| 129 |
else { |
| 130 |
for (int i = cBlt; i--;) { |
| 131 |
XPutImage (pEvent->display, pEvent->window, gc, m_pImage, |
| 132 |
0, 0, 0, 0, m_pImage->width, m_pImage->height); |
| 133 |
m_pDisplay->flush (); |
| 134 |
} |
| 135 |
} |
| 136 |
{ |
| 137 |
long delta = time.delta (); |
| 138 |
double r = ((delta - overhead)*1e-6)/cBlt; |
| 139 |
printf ("took %d.%07d s/blt\n", int (r), int ((r - int (r))*1e7)); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
void register_base_classes (LDisplay* pDisplay) |
| 145 |
{ |
| 146 |
LWindow* pWindow = new LWindow (pDisplay); |
| 147 |
pWindow->event_map (g_rgEMTop); |
| 148 |
pWindow->select_events (ButtonPressMask | ExposureMask); |
| 149 |
pWindow->set_background_pixel (XBlackPixel (pDisplay->display (), 0)); |
| 150 |
pWindow->set_bit_gravity (NorthWestGravity); |
| 151 |
// pWindow->notify ((PFNEvent) WTopLevel::child); |
| 152 |
if (!pDisplay->hash_template ("top-level", pWindow)) |
| 153 |
return; |
| 154 |
|
| 155 |
// WButton::register_template (pDisplay); |
| 156 |
// WTextEdit::register_template (pDisplay); |
| 157 |
// WDialog::register_template (pDisplay); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
void WTopLevel::load_image (const char* szFile) |
| 163 |
{ |
| 164 |
LPicture picture; |
| 165 |
if (!picture.read (szFile)) |
| 166 |
return; |
| 167 |
|
| 168 |
int width = picture.m_width; |
| 169 |
int height = picture.m_height; |
| 170 |
int cbRowSrc = picture.m_cbStride; |
| 171 |
|
| 172 |
// --- Create the XImage |
| 173 |
Visual* pVisual = m_pDisplay->visual (); |
| 174 |
int shiftRed = m_pDisplay->shift_red (); |
| 175 |
int shiftGreen = m_pDisplay->shift_green (); |
| 176 |
int shiftBlue = m_pDisplay->shift_blue (); |
| 177 |
int depth = m_pDisplay->depth (); |
| 178 |
int cbRowDst = ROWWIDTH (width, depth); |
| 179 |
unsigned8* pbDst; |
| 180 |
|
| 181 |
if ( |
| 182 |
#if !defined (USE_SHM) |
| 183 |
0 && |
| 184 |
#endif |
| 185 |
XShmQueryExtension (m_pDisplay->display ())) { |
| 186 |
|
| 187 |
m_pImage = XShmCreateImage (m_pDisplay->display (), |
| 188 |
pVisual, |
| 189 |
depth, ZPixmap, NULL, &m_shmInfo, |
| 190 |
width, height); |
| 191 |
m_shmInfo.shmid = shmget (IPC_PRIVATE, cbRowDst*height, |
| 192 |
IPC_CREAT | 0777); |
| 193 |
m_shmInfo.shmaddr = m_pImage->data |
| 194 |
= (char*) (pbDst = (unsigned8*) shmat (m_shmInfo.shmid, 0, 0)); |
| 195 |
m_shmInfo.readOnly = false; |
| 196 |
XShmAttach (m_pDisplay->display (), &m_shmInfo); |
| 197 |
m_fSharedMemory = true; |
| 198 |
} |
| 199 |
else { |
| 200 |
pbDst = (unsigned8*) malloc (height*cbRowDst); |
| 201 |
m_pImage = XCreateImage (m_pDisplay->display (), |
| 202 |
pVisual, |
| 203 |
depth, ZPixmap, 0, (char*) pbDst, width, height, |
| 204 |
m_pDisplay->pad (), cbRowDst); |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
if (picture.m_depth == 8) { |
| 209 |
int cbDeltaSrc = width - cbRowSrc; |
| 210 |
int cbDeltaDst = width*(depth/8) - cbRowDst; |
| 211 |
unsigned8* pbS = (unsigned8*) picture.m_pv; |
| 212 |
unsigned8* pbD = pbDst; |
| 213 |
if (picture.m_fNonNormal) { |
| 214 |
pbS += (height - 1)*cbRowSrc; |
| 215 |
cbDeltaSrc -= 2*cbRowSrc; |
| 216 |
} |
| 217 |
for (int row = 0; row < height; |
| 218 |
++row, pbS += cbDeltaSrc, pbD += cbDeltaDst) { |
| 219 |
for (int col = 0; col < width; ++col, ++pbS, pbD += depth/8) { |
| 220 |
RGBQUAD& rgb = picture.m_rgRGB[*pbS]; |
| 221 |
unsigned16 pel |
| 222 |
= SHM (rgb.rgbRed, shiftRed, pVisual->red_mask) |
| 223 |
| SHM (rgb.rgbGreen, shiftGreen, pVisual->green_mask) |
| 224 |
| SHM (rgb.rgbBlue, shiftBlue, pVisual->blue_mask); |
| 225 |
if (depth == 16) |
| 226 |
*(unsigned16*)pbD = pel; |
| 227 |
else |
| 228 |
*pbD = (unsigned8) pel; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
#if defined (USE_DITHER) |
| 233 |
if (picture.m_depth == 24) { |
| 234 |
unsigned8* pbSrc = (unsigned8*) picture.m_pv; |
| 235 |
LDither dither; |
| 236 |
dither.init (); |
| 237 |
dither.dither (width, height, pbSrc, pbDst); |
| 238 |
cbRowSrc = ROWWIDTH (width, 8); |
| 239 |
pbSrc = (unsigned8*) malloc (height*cbRowSrc); |
| 240 |
memcpy (pbSrc, pbDst, height*cbRowSrc); |
| 241 |
RGBQUAD* rgRGB = (RGBQUAD*) dither.palette (); |
| 242 |
|
| 243 |
int cbDeltaSrc = width - cbRowSrc; |
| 244 |
int cbDeltaDst = width*(depth/8) - cbRowDst; |
| 245 |
unsigned8* pbS = pbSrc; |
| 246 |
unsigned8* pbD = pbDst; |
| 247 |
if (picture.m_fNonNormal) { |
| 248 |
pbS += (height - 1)*cbRowSrc; |
| 249 |
cbDeltaSrc -= 2*cbRowSrc; |
| 250 |
} |
| 251 |
for (int row = 0; row < height; |
| 252 |
++row, pbS += cbDeltaSrc, pbD += cbDeltaDst) { |
| 253 |
for (int col = 0; col < width; ++col, ++pbS, pbD += depth/8) { |
| 254 |
RGBQUAD& rgb = rgRGB[*pbS]; |
| 255 |
unsigned16 pel |
| 256 |
= SHM (rgb.rgbRed, shiftRed, pVisual->red_mask) |
| 257 |
| SHM (rgb.rgbGreen, shiftGreen, pVisual->green_mask) |
| 258 |
| SHM (rgb.rgbBlue, shiftBlue, pVisual->blue_mask); |
| 259 |
if (depth == 16) |
| 260 |
*(unsigned16*)pbD = pel; |
| 261 |
else |
| 262 |
*pbD = (unsigned8) pel; |
| 263 |
} |
| 264 |
} |
| 265 |
free (pbSrc); |
| 266 |
} |
| 267 |
|
| 268 |
#else |
| 269 |
if (picture.m_depth == 24) { |
| 270 |
int cbDeltaSrc = width*(24/8) - cbRowSrc; |
| 271 |
int cbDeltaDst = width*(depth/8) - cbRowDst; |
| 272 |
unsigned8* pbS = (unsigned8*) picture.m_pv; |
| 273 |
unsigned8* pbD = pbDst; |
| 274 |
if (picture.m_fNonNormal) { |
| 275 |
pbS += (height - 1)*cbRowSrc; |
| 276 |
cbDeltaSrc -= 2*cbRowSrc; |
| 277 |
} |
| 278 |
for (int row = 0; row < height; |
| 279 |
++row, pbS += cbDeltaSrc, pbD += cbDeltaDst) { |
| 280 |
for (int col = 0; col < width; ++col, pbS += 3, pbD += depth/8) { |
| 281 |
unsigned16 pel |
| 282 |
= SHM (pbS[2], shiftRed, pVisual->red_mask) |
| 283 |
| SHM (pbS[1], shiftGreen, pVisual->green_mask) |
| 284 |
| SHM (pbS[0], shiftBlue, pVisual->blue_mask); |
| 285 |
if (depth == 16) |
| 286 |
*(unsigned16*)pbD = pel; |
| 287 |
else |
| 288 |
*pbD = (unsigned8) pel; |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
#endif |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
int main (int argc, char** argv) |
| 298 |
{ |
| 299 |
if (argc != 2) { |
| 300 |
fprintf (stderr, "usage: %s <bitmapfilename>\n", argv[0]); |
| 301 |
exit (1); |
| 302 |
} |
| 303 |
|
| 304 |
{ |
| 305 |
LDisplay display; |
| 306 |
if (!display.open ()) |
| 307 |
exit_error ("unable to open display"); |
| 308 |
|
| 309 |
register_base_classes (&display); |
| 310 |
|
| 311 |
WTopLevel* pWindow = new WTopLevel (display.find_template ("top-level")); |
| 312 |
pWindow->load_image (argv[1]); |
| 313 |
pWindow->position (100, 100, |
| 314 |
pWindow->m_pImage->width, pWindow->m_pImage->height); |
| 315 |
if (!pWindow->create (0)) |
| 316 |
exit_error ("unable to open window"); |
| 317 |
pWindow->map (); |
| 318 |
|
| 319 |
while (!g_fQuit) { |
| 320 |
display.dispatch_next_event (); |
| 321 |
} |
| 322 |
|
| 323 |
pWindow->unmap (); // Make a clean (looking) exit |
| 324 |
display.flush (); |
| 325 |
|
| 326 |
delete pWindow; |
| 327 |
} |
| 328 |
// dmalloc_exit (); // See what is left allocated |
| 329 |
} |