Subversion Repositories filter_foundry

Rev

Rev 171 | Rev 189 | 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
171 dmarschall 3
    Copyright (C) 2003-2019 Toby Thain, toby@telegraphics.com.au
2 toby 4
 
5
    This program is free software; you can redistribute it and/or modify
110 dmarschall 6
    it under the terms of the GNU General Public License as published by
2 toby 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
 
110 dmarschall 15
    You should have received a copy of the GNU General Public License
2 toby 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
 
94 toby 22
#include "preview.h"
2 toby 23
 
94 toby 24
#ifdef MAC_ENV
25
        #include <fp.h>
26
#endif
27
#include <math.h>
28
 
29 toby 29
#include "PIProperties.h"
2 toby 30
 
94 toby 31
extern FilterRecordPtr gpb;
2 toby 32
 
33
PSPixelMap preview_pmap;
34
PSPixelMask preview_pmask;
35
Handle preview_handle;
36
UIRECT preview_rect;
94 toby 37
int preview_w,preview_h,previewerr = false,needall = false,needinput = true;
2 toby 38
Point preview_scroll;
39
Boolean preview_complete = false;
94 toby 40
double zoomfactor,fitzoom;
2 toby 41
 
94 toby 42
Boolean setup_preview(FilterRecordPtr pb, int nplanes){
43
        double zh,zv;
44
 
2 toby 45
        if(pb->displayPixels && pb->advanceState){
157 dmarschall 46
                // Possibility 1: Only the part of the preview rect is filled with background color,
47
                // which can be occupied by image data if zoom factor becomes 100%
48
                /*
155 dmarschall 49
                preview_w = MIN(preview_rect.right - preview_rect.left,
89 toby 50
                                                pb->filterRect.right - pb->filterRect.left);
155 dmarschall 51
                preview_h = MIN(preview_rect.bottom - preview_rect.top,
89 toby 52
                                                pb->filterRect.bottom - pb->filterRect.top);
157 dmarschall 53
                */
54
                // Possibility 2: The whole preview rect is always filled with the background color,
55
                // so you can easily see what is the preview area and what is not
56
                preview_w = preview_rect.right - preview_rect.left;
57
                preview_h = preview_rect.bottom - preview_rect.top;
58
 
94 toby 59
                zh = (pb->filterRect.right - pb->filterRect.left)/(double)preview_w;
60
                zv = (pb->filterRect.bottom - pb->filterRect.top)/(double)preview_h;
61
                fitzoom = zh > zv ? zh : zv;
110 dmarschall 62
 
2 toby 63
                preview_pmap.version = 1;
64
                preview_pmap.bounds.left = preview_pmap.bounds.top = 0;
65
                preview_pmap.bounds.right = preview_w;
66
                preview_pmap.bounds.bottom = preview_h;
94 toby 67
                preview_pmap.imageMode = pb->imageMode;
2 toby 68
                preview_pmap.rowBytes = nplanes*preview_w;
69
                preview_pmap.colBytes = nplanes;
70
                preview_pmap.planeBytes = 1; /*interleaved*/
71
        //      preview_pmap.baseAddr = preview_data;
72
        /* baseAddr must be set before using pixelmap */
110 dmarschall 73
 
2 toby 74
                //---------------------------------------------------------------------------
75
                // Fields new in version 1:
110 dmarschall 76
                //---------------------------------------------------------------------------
2 toby 77
                preview_pmap.mat = NULL;
110 dmarschall 78
 
94 toby 79
                if( (pb->imageMode == plugInModeRGBColor && nplanes == 4)
80
                 || (pb->imageMode == plugInModeLabColor && nplanes == 4)
81
                 || (pb->imageMode == plugInModeGrayScale && nplanes == 2)
82
                 || (pb->imageMode == plugInModeDuotone && nplanes == 2) )
89 toby 83
                {
2 toby 84
                        preview_pmask.next = NULL;
85
        //              preview_pmask.maskData = preview_data+3;
86
                        preview_pmask.rowBytes = preview_pmap.rowBytes;
87
                        preview_pmask.colBytes = nplanes;
88
                        preview_pmask.maskDescription = kSimplePSMask;
89
                        preview_pmap.masks = &preview_pmask;
90
                }else
91
                        preview_pmap.masks = NULL;
110 dmarschall 92
 
2 toby 93
                preview_handle = PINEWHANDLE((long)preview_h * preview_pmap.rowBytes);
94
        }else
95
                preview_handle = NULL;
96
        return preview_handle != NULL;
97
 
98
        //---------------------------------------------------------------------------
99
        // Fields new in version 2:
110 dmarschall 100
        //---------------------------------------------------------------------------
2 toby 101
//      preview_pmap.pixelOverlays;
102
//      preview_pmap.colorManagementOptions;
103
}
104
 
105
void dispose_preview(){
106
        if(preview_handle){
107
                PIDISPOSEHANDLE(preview_handle);
108
                preview_handle = NULL;
109
        }
110
}
111
 
158 dmarschall 112
void* memset_bgcolor(void* ptr, size_t num) {
157 dmarschall 113
        int i;
156 dmarschall 114
        byte* p;
157 dmarschall 115
 
116
        i = 0;
156 dmarschall 117
        p = (byte*)ptr;
185 dmarschall 118
        for (i=0; i<(int)num; ++i) {
158 dmarschall 119
#ifdef WIN_ENV
159 dmarschall 120
                DWORD color;
121
 
158 dmarschall 122
                color = GetSysColor(COLOR_APPWORKSPACE);
159 dmarschall 123
 
124
                if (gpb->imageMode == plugInModeRGBColor) {
125
                        if (i%nplanes == 0) p[i] = GetRValue(color);
126
                        if (i%nplanes == 1) p[i] = GetGValue(color);
127
                        if (i%nplanes == 2) p[i] = GetBValue(color);
128
                        if (i%nplanes == 3) p[i] = 255; // alpha channel
129
                } else if (gpb->imageMode == plugInModeGrayScale) {
130
                        byte r, g, b;
131
 
132
                        r = GetRValue(color);
133
                        g = GetGValue(color);
134
                        b = GetBValue(color);
135
 
185 dmarschall 136
                        if (i%nplanes == 0) p[i] = (byte)(((299L*r)+(587L*g)+(114L*b))/1000);
159 dmarschall 137
                        if (i%nplanes == 1) p[i] = 255; // alpha channel
138
                } else if (gpb->imageMode == plugInModeCMYKColor) {
139
                        byte r, g, b;
140
                        double dr, dg, db, k, c, m, y;
141
 
142
                        r = GetRValue(color);
143
                        g = GetGValue(color);
144
                        b = GetBValue(color);
145
 
146
                        dr = (double)r / 255;
147
                        dg = (double)g / 255;
148
                        db = (double)b / 255;
149
                        k = 1 - max(max(dr, dg), db);
150
                        c = (1 - dr - k) / (1 - k);
151
                        m = (1 - dg - k) / (1 - k);
152
                        y = (1 - db - k) / (1 - k);
153
 
185 dmarschall 154
                        if (i%nplanes == 0) p[i] = (byte)(255 - c * 255);
155
                        if (i%nplanes == 1) p[i] = (byte)(255 - m * 255);
156
                        if (i%nplanes == 2) p[i] = (byte)(255 - y * 255);
157
                        if (i%nplanes == 3) p[i] = (byte)(255 - k * 255);
159 dmarschall 158
                } else {
159
                        // FIXME: If we are in such a non supported color mode, then
160
                        //        these color codes would be all wrong!
161
                        //        Just to be safe use (what is probably) white
162
                        p[i] = 0xFF;
163
 
164
                        /*
165
                        #define plugInModeBitmap                        0
166
                        #define plugInModeGrayScale                     1 supported
167
                        #define plugInModeIndexedColor          2
168
                        #define plugInModeRGBColor                      3 supported
169
                        #define plugInModeCMYKColor                     4 supported
170
                        #define plugInModeHSLColor                      5
171
                        #define plugInModeHSBColor                      6
172
                        #define plugInModeMultichannel          7
173
                        #define plugInModeDuotone                       8
174
                        #define plugInModeLabColor                      9
175
                        #define plugInModeGray16                        10
176
                        #define plugInModeRGB48                         11
177
                        #define plugInModeLab48                         12
178
                        #define plugInModeCMYK64                        13
179
                        #define plugInModeDeepMultichannel      14
180
                        #define plugInModeDuotone16                     15
181
                        #define plugInModeRGB96                         16
182
                        #define plugInModeGray32                        17
183
                        */
184
 
185
                }
158 dmarschall 186
#else
187
                // This is the behavior of FilterFoundry <1.7 was this (filled with 0xFF)
188
                // FIXME: Should we do something fancy here, too?
189
                p[i] = 0xFF;
190
#endif
156 dmarschall 191
        }
192
        return ptr;
193
}
194
 
2 toby 195
void recalc_preview(FilterRecordPtr pb,DIALOGREF dp){
196
        OSErr e;
185 dmarschall 197
        double scaledw, scaledh;
198
        int j,n,imgw,imgh;
2 toby 199
        Rect r,outRect;
200
        Ptr outrow;
201
 
37 toby 202
        preview_complete = false;
110 dmarschall 203
 
2 toby 204
        if(preview_handle){
205
                /* size of previewed area, of source image; but no larger than filtered area (selection) */
206
                scaledw = zoomfactor*preview_w;
207
                if(scaledw > (pb->filterRect.right - pb->filterRect.left))
89 toby 208
                        scaledw = pb->filterRect.right - pb->filterRect.left;
2 toby 209
                scaledh = zoomfactor*preview_h;
210
                if(scaledh > (pb->filterRect.bottom - pb->filterRect.top))
89 toby 211
                        scaledh = pb->filterRect.bottom - pb->filterRect.top;
110 dmarschall 212
 
2 toby 213
                /* scale clipped preview area down again - this becomes the pixel size of preview */
185 dmarschall 214
                imgw = (int)ceil(scaledw/zoomfactor);
2 toby 215
                if(imgw > preview_w)
216
                        imgw = preview_w;
185 dmarschall 217
                imgh = (int)ceil(scaledh/zoomfactor);
2 toby 218
                if(imgh > preview_h)
219
                        imgh = preview_h;
220
 
221
                /* compute source data rectangle (inRect) */
222
 
223
                /* centre preview on filtered part of input image, adding scroll offset */
185 dmarschall 224
                r.left = (int16)((pb->filterRect.left + pb->filterRect.right - scaledw)/2 + preview_scroll.h);
2 toby 225
                /* make sure it does not go outside the input area */
110 dmarschall 226
                if(r.left < pb->filterRect.left) {
120 dmarschall 227
                        preview_scroll.h += pb->filterRect.left - r.left;
2 toby 228
                        r.left = pb->filterRect.left;
110 dmarschall 229
                }
230
                else if(r.left+scaledw > pb->filterRect.right) {
185 dmarschall 231
                        preview_scroll.h += (int16)(pb->filterRect.right - (r.left+scaledw));
232
                        r.left = (int16)(pb->filterRect.right - scaledw);
110 dmarschall 233
                }
185 dmarschall 234
                r.right = (int16)(r.left + scaledw);
235
                preview_pmap.maskPhaseCol = (int32)((preview_scroll.h)/zoomfactor); // phase of the checkerboard
2 toby 236
 
237
                /* now compute for vertical */
185 dmarschall 238
                r.top = (int16)((pb->filterRect.top + pb->filterRect.bottom - scaledh)/2 + preview_scroll.v);
110 dmarschall 239
                if(r.top < pb->filterRect.top) {
120 dmarschall 240
                        preview_scroll.v += pb->filterRect.top - r.top;
2 toby 241
                        r.top = pb->filterRect.top;
110 dmarschall 242
                }
243
                else if(r.top+scaledh > pb->filterRect.bottom) {
185 dmarschall 244
                        preview_scroll.v += (int16)(pb->filterRect.bottom - (r.top+scaledh));
245
                        r.top = (int16)(pb->filterRect.bottom - scaledh);
110 dmarschall 246
                }
185 dmarschall 247
                r.bottom = (int16)(r.top + scaledh);
248
                preview_pmap.maskPhaseRow = (int32)((preview_scroll.v)/zoomfactor); // phase of the checkerboard
2 toby 249
 
250
                /* if formulae need random access to image - src(), rad() - we must request entire area: */
94 toby 251
                if(needall)
2 toby 252
                        SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v);
94 toby 253
                else
2 toby 254
                        pb->inRect = r;
110 dmarschall 255
 
2 toby 256
                pb->outRect = pb->inRect;
257
                SETRECT(pb->maskRect,0,0,0,0);
258
                pb->inLoPlane = pb->outLoPlane = 0;
259
                pb->inHiPlane = pb->outHiPlane = nplanes-1;
37 toby 260
 
2 toby 261
                if( !needinput || !(e = pb->advanceState()) ){
262
                        Ptr outptr = PILOCKHANDLE(preview_handle,false);
53 toby 263
                        int blankrows = (preview_h - imgh)/2,
89 toby 264
                                blankcols = (preview_w - imgw)/2,
265
                                pmrb = preview_pmap.rowBytes;
2 toby 266
 
94 toby 267
                        evalinit();
110 dmarschall 268
 
2 toby 269
                        SETRECT(outRect,0,0,imgw,imgh);
110 dmarschall 270
 
53 toby 271
                        e = process_scaled(pb, false, &r, &outRect,
272
                                        outptr + pmrb*blankrows + nplanes*blankcols, pmrb, zoomfactor);
2 toby 273
                        if(blankrows){
157 dmarschall 274
                                // blank rows on top of preview:
158 dmarschall 275
                                memset_bgcolor(outptr, pmrb*blankrows);
157 dmarschall 276
                                // blank rows below preview:
277
                                n = preview_h - blankrows - imgh;
158 dmarschall 278
                                memset_bgcolor(outptr + pmrb*(blankrows+imgh), pmrb*n);
2 toby 279
                        }
280
                        if(blankcols){
157 dmarschall 281
                                n = preview_w - blankcols - imgw;
53 toby 282
                                outrow = outptr + pmrb*blankrows;
94 toby 283
                                for(j = blankrows; j < preview_h - blankrows; ++j){
157 dmarschall 284
                                        // blank columns on left side of preview (if picture is smaller than the preview area):
158 dmarschall 285
                                        memset_bgcolor(outrow, nplanes*blankcols);
157 dmarschall 286
                                        // blank columns on right side of preview (if picture is smaller than the preview area):
158 dmarschall 287
                                        memset_bgcolor(outrow + nplanes*(blankcols+imgw), nplanes*n);
53 toby 288
                                        outrow += pmrb;
2 toby 289
                                }
290
                        }
291
 
292
                        if(!e){
293
                                preview_complete = true;
294
 
295
#ifdef WIN_ENV
296
                                {
297
                                extern HWND preview_hwnd;
298
                                HDC hdc = GetDC(preview_hwnd);
89 toby 299
 
2 toby 300
                                drawpreview(dp,hdc,outptr);
110 dmarschall 301
 
2 toby 302
                                ReleaseDC(preview_hwnd,hdc);
303
                                }
304
#else
305
                                {
306
                                GrafPtr saveport;
307
 
308
                                GetPort(&saveport);
309
                                SetPortDialogPort(dp);
110 dmarschall 310
 
2 toby 311
                                drawpreview(dp,NULL,outptr);
110 dmarschall 312
 
2 toby 313
                                SetPort(saveport);
314
                                }
315
#endif
316
                        }
317
 
318
                        PIUNLOCKHANDLE(preview_handle);
94 toby 319
                }
2 toby 320
 
37 toby 321
                if(e && !previewerr){
39 toby 322
                        alertuser("Could not build preview at chosen zoom level.",
94 toby 323
                                e == memFullErr && !needall ? "The image is too large for available memory. Try zooming in.\nIf that does not help, Cancel and retry the filter." : "");
37 toby 324
                        previewerr = true;
2 toby 325
                }
37 toby 326
 
2 toby 327
        }
328
}
329
 
330
OSErr drawpreview(DIALOGREF dp,void *hdc,Ptr imageptr){
114 dmarschall 331
        intptr_t watchsusp;
2 toby 332
        OSErr e = noErr;
333
        VRect srcRect;
334
        UIRECT imagebounds;
335
 
336
        if(preview_handle && preview_complete){
337
 
338
                srcRect = preview_pmap.bounds;
339
 
53 toby 340
                imagebounds.left = (preview_rect.left + preview_rect.right - preview_w)/2;
341
                imagebounds.top = (preview_rect.top + preview_rect.bottom - preview_h)/2;
2 toby 342
                imagebounds.right = imagebounds.left + preview_w;
343
                imagebounds.bottom = imagebounds.top + preview_h;
344
 
89 toby 345
                preview_pmap.baseAddr = imageptr;
94 toby 346
                preview_pmask.maskData = imageptr+3; // FIXME: is this offset correct for all modes?!
2 toby 347
 
348
                if(gpb->propertyProcs->getPropertyProc){
349
                        gpb->propertyProcs->getPropertyProc(kPhotoshopSignature,propWatchSuspension,0,&watchsusp,NULL);
350
                        gpb->propertyProcs->setPropertyProc(kPhotoshopSignature,propWatchSuspension,0,watchsusp+1,NULL);
351
                }
89 toby 352
 
2 toby 353
                e = gpb->displayPixels(&preview_pmap,&srcRect,imagebounds.top,imagebounds.left,hdc);
354
 
355
                if(gpb->propertyProcs->getPropertyProc)
356
                        gpb->propertyProcs->setPropertyProc(kPhotoshopSignature,propWatchSuspension,0,watchsusp,NULL);
357
        }
358
        return e;
359
}