Diff of /cpyBuf.cpp [68cdf8] .. [ed05d7]  Maximize  Restore

Switch to unified view

a/cpyBuf.cpp b/cpyBuf.cpp
1
2
// PTLens, Copyright (C) 2004, Thomas Niemann
3
// email: thomasn@epaperpress.com, subject=ptlens
4
// 
5
// This program is free software; you can redistribute 
1
// This program is free software; you can redistribute 
6
// it and/or modify it under the terms of the GNU
2
// it and/or modify it under the terms of the GNU
7
// General Public License as published by the Free 
3
// General Public License as published by the Free 
8
// Software Foundation; either version 2 of the 
4
// Software Foundation; either version 2 of the 
9
// License, or (at your option) any later version. 
5
// License, or (at your option) any later version. 
...
...
17
// You should have received a copy of the GNU General 
13
// You should have received a copy of the GNU General 
18
// Public License along with this program; if not, 
14
// Public License along with this program; if not, 
19
// write to the Free Software Foundation, Inc., 675 
15
// write to the Free Software Foundation, Inc., 675 
20
// Mass Ave, Cambridge, MA 02139, USA.
16
// Mass Ave, Cambridge, MA 02139, USA.
21
17
18
/**
19
* @file CypBuf.cpp
20
* This file is included in all four plug-ins (PTAdjust, PTCorrect, PTPerspect, and PTRemap)
21
* It contains two functions for converting image buffers between photoshop and 
22
* panotools internal formats.
23
* @date January 2007
24
*/
25
22
#include "main.h"
26
#include "main.h"
23
27
28
/**
29
* Copy Image Buffer data from Panotool to Photoshop format
30
* @param[in,out] buf the buffer to copy
31
* @return 0 on success
32
**/
24
int cpyBufToPs(void *buf) {
33
int cpyBufToPs(void *buf)
25
34
{
26
  // some local variables for efficiency
35
  // some local variables for efficiency
27
  int16 tileHeight = gFr->filterRect.bottom - gFr->filterRect.top;
36
  int16 tileHeight = gFr->filterRect.bottom - gFr->filterRect.top;
28
  int16 tileWidth = gFr->filterRect.right - gFr->filterRect.left;
37
  int16 tileWidth = gFr->filterRect.right - gFr->filterRect.left;
29
  Rect *outRect = &gFr->outRect;
38
  Rect *outRect = &gFr->outRect;
30
  Rect *filterRect = &gFr->filterRect;
39
  Rect *filterRect = &gFr->filterRect;
31
  int16 imageWidth = PHO.imageWidth;
40
  int16 imageWidth = PHO.imageWidth;
32
  int16 imageHeight = PHO.imageHeight;
41
  int16 imageHeight = PHO.imageHeight;
33
  int32 imageDepth = PHO.imageDepth;
42
  int32 imageDepth = PHO.imageDepth;
34
  int32 bytesPerPixel = (imageDepth/8) * 3;
43
  int32 bytesPerPixel = (imageDepth/8) * 3;
35
44
36
  // round up to the nearest horizontal and vertical tile count
45
  // round up to the nearest horizontal and vertical tile count
37
  uint16 tilesVert = (uint16)((tileHeight - 1 + imageHeight) / tileHeight);
46
  uint16 tilesVert = (uint16)((tileHeight - 1 + imageHeight) / tileHeight);
38
  uint16 tilesHoriz = (uint16)((tileWidth - 1 + imageWidth) / tileWidth);
47
  uint16 tilesHoriz = (uint16)((tileWidth - 1 + imageWidth) / tileWidth);
39
48
40
  // Fixed numbers are 16.16 values 
49
  // Fixed numbers are 16.16 values 
41
  // the first 16 bits represent the whole number
50
  // the first 16 bits represent the whole number
42
  // the last 16 bits represent the fraction
51
  // the last 16 bits represent the fraction
43
  gFr->inputRate = (int32)1 << 16;
52
  gFr->inputRate = (int32)1 << 16;
44
  gFr->maskRate = (int32)1 << 16;
53
  gFr->maskRate = (int32)1 << 16;
45
 
54
 
46
  // loop through each tile makeing sure we don't go over the bounds
55
  // loop through each tile makeing sure we don't go over the bounds
47
  // of the imageHeight or imageWidth
56
  // of the imageHeight or imageWidth
48
  for (uint16 vertTile = 0; vertTile < tilesVert; vertTile++)  {
57
  for (uint16 vertTile = 0; vertTile < tilesVert; vertTile++)
58
  {
49
      for (uint16 horizTile = 0; horizTile < tilesHoriz; horizTile++) {
59
    for (uint16 horizTile = 0; horizTile < tilesHoriz; horizTile++)
60
    {
50
          outRect->top = (int16)(vertTile * tileHeight);
61
      outRect->top = (int16)(vertTile * tileHeight);
51
          outRect->left = (int16)(horizTile * tileWidth);
62
      outRect->left = (int16)(horizTile * tileWidth);
52
          outRect->bottom = (int16)(outRect->top + tileHeight);
63
      outRect->bottom = (int16)(outRect->top + tileHeight);
53
          outRect->right = (int16)(outRect->left + tileWidth);
64
      outRect->right = (int16)(outRect->left + tileWidth);
54
65
55
          if (outRect->bottom > imageHeight)
66
      if (outRect->bottom > imageHeight)
67
      {
56
              outRect->bottom = imageHeight;
68
        outRect->bottom = imageHeight;
69
      }
57
          if (outRect->right > imageWidth)
70
      if (outRect->right > imageWidth)
71
      {
58
              outRect->right = imageWidth;
72
        outRect->right = imageWidth;
73
      }
59
74
60
          for (uint16 plane = 0; plane < 3; plane++) {
75
      for (uint16 plane = 0; plane < 3; plane++)
76
      {
61
              // we want one plane at a time, small memory foot print is good
77
        // we want one plane at a time, small memory foot print is good
62
              gFr->outLoPlane = plane;
78
        gFr->outLoPlane = plane;
63
              gFr->outHiPlane = plane;
79
        gFr->outHiPlane = plane;
64
  
80
  
65
              // update the gFr with our latest request
81
        // update the gFr with our latest request
66
              *gResult = gFr->advanceState();
82
        *gResult = gFr->advanceState();
67
              if (*gResult != noErr) 
83
        if (*gResult != noErr) 
84
        {
68
                  return display("cpyBufToPs: advanceState");
85
          return display("cpyBufToPs: advanceState");
69
              
86
        }
87
        
70
              // copy one channel at a time
88
        // copy one channel at a time
71
              if (imageDepth == 8) {
89
        if (imageDepth == 8)
90
        {
72
                  uint8* pixel = (uint8*)gFr->outData;
91
          uint8* pixel = (uint8*)gFr->outData;
73
                  uint16 rectHeight = (uint16)(outRect->bottom - outRect->top);
92
          uint16 rectHeight = (uint16)(outRect->bottom - outRect->top);
74
                  uint16 rectWidth = (uint16)(outRect->right - outRect->left);
93
          uint16 rectWidth = (uint16)(outRect->right - outRect->left);
75
94
76
                  for(uint16 pixelY = 0; pixelY < rectHeight; pixelY++) {
95
          for(uint16 pixelY = 0; pixelY < rectHeight; pixelY++)
96
          {
77
                      Ptr dissolve = (char *)buf + bytesPerPixel * 
97
            Ptr dissolve = (char *)buf + bytesPerPixel * 
78
                          (imageWidth * (outRect->top + pixelY) + outRect->left) + plane;
98
              (imageWidth * (outRect->top + pixelY) + outRect->left) + plane;
79
                      for(uint16 pixelX = 0; pixelX < rectWidth; pixelX++) {
99
            for(uint16 pixelX = 0; pixelX < rectWidth; pixelX++)
80
                          *pixel++ = *dissolve;
100
            {
81
                          dissolve += bytesPerPixel;
101
              *pixel++ = *dissolve;
82
                      }
102
              dissolve += bytesPerPixel;
103
            }
83
                      pixel += gFr->outRowBytes - rectWidth;
104
            pixel += gFr->outRowBytes - rectWidth;
84
                  }
105
          }//pixelY
85
              } else {
106
        }//8 bit
86
                  // assume 16-bit
107
        else
108
        {
109
          // assume 16-bit
87
                  uint16* pixel = (uint16*)gFr->outData;
110
          uint16* pixel = (uint16*)gFr->outData;
88
                  uint16 rectHeight = (uint16)(outRect->bottom - outRect->top);
111
          uint16 rectHeight = (uint16)(outRect->bottom - outRect->top);
89
                  uint16 rectWidth = (uint16)(outRect->right - outRect->left);
112
          uint16 rectWidth = (uint16)(outRect->right - outRect->left);
90
                  int bytesPerPixel2 = bytesPerPixel >> 1;
113
          int bytesPerPixel2 = bytesPerPixel >> 1;
91
114
92
                  for(uint16 pixelY = 0; pixelY < rectHeight; pixelY++) {
115
          for(uint16 pixelY = 0; pixelY < rectHeight; pixelY++)
116
          {
93
                      uint16 *dissolve = (uint16 *)((char *)buf + bytesPerPixel * 
117
            uint16 *dissolve = (uint16 *)((char *)buf + bytesPerPixel * 
94
                          (imageWidth * (outRect->top + pixelY) + outRect->left)
118
              (imageWidth * (outRect->top + pixelY) + outRect->left)
95
                          + 2*plane);
119
              + 2*plane);
96
                      for(uint16 pixelX = 0; pixelX < rectWidth; pixelX++) {
120
            for(uint16 pixelX = 0; pixelX < rectWidth; pixelX++)
121
            {
97
                          // convert 0..65535 to 0..32768
122
              // convert 0..65535 to 0..32768
98
                          *pixel++ = (*dissolve + 1U) >> 1;
123
              *pixel++ = (*dissolve + 1U) >> 1;
99
                          dissolve += bytesPerPixel2;
124
              dissolve += bytesPerPixel2;
100
                      }
125
            }
101
                      pixel += (gFr->outRowBytes - 2*rectWidth) >> 1;
126
            pixel += (gFr->outRowBytes - 2*rectWidth) >> 1;
102
                  }                           
127
          }//pixelY
103
              }
128
        }//16 bit
104
          }
129
      }//plane
130
    }//horizTile
131
  }//vertTile
105
132
106
      }
107
  }
108
109
  return 0;
133
  return 0;
110
}
134
}
111
135
136
/**
137
* Copy Image Buffer data from Photoshop to Panotool format
138
* @param[in,out] buf the buffer to copy
139
* @return none
140
**/
112
int cpyPsToBuf(void *buf) {
141
int cpyPsToBuf(void *buf)
142
{
113
  // some local variables for efficiency
143
  // some local variables for efficiency
114
  int16 tileHeight = gFr->filterRect.bottom - gFr->filterRect.top;
144
  int16 tileHeight = gFr->filterRect.bottom - gFr->filterRect.top;
115
  int16 tileWidth = gFr->filterRect.right - gFr->filterRect.left;
145
  int16 tileWidth = gFr->filterRect.right - gFr->filterRect.left;
116
  Rect *inRect = &gFr->inRect;
146
  Rect *inRect = &gFr->inRect;
117
  Rect *filterRect = &gFr->filterRect;
147
  Rect *filterRect = &gFr->filterRect;
118
  int16 imageWidth = PHO.imageWidth;
148
  int16 imageWidth = PHO.imageWidth;
119
  int16 imageHeight = PHO.imageHeight;
149
  int16 imageHeight = PHO.imageHeight;
120
  int32 imageDepth = PHO.imageDepth;
150
  int32 imageDepth = PHO.imageDepth;
121
  int32 bytesPerPixel = (imageDepth/8) * 3;
151
  int32 bytesPerPixel = (imageDepth/8) * 3;
122
152
123
  // round up to the nearest horizontal and vertical tile count
153
  // round up to the nearest horizontal and vertical tile count
124
  uint16 tilesVert = (uint16)((tileHeight - 1 + imageHeight) / tileHeight);
154
  uint16 tilesVert = (uint16)((tileHeight - 1 + imageHeight) / tileHeight);
125
  uint16 tilesHoriz = (uint16)((tileWidth - 1 + imageWidth) / tileWidth);
155
  uint16 tilesHoriz = (uint16)((tileWidth - 1 + imageWidth) / tileWidth);
126
156
127
  // Fixed numbers are 16.16 values 
157
  // Fixed numbers are 16.16 values 
128
  // the first 16 bits represent the whole number
158
  // the first 16 bits represent the whole number
129
  // the last 16 bits represent the fraction
159
  // the last 16 bits represent the fraction
130
  gFr->inputRate = (int32)1 << 16;
160
  gFr->inputRate = (int32)1 << 16;
131
  gFr->maskRate = (int32)1 << 16;
161
  gFr->maskRate = (int32)1 << 16;
132
 
162
 
133
  // loop through each tile makeing sure we don't go over the bounds
163
  // loop through each tile makeing sure we don't go over the bounds
134
  // of the imageHeight or imageWidth
164
  // of the imageHeight or imageWidth
135
  for (uint16 vertTile = 0; vertTile < tilesVert; vertTile++)  {
165
  for (uint16 vertTile = 0; vertTile < tilesVert; vertTile++)
166
  {
136
      for (uint16 horizTile = 0; horizTile < tilesHoriz; horizTile++)  {
167
    for (uint16 horizTile = 0; horizTile < tilesHoriz; horizTile++)
168
    {
137
          inRect->top = (int16)(vertTile * tileHeight);
169
      inRect->top = (int16)(vertTile * tileHeight);
138
          inRect->left = (int16)(horizTile * tileWidth);
170
      inRect->left = (int16)(horizTile * tileWidth);
139
          inRect->bottom = (int16)(inRect->top + tileHeight);
171
      inRect->bottom = (int16)(inRect->top + tileHeight);
140
          inRect->right = (int16)(inRect->left + tileWidth);
172
      inRect->right = (int16)(inRect->left + tileWidth);
141
173
142
          if (inRect->bottom > imageHeight)
174
      if (inRect->bottom > imageHeight)
143
              inRect->bottom = imageHeight;
175
        inRect->bottom = imageHeight;
144
          if (inRect->right > imageWidth)
176
      if (inRect->right > imageWidth)
145
              inRect->right = imageWidth;
177
        inRect->right = imageWidth;
146
178
147
          for (uint16 plane = 0; plane < 3; plane++) {
179
      for (uint16 plane = 0; plane < 3; plane++)
180
      {
148
              // we want one plane at a time, small memory foot print is good
181
        // we want one plane at a time, small memory foot print is good
149
              gFr->inLoPlane = plane;
182
        gFr->inLoPlane = plane;
150
              gFr->inHiPlane = plane;
183
        gFr->inHiPlane = plane;
151
  
184
  
152
              // update the gFr with our latest request
185
        // update the gFr with our latest request
153
              *gResult = gFr->advanceState();
186
        *gResult = gFr->advanceState();
154
              if (*gResult != noErr) 
187
        if (*gResult != noErr) 
188
        {
155
                  return display("cpyPsToBuf: advanceState");
189
          return display("cpyPsToBuf: advanceState");
190
        }
156
191
157
              // copy one channel at a time
192
        // copy one channel at a time
158
              if (imageDepth == 8) {
193
        if (imageDepth == 8)
194
        {
159
                  uint8* pixel = (uint8*)gFr->inData;
195
          uint8* pixel = (uint8*)gFr->inData;
160
                  uint16 rectHeight = (uint16)(inRect->bottom - inRect->top);
196
          uint16 rectHeight = (uint16)(inRect->bottom - inRect->top);
161
                  uint16 rectWidth = (uint16)(inRect->right - inRect->left);
197
          uint16 rectWidth = (uint16)(inRect->right - inRect->left);
162
198
163
                  for(uint16 pixelY = 0; pixelY < rectHeight; pixelY++) {
199
          for(uint16 pixelY = 0; pixelY < rectHeight; pixelY++)
200
          {
164
                      Ptr dissolve = (char *)buf + bytesPerPixel * 
201
            Ptr dissolve = (char *)buf + bytesPerPixel * 
165
                          (imageWidth * (inRect->top + pixelY) + inRect->left) + plane;
202
              (imageWidth * (inRect->top + pixelY) + inRect->left) + plane;
166
                      for(uint16 pixelX = 0; pixelX < rectWidth; pixelX++) {
203
            for(uint16 pixelX = 0; pixelX < rectWidth; pixelX++)
167
                          *dissolve = *pixel++;
204
            {
168
                          dissolve += bytesPerPixel;
205
              *dissolve = *pixel++;
169
                      }
206
              dissolve += bytesPerPixel;
207
            }
170
                      pixel += gFr->inRowBytes - rectWidth;
208
            pixel += gFr->inRowBytes - rectWidth;
171
                  }
209
          }//pixelY
172
              } else {
210
        }//8 bit
173
                  // assume 16-bit
211
        else
212
        {
213
          // assume 16-bit
174
                  uint16* pixel = (uint16*)gFr->inData;
214
          uint16* pixel = (uint16*)gFr->inData;
175
                  uint16 rectHeight = (uint16)(inRect->bottom - inRect->top);
215
          uint16 rectHeight = (uint16)(inRect->bottom - inRect->top);
176
                  uint16 rectWidth = (uint16)(inRect->right - inRect->left);
216
          uint16 rectWidth = (uint16)(inRect->right - inRect->left);
177
                  int bytesPerPixel2 = bytesPerPixel >> 1;
217
          int bytesPerPixel2 = bytesPerPixel >> 1;
178
218
179
                  for(uint16 pixelY = 0; pixelY < rectHeight; pixelY++) {
219
          for(uint16 pixelY = 0; pixelY < rectHeight; pixelY++)
220
          {
180
                      uint16 *dissolve = (uint16 *)((char *)buf + 
221
            uint16 *dissolve = (uint16 *)((char *)buf + 
181
                          bytesPerPixel * 
222
              bytesPerPixel * 
182
                          (imageWidth * (inRect->top + pixelY) + inRect->left)
223
              (imageWidth * (inRect->top + pixelY) + inRect->left)
183
                          + 2*plane);
224
              + 2*plane);
184
                      for(uint16 pixelX = 0; pixelX < rectWidth; pixelX++) {
225
            for(uint16 pixelX = 0; pixelX < rectWidth; pixelX++)
226
            {
185
                          // convert 0..32768 to 0..65535
227
              // convert 0..32768 to 0..65535
186
                          *dissolve = (*pixel++ * 65535U + 16384U) >> 15;
228
              *dissolve = (*pixel++ * 65535U + 16384U) >> 15;
187
                          dissolve += bytesPerPixel2;
229
              dissolve += bytesPerPixel2;
188
                      }
230
            }
189
                      pixel += (gFr->inRowBytes - 2*rectWidth) >> 1;
231
            pixel += (gFr->inRowBytes - 2*rectWidth) >> 1;
190
                  }               
232
          }//pixelY
191
              }
233
        }//16 bit
192
          }
234
      }//plane
235
    }//horizTile
236
  }//vertTile
193
237
194
      }
195
  }
196
197
  return 0;
238
  return 0;
198
}
239
}