backup tgids master
[scannr.git] / js / flotr2 / lib / canvas2image.js
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
/*
 * Canvas2Image v0.1
 * Copyright (c) 2008 Jacob Seidelin, cupboy@gmail.com
 * MIT License [http://www.opensource.org/licenses/mit-license.php]
 */
 
var Canvas2Image = (function() {
  // check if we have canvas support
  var oCanvas = document.createElement("canvas"),
      sc = String.fromCharCode,
      strDownloadMime = "image/octet-stream",
      bReplaceDownloadMime = false;
  
  // no canvas, bail out.
  if (!oCanvas.getContext) {
    return {
      saveAsBMP : function(){},
      saveAsPNG : function(){},
      saveAsJPEG : function(){}
    }
  }
 
  var bHasImageData = !!(oCanvas.getContext("2d").getImageData),
      bHasDataURL = !!(oCanvas.toDataURL),
      bHasBase64 = !!(window.btoa);
 
  // ok, we're good
  var readCanvasData = function(oCanvas) {
    var iWidth = parseInt(oCanvas.width),
        iHeight = parseInt(oCanvas.height);
    return oCanvas.getContext("2d").getImageData(0,0,iWidth,iHeight);
  }
 
  // base64 encodes either a string or an array of charcodes
  var encodeData = function(data) {
    var i, aData, strData = "";
    
    if (typeof data == "string") {
      strData = data;
    } else {
      aData = data;
      for (i = 0; i < aData.length; i++) {
        strData += sc(aData[i]);
      }
    }
    return btoa(strData);
  }
 
  // creates a base64 encoded string containing BMP data takes an imagedata object as argument
  var createBMP = function(oData) {
    var strHeader = '',
        iWidth = oData.width,
        iHeight = oData.height;
 
    strHeader += 'BM';
  
    var iFileSize = iWidth*iHeight*4 + 54; // total header size = 54 bytes
    strHeader += sc(iFileSize % 256); iFileSize = Math.floor(iFileSize / 256);
    strHeader += sc(iFileSize % 256); iFileSize = Math.floor(iFileSize / 256);
    strHeader += sc(iFileSize % 256); iFileSize = Math.floor(iFileSize / 256);
    strHeader += sc(iFileSize % 256);
 
    strHeader += sc(0, 0, 0, 0, 54, 0, 0, 0); // data offset
    strHeader += sc(40, 0, 0, 0); // info header size
 
    var iImageWidth = iWidth;
    strHeader += sc(iImageWidth % 256); iImageWidth = Math.floor(iImageWidth / 256);
    strHeader += sc(iImageWidth % 256); iImageWidth = Math.floor(iImageWidth / 256);
    strHeader += sc(iImageWidth % 256); iImageWidth = Math.floor(iImageWidth / 256);
    strHeader += sc(iImageWidth % 256);
  
    var iImageHeight = iHeight;
    strHeader += sc(iImageHeight % 256); iImageHeight = Math.floor(iImageHeight / 256);
    strHeader += sc(iImageHeight % 256); iImageHeight = Math.floor(iImageHeight / 256);
    strHeader += sc(iImageHeight % 256); iImageHeight = Math.floor(iImageHeight / 256);
    strHeader += sc(iImageHeight % 256);
  
    strHeader += sc(1, 0, 32, 0); // num of planes & num of bits per pixel
    strHeader += sc(0, 0, 0, 0); // compression = none
  
    var iDataSize = iWidth*iHeight*4; 
    strHeader += sc(iDataSize % 256); iDataSize = Math.floor(iDataSize / 256);
    strHeader += sc(iDataSize % 256); iDataSize = Math.floor(iDataSize / 256);
    strHeader += sc(iDataSize % 256); iDataSize = Math.floor(iDataSize / 256);
    strHeader += sc(iDataSize % 256); 
  
    strHeader += sc(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); // these bytes are not used
  
    var aImgData = oData.data,
        strPixelData = "",
        c, x, y = iHeight,
        iOffsetX, iOffsetY, strPixelRow;
    
    do {
      iOffsetY = iWidth*(y-1)*4;
      strPixelRow = "";
      for (x = 0; x < iWidth; x++) {
        iOffsetX = 4*x;
        strPixelRow += sc(
          aImgData[iOffsetY + iOffsetX + 2], // B
          aImgData[iOffsetY + iOffsetX + 1], // G
          aImgData[iOffsetY + iOffsetX],     // R
          aImgData[iOffsetY + iOffsetX + 3]  // A
        );
      }
      strPixelData += strPixelRow;
    } while (--y);
 
    return encodeData(strHeader + strPixelData);
  }
 
  // sends the generated file to the client
  var saveFile = function(strData) {
    if (!window.open(strData)) {
      document.location.href = strData;
    }
  }
 
  var makeDataURI = function(strData, strMime) {
    return "data:" + strMime + ";base64," + strData;
  }
 
  // generates a <img> object containing the imagedata
  var makeImageObject = function(strSource) {
    var oImgElement = document.createElement("img");
    oImgElement.src = strSource;
    return oImgElement;
  }
 
  var scaleCanvas = function(oCanvas, iWidth, iHeight) {
    if (iWidth && iHeight) {
      var oSaveCanvas = document.createElement("canvas");
      
      oSaveCanvas.width = iWidth;
      oSaveCanvas.height = iHeight;
      oSaveCanvas.style.width = iWidth+"px";
      oSaveCanvas.style.height = iHeight+"px";
 
      var oSaveCtx = oSaveCanvas.getContext("2d");
 
      oSaveCtx.drawImage(oCanvas, 0, 0, oCanvas.width, oCanvas.height, 0, 0, iWidth, iWidth);
      
      return oSaveCanvas;
    }
    return oCanvas;
  }
 
  return {
    saveAsPNG : function(oCanvas, bReturnImg, iWidth, iHeight) {
      if (!bHasDataURL) return false;
      
      var oScaledCanvas = scaleCanvas(oCanvas, iWidth, iHeight),
          strMime = "image/png",
          strData = oScaledCanvas.toDataURL(strMime);
        
      if (bReturnImg) {
        return makeImageObject(strData);
      } else {
        saveFile(bReplaceDownloadMime ? strData.replace(strMime, strDownloadMime) : strData);
      }
      return true;
    },
 
    saveAsJPEG : function(oCanvas, bReturnImg, iWidth, iHeight) {
      if (!bHasDataURL) return false;
 
      var oScaledCanvas = scaleCanvas(oCanvas, iWidth, iHeight),
          strMime = "image/jpeg",
          strData = oScaledCanvas.toDataURL(strMime);
  
      // check if browser actually supports jpeg by looking for the mime type in the data uri. if not, return false
      if (strData.indexOf(strMime) != 5) return false;
 
      if (bReturnImg) {
        return makeImageObject(strData);
      } else {
        saveFile(bReplaceDownloadMime ? strData.replace(strMime, strDownloadMime) : strData);
      }
      return true;
    },
 
    saveAsBMP : function(oCanvas, bReturnImg, iWidth, iHeight) {
      if (!(bHasDataURL && bHasImageData && bHasBase64)) return false;
 
      var oScaledCanvas = scaleCanvas(oCanvas, iWidth, iHeight),
          strMime = "image/bmp",
          oData = readCanvasData(oScaledCanvas),
          strImgData = createBMP(oData);
        
      if (bReturnImg) {
        return makeImageObject(makeDataURI(strImgData, strMime));
      } else {
        saveFile(makeDataURI(strImgData, strMime));
      }
      return true;
    }
  };
})();