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
|
/*
* layout_imageinfo.cpp - A base class for layout images.
*
* Copyright (c) 2004-2008 by Alastair M. Robinson
* Distributed under the terms of the GNU General Public License -
* see the file named "COPYING" for more details.
*
*/
#include <iostream>
#include <string.h>
#include <gtk/gtk.h>
#include "config.h"
#include "dialogs.h"
#include "miscwidgets/generaldialogs.h"
#include "pixbufthumbnail/egg-pixbuf-thumbnail.h"
#include "imagesource/pixbuf_from_imagesource.h"
#include "imageutils/rotatepixbuf.h"
#include "imageutils/maskpixbuf.h"
#include "support/thread.h"
#include "support/progressthread.h"
#include "imagesource/imagesource.h"
#include "imagesource/imagesource_gdkpixbuf.h"
#include "imagesource/imagesource_cms.h"
#include "imagesource/imagesource_util.h"
#include "imagesource/imagesource_mask.h"
#include "imagesource/imagesource_rotate.h"
#include "imagesource/imagesource_promote.h"
#include "imagesource/imagesource_invert.h"
#include "photoprint_state.h"
#include "support/debug.h"
#include "support/progress.h"
#include "support/util.h"
#include "support/layoutrectangle.h"
#include "layout_imageinfo.h"
using namespace std;
class PPIS_Histogram : public ImageSource
{
public:
PPIS_Histogram(ImageSource *source,PPHistogram &hist) : ImageSource(source), source(source,hist), histogram(hist)
{
Debug[TRACE] << "PPIS_Histogram obtaining Histogram mutex in exclusive mode from " << long(Thread::GetThreadID()) << endl;
histogram.ObtainMutex();
Debug[TRACE] << "Histogram address: " << long(&histogram) << endl;
}
virtual ~PPIS_Histogram()
{
Debug[TRACE] << "PPIS_Histogram triggering complete signal" << endl;
histogram.Trigger();
Debug[TRACE] << "PPIS_Histogram releasing Histogram mutex from " << long(Thread::GetThreadID()) << endl;
histogram.ReleaseMutex();
}
virtual ISDataType *GetRow(int row)
{
return(source.GetRow(row));
}
protected:
ImageSource_Histogram source;
PPHistogram &histogram;
};
Layout_ImageInfo::Layout_ImageInfo(Layout &layout, const char *filename, int page, bool allowcropping, PP_ROTATION rotation)
: PPEffectHeader(), page(page), allowcropping(allowcropping), crop_hpan(CENTRE), crop_vpan(CENTRE),
rotation(rotation), layout(layout), maskfilename(NULL), thumbnail(NULL), mask(NULL), hrpreview(NULL),
selected(false), customprofile(NULL), customintent(LCMSWRAPPER_INTENT_DEFAULT), hrrenderthread(NULL),
threadevents(), histogram(threadevents)
{
bool relative=true;
if(filename[0]=='/' || filename[1]==':')
relative=false;
if(filename[0]=='\\' && filename[1]=='\\')
relative=false;
if(relative)
this->filename=BuildAbsoluteFilename(filename);
else
this->filename=strdup(filename);
ImageSource *is=ISLoadImage(this->filename);
if(!is)
throw "Can't open image!";
width=is->width;
height=is->height;
xres=is->xres;
yres=is->yres;
// FIXME - can we grab the embedded profile's name here (for the ImageInfo widget)?
delete is;
GetThumbnail();
}
Layout_ImageInfo::Layout_ImageInfo(Layout &layout, Layout_ImageInfo *ii, int page)
: PPEffectHeader(*ii), page(page), allowcropping(false), crop_hpan(CENTRE), crop_vpan(CENTRE),
rotation(PP_ROTATION_AUTO), layout(layout), maskfilename(NULL), thumbnail(NULL), mask(NULL), hrpreview(NULL),
selected(false), customprofile(NULL), customintent(LCMSWRAPPER_INTENT_DEFAULT), hrrenderthread(NULL),
threadevents(), histogram(threadevents)
{
// Effects are copied by the "PPEffectHeader(*ii) above
if(ii)
{
// We duplicate and reference the thumbnail and mask from the old image...
thumbnail=ii->thumbnail;
if(thumbnail)
g_object_ref(G_OBJECT(thumbnail));
mask=ii->mask;
if(mask)
g_object_ref(G_OBJECT(mask));
// Duplicate name of manually-applied profile...
if(ii->customprofile)
{
customprofile=strdup(ii->customprofile);
Debug[TRACE] << "Copying profile: " << ii->customprofile << endl;
}
customintent=ii->customintent;
// Copy general data
if(ii->filename)
this->filename=strdup(ii->filename);
if(ii->maskfilename)
this->maskfilename=strdup(ii->maskfilename);
allowcropping=ii->allowcropping;
crop_hpan=ii->crop_hpan;
crop_vpan=ii->crop_vpan;
rotation=ii->rotation;
width=ii->width;
height=ii->height;
xres=ii->xres;
yres=ii->yres;
}
}
Layout_ImageInfo::~Layout_ImageInfo()
{
// Must flush the HRPreview and any rendering thread first - this ensures the thread isn't running any more.
FlushHRPreview();
ObtainMutex();
if(thumbnail)
g_object_unref(thumbnail);
if(mask)
g_object_unref(mask);
layout.imagelist.remove(this);
if(customprofile)
free(customprofile);
free(filename);
Debug[COMMENT] << "Layout_ImageInfo successfully disposed" << endl;
}
// Subthread for rendering high-resolution previews.
// This code uses the subthread to create a GdkPixbuf from
// an image, then defers to the main thread to perform the
// actual rendering.
class hr_payload : public PTMutex, public ThreadFunction
{
public:
hr_payload(ProfileManager *p,CMTransformFactory *f,Layout_ImageInfo *ii,GtkWidget *wid,int x,int y,int w,int h)
: PTMutex(), ThreadFunction(), profman(p), factory(f), ii(ii), widget(wid),
xpos(x), ypos(y), width(w), height(h), transformed(NULL), thread(this)
{
thread.Start();
thread.WaitSync();
}
~hr_payload()
{
}
void Stop()
{
thread.Stop();
}
static bool testbreakfunc(void *ud)
{
Thread *t=(Thread *)ud;
return(t->TestBreak());
}
virtual int Entry(Thread &t)
{
ObtainMutex(); // We use this to avoid race conditions when cleaning up.
// To avoid a deadlock situation if, say, the Apply Profile dialog is open and GTK decides that
// now is a good time to redraw the window, we only attempt the mutex, and bail out if it fails.
int count=10;
while(!ii->AttemptMutexShared())
{
if(count==0)
{
Debug[WARN] << "Giving up attempt on mutex - bailing out" << endl;
// The calling thread is waiting for us to acknowledge startup, so we have to send
// the Sync before bailing out.
t.SendSync();
g_timeout_add(1,hr_payload::CleanupFunc,this);
ReleaseMutex();
return(0);
}
#ifdef WIN32
Sleep(50);
#else
usleep(50000);
#endif
--count;
}
t.SendSync();
// We sleep briefly before doing anything time-intensive - that way we can bail out rapidly
// if the user's doing something heavily interactive, like panning an image, or resizing the window
for(int i=0;i<75;++i)
{
#ifdef WIN32
Sleep(10);
#else
usleep(10000);
#endif
if(t.TestBreak())
{
// Debug[TRACE] << "Got break signal while pausing - Releasing" << endl;
ii->ReleaseMutex();
g_timeout_add(1,hr_payload::CleanupFunc,this);
ReleaseMutex();
return(0);
}
}
if(t.TestBreak())
{
// Debug[TRACE] << "Subthread releasing mutex and cancelling" << endl;
ii->ReleaseMutex();
g_timeout_add(1,hr_payload::CleanupFunc,this);
ReleaseMutex();
return(0);
}
try
{
CMSProfile *targetprof;
CMColourDevice tdev=CM_COLOURDEVICE_NONE;
if((targetprof=profman->GetProfile(CM_COLOURDEVICE_PRINTERPROOF)))
tdev=CM_COLOURDEVICE_PRINTERPROOF;
else if((targetprof=profman->GetProfile(CM_COLOURDEVICE_DISPLAY)))
tdev=CM_COLOURDEVICE_DISPLAY;
else if((targetprof=profman->GetProfile(CM_COLOURDEVICE_DEFAULTRGB)))
tdev=CM_COLOURDEVICE_DEFAULTRGB;
if(targetprof)
delete targetprof;
if(t.TestBreak())
{
// Debug[TRACE] << "Subthread releasing mutex and cancelling" << endl;
ii->ReleaseMutex();
g_timeout_add(1,hr_payload::CleanupFunc,this);
return(0);
}
// Debug[TRACE] << "Generating high-res preview - Using tdev: " << tdev << endl;
ImageSource *is=ii->GetImageSource(tdev,factory);
// Debug[TRACE] << "Got imagesource - fitting and rendering" << endl;
LayoutRectangle r(is->width,is->height);
LayoutRectangle target(xpos,ypos,width,height);
RectFit *fit=r.Fit(target,ii->allowcropping,ii->rotation,ii->crop_hpan,ii->crop_vpan);
if(fit->rotation)
{
ImageSource_Interruptible *ii=new ImageSource_Rotate(is,fit->rotation);
ii->SetTestBreak(testbreakfunc,&t);
is=ii;
}
is=ISScaleImageBySize(is,fit->width,fit->height,IS_SCALING_AUTOMATIC);
delete fit;
// We create new Fit in the idle-function because the hpan/vpan may have changed.
ProgressThread prog(t);
transformed=pixbuf_from_imagesource(is,ii->layout.bgcol.red>>8,ii->layout.bgcol.green>>8,ii->layout.bgcol.blue>>8,&prog);
delete is;
// Debug[TRACE] << "finished - finalising" << endl;
if(transformed)
{
// Now we defer to the main thread...
// We add this as a high-priority event because we want it to be
// run as soon as possible, and within a gtk_main_iteration() loop
// if necessary.
// g_idle_add_full(G_PRIORITY_HIGH,hr_payload::IdleFunc,p,NULL);
g_timeout_add(1,hr_payload::IdleFunc,this);
// And wait for the main thread to have rendered the preview
// Because the rendering will be done via a GTK event callback, there's
// a potential deadlock here if the main app attempts to delete this ImageInfo
// between the main thread having completed and the idle function being
// triggered to draw the thumbnail. For this reason we'll have to use a
// tie-break in the ImageInfo destructor.
// Debug[TRACE] << "Waiting for all-clear from main thread..." << endl;
// t->WaitSync();
}
else
{
// Debug[TRACE] << "Thread cancelled" << endl;
g_timeout_add(1,hr_payload::CleanupFunc,this);
ii->ReleaseMutex();
ReleaseMutex();
return(0);
}
}
catch (const char *err)
{
// Debug[TRACE] << "Subthread caught exception: " << err << endl;
g_timeout_add(1,hr_payload::CleanupFunc,this);
}
Debug[COMMENT] << "Subthread waiting for main thread to finish drawing" << endl;
thread.WaitSync();
Debug[COMMENT] << "Subthread releasing mutex and exiting" << endl;
ii->ReleaseMutex();
ReleaseMutex();
return(0);
}
// CleanupFunc - runs on the main thread's context.
static gboolean CleanupFunc(gpointer ud)
{
hr_payload *p=(hr_payload *)ud;
// Debug[TRACE] << "Main thread sending sync signal" << endl;
p->thread.SendSync();
// There's a race condition here. Once we send this signal the subthread will
// release the mutex - but it's possible this function will have deleted the object first.
// To avoid this, we obtain the mutex here, then release it again.
// (In actual fact this race condition should be avoided by the fact that
// this class's destructor deletes the thread - thus the subthread should be
// guaranteed to have exited before this class is deleted.)
// Debug[TRACE] << "Thread cleanup - race prevention - obtaining mutex from thread " << p->thread.GetThreadID() << endl;
p->ObtainMutex();
// Debug[TRACE] << "Thread cleanup - race prevention - releasing mutex" << endl;
p->ReleaseMutex();
Debug[TRACE] << "Done" << endl;
// We clear the renderthread pointer in the ImageInfo here before deleting it
// to avoid the main thread trying to cancel it after deletion.
// This should be safe since this function runs on the main thread's context.
if(p->ii->hrrenderthread==p)
p->ii->hrrenderthread=NULL;
delete p;
return(FALSE);
}
// IdleFunc - runs on the main thread's context,
// thus, can safely render into the UI.
static gboolean IdleFunc(gpointer ud)
{
// Once control reaches here the subthread should have
// completed. There's a brief window in which the ImageInfo
// could have been deleted by the main thread before this idle-handler
// was launched. To fix this, we hold the mutex in the sub-thread, until
// this function, running in the main context, has finished with the
// ImageInfo.
hr_payload *p=(hr_payload *)ud;
// This function runs in the context of the main thread, so it's safe
// to clear the ImageInfo's RenderThread member, since only the main thread
// creates such.
// This idle-function is responsible for disposing of the payload, which
// will also delete the thread.
if(!p->thread.TestBreak())
{
if(p->ii->hrrenderthread==p)
p->ii->hrrenderthread=NULL;
p->ii->SetHRPreview(p->transformed);
LayoutRectangle r(gdk_pixbuf_get_width(p->transformed),gdk_pixbuf_get_height(p->transformed));
LayoutRectangle target(p->xpos,p->ypos,p->width,p->height);
// Disallow rotation here since the image will be rotated already.
RectFit *fit=r.Fit(target,p->ii->allowcropping,PP_ROTATION_NONE,p->ii->crop_hpan,p->ii->crop_vpan);
int dw=fit->width;
int dh=fit->height;
if(dw > p->width)
dw=p->width;
if(dh > p->height)
dh=p->height;
if(dw>gdk_pixbuf_get_width(p->transformed))
{
dw=gdk_pixbuf_get_width(p->transformed);
}
if(dh>gdk_pixbuf_get_height(p->transformed))
{
dh=gdk_pixbuf_get_height(p->transformed);
}
if(p->ii->mask)
{
p->transformed=gdk_pixbuf_copy(p->transformed);
maskpixbuf(p->transformed,fit->xoffset,fit->yoffset,dw,dh,p->ii->mask,
p->ii->layout.bgcol.red>>8,p->ii->layout.bgcol.green>>8,p->ii->layout.bgcol.blue>>8);
}
gdk_draw_pixbuf(p->widget->window,NULL,p->transformed,
fit->xoffset,fit->yoffset,
fit->xpos,fit->ypos,
dw,dh,
GDK_RGB_DITHER_NONE,0,0);
if(p->ii->mask)
g_object_unref(p->transformed);
delete fit;
}
// Debug[TRACE] << "Preview drawn - sending sync to sub-thread" << endl;
p->thread.SendSync();
// There's a race condition here. Once we send this signal the subthread will
// release the mutex - but it's possible this function will have deleted the object first.
// To avoid this, we obtain the mutex here, then release it again.
// (In actual fact this race condition should be avoided by the fact that
// this class's destructor deletes the thread - thus the subthread should be
// guaranteed to have exited before this class is deleted.)
// Debug[TRACE] << "Thread cleanup - race prevention - obtaining mutex from thread " << p->thread.GetThreadID() << endl;
p->ObtainMutex();
Debug[COMMENT] << "Thread cleanup - race prevention - releasing mutex" << endl;
p->ReleaseMutex();
// Debug[TRACE] << "Done" << endl;
// We clear the renderthread pointer in the ImageInfo here before deleting it
// to avoid the main thread trying to cancel it after deletion.
// This should be safe since this function runs on the main thread's context.
if(p->ii->hrrenderthread==p)
p->ii->hrrenderthread=NULL;
delete p;
return(FALSE);
}
protected:
ProfileManager *profman;
CMTransformFactory *factory;
Layout_ImageInfo *ii;
GtkWidget *widget;
int xpos,ypos;
int width,height;
GdkPixbuf *transformed;
Thread thread;
};
void Layout_ImageInfo::DrawThumbnail(GtkWidget *widget,int xpos,int ypos,int width,int height)
{
GdkPixbuf *thumbnail=hrpreview;
GdkPixbuf *transformed=NULL;
RectFit *fit=NULL;
int dw,dh;
if(thumbnail)
{
LayoutRectangle r(gdk_pixbuf_get_width(thumbnail),gdk_pixbuf_get_height(thumbnail));
LayoutRectangle target(xpos,ypos,width,height);
fit=r.Fit(target,allowcropping,PP_ROTATION_NONE,crop_hpan,crop_vpan);
dw=fit->width;
dh=fit->height;
if(dw > width)
dw=width;
if(dh > height)
dh=height;
if(dw>gdk_pixbuf_get_width(thumbnail))
dw=gdk_pixbuf_get_width(thumbnail);
if(dh>gdk_pixbuf_get_height(thumbnail))
dh=gdk_pixbuf_get_height(thumbnail);
if(mask)
{
transformed=gdk_pixbuf_copy(hrpreview);
maskpixbuf(transformed,fit->xoffset,fit->yoffset,dw,dh,mask,
layout.bgcol.red>>8,layout.bgcol.green>>8,layout.bgcol.blue>>8);
thumbnail=transformed;
}
}
else
{
thumbnail=GetThumbnail();
LayoutRectangle r(gdk_pixbuf_get_width(thumbnail),gdk_pixbuf_get_height(thumbnail));
LayoutRectangle target(xpos,ypos,width,height);
fit=r.Fit(target,allowcropping,rotation,crop_hpan,crop_vpan);
GdkPixbuf *tmp;
switch(fit->rotation)
{
case 0:
transformed=gdk_pixbuf_scale_simple(thumbnail,fit->width,fit->height,GDK_INTERP_NEAREST);
break;
case 270:
tmp=gdk_pixbuf_rotate_simple(thumbnail,GDK_PIXBUF_ROTATE_CLOCKWISE);
transformed=gdk_pixbuf_scale_simple(tmp,fit->width,fit->height,GDK_INTERP_NEAREST);
g_object_unref(G_OBJECT(tmp));
break;
case 180:
tmp=gdk_pixbuf_rotate_simple(thumbnail,GDK_PIXBUF_ROTATE_UPSIDEDOWN);
transformed=gdk_pixbuf_scale_simple(tmp,fit->width,fit->height,GDK_INTERP_NEAREST);
g_object_unref(G_OBJECT(tmp));
break;
case 90:
tmp=gdk_pixbuf_rotate_simple(thumbnail,GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
transformed=gdk_pixbuf_scale_simple(tmp,fit->width,fit->height,GDK_INTERP_NEAREST);
g_object_unref(G_OBJECT(tmp));
break;
}
dw=fit->width;
dh=fit->height;
if(dw > width)
dw=width;
if(dh > height)
dh=height;
if(dw>gdk_pixbuf_get_width(transformed))
dw=gdk_pixbuf_get_width(transformed);
if(dh>gdk_pixbuf_get_height(transformed))
dh=gdk_pixbuf_get_height(transformed);
if(mask)
maskpixbuf(transformed,fit->xoffset,fit->yoffset,dw,dh,mask,
layout.bgcol.red>>8,layout.bgcol.green>>8,layout.bgcol.blue>>8);
thumbnail=transformed;
// Trigger a rendering thread if there isn't one already
// and if high-res previews are enabled
if(hrrenderthread==NULL && layout.state.FindInt("HighresPreviews"))
{
if(width>192 && height>192) // Generating lots of thumbs simultaneously is expensive, and if the images are small,
{ // highres previews are of limited value anyway.
hrrenderthread=new hr_payload(&layout.state.profilemanager,layout.factory,this,widget,xpos,ypos,width,height);
}
}
}
gdk_draw_pixbuf(widget->window,NULL,thumbnail,
fit->xoffset,fit->yoffset,
fit->xpos,fit->ypos,
dw,dh,
GDK_RGB_DITHER_NONE,0,0);
if(transformed)
g_object_unref(transformed);
delete fit;
}
void Layout_ImageInfo::SetMask(const char *filename)
{
if(mask)
{
g_object_unref(mask);
mask=NULL;
}
if(maskfilename)
{
free(maskfilename);
maskfilename=NULL;
}
if(filename)
maskfilename=strdup(filename);
FlushThumbnail();
}
GdkPixbuf *Layout_ImageInfo::GetThumbnail()
{
if(thumbnail)
return(thumbnail);
GError *err=NULL;
if(layout.state.batchmode)
return(NULL);
if(maskfilename && !mask)
{
mask=egg_pixbuf_get_thumbnail_for_file (maskfilename, EGG_PIXBUF_THUMBNAIL_LARGE, &err);
// Debug[TRACE] << "Attempting to load mask from: " << maskfilename << endl;
if(!mask)
{
Debug[WARN] << "Mask loading failed - trying ImageSource method" << endl;
try
{
ImageSource *src=ISLoadImage(maskfilename);
if(src)
{
int w,h;
w=(src->width*256)/src->height;
h=256;
if(w>256)
{
w=256;
h=(src->height*256)/src->width;
}
src=ISScaleImageBySize(src,w,h,IS_SCALING_NEARESTNEIGHBOUR);
mask=pixbuf_from_imagesource(src);
delete src;
}
}
catch(const char *err)
{
Debug[ERROR] << "Error: " << err << endl;
}
if(!mask)
{
if(err && err->message)
Debug[ERROR] << "Error: " << err->message << endl;
else
Debug[ERROR] << "Can't get mask thumbnail" << endl;
free(maskfilename);
maskfilename=NULL;
}
}
}
Debug[TRACE] << "Thumbnail not cached - loading..." << endl;
ImageSource *src=NULL;
thumbnail=egg_pixbuf_get_thumbnail_for_file (filename, EGG_PIXBUF_THUMBNAIL_LARGE, &err);
if(!thumbnail)
{
Debug[WARN] << "Can't get pixbuf - loading thumbnail via ImageSource..." << endl;
src=ISLoadImage(filename);
if(src)
{
int w,h;
w=(src->width*256)/src->height;
h=256;
if(w>256)
{
w=256;
h=(src->height*256)/src->width;
}
src=ISScaleImageBySize(src,w,h,IS_SCALING_NEARESTNEIGHBOUR);
thumbnail=pixbuf_from_imagesource(src);
delete src;
src=NULL;
}
if(!thumbnail)
{
if(err && err->message)
throw err->message;
else
throw "Can't get thumbnail";
}
}
// Apply effects here...
if(thumbnail)
{
ImageSource *src2=new ImageSource_GdkPixbuf(thumbnail);
src2=ApplyEffects(src2,PPEFFECT_PRESCALE);
GdkPixbuf *tn2=pixbuf_from_imagesource(src2);
delete src2;
g_object_unref(G_OBJECT(thumbnail));
thumbnail=tn2;
}
// If there's no display profile, then we can use the Default RGB profile instead...
// Debug[TRACE] << "Checking for Display Profile..." << endl;
CMSProfile *targetprof;
CMColourDevice target=CM_COLOURDEVICE_NONE;
if((targetprof=layout.state.profilemanager.GetProfile(CM_COLOURDEVICE_PRINTERPROOF)))
target=CM_COLOURDEVICE_PRINTERPROOF;
else if((targetprof=layout.state.profilemanager.GetProfile(CM_COLOURDEVICE_DISPLAY)))
target=CM_COLOURDEVICE_DISPLAY;
else if((targetprof=layout.state.profilemanager.GetProfile(CM_COLOURDEVICE_DEFAULTRGB)))
target=CM_COLOURDEVICE_DEFAULTRGB;
if(targetprof)
delete targetprof;
if(target!=CM_COLOURDEVICE_NONE)
{
if(!src)
{
src=ISLoadImage(filename);
}
CMSTransform *transform=NULL;
if(src)
{
CMSProfile *emb;
if(customprofile)
emb=new CMSProfile(customprofile); // FIXME: lifespan?
else
emb=src->GetEmbeddedProfile();
if(emb)
{
// Debug[TRACE] << "Creating embedded->monitor transform..." << endl;
if(emb->GetColourSpace()!=IS_TYPE_RGB)
{
// Need to replace the RGB thumbnail with a CMYK or Greyscale version!
Debug[TRACE] << "Creating new thumbnail - CMYK->monitor" << endl;
int w,h;
w=(src->width*256)/src->height;
h=256;
if(w>256)
{
w=256;
h=(src->height*256)/src->width;
}
src=ISScaleImageBySize(src,w,h,IS_SCALING_NEARESTNEIGHBOUR);
if((transform = layout.factory->GetTransform(target,emb,customintent)))
src=new ImageSource_CMS(src,transform);
thumbnail=pixbuf_from_imagesource(src);
delete src;
src=NULL;
transform=NULL; // Don't want to apply the transform a second time...
}
else
{
transform = layout.factory->GetTransform(target,emb,customintent);
}
}
else
{
// Debug[TRACE] << "Creating default->monitor transform..." << endl;
transform = layout.factory->GetTransform(target,IS_TYPE_RGB,customintent);
}
}
if(transform)
{
// Debug[TRACE] << "Applying transform..." << endl;
ImageSource *src2=new ImageSource_GdkPixbuf(thumbnail);
src2=new ImageSource_CMS(src2,transform);
GdkPixbuf *tn2=pixbuf_from_imagesource(src2);
delete src2;
g_object_unref(G_OBJECT(thumbnail));
thumbnail=tn2;
}
}
if(src)
{
delete src;
}
Debug[TRACE] << "done" << endl;
return(thumbnail);
}
LayoutRectangle *Layout_ImageInfo::GetBounds()
{
// Dummy function - override in subclasses!
throw "Layout_ImageInfo::GetBounds() method should be overridden by subclass!";
}
RectFit *Layout_ImageInfo::GetFit(double scale)
{
RectFit *result=NULL;
LayoutRectangle *bounds=GetBounds();
bounds->Scale(scale);
LayoutRectangle r(width,height);
result=r.Fit(*bounds,allowcropping,rotation,crop_hpan,crop_vpan);
delete bounds;
return(result);
}
ImageSource *Layout_ImageInfo::GetImageSource(CMColourDevice target,CMTransformFactory *factory)
{
ImageSource *result=NULL;
ImageSource *is=ISLoadImage(filename);
is=ApplyEffects(is,PPEFFECT_PRESCALE);
IS_TYPE colourspace=layout.GetColourSpace(target);
if(STRIP_ALPHA(is->type)==IS_TYPE_GREY)
is=new ImageSource_Promote(is,colourspace);
if(STRIP_ALPHA(is->type)==IS_TYPE_BW)
is=new ImageSource_Promote(is,colourspace);
// If this fails we don't bother with the histogram, since another thread has it
// locked for writing.
if(histogram.AttemptMutexShared())
{
is=new PPIS_Histogram(is,histogram);
histogram.ReleaseMutexShared(); // ReleaseShared because the Histogram itself holds an exclusive lock
// and we don't want to cancel its exclusivity!
}
CMSTransform *transform=NULL;
if(factory)
{
CMSProfile *emb;
if(customprofile)
emb=new CMSProfile(customprofile); // FIXME: Lifespan!
else
emb=is->GetEmbeddedProfile();
if(emb)
{
// Debug[TRACE] << "Has embedded / assigned profile..." << endl;
transform=factory->GetTransform(target,emb,customintent); // FIXME: intent!
}
else
{
// Debug[TRACE] << "No embedded profile - using default" << endl;
transform=factory->GetTransform(target,IS_TYPE(STRIP_ALPHA(is->type)),customintent);
}
if(transform)
is=new ImageSource_CMS(is,transform);
}
result=is;
return(result);
}
ImageSource *Layout_ImageInfo::ApplyMask(ImageSource *is)
{
if(maskfilename)
{
ImageSource *mask=ISLoadImage(maskfilename);
if((is->width>is->height)^(mask->width>mask->height))
{
mask=new ImageSource_Rotate(mask,90);
// Debug[TRACE] << "Rotating mask" << endl;
}
mask=ISScaleImageBySize(mask,is->width,is->height,IS_SCALING_AUTOMATIC);
mask=new ImageSource_Invert(mask);
is=new ImageSource_Mask(is,mask);
}
return(is);
}
bool Layout_ImageInfo::GetSelected()
{
return(selected);
}
void Layout_ImageInfo::SetSelected(bool sel)
{
selected=sel;
}
void Layout_ImageInfo::ToggleSelected()
{
selected=!selected;
}
const char *Layout_ImageInfo::GetFilename()
{
return(filename);
}
void Layout_ImageInfo::FlushThumbnail()
{
if(thumbnail)
g_object_unref(thumbnail);
thumbnail=NULL;
FlushHRPreview();
}
void Layout_ImageInfo::CancelRenderThread()
{
if(hrrenderthread)
{
hrrenderthread->Stop(); // We don't actually delete it here - the thread is responsible for its own
// demise (by way of a GTK Idle function running on the main thread's context)
// but having signalled it to stop, we can discard this pointer to it.
}
hrrenderthread=NULL;
}
void Layout_ImageInfo::FlushHRPreview()
{
CancelRenderThread();
if(hrpreview)
g_object_unref(hrpreview);
hrpreview=NULL;
}
void Layout_ImageInfo::SetHRPreview(GdkPixbuf *preview)
{
if(hrpreview)
g_object_unref(hrpreview);
hrpreview=NULL;
hrpreview=preview;
}
void Layout_ImageInfo::AssignProfile(const char *filename)
{
if(customprofile)
free(customprofile);
customprofile=NULL;
if(filename)
customprofile=strdup(filename);
FlushThumbnail();
}
const char *Layout_ImageInfo::GetAssignedProfile()
{
return(customprofile);
}
void Layout_ImageInfo::SetRenderingIntent(LCMSWrapper_Intent intent)
{
FlushThumbnail();
customintent=intent;
}
LCMSWrapper_Intent Layout_ImageInfo::GetRenderingIntent()
{
return(customintent);
}
int Layout_ImageInfo::GetWidth()
{
return(width);
}
int Layout_ImageInfo::GetHeight()
{
return(height);
}
int Layout_ImageInfo::GetXRes()
{
return(xres);
}
int Layout_ImageInfo::GetYRes()
{
return(yres);
}
// Because the only reason you would need to ObtainMutex() the ImageInfo (rather than ObtainShared())
// is to make a write-change to it, we flush the high-res preview on the assumption that the change
// will invalidate it. Note, also, we won't be able to obtain the exclusive lock while the thread's
// running.
// FIXME - would be better to require an explicit flush() of some kind.
void Layout_ImageInfo::ObtainMutex()
{
// Debug[TRACE] << "In custom Obtain method - flushing preview..." << endl;
FlushHRPreview();
// Debug[TRACE] << "Now attempting to obtain exclusive lock..." << endl;
while(!PPEffectHeader::AttemptMutex())
{
// Debug[TRACE] << "Can't get exclusive lock - performing main loop iteration" << endl;
gtk_main_iteration();
}
// Debug[TRACE] << "Done" << endl;
}
PPHistogram &Layout_ImageInfo::GetHistogram()
{
return(histogram);
}
|