summaryrefslogtreecommitdiff
path: root/src/preview.c
blob: 6fb21c0b352d5d5cc2a1ca09ab10909406ab7427 (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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
/* sane - Scanner Access Now Easy.
   Copyright (C) 1997 David Mosberger-Tang and Tristan Tarrant
   This file is part of the SANE package.

   SANE 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 2 of the License, or (at your
   option) any later version.

   SANE 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 sane; see the file COPYING.  If not, write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
/*
  The preview strategy is as follows:

   1) A preview always acquires an image that covers the entire
      scan surface.  This is necessary so the user can see not
      only what is, but also what isn't selected.

   2) The preview must be zoomable so the user can precisely pick
      the selection area even for small scans on a large scan
      surface.  The user also should have the option of resizing
      the preview window.

   3) We let the user/backend pick whether a preview is in color,
      grayscale, lineart or what not.  The only options that the
      preview may (temporarily) modify are:

	- resolution (set so the preview fills the window)
	- scan area options (top-left corner, bottom-right corner)
	- preview option (to let the backend know we're doing a preview)

   4) The size of the scan surface is determined based on the constraints
      of the four corner coordinates.  Missing constraints are replaced
      by +/-INF as appropriate (-INF form top-left, +INF for bottom-right
      coords).

   5) The size of the preview window is determined based on the scan
      surface size:

          If the surface area is specified in pixels and if that size
	  fits on the screen, we use that size.  In all other cases,
	  we make the window of a size so that neither the width nor
	  the height is bigger than some fraction of the screen-size
	  while preserving the aspect ratio (a surface width or height
	  of INF implies an aspect ratio of 1).

   6) Given the preview window size and the scan surface size, we
      select the resolution so the acquired preview image just fits
      in the preview window.  The resulting resolution may be out
      of range in which case we pick the minum/maximum if there is
      a range or word-list constraint or a default value if there is
      no such constraint.

   7) Once a preview image has been acquired, we know the size of the
      preview image (in pixels).  An initial scale factor is chosen
      so the image fits into the preview window.

      */

#include "../include/sane/config.h"

#include <assert.h>
#include <math.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <sys/param.h>

#include "gtkglue.h"
#include "preview.h"
#include "preferences.h"

#define DBG_fatal 0
#define DBG_error 1
#define DBG_warning 2
#define DBG_info 3
#define DBG_debug 4

#define BACKEND_NAME preview
#include "../include/sane/sanei_debug.h"

#ifndef PATH_MAX
# define PATH_MAX	1024
#endif

/* Anything bigger than 2G will do, since SANE coordinates are 32 bit
   values.  */
#define INF		5.0e9

#define MM_PER_INCH	25.4

/* Cut fp conversion routines some slack: */
#define GROSSLY_DIFFERENT(f1,f2)	(fabs ((f1) - (f2)) > 1e-3)

#ifdef __alpha__
  /* This seems to be necessary for at least some XFree86 3.1.2
     servers.  It's known to be necessary for the XF86_TGA server for
     Linux/Alpha.  Fortunately, it's no great loss so we turn this on
     by default for now.  */
# define XSERVER_WITH_BUGGY_VISUALS
#endif

/* forward declarations */
static void scan_start (Preview * p);
static void scan_done (Preview * p);

static void
draw_rect (GdkWindow * win, GdkGC * gc, int coord[4])
{
  gint x, y, w, h;

  x = coord[0];
  y = coord[1];
  w = coord[2] - x;
  h = coord[3] - y;
  if (w < 0)
    {
      x = coord[2];
      w = -w;
    }
  if (h < 0)
    {
      y = coord[3];
      h = -h;
    }
  gdk_draw_rectangle (win, gc, FALSE, x, y, w + 1, h + 1);
}

static void
draw_selection (Preview * p)
{
  if (!p->gc)
    /* window isn't mapped yet */
    return;

  if (p->previous_selection.active)
    draw_rect (p->window->window, p->gc, p->previous_selection.coord);

  if (p->selection.active)
    draw_rect (p->window->window, p->gc, p->selection.coord);

  p->previous_selection = p->selection;
}

static void
update_selection (Preview * p)
{
  float min, max, normal, dev_selection[4];
  const SANE_Option_Descriptor *opt;
  SANE_Status status;
  SANE_Word val;
  int i, optnum;

  p->previous_selection = p->selection;

  memcpy (dev_selection, p->surface, sizeof (dev_selection));
  for (i = 0; i < 4; ++i)
    {
      optnum = p->dialog->well_known.coord[i];
      if (optnum > 0)
	{
	  opt = sane_get_option_descriptor (p->dialog->dev, optnum);
	  status = sane_control_option (p->dialog->dev, optnum,
					SANE_ACTION_GET_VALUE, &val, 0);
	  if (status != SANE_STATUS_GOOD)
	    continue;
	  if (opt->type == SANE_TYPE_FIXED)
	    dev_selection[i] = SANE_UNFIX (val);
	  else
	    dev_selection[i] = val;
	}
    }
  for (i = 0; i < 2; ++i)
    {
      min = p->surface[i];
      if (min <= -INF)
	min = 0.0;
      max = p->surface[i + 2];
      if (max >= INF)
	max = p->preview_width;

      normal = ((i == 0) ? p->preview_width : p->preview_height) - 1;
      normal /= (max - min);
      p->selection.active = TRUE;
      p->selection.coord[i] = ((dev_selection[i] - min) * normal) + 0.5;
      p->selection.coord[i + 2] =
	((dev_selection[i + 2] - min) * normal) + 0.5;
      if (p->selection.coord[i + 2] < p->selection.coord[i])
	p->selection.coord[i + 2] = p->selection.coord[i];
    }
  draw_selection (p);
}

static void
get_image_scale (Preview * p, float *xscalep, float *yscalep)
{
  float xscale, yscale;

  if (p->image_width == 0)
    xscale = 1.0;
  else
    {
      xscale = p->image_width / (float) p->preview_width;
      if (p->image_height > 0 && p->preview_height * xscale < p->image_height)
	xscale = p->image_height / (float) p->preview_height;
    }
  yscale = xscale;

  if (p->surface_unit == SANE_UNIT_PIXEL
      && p->image_width <= p->preview_width
      && p->image_height <= p->preview_height)
    {
      float swidth, sheight;

      assert (p->surface_type == SANE_TYPE_INT);
      swidth = (p->surface[GSG_BR_X] - p->surface[GSG_TL_X] + 1);
      sheight = (p->surface[GSG_BR_Y] - p->surface[GSG_TL_Y] + 1);
      xscale = 1.0;
      yscale = 1.0;
      if (p->image_width > 0 && swidth < INF)
	xscale = p->image_width / swidth;
      if (p->image_height > 0 && sheight < INF)
	yscale = p->image_height / sheight;
    }
  *xscalep = xscale;
  *yscalep = yscale;
}

static void
paint_image (Preview * p)
{
  float xscale, yscale, src_x, src_y;
  int dst_x, dst_y, height, x, y, src_offset;
  gint gwidth, gheight;

  gwidth = p->preview_width;
  gheight = p->preview_height;

  get_image_scale (p, &xscale, &yscale);

  memset (p->preview_row, 0xff, 3 * gwidth);

  /* don't draw last line unless it's complete: */
  height = p->image_y;
  if (p->image_x == 0 && height < p->image_height)
    ++height;

  /* for now, use simple nearest-neighbor interpolation: */
  src_offset = 0;
  src_x = src_y = 0.0;
  for (dst_y = 0; dst_y < gheight; ++dst_y)
    {
      y = (int) (src_y + 0.5);
      if (y >= height)
	break;
      src_offset = y * 3 * p->image_width;

      if (p->image_data)
	for (dst_x = 0; dst_x < gwidth; ++dst_x)
	  {
	    x = (int) (src_x + 0.5);
	    if (x >= p->image_width)
	      break;

	    p->preview_row[3 * dst_x + 0] =
	      p->image_data[src_offset + 3 * x + 0];
	    p->preview_row[3 * dst_x + 1] =
	      p->image_data[src_offset + 3 * x + 1];
	    p->preview_row[3 * dst_x + 2] =
	      p->image_data[src_offset + 3 * x + 2];
	    src_x += xscale;
	  }
      gtk_preview_draw_row (GTK_PREVIEW (p->window), p->preview_row,
			    0, dst_y, gwidth);
      src_x = 0.0;
      src_y += yscale;
    }
}

static void
display_partial_image (Preview * p)
{
  paint_image (p);

  if (GTK_WIDGET_DRAWABLE (p->window))
    {
      GtkPreview *preview = GTK_PREVIEW (p->window);
      int src_x, src_y;

      src_x = (p->window->allocation.width - preview->buffer_width) / 2;
      src_y = (p->window->allocation.height - preview->buffer_height) / 2;
      gtk_preview_put (preview, p->window->window, p->window->style->black_gc,
		       src_x, src_y,
		       0, 0, p->preview_width, p->preview_height);
    }
}

static void
display_maybe (Preview * p)
{
  time_t now;

  time (&now);
  if (now > p->image_last_time_updated)
    {
      p->image_last_time_updated = now;
      display_partial_image (p);
    }
}

static void
display_image (Preview * p)
{
  if (p->params.lines <= 0 && p->image_y < p->image_height)
    {
      p->image_height = p->image_y;
      p->image_data = realloc (p->image_data,
			       3 * p->image_width * p->image_height);
      assert (p->image_data);
    }
  display_partial_image (p);
  scan_done (p);
}

static void
preview_area_resize (GtkWidget * widget)
{
  float min_x, max_x, min_y, max_y, xscale, yscale, f;
  Preview *p;

  p = gtk_object_get_data (GTK_OBJECT (widget), "PreviewPointer");

  p->preview_width = widget->allocation.width;
  p->preview_height = widget->allocation.height;

  if (p->preview_row)
    p->preview_row = realloc (p->preview_row, 3 * p->preview_width);
  else
    p->preview_row = malloc (3 * p->preview_width);

  /* set the ruler ranges: */

  min_x = p->surface[GSG_TL_X];
  if (min_x <= -INF)
    min_x = 0.0;

  max_x = p->surface[GSG_BR_X];
  if (max_x >= INF)
    max_x = p->preview_width - 1;

  min_y = p->surface[GSG_TL_Y];
  if (min_y <= -INF)
    min_y = 0.0;

  max_y = p->surface[GSG_BR_Y];
  if (max_y >= INF)
    max_y = p->preview_height - 1;

  /* convert mm to inches if that's what the user wants: */

  if (p->surface_unit == SANE_UNIT_MM)
    {
      double factor = 1.0 / preferences.length_unit;
      min_x *= factor;
      max_x *= factor;
      min_y *= factor;
      max_y *= factor;
    }

  get_image_scale (p, &xscale, &yscale);

  if (p->surface_unit == SANE_UNIT_PIXEL)
    f = 1.0 / xscale;
  else if (p->image_width)
    f = xscale * p->preview_width / p->image_width;
  else
    f = 1.0;
  gtk_ruler_set_range (GTK_RULER (p->hruler), f * min_x, f * max_x, f * min_x,
		       /* max_size */ 20);

  if (p->surface_unit == SANE_UNIT_PIXEL)
    f = 1.0 / yscale;
  else if (p->image_height)
    f = yscale * p->preview_height / p->image_height;
  else
    f = 1.0;
  gtk_ruler_set_range (GTK_RULER (p->vruler), f * min_y, f * max_y, f * min_y,
		       /* max_size */ 20);

  paint_image (p);
  update_selection (p);
}

static void
get_bounds (const SANE_Option_Descriptor * opt, float *minp, float *maxp)
{
  float min, max;
  int i;

  min = -INF;
  max = INF;
  switch (opt->constraint_type)
    {
    case SANE_CONSTRAINT_RANGE:
      min = opt->constraint.range->min;
      max = opt->constraint.range->max;
      break;

    case SANE_CONSTRAINT_WORD_LIST:
      min = INF;
      max = -INF;
      for (i = 1; i <= opt->constraint.word_list[0]; ++i)
	{
	  if (opt->constraint.word_list[i] < min)
	    min = opt->constraint.word_list[i];
	  if (opt->constraint.word_list[i] > max)
	    max = opt->constraint.word_list[i];
	}
      break;

    default:
      break;
    }
  if (opt->type == SANE_TYPE_FIXED)
    {
      if (min > -INF && min < INF)
	min = SANE_UNFIX (min);
      if (max > -INF && max < INF)
	max = SANE_UNFIX (max);
    }
  *minp = min;
  *maxp = max;
}

static void
save_option (Preview * p, int option, SANE_Word * save_loc, int *valid)
{
  SANE_Status status;

  if (option <= 0)
    {
      *valid = 0;
      return;
    }
  status = sane_control_option (p->dialog->dev, option, SANE_ACTION_GET_VALUE,
				save_loc, 0);
  *valid = (status == SANE_STATUS_GOOD);
}

static void
restore_option (Preview * p, int option, SANE_Word saved_value, int valid)
{
  const SANE_Option_Descriptor *opt;
  SANE_Status status;
  SANE_Handle dev;

  if (!valid)
    return;

  dev = p->dialog->dev;
  status = sane_control_option (dev, option, SANE_ACTION_SET_VALUE,
				&saved_value, 0);
  if (status != SANE_STATUS_GOOD)
    {
      char buf[256];
      opt = sane_get_option_descriptor (dev, option);
      snprintf (buf, sizeof (buf), "Failed restore value of option %s: %s.",
		opt->name, sane_strstatus (status));
      gsg_error (buf);
    }
}

static SANE_Status
set_option_float (Preview * p, int option, float value)
{
  const SANE_Option_Descriptor *opt;
  SANE_Handle dev;
  SANE_Word word;
  SANE_Status status;

  if (option <= 0 || value <= -INF || value >= INF)
    return SANE_STATUS_INVAL;

  dev = p->dialog->dev;
  opt = sane_get_option_descriptor (dev, option);
  if (opt->type == SANE_TYPE_FIXED)
    word = SANE_FIX (value) + 0.5;
  else
    word = value + 0.5;
  status = sane_control_option (dev, option, SANE_ACTION_SET_VALUE, &word, 0);
  if (status != SANE_STATUS_GOOD)
    {
      char buf[256];
      opt = sane_get_option_descriptor (dev, option);
      snprintf (buf, sizeof (buf), "Failed to set option %s: %s.",
		opt->name, sane_strstatus (status));
      gsg_error (buf);
      return status;
    }
  return SANE_STATUS_GOOD;
}

static void
set_option_bool (Preview * p, int option, SANE_Bool value)
{
  SANE_Handle dev;
  SANE_Status status;

  if (option <= 0)
    return;

  dev = p->dialog->dev;
  status =
    sane_control_option (dev, option, SANE_ACTION_SET_VALUE, &value, 0);
  if (status != SANE_STATUS_GOOD)
    {
      DBG (DBG_fatal, "set_option_bool: sane_control_option failed: %s\n",
	   sane_strstatus (status));
      return;
    }
}

static int
increment_image_y (Preview * p)
{
  size_t extra_size, offset;
  char buf[256];

  p->image_x = 0;
  ++p->image_y;
  if (p->params.lines <= 0 && p->image_y >= p->image_height)
    {
      offset = 3 * p->image_width * p->image_height;
      extra_size = 3 * 32 * p->image_width;
      p->image_height += 32;
      p->image_data = realloc (p->image_data, offset + extra_size);
      if (!p->image_data)
	{
	  snprintf (buf, sizeof (buf),
		    "Failed to reallocate image memory: %s.",
		    strerror (errno));
	  gsg_error (buf);
	  scan_done (p);
	  return -1;
	}
      memset (p->image_data + offset, 0xff, extra_size);
    }
  return 0;
}

static void
input_available (gpointer data, gint source, GdkInputCondition cond)
{
  SANE_Status status;
  Preview *p = data;
  u_char buf[8192];
  SANE_Handle dev;
  SANE_Int len;
  int i, j;

  DBG_INIT ();

  DBG (DBG_debug, "input_available: enter\n");

  dev = p->dialog->dev;
  while (1)
    {
      status = sane_read (dev, buf, sizeof (buf), &len);
      if (status != SANE_STATUS_GOOD)
	{
	  if (status == SANE_STATUS_EOF)
	    {
	      if (p->params.last_frame)
		display_image (p);
	      else
		{
		  if (p->input_tag < 0)
		    {
		      display_maybe (p);
		      while (gtk_events_pending ())
			gtk_main_iteration ();
		    }
		  else
		    {
		      gdk_input_remove (p->input_tag);
		      p->input_tag = -1;
		    }
		  scan_start (p);
		  break;
		}
	    }
	  else
	    {
	      snprintf (buf, sizeof (buf), "Error during read: %s.",
			sane_strstatus (status));
	      gsg_error (buf);
	    }
	  scan_done (p);
	  return;
	}
      if (!len)			/* out of data for now */
	{
	  if (p->input_tag < 0)
	    {
	      display_maybe (p);
	      while (gtk_events_pending ())
		gtk_main_iteration ();
	      continue;
	    }
	  else
	    break;
	}

      switch (p->params.format)
	{
	case SANE_FRAME_RGB:
	  switch (p->params.depth)
	    {
	    case 1:
	      for (i = 0; i < len; ++i)
		{
		  u_char mask = buf[i];

		  for (j = 7; j >= 0; --j)
		    {
		      u_char gl = (mask & (1 << j)) ? 0xff : 0x00;

		      p->image_data[p->image_offset] = gl;
		      if (j != 0)
			p->image_offset += 3;
		      else
			{
			  if ((p->image_offset % 3) != 2)
			    p->image_offset -= 20;
			  else
			    p->image_offset++;
			}
		      if (p->image_offset % 3 == 0)
			{
			  if (++p->image_x >= p->image_width)
			    {
			      if (increment_image_y (p) < 0)
				return;
			    }
			}
		    }
		}
	      break;

	    case 8:
	      for (i = 0; i < len; ++i)
		{
		  p->image_data[p->image_offset++] = buf[i];
		  if (p->image_offset % 3 == 0)
		    {
		      if (++p->image_x >= p->image_width
			  && increment_image_y (p) < 0)
			return;
		    }
		}
	      break;
	    case 16:
	      for (i = 0; i < len; ++i)
		{
		  guint16 value = buf[i];
		  if ((i % 2) == 1)
		    p->image_data[p->image_offset++] = *(guint8 *) (&value);
		  if (p->image_offset % 6 == 0)
		    {
		      if (++p->image_x >= p->image_width
			  && increment_image_y (p) < 0)
			return;
		    }
		}
	      break;
	    default:
	      goto bad_depth;
	    }
	  break;

	case SANE_FRAME_GRAY:
	  switch (p->params.depth)
	    {
	    case 1:
	      for (i = 0; i < len; ++i)
		{
		  u_char mask = buf[i];

		  for (j = 7; j >= 0; --j)
		    {
		      u_char gl = (mask & (1 << j)) ? 0x00 : 0xff;
		      p->image_data[p->image_offset++] = gl;
		      p->image_data[p->image_offset++] = gl;
		      p->image_data[p->image_offset++] = gl;
		      if (++p->image_x >= p->image_width)
			{
			  if (increment_image_y (p) < 0)
			    return;
			  break;	/* skip padding bits */
			}
		    }
		}
	      break;

	    case 8:
	      for (i = 0; i < len; ++i)
		{
		  u_char gl = buf[i];
		  p->image_data[p->image_offset++] = gl;
		  p->image_data[p->image_offset++] = gl;
		  p->image_data[p->image_offset++] = gl;
		  if (++p->image_x >= p->image_width
		      && increment_image_y (p) < 0)
		    return;
		}
	      break;
	    case 16:
	      for (i = 0; i < len; ++i)
		{
		  guint16 value = buf[i];
		  if ((i % 2) == 1)
		    {
		      p->image_data[p->image_offset++] = *(guint8 *) (&value);
		      p->image_data[p->image_offset++] = *(guint8 *) (&value);
		      p->image_data[p->image_offset++] = *(guint8 *) (&value);
		    }
		  if (p->image_offset % 2 == 0)
		    {
		      if (++p->image_x >= p->image_width
			  && increment_image_y (p) < 0)
			return;
		    }
		}
	      break;

	    default:
	      goto bad_depth;
	    }
	  break;

	case SANE_FRAME_RED:
	case SANE_FRAME_GREEN:
	case SANE_FRAME_BLUE:
	  switch (p->params.depth)
	    {
	    case 1:
	      for (i = 0; i < len; ++i)
		{
		  u_char mask = buf[i];

		  for (j = 7; j >= 0; --j)
		    {
		      u_char gl = (mask & (1 << j)) ? 0xff : 0x00;
		      p->image_data[p->image_offset] = gl;
		      p->image_offset += 3;
		      if (++p->image_x >= p->image_width
			  && increment_image_y (p) < 0)
			return;
		    }
		}
	      break;

	    case 8:
	      for (i = 0; i < len; ++i)
		{
		  p->image_data[p->image_offset] = buf[i];
		  p->image_offset += 3;
		  if (++p->image_x >= p->image_width
		      && increment_image_y (p) < 0)
		    return;
		}
	      break;

	    case 16:
	      for (i = 0; i < len; ++i)
		{
		  guint16 value = buf[i];
		  if ((i % 2) == 1)
		    {
		      p->image_data[p->image_offset] = *(guint8 *) (&value);
		      p->image_offset += 3;
		    }
		  if (p->image_offset % 2 == 0)
		    {
		      if (++p->image_x >= p->image_width
			  && increment_image_y (p) < 0)
			return;
		    }
		}
	      break;

	    default:
	      goto bad_depth;
	    }
	  break;

	default:
	  fprintf (stderr, "preview.input_available: bad frame format %d\n",
		   p->params.format);
	  scan_done (p);
	  return;
	}
      if (p->input_tag < 0)
	{
	  display_maybe (p);
	  while (gtk_events_pending ())
	    gtk_main_iteration ();
	}
    }
  display_maybe (p);
  return;

bad_depth:
  snprintf (buf, sizeof (buf), "Preview cannot handle depth %d.",
	    p->params.depth);
  gsg_error (buf);
  scan_done (p);
  return;
}

static void
scan_done (Preview * p)
{
  int i;

  p->scanning = FALSE;
  if (p->input_tag >= 0)
    {
      gdk_input_remove (p->input_tag);
      p->input_tag = -1;
    }
  sane_cancel (p->dialog->dev);

  restore_option (p, p->dialog->well_known.dpi,
		  p->saved_dpi, p->saved_dpi_valid);
  for (i = 0; i < 4; ++i)
    restore_option (p, p->dialog->well_known.coord[i],
		    p->saved_coord[i], p->saved_coord_valid[i]);
  set_option_bool (p, p->dialog->well_known.preview, SANE_FALSE);

  gtk_widget_set_sensitive (p->cancel, FALSE);
  gtk_widget_set_sensitive (p->preview, TRUE);
  gsg_set_sensitivity (p->dialog, TRUE);
  gtk_widget_set_sensitive (p->dialog->window->parent->parent->parent, TRUE);
}

static void
scan_start (Preview * p)
{
  SANE_Handle dev = p->dialog->dev;
  SANE_Status status;
  char buf[256];
  int fd, y;

  gtk_widget_set_sensitive (p->cancel, TRUE);
  gtk_widget_set_sensitive (p->preview, FALSE);
  gsg_set_sensitivity (p->dialog, FALSE);
  gtk_widget_set_sensitive (p->dialog->window->parent->parent->parent, FALSE);

  /* clear old preview: */
  memset (p->preview_row, 0xff, 3 * p->preview_width);
  for (y = 0; y < p->preview_height; ++y)
    gtk_preview_draw_row (GTK_PREVIEW (p->window), p->preview_row,
			  0, y, p->preview_width);

  if (p->input_tag >= 0)
    {
      gdk_input_remove (p->input_tag);
      p->input_tag = -1;
    }

  gsg_sync (p->dialog);

  status = sane_start (dev);
  if (status != SANE_STATUS_GOOD)
    {
      snprintf (buf, sizeof (buf),
		"Failed to start scanner: %s.", sane_strstatus (status));
      gsg_error (buf);
      scan_done (p);
      return;
    }

  status = sane_get_parameters (dev, &p->params);
  if (status != SANE_STATUS_GOOD)
    {
      snprintf (buf, sizeof (buf),
		"Failed to obtain parameters: %s.", sane_strstatus (status));
      gsg_error (buf);
      scan_done (p);
      return;
    }

  if ((p->params.format >= SANE_FRAME_RGB &&
       p->params.format <= SANE_FRAME_BLUE) &&
      p->params.depth == 1 && p->params.pixels_per_line % 8 != 0)
    {
      snprintf (buf, sizeof (buf),
		"Can't handle unaligned 1 bit RGB or three-pass mode.");
      gsg_error (buf);
      scan_done (p);
      return;
    }

  p->image_offset = p->image_x = p->image_y = 0;

  if (p->params.format >= SANE_FRAME_RED
      && p->params.format <= SANE_FRAME_BLUE)
    p->image_offset = p->params.format - SANE_FRAME_RED;

  if (!p->image_data || p->params.pixels_per_line != p->image_width
      || (p->params.lines >= 0 && p->params.lines != p->image_height))
    {
      /* image size changed */
      if (p->image_data)
	free (p->image_data);

      p->image_width = p->params.pixels_per_line;
      p->image_height = p->params.lines;
      if (p->image_height < 0)
	p->image_height = 32;	/* may have to adjust as we go... */

      p->image_data = malloc (p->image_width * p->image_height * 3);
      if (!p->image_data)
	{
	  snprintf (buf, sizeof (buf),
		    "Failed to allocate image memory: %s.", strerror (errno));
	  gsg_error (buf);
	  scan_done (p);
	  return;
	}
      memset (p->image_data, 0xff, 3 * p->image_width * p->image_height);
    }

  if (p->selection.active)
    {
      p->previous_selection = p->selection;
      p->selection.active = FALSE;
      draw_selection (p);
    }
  p->scanning = TRUE;

  if (sane_set_io_mode (dev, SANE_TRUE) == SANE_STATUS_GOOD
      && sane_get_select_fd (dev, &fd) == SANE_STATUS_GOOD)
    p->input_tag =
      gdk_input_add (fd, GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
		     input_available, p);
  else
    input_available (p, -1, GDK_INPUT_READ);
}

static void
establish_selection (Preview * p)
{
  float min, max, normal, dev_selection[4];
  int i;

  memcpy (dev_selection, p->surface, sizeof (dev_selection));
  if (p->selection.active)
    for (i = 0; i < 2; ++i)
      {
	min = p->surface[i];
	if (min <= -INF)
	  min = 0.0;
	max = p->surface[i + 2];
	if (max >= INF)
	  max = p->preview_width;

	normal =
	  1.0 / (((i == 0) ? p->preview_width : p->preview_height) - 1);
	normal *= (max - min);
	dev_selection[i] = p->selection.coord[i] * normal + min;
	dev_selection[i + 2] = p->selection.coord[i + 2] * normal + min;
      }
  for (i = 0; i < 4; ++i)
    set_option_float (p, p->dialog->well_known.coord[i], dev_selection[i]);
  gsg_update_scan_window (p->dialog);
  if (p->dialog->param_change_callback)
    (*p->dialog->param_change_callback) (p->dialog,
					 p->dialog->param_change_arg);
}

static int
make_preview_image_path (Preview * p, size_t filename_size, char *filename)
{
  return gsg_make_path (filename_size, filename, 0, "preview-",
			p->dialog->dev_name, ".ppm");
}

static void
restore_preview_image (Preview * p)
{
  u_int psurface_type, psurface_unit;
  char filename[PATH_MAX];
  int width, height;
  float psurface[4];
  size_t nread;
  FILE *in;

  /* See whether there is a saved preview and load it if present: */

  if (make_preview_image_path (p, sizeof (filename), filename) < 0)
    return;

  in = fopen (filename, "r");
  if (!in)
    return;

  /* Be careful about consuming too many bytes after the final newline
     (e.g., consider an image whose first image byte is 13 (`\r').  */
  if (fscanf (in, "P6\n# surface: %g %g %g %g %u %u\n%d %d\n255%*[\n]",
	      psurface + 0, psurface + 1, psurface + 2, psurface + 3,
	      &psurface_type, &psurface_unit, &width, &height) != 8)
    return;

  if (GROSSLY_DIFFERENT (psurface[0], p->surface[0])
      || GROSSLY_DIFFERENT (psurface[1], p->surface[1])
      || GROSSLY_DIFFERENT (psurface[2], p->surface[2])
      || GROSSLY_DIFFERENT (psurface[3], p->surface[3])
      || psurface_type != p->surface_type || psurface_unit != p->surface_unit)
    /* ignore preview image that was acquired for/with a different surface */
    return;

  p->image_width = width;
  p->image_height = height;
  if ((width == 0) || (height == 0))
    return;
  p->image_data = malloc (3 * width * height);
  if (!p->image_data)
    return;

  nread = fread (p->image_data, 3, width * height, in);

  p->image_y = nread / width;
  p->image_x = nread % width;
}

/* This is executed _after_ the gtkpreview's expose routine.  */
static gint
expose_handler (GtkWidget * window, GdkEvent * event, gpointer data)
{
  Preview *p = data;

  p->selection.active = FALSE;
  update_selection (p);
  return FALSE;
}

static gint
event_handler (GtkWidget * window, GdkEvent * event, gpointer data)
{
  Preview *p = data;
  int i, tmp;

  if (event->type == GDK_EXPOSE)
    {
      if (!p->gc)
	{
	  p->gc = gdk_gc_new (p->window->window);
	  gdk_gc_set_function (p->gc, GDK_INVERT);
	  gdk_gc_set_line_attributes (p->gc, 1, GDK_LINE_ON_OFF_DASH,
				      GDK_CAP_BUTT, GDK_JOIN_MITER);
	  paint_image (p);
	}
      else
	{
	  p->selection.active = FALSE;
	  draw_selection (p);
	}
    }
  else if (!p->scanning)
    switch (event->type)
      {
      case GDK_UNMAP:
      case GDK_MAP:
	break;

      case GDK_BUTTON_PRESS:
	p->selection.coord[0] = event->button.x;
	p->selection.coord[1] = event->button.y;
	p->selection_drag = TRUE;
	break;

      case GDK_BUTTON_RELEASE:
	if (!p->selection_drag)
	  break;
	p->selection_drag = FALSE;

	p->selection.coord[2] = event->button.x;
	p->selection.coord[3] = event->button.y;
	p->selection.active =
	  (p->selection.coord[0] != p->selection.coord[2]
	   || p->selection.coord[1] != p->selection.coord[3]);

	if (p->selection.active)
	  {
	    for (i = 0; i < 2; i += 1)
	      if (p->selection.coord[i] > p->selection.coord[i + 2])
		{
		  tmp = p->selection.coord[i];
		  p->selection.coord[i] = p->selection.coord[i + 2];
		  p->selection.coord[i + 2] = tmp;
		}
	    if (p->selection.coord[0] < 0)
	      p->selection.coord[0] = 0;
	    if (p->selection.coord[1] < 0)
	      p->selection.coord[1] = 0;
	    if (p->selection.coord[2] >= p->preview_width)
	      p->selection.coord[2] = p->preview_width - 1;
	    if (p->selection.coord[3] >= p->preview_height)
	      p->selection.coord[3] = p->preview_height - 1;
	  }
	draw_selection (p);
	establish_selection (p);
	break;

      case GDK_MOTION_NOTIFY:
	if (p->selection_drag)
	  {
	    p->selection.active = TRUE;
	    p->selection.coord[2] = event->motion.x;
	    p->selection.coord[3] = event->motion.y;
	    draw_selection (p);
	  }
	break;

      default:
#if 0
	fprintf (stderr, "event_handler: unhandled event type %d\n",
		 event->type);
#endif
	break;
      }
  return FALSE;
}

static void
start_button_clicked (GtkWidget * widget, gpointer data)
{
  preview_scan (data);
}

static void
cancel_button_clicked (GtkWidget * widget, gpointer data)
{
  scan_done (data);
}

static void
top_destroyed (GtkWidget * widget, gpointer call_data)
{
  Preview *p = call_data;

  p->top = NULL;
}

Preview *
preview_new (GSGDialog * dialog)
{
  static int first_time = 1;
  GtkWidget *table, *frame;
  GtkSignalFunc signal_func;
  GtkWidgetClass *class;
  GtkBox *vbox, *hbox;
  Preview *p;

  p = malloc (sizeof (*p));
  if (!p)
    return 0;
  memset (p, 0, sizeof (*p));

  p->dialog = dialog;
  p->input_tag = -1;

  if (first_time)
    {
      first_time = 0;
      gtk_preview_set_gamma (preferences.preview_gamma);
      gtk_preview_set_install_cmap (preferences.preview_own_cmap);
    }

#ifndef XSERVER_WITH_BUGGY_VISUALS
  gtk_widget_push_visual (gtk_preview_get_visual ());
#endif
  gtk_widget_push_colormap (gtk_preview_get_cmap ());

  p->top = gtk_dialog_new ();
  gtk_signal_connect (GTK_OBJECT (p->top), "destroy",
		      GTK_SIGNAL_FUNC (top_destroyed), p);
  gtk_window_set_title (GTK_WINDOW (p->top), "xscanimage preview");
  vbox = GTK_BOX (GTK_DIALOG (p->top)->vbox);
  hbox = GTK_BOX (GTK_DIALOG (p->top)->action_area);

  /* construct the preview area (table with sliders & preview window) */
  table = gtk_table_new (2, 2, /* homogeneous */ FALSE);
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 1);
  gtk_table_set_row_spacing (GTK_TABLE (table), 0, 1);
  gtk_container_border_width (GTK_CONTAINER (table), 2);
  gtk_box_pack_start (vbox, table, /* expand */ TRUE, /* fill */ TRUE,
		      /* padding */ 0);

  /* the empty box in the top-left corner */
  frame = gtk_frame_new ( /* label */ 0);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
  gtk_table_attach (GTK_TABLE (table), frame, 0, 1, 0, 1,
		    GTK_FILL, GTK_FILL, 0, 0);

  /* the horizontal ruler */
  p->hruler = gtk_hruler_new ();
  gtk_table_attach (GTK_TABLE (table), p->hruler, 1, 2, 0, 1,
		    GTK_FILL, 0, 0, 0);

  /* the vertical ruler */
  p->vruler = gtk_vruler_new ();
  gtk_table_attach (GTK_TABLE (table), p->vruler, 0, 1, 1, 2, 0,
		    GTK_FILL, 0, 0);

  /* the preview area */

  p->window = gtk_preview_new (GTK_PREVIEW_COLOR);
  gtk_preview_set_expand (GTK_PREVIEW (p->window), TRUE);
  gtk_widget_set_events (p->window,
			 GDK_EXPOSURE_MASK |
			 GDK_POINTER_MOTION_MASK |
			 GDK_POINTER_MOTION_HINT_MASK |
			 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
  gtk_signal_connect (GTK_OBJECT (p->window), "event",
		      (GtkSignalFunc) event_handler, p);
  gtk_signal_connect_after (GTK_OBJECT (p->window), "expose_event",
			    (GtkSignalFunc) expose_handler, p);
  gtk_signal_connect_after (GTK_OBJECT (p->window), "size_allocate",
			    (GtkSignalFunc) preview_area_resize, 0);
  gtk_object_set_data (GTK_OBJECT (p->window), "PreviewPointer", p);

  /* Connect the motion-notify events of the preview area with the
     rulers.  Nifty stuff!  */

#if GTK_MAJOR_VERSION == 2
  class = GTK_WIDGET_CLASS (GTK_OBJECT_GET_CLASS (GTK_OBJECT (p->hruler)));
#else
  class = GTK_WIDGET_CLASS (GTK_OBJECT (p->hruler)->klass);
#endif /* GTK_MAJOR_VERSION == 2 */
  signal_func = (GtkSignalFunc) class->motion_notify_event;
  gtk_signal_connect_object (GTK_OBJECT (p->window), "motion_notify_event",
			     signal_func, GTK_OBJECT (p->hruler));

#if GTK_MAJOR_VERSION == 2
  class = GTK_WIDGET_CLASS (GTK_OBJECT_GET_CLASS (GTK_OBJECT (p->vruler)));
#else
  class = GTK_WIDGET_CLASS (GTK_OBJECT (p->vruler)->klass);
#endif /* GTK_MAJOR_VERSION == 2 */
  signal_func = (GtkSignalFunc) class->motion_notify_event;
  gtk_signal_connect_object (GTK_OBJECT (p->window), "motion_notify_event",
			     signal_func, GTK_OBJECT (p->vruler));

  p->viewport = gtk_frame_new ( /* label */ 0);
  gtk_frame_set_shadow_type (GTK_FRAME (p->viewport), GTK_SHADOW_IN);
  gtk_container_add (GTK_CONTAINER (p->viewport), p->window);

  gtk_table_attach (GTK_TABLE (table), p->viewport, 1, 2, 1, 2,
		    GTK_FILL | GTK_EXPAND | GTK_SHRINK,
		    GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 0);

  preview_update (p);

  /* fill in action area: */

  /* Preview button */
  p->preview = gtk_button_new_with_label ("Acquire Preview");
  gtk_signal_connect (GTK_OBJECT (p->preview), "clicked",
		      (GtkSignalFunc) start_button_clicked, p);
  gtk_box_pack_start (GTK_BOX (hbox), p->preview, TRUE, TRUE, 0);
  gtk_widget_show (p->preview);

  /* Cancel button */
  p->cancel = gtk_button_new_with_label ("Cancel Preview");
  gtk_signal_connect (GTK_OBJECT (p->cancel), "clicked",
		      (GtkSignalFunc) cancel_button_clicked, p);
  gtk_box_pack_start (GTK_BOX (hbox), p->cancel, TRUE, TRUE, 0);
  gtk_widget_set_sensitive (p->cancel, FALSE);

  gtk_widget_show (p->cancel);
  gtk_widget_show (p->viewport);
  gtk_widget_show (p->window);
  gtk_widget_show (p->hruler);
  gtk_widget_show (p->vruler);
  gtk_widget_show (frame);
  gtk_widget_show (table);
  gtk_widget_show (p->top);

  gtk_widget_pop_colormap ();
#ifndef XSERVER_WITH_BUGGY_VISUALS
  gtk_widget_pop_visual ();
#endif
  return p;
}

void
preview_update (Preview * p)
{
  float val, width, height, max_width, max_height;
  const SANE_Option_Descriptor *opt;
  int i, surface_changed;
  SANE_Value_Type type;
  SANE_Unit unit;
  float min, max;

  surface_changed = 0;
  unit = SANE_UNIT_PIXEL;
  type = SANE_TYPE_INT;
  for (i = 0; i < 4; ++i)
    {
      val = (i & 2) ? INF : -INF;
      if (p->dialog->well_known.coord[i] > 0)
	{
	  opt = sane_get_option_descriptor (p->dialog->dev,
					    p->dialog->well_known.coord[i]);
	  assert (opt->unit == SANE_UNIT_PIXEL || opt->unit == SANE_UNIT_MM);
	  unit = opt->unit;
	  type = opt->type;

	  get_bounds (opt, &min, &max);
	  if (i & 2)
	    val = max;
	  else
	    val = min;
	}
      if (p->surface[i] != val)
	{
	  surface_changed = 1;
	  p->surface[i] = val;
	}
    }
  if (p->surface_unit != unit)
    {
      surface_changed = 1;
      p->surface_unit = unit;
    }
  if (p->surface_type != type)
    {
      surface_changed = 1;
      p->surface_type = type;
    }
  if (surface_changed && p->image_data)
    {
      free (p->image_data);
      p->image_data = 0;
      p->image_width = 0;
      p->image_height = 0;
    }

  /* guess the initial preview window size: */

  width = p->surface[GSG_BR_X] - p->surface[GSG_TL_X];
  height = p->surface[GSG_BR_Y] - p->surface[GSG_TL_Y];
  if (p->surface_type == SANE_TYPE_INT)
    {
      width += 1.0;
      height += 1.0;
    }
  else
    {
      width += SANE_UNFIX (1.0);
      height += SANE_UNFIX (1.0);
    }

  assert (width > 0.0 && height > 0.0);

  if (width >= INF || height >= INF)
    p->aspect = 1.0;
  else
    p->aspect = width / height;

  if (surface_changed)
    {
      max_width = 0.5 * gdk_screen_width ();
      max_height = 0.5 * gdk_screen_height ();
    }
  else
    {
      max_width = p->window->allocation.width;
      max_height = p->window->allocation.height;
    }

  if (p->surface_unit != SANE_UNIT_PIXEL)
    {
      width = max_width;
      height = width / p->aspect;

      if (height > max_height)
	{
	  height = max_height;
	  width = height * p->aspect;
	}
    }
  else
    {
      if (width > max_width)
	width = max_width;

      if (height > max_height)
	height = max_height;
    }

  /* re-adjust so we maintain aspect without exceeding max size: */
  if (width / height != p->aspect)
    {
      if (p->aspect > 1.0)
	height = width / p->aspect;
      else
	width = height * p->aspect;
    }

  p->preview_width = width + 0.5;
  p->preview_height = height + 0.5;
  if (surface_changed)
    {
      gtk_widget_set_usize (GTK_WIDGET (p->window),
			    p->preview_width, p->preview_height);
      if (GTK_WIDGET_DRAWABLE (p->window))
	preview_area_resize (p->window);

      if (preferences.preserve_preview)
	restore_preview_image (p);
    }
  update_selection (p);
}

void
preview_scan (Preview * p)
{
  float min, max, swidth, sheight, width, height, dpi = 0;
  const SANE_Option_Descriptor *opt;
  gint gwidth, gheight;
  int i;
  SANE_Status status;


  save_option (p, p->dialog->well_known.dpi,
	       &p->saved_dpi, &p->saved_dpi_valid);
  for (i = 0; i < 4; ++i)
    save_option (p, p->dialog->well_known.coord[i],
		 &p->saved_coord[i], p->saved_coord_valid + i);

  /* determine dpi, if necessary: */

  if (p->dialog->well_known.dpi > 0)
    {
      opt = sane_get_option_descriptor (p->dialog->dev,
					p->dialog->well_known.dpi);

      gwidth = p->preview_width;
      gheight = p->preview_height;

      height = gheight;
      width = height * p->aspect;
      if (width > gwidth)
	{
	  width = gwidth;
	  height = width / p->aspect;
	}

      swidth = (p->surface[GSG_BR_X] - p->surface[GSG_TL_X]);
      if (swidth < INF)
	dpi = MM_PER_INCH * width / swidth;
      else
	{
	  sheight = (p->surface[GSG_BR_Y] - p->surface[GSG_TL_Y]);
	  if (sheight < INF)
	    dpi = MM_PER_INCH * height / sheight;
	  else
	    dpi = 18.0;
	}
      get_bounds (opt, &min, &max);
      if (dpi < min)
	dpi = min;
      if (dpi > max)
	dpi = max;
      status = set_option_float (p, p->dialog->well_known.dpi, dpi);
      if (status != SANE_STATUS_GOOD)
	return;
    }

  /* set the scan window (necessary since backends may default to
     non-maximum size):  */
  for (i = 0; i < 4; ++i)
    set_option_float (p, p->dialog->well_known.coord[i], p->surface[i]);
  set_option_bool (p, p->dialog->well_known.preview, SANE_TRUE);

  /* OK, all set to go */
  scan_start (p);
}

void
preview_destroy (Preview * p)
{
  char filename[PATH_MAX];
  FILE *out;

  if (p->scanning)
    scan_done (p);		/* don't save partial window */
  else if (preferences.preserve_preview && p->image_data
	   && make_preview_image_path (p, sizeof (filename), filename) >= 0)
    {
      /* save preview image */
      out = fopen (filename, "w");
      if (out)
	{
	  /* always save it as a PPM image: */
	  fprintf (out, "P6\n# surface: %g %g %g %g %u %u\n%d %d\n255\n",
		   p->surface[0], p->surface[1], p->surface[2], p->surface[3],
		   p->surface_type, p->surface_unit,
		   p->image_width, p->image_height);
	  fwrite (p->image_data, 3, p->image_width * p->image_height, out);
	  fclose (out);
	}
    }
  if (p->image_data)
    free (p->image_data);
  if (p->preview_row)
    free (p->preview_row);
  if (p->gc)
    gdk_gc_destroy (p->gc);
  if (p->top)
    gtk_widget_destroy (p->top);
  free (p);
}