Subversion Repositories filter_foundry

Rev

Rev 39 | Rev 53 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 toby 1
/*
18 toby 2
    This file is part of Filter Foundry, a filter plugin for Adobe Photoshop
2 toby 3
    Copyright (C) 2003-5 Toby Thain, toby@telegraphics.com.au
4
 
5
    This program is free software; you can redistribute it and/or modify
6
    it under the terms of the GNU General Public License as published by  
7
    the Free Software Foundation; either version 2 of the License, or
8
    (at your option) any later version.
9
 
10
    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
14
 
15
    You should have received a copy of the GNU General Public License  
16
    along with this program; if not, write to the Free Software
17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
*/
19
 
20
/* This is PLATFORM INDEPENDENT user interface code - mainly dialog logic */
21
 
22
#include "ff.h"
23
 
29 toby 24
#include "PIProperties.h"
2 toby 25
 
26
#include "node.h"
27
#include "funcs.h"
28
#include "y.tab.h"
29
#include "choosefile.h"
30
 
31
PSPixelMap preview_pmap;
32
PSPixelMask preview_pmask;
33
Handle preview_handle;
34
UIRECT preview_rect;
37 toby 35
int preview_w,preview_h,previewerr = false;
2 toby 36
Point preview_scroll;
37
Boolean preview_complete = false;
38
 
39
Boolean setup_preview(FilterRecordPtr pb){
40
 
41
        //dbg("setup_preview");
42
 
43
        if(pb->displayPixels && pb->advanceState){
44
 
45
                preview_w = MIN(preview_rect.right - preview_rect.left - 2,pb->filterRect.right - pb->filterRect.left);
46
                preview_h = MIN(preview_rect.bottom - preview_rect.top - 2,pb->filterRect.bottom - pb->filterRect.top);
47
 
48
                preview_pmap.version = 1;
49
                preview_pmap.bounds.left = preview_pmap.bounds.top = 0;
50
                preview_pmap.bounds.right = preview_w;
51
                preview_pmap.bounds.bottom = preview_h;
52
                preview_pmap.imageMode = nplanes>1 ? plugInModeRGBColor : plugInModeGrayScale;
53
                preview_pmap.rowBytes = nplanes*preview_w;
54
                preview_pmap.colBytes = nplanes;
55
                preview_pmap.planeBytes = 1; /*interleaved*/
56
        //      preview_pmap.baseAddr = preview_data;
57
        /* baseAddr must be set before using pixelmap */
58
 
59
                //---------------------------------------------------------------------------
60
                // Fields new in version 1:
61
                //---------------------------------------------------------------------------   
62
                preview_pmap.mat = NULL;
63
 
64
                if(nplanes==4){
65
                        preview_pmask.next = NULL;
66
        //              preview_pmask.maskData = preview_data+3;
67
                        preview_pmask.rowBytes = preview_pmap.rowBytes;
68
                        preview_pmask.colBytes = nplanes;
69
                        preview_pmask.maskDescription = kSimplePSMask;
70
                        preview_pmap.masks = &preview_pmask;
71
                }else
72
                        preview_pmap.masks = NULL;
73
 
74
                preview_handle = PINEWHANDLE((long)preview_h * preview_pmap.rowBytes);
75
        }else
76
                preview_handle = NULL;
77
        return preview_handle != NULL;
78
 
79
        //---------------------------------------------------------------------------
80
        // Fields new in version 2:
81
        //---------------------------------------------------------------------------   
82
//      preview_pmap.pixelOverlays;
83
//      preview_pmap.colorManagementOptions;
84
 
85
//      setup(pb); // prepare for evaluations
86
}
87
 
88
void dispose_preview(){
89
        if(preview_handle){
90
                PIDISPOSEHANDLE(preview_handle);
91
                preview_handle = NULL;
92
        }
93
}
94
 
95
void recalc_preview(FilterRecordPtr pb,DIALOGREF dp){
96
        extern int srcradused,needinput;
97
        extern double zoomfactor;
98
        OSErr e;
45 toby 99
        int j,n,scaledw,scaledh,imgw,imgh;
2 toby 100
        Rect r,outRect;
101
        Ptr outrow;
102
 
37 toby 103
        preview_complete = false;
104
 
2 toby 105
        if(preview_handle){
106
                /* size of previewed area, of source image; but no larger than filtered area (selection) */
107
                scaledw = zoomfactor*preview_w;
108
                if(scaledw > (pb->filterRect.right - pb->filterRect.left))
109
                        scaledw = (pb->filterRect.right - pb->filterRect.left);
110
                scaledh = zoomfactor*preview_h;
111
                if(scaledh > (pb->filterRect.bottom - pb->filterRect.top))
112
                        scaledh = (pb->filterRect.bottom - pb->filterRect.top);
113
 
114
                /* scale clipped preview area down again - this becomes the pixel size of preview */
115
                imgw = scaledw/zoomfactor;
116
                if(imgw > preview_w)
117
                        imgw = preview_w;
118
                imgh = scaledh/zoomfactor;
119
                if(imgh > preview_h)
120
                        imgh = preview_h;
121
 
122
                // Use to set the phase of the checkerboard:
123
                preview_pmap.maskPhaseRow = preview_scroll.v/zoomfactor;
124
                preview_pmap.maskPhaseCol = preview_scroll.h/zoomfactor;
125
 
126
                /* compute source data rectangle (inRect) */
127
 
128
                /* centre preview on filtered part of input image, adding scroll offset */
129
                r.left = (pb->filterRect.left+pb->filterRect.right-scaledw)/2 + preview_scroll.h;
130
                /* make sure it does not go outside the input area */
131
                if(r.left < pb->filterRect.left)
132
                        r.left = pb->filterRect.left;
133
                else if(r.left > pb->filterRect.right-scaledw)
134
                        r.left = pb->filterRect.right-scaledw;
135
                r.right = r.left + scaledw;
136
 
137
                /* now compute for vertical */
138
                r.top = (pb->filterRect.top+pb->filterRect.bottom-scaledh)/2 + preview_scroll.v;
139
                if(r.top < pb->filterRect.top)
140
                        r.top = pb->filterRect.top;
141
                else if(r.top > pb->filterRect.bottom-scaledh)
142
                        r.top = pb->filterRect.bottom-scaledh;
143
                r.bottom = r.top + scaledh;
144
 
145
                /* if formulae need random access to image - src(), rad() - we must request entire area: */
146
                if(srcradused){
147
                        SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v);
148
                }else
149
                        pb->inRect = r;
150
 
151
                pb->outRect = pb->inRect;
152
                SETRECT(pb->maskRect,0,0,0,0);
153
                pb->inLoPlane = pb->outLoPlane = 0;
154
                pb->inHiPlane = pb->outHiPlane = nplanes-1;
155
 
156
//dbg("recalc_preview: about to call advanceState()");
37 toby 157
 
2 toby 158
                if( !needinput || !(e = pb->advanceState()) ){
159
                        Ptr outptr = PILOCKHANDLE(preview_handle,false);
160
                        int blankrows = (preview_h-imgh)/2,
161
                                blankcols = (preview_w-imgw)/2;
162
 
163
                        INITRANDSEED();
164
//dbg("recalc_preview: about to call process()");
165
 
166
                        SETRECT(outRect,0,0,imgw,imgh);
167
 
168
                        e = process_scaled(pb,false,
169
                                        &pb->inRect,&r,&outRect,
170
                                        outptr+preview_pmap.rowBytes*blankrows+nplanes*blankcols,preview_pmap.rowBytes,
171
                                        zoomfactor);
172
                        if(blankrows){
173
                                memset(outptr,0xff,preview_pmap.rowBytes*blankrows);
174
                                n = preview_h - blankrows - imgh; /* blank rows below preview */
175
                                memset(outptr+preview_pmap.rowBytes*(blankrows+imgh),0xff,preview_pmap.rowBytes*n);
176
                        }
177
                        if(blankcols){
178
                                n = preview_w - blankcols - imgw; /* blank columns on right side of preview */
179
                                outrow = outptr+preview_pmap.rowBytes*blankrows;
180
                                for( j = blankrows ; j < preview_h - blankrows ; ++j ){
181
                                        memset(outrow,0xff,nplanes*blankcols);
182
                                        memset(outrow+nplanes*(blankcols+imgw),0xff,nplanes*n);
183
                                        outrow += preview_pmap.rowBytes;
184
                                }
185
                        }
186
 
187
                        if(!e){
188
                                preview_complete = true;
189
 
190
#ifdef WIN_ENV
191
                                {
192
                                extern HWND preview_hwnd;
193
                                HDC hdc = GetDC(preview_hwnd);
194
 
195
//dbg("recalc_preview: about to call drawpreview()");
196
                                drawpreview(dp,hdc,outptr);
197
 
198
                                ReleaseDC(preview_hwnd,hdc);
199
                                }
200
#else
201
                                {
202
                                GrafPtr saveport;
203
 
204
                                GetPort(&saveport);
205
                                SetPortDialogPort(dp);
206
 
207
                                drawpreview(dp,NULL,outptr);
208
 
209
                                SetPort(saveport);
210
                                }
211
#endif
212
                        }
213
 
214
                        PIUNLOCKHANDLE(preview_handle);
215
 
37 toby 216
                }/*else{
217
                        char s[0x100];
2 toby 218
                        sprintf(s,"recalc_preview: advanceState failed (%d)\n\
219
inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d) inLoPlane=%d inHiPlane=%d ",
220
                                e,
221
                                pb->inRect.left,pb->inRect.top,pb->inRect.right,pb->inRect.bottom,
222
                                pb->filterRect.left,pb->filterRect.top,pb->filterRect.right,pb->filterRect.bottom,
223
                                pb->inLoPlane,pb->inHiPlane);
224
                        dbg(s);
37 toby 225
                }*/
226
 
227
                if(e && !previewerr){
39 toby 228
                        alertuser("Could not build preview at chosen zoom level.",
37 toby 229
                                e == memFullErr && !srcradused ? "The image is too large for available memory. Try zooming in.\nIf that does not help, Cancel and retry the filter." : "");
230
                        previewerr = true;
2 toby 231
                }
37 toby 232
 
2 toby 233
        }
234
}
235
 
236
OSErr drawpreview(DIALOGREF dp,void *hdc,Ptr imageptr){
237
        int32 watchsusp;
238
        OSErr e = noErr;
239
        VRect srcRect;
240
        UIRECT imagebounds;
241
 
242
        if(preview_handle && preview_complete){
243
 
244
                srcRect = preview_pmap.bounds;
245
 
246
                imagebounds.left = (preview_rect.left+preview_rect.right-preview_w)/2;
247
                imagebounds.top = (preview_rect.top+preview_rect.bottom-preview_h)/2;
248
                imagebounds.right = imagebounds.left + preview_w;
249
                imagebounds.bottom = imagebounds.top + preview_h;
250
 
251
                preview_pmap.baseAddr = imageptr;//PILOCKHANDLE(preview_handle,false);
252
                preview_pmask.maskData = imageptr+3;
253
 
254
                if(gpb->propertyProcs->getPropertyProc){
255
                        gpb->propertyProcs->getPropertyProc(kPhotoshopSignature,propWatchSuspension,0,&watchsusp,NULL);
256
                        gpb->propertyProcs->setPropertyProc(kPhotoshopSignature,propWatchSuspension,0,watchsusp+1,NULL);
257
                }
258
                e = gpb->displayPixels(&preview_pmap,&srcRect,imagebounds.top,imagebounds.left,hdc);
259
 
260
                if(gpb->propertyProcs->getPropertyProc)
261
                        gpb->propertyProcs->setPropertyProc(kPhotoshopSignature,propWatchSuspension,0,watchsusp,NULL);
262
        }
263
        return e;
264
}