/*
* -----
* Screenshot Sender - __gdip.js
* -----
* Gdip functions for Screenshot Sender
* -----
*/
var Gdip = function (bSaveImage) {
this.GdipToken = Interop.Allocate(4);
this.GdipStartupInput = Interop.Allocate(16)
this.GdipStartupInput.WriteDWORD(0, 1);
this.Initialized = false, this.ImageWidth, this.ImageHeight;
this.SaveSuccessful = false, this.ImageLocation, this.hBitmap = 0;
this.ImageFormats = {'BMP' : '{557CF400-1A04-11D3-9A73-0000F81EF32E}',
'JPG' : '{557CF401-1A04-11D3-9A73-0000F81EF32E}',
'GIF' : '{557CF402-1A04-11D3-9A73-0000F81EF32E}',
'PNG' : '{557CF406-1A04-11D3-9A73-0000F81EF32E}' }
this.objImageQuality = {'DEFAULT_QUALITY' : 0,
'DRAFT_QUALITY' : 1,
'PROOF_QUALITY' : 2,
'NONANTIALIASED_QUALITY' : 3,
'ANTIALIASED_QUALITY' : 4,
'CLEARTYPE_QUALITY' : 5};
this.bSaveImage = bSaveImage;
}
Gdip.prototype = {
"Initialize" : function () { return this.Initialized = Interop.Call('gdiplus', 'GdiplusStartup', this.GdipToken, this.GdipStartupInput, 0) === 0; },
"Uninitialize" : function () { Interop.Call('gdiplus', 'GdiplusShutdown', this.GdipToken.ReadDWORD(0) ); },
"SaveImage" : function (hPicture, sLocation, JpgQuality) {
if (typeof JpgQuality === 'undefined') JpgQuality = 75;
if (typeof CreateThumbnail === 'undefined') CreateThumbnail = false;
if (typeof hPicture === 'undefined' || typeof(sLocation) === 'undefined') return false;
if (this.Initialized == false) return false;
var ImageFormat = Interop.Allocate(16);
var lBitmap = Interop.Allocate(4);
Interop.Call('ole32', 'CLSIDFromString', this.ImageFormats[aImageFormats[objPreferences['cFileType']]], ImageFormat);
if (Interop.Call('gdiplus', 'GdipCreateBitmapFromHBITMAP', hPicture, 0, lBitmap) === 0) {
objPictures[sLocation.substr(sLocation.lastIndexOf('\\') +1) ] = {};
objPictures[sLocation.substr(sLocation.lastIndexOf('\\') +1) ].Picture = hPicture;
objPictures[sLocation.substr(sLocation.lastIndexOf('\\') +1) ].Quality = JpgQuality;
this.hBitmap = hPicture;
var EncoderParameters = Interop.Allocate(32);
with (EncoderParameters) {
WriteDWORD(0, 1);
WriteDWORD(20, 1);
WriteDWORD(24, 4);
if (objPreferences['cFileType'] === 1) {
var Ptr = Interop.Allocate(4);
Ptr.WriteDWORD(0, JpgQuality /* JPG QUALITY */);
WriteDWORD(28, Ptr.DataPtr);
}else{
WriteDWORD(28, 0);
}
}
Interop.Call('ole32', 'CLSIDFromString', '{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}', EncoderParameters.DataPtr+4);
if (Interop.Call('gdiplus', 'GdipSaveImageToFile', lBitmap.ReadDWORD(0), sLocation, ImageFormat, EncoderParameters) === 0) {
this.SaveSuccessful = true;
this.ImageLocation = sLocation;
if (typeof objWindows['ScreenshotViewer'] === 'object') {
RefreshScreenshotViewerList();
}
}
}
this.DisposeImage(lBitmap.ReadDWORD(0) );
if (objPreferences['cPreviews'] == false) {
Interop.Call('gdi32', 'DeleteObject', hPicture);
}
},
"CreateThumbnail" : function (hPicture) {
var ImageThumb = Interop.Allocate(4);
var lBitmap = Interop.Allocate(4);
var hbmBitmap = Interop.Allocate(4);
Interop.Call('gdiplus', 'GdipCreateBitmapFromHBITMAP', hPicture, 0, lBitmap)
Interop.Call('gdiplus', 'GdipGetImageThumbnail', lBitmap.ReadDWORD(0), 100, this.ImageHeight / (this.ImageWidth/100), ImageThumb.DataPtr, 0, 0);
Interop.Call('gdiplus', 'GdipCreateHBITMAPFromBitmap', ImageThumb.ReadDWORD(0), hbmBitmap, 0x00000000);
return hbmBitmap.ReadDWORD(0);
},
"UpdateQuality" : function (hBitmap, sImage, JpgQuality) {
var EncoderParameters = Interop.Allocate(32);
var ImageFormat = Interop.Allocate(16);
var lBitmap = Interop.Allocate(4);
Interop.Call('ole32', 'CLSIDFromString', '{557CF401-1A04-11D3-9A73-0000F81EF32E}', ImageFormat);
if (Interop.Call('gdiplus', 'GdipCreateBitmapFromHBITMAP', hBitmap, 0, lBitmap) === 0) {
with (EncoderParameters) {
WriteDWORD(0, 1);
WriteDWORD(20, 1);
WriteDWORD(24, 4);
var Ptr = Interop.Allocate(4);
Ptr.WriteDWORD(0, JpgQuality);
WriteDWORD(28, Ptr.DataPtr);
}
Interop.Call('ole32', 'CLSIDFromString', '{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}', EncoderParameters.DataPtr+4);
var ret = Interop.Call('gdiplus', 'GdipSaveImageToFile', lBitmap.ReadDWORD(0), sImage, ImageFormat, EncoderParameters);
this.DisposeImage(lBitmap.ReadDWORD(0) );
return ret === 0;
}
},
"ConvertImage" : function (sImageIn, sImageOut, nImgFrmt, JpgQuality) {
var ret, hImage = this.LoadImageFromFile(sImageIn);
if (hImage !== 0) {
var ImageFormat = Interop.Allocate(16);
Interop.Call('ole32', 'CLSIDFromString', this.ImageFormats[nImgFrmt], ImageFormat);
var EncoderParameters = Interop.Allocate(32);
with (EncoderParameters) {
WriteDWORD(0, 1);
WriteDWORD(20, 1);
WriteDWORD(24, 4);
if (nImgFrmt.toLowerCase() === 'jpg') {
var Ptr = Interop.Allocate(4);
Ptr.WriteDWORD(0, JpgQuality /* JPG QUALITY */);
WriteDWORD(28, Ptr.DataPtr);
}else { WriteDWORD(28, 0); }
}
Interop.Call('ole32', 'CLSIDFromString', '{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}', EncoderParameters.DataPtr+4);
ret = Interop.Call('gdiplus', 'GdipSaveImageToFile', hImage, sImageOut, ImageFormat, EncoderParameters);
this.DisposeImage(hImage);
}
return ret === 0;
},
"CaptureWindow" : function (hWnd, rect, bIsFullscreen, bIsSelectedArea, bIsPointClickCapture, bIsThumbRequired) {
if (typeof bIsPointClickCapture === 'undefined') bIsPointClickCapture = false;
if (typeof bIsThumbRequired === 'undefined') bIsThumbRequired = false;
if (typeof bIsSelectedArea === 'undefined') bIsSelectedArea = false;
var sDC = _win32.GetWindowDC(hWnd);
this.ImageWidth = rect.ReadDWORD(8) - rect.ReadDWORD(0);
this.ImageHeight = rect.ReadDWORD(12) - rect.ReadDWORD(4);
var destDC = Interop.Call('gdi32', 'CreateCompatibleDC', sDC),
hBMP = Interop.Call('gdi32', 'CreateCompatibleBitmap', sDC, this.ImageWidth, this.ImageHeight),
hobj = Interop.Call('gdi32', 'SelectObject', destDC, hBMP),
SystemArea = GetSystemArea();
if (bIsSelectedArea) {
// Selected area - Position of rectangle on screen
rect.WriteDWORD(0, rect.ReadDWORD(0) + SystemArea.Left);
rect.WriteDWORD(4, rect.ReadDWORD(4) + SystemArea.Top);
} else if (bIsFullscreen) {
// Full screen - Top left corner of screen
rect.WriteDWORD(0, SystemArea.Left);
rect.WriteDWORD(4, SystemArea.Top);
} else {
// Window - Top left corner of window
rect.WriteDWORD(0, 0);
rect.WriteDWORD(4, 0);
}
Interop.Call('gdi32', 'BitBlt', destDC, 0, 0, this.ImageWidth, this.ImageHeight, sDC, rect.ReadDWORD(0), rect.ReadDWORD(4), 0x40000000 /* CAPTUREBLT */ | 0xCC0020 /* SRCCOPY */);
var oOverlayText = new OverlayText(destDC, this.bSaveImage, this.ImageWidth, this.ImageHeight);
Interop.Call('gdi32', 'SelectObject', destDC, hobj);
_win32.ReleaseDC(hWnd, destDC);
return hBMP;
},
"CaptureWindow_Active" : function () {
var hWnd = _win32.GetForegroundWindow(), rect = Interop.Allocate(16);
_win32.GetWindowRect(hWnd, rect);
var bIsSelectedArea = false;
/*_win32.GetClassNameW(hWnd, lpsz, 512);
//Check if this is a chat window
if (lpsz.ReadString(0) === 'IMWindowClass' || lpsz.ReadString(0) === 'MSBLWindowClass') {
if (_win32.WindowFromPoint(rect.ReadDWORD(0) +10, rect.ReadDWORD(4) +10) !== hWnd) {
//Use the DirectUIHWND instead
hWnd = _win32.FindWindowExW(hWnd, 0, 'DirectUIHWND', '');
_win32.GetWindowRect(hWnd, rect);
// Treat this window as a selected area
bIsSelectedArea = true;
}
}*/
return this.CaptureWindow(hWnd, rect, false, bIsSelectedArea);
},
"CaptureWindow_Fullscreen" : function () {
var rect = Interop.Allocate(16), SystemArea = GetSystemArea();
rect.WriteDWORD(0, SystemArea.Top);
rect.WriteDWORD(4, SystemArea.Left);
rect.WriteDWORD(8, SystemArea.Width);
rect.WriteDWORD(12, SystemArea.Height);
return this.CaptureWindow(_win32.GetDesktopWindow(), rect, true);
},
"CaptureWindow_Clipboard" : function () {
var nImgPtr = 0;
try {
_win32.OpenClipboard(0);
nImgPtr = _win32.GetClipboardData(_win32._const._CF_BITMAP);
} catch(e) {
_debug.error(e, 'CaptureWindow_Clipboard');
} finally {
_win32.CloseClipboard();
}
return nImgPtr;
},
"LoadImageFromFile" : function (sImage) {
var nImgPtr = Interop.Allocate(4);
Interop.Call('gdiplus', 'GdipLoadImageFromFile', sImage, nImgPtr);
return nImgPtr.ReadDWORD(0) !== 0 ? nImgPtr.ReadDWORD(0) : false;
},
"DisposeImage" : function (hImage) { Interop.Call('gdiplus', 'GdipDisposeImage', hImage); },
"GdiCreateHBITMAPFromFile" : function (sImage) {
if (this.Initialized == false) return false;
var lngBitmap = Interop.Allocate(4), hbm = false;
var lGdipCreateBitmapFromFile = Interop.Call('gdiplus', 'GdipCreateBitmapFromFile', sImage, lngBitmap);
if (lGdipCreateBitmapFromFile == 0) {
var hbmBitmap = Interop.Allocate(4);
var lGdipCreateHBITMAPFromBitmap = Interop.Call('gdiplus', 'GdipCreateHBITMAPFromBitmap', lngBitmap.ReadDWORD(0), hbmBitmap, 0x00000000);
if (lGdipCreateHBITMAPFromBitmap === 0) hbm = hbmBitmap.ReadDWORD(0);
}
this.DisposeImage(lngBitmap.ReadDWORD(0) );
return hbm;
},
"Clipboard_SetData" : function (hBitmap, hWnd) {
if (typeof hWnd === 'undefined') hWnd = 0;
try {
_win32.OpenClipboard(hWnd);
_win32.EmptyClipboard();
_debug.trace(_win32.SetClipboardData(_win32._const._CF_BITMAP, hBitmap) );
} catch(e) {
_debug.error(e, 'Clipboard_SetData');
} finally {
_win32.CloseClipboard();
}
},
"GetImageDimensions" : function (sImageLocation) {
var _width = Interop.Allocate(4),
_height = Interop.Allocate(4),
hImage = this.LoadImageFromFile(sImageLocation);
if (hImage !== 0) {
Interop.Call('gdiplus', 'GdipGetImageWidth', hImage, _width);
Interop.Call('gdiplus', 'GdipGetImageHeight', hImage, _height);
this.DisposeImage(hImage);
}
return _width.ReadDWORD(0)+' × '+_height.ReadDWORD(0);
},
"GetDateCreated" : function (sImageLocation) {
var BY_HANDLE_FILE_INFORMATION = Interop.Allocate(52);
var _h_file = _win32.CreateFileW(sImageLocation, _win32._const._GENERIC_READ, _win32._const._FILE_SHARE_READ, 0, _win32._const._OPEN_EXISTING, 0, 0);
_win32.GetFileInformationByHandle(_h_file, BY_HANDLE_FILE_INFORMATION);
_win32.CloseHandle(_h_file);
var FILETIME = Interop.Allocate(8),
FILETIMELOCALTIME = Interop.Allocate(8);
FILETIME.WriteDWORD(0, BY_HANDLE_FILE_INFORMATION.ReadDWORD(4) );
FILETIME.WriteDWORD(4, BY_HANDLE_FILE_INFORMATION.ReadDWORD(8) );
Interop.Call('kernel32', 'FileTimeToLocalFileTime', FILETIME, FILETIMELOCALTIME);
var SYSTEMTIME = Interop.Allocate(16);
Interop.Call('kernel32', 'FileTimeToSystemTime', FILETIMELOCALTIME, SYSTEMTIME);
var sTime = Interop.Allocate(512),
sDate = Interop.Allocate(512);
Interop.Call('kernel32', 'GetTimeFormatW', 0x400 /* LOCALE_USER_DEFAULT */, 0, SYSTEMTIME, 0, sTime, 512);
Interop.Call('kernel32', 'GetDateFormatW', 0x400 /* LOCALE_USER_DEFAULT */, 0, SYSTEMTIME, 0, sDate, 512);
return sDate.ReadString(0)+' '+sTime.ReadString(0);
},
"GetImageSizeInBytes" : function (sImageLocation) {
var BY_HANDLE_FILE_INFORMATION = Interop.Allocate(52);
var _h_file = _win32.CreateFileW(sImageLocation, _win32._const._GENERIC_READ, _win32._const._FILE_SHARE_READ, 0, _win32._const._OPEN_EXISTING, 0, 0);
_win32.GetFileInformationByHandle(_h_file, BY_HANDLE_FILE_INFORMATION);
_win32.CloseHandle(_h_file);
return BY_HANDLE_FILE_INFORMATION.ReadDWORD(36); /* size of file */
},
"FormatFileSize" : (function() {
var dwKB = 0x400,
dwMB = 0x400 * dwKB,
dwGB = 0x400 * dwMB;
return function (_lsize) {
if (_lsize >= dwGB) return Math.round(_lsize/dwGB) +' GB';
else if (_lsize >= dwMB) return Math.round(_lsize/dwMB) +' MB';
else if (_lsize >= dwKB) return Math.round(_lsize/dwKB) +' KB';
else if (_lsize > 0 && _lsize < 0x400) return Math.round(_lsize) +' B';
else return '0 KB';
}
})()
};