proog128 / jswin

Loading dynamic link libraries in JavaScript
Other
47 stars 11 forks source link

Any chance of a GDI+ example? #1

Closed PAEz closed 11 years ago

PAEz commented 11 years ago

I really did try, but this sorta stuff is beyond me. The only time Ive used the GDI+ stuff before is in AutoIt and PowerBasic, both had all this work done for me. I tried looking for docs, but I dont seem to be able to get much on some gdi+ stuff...like GdipLoadImageFromFile, all I could find was http://msdn.microsoft.com/en-us/library/windows/desktop/ms534041(v=vs.85).aspx ... real helpful....not! Here's my lame attempt....

// GDI+
var gdip = loadLibrary("GDIPlus.dll");
var gdipStartup = gdip.getProc("GdiplusStartup", "ssi", "stdcall");  // http://msdn.microsoft.com/en-us/library/windows/desktop/ms534077(v=vs.85).aspx
var gdipShutdown = gdip.getProc("GdiplusShutdown", "i", "stdcall");  // http://msdn.microsoft.com/en-us/library/windows/desktop/ms534076(v=vs.85).aspx
// Where the hell do I get docs on these???  The MSDN says these are flat functions and only doc the wrappers and says these shouldnt be called directly....gee, thanks
var gdipImageLoadFromFile = gdip.getProc("GdipLoadImageFromFile", "cs", "stdcall");
var gdipImageDispose = gdip.getProc("GdipDisposeImage", "i", "stdcall");

var gdipToken = new DataView(new ArrayBuffer(4));
var gdipTokenPTR = getBaseAddress(gdipToken.buffer);

var gdipStartupInput = new DataView(new ArrayBuffer(16));
//  http://msdn.microsoft.com/en-us/library/windows/desktop/ms534067(v=vs.85).aspx
//  UINT32         GdiplusVersion;
//  DebugEventProc DebugEventCallback;
//  BOOL           SuppressBackgroundThread;
//  BOOL           SuppressExternalCodecs;
gdipStartupInput.setUint32(0, 1,                         true);   // GdiplusVersion
gdipStartupInput.setUint32(4, 0,                         true);   // DebugEventCallback
gdipStartupInput.setUint32(8, 0,                         true);   // SuppressBackgroundThread
gdipStartupInput.setUint32(12, 0,                         true);   // SuppressExternalCodecs

var gdipStartupInputPTR = getBaseAddress(gdipStartupInput.buffer);

// http://msdn.microsoft.com/en-us/library/windows/desktop/ms534175(v=vs.85).aspx
var gdipStatus=[
"OK",
"GenericError",
"InvalidParameter",
"OutOfMemory",
"ObjectBusy",
"InsufficientBuffer",
"NotImplemented",
"Win32Error",
"WrongState",
"Aborted",
"FileNotFound",
"ValueOverflow",
"AccessDenied",
"UnknownImageFormat",
"FontFamilyNotFound",
"FontStyleNotFound",
"NotTrueTypeFont",
"UnsupportedGdiplusVersion",
"GdiplusNotInitialized",
"PropertyNotFound",
"PropertyNotFound",
"ProfileNotFound"
];

var status = gdipStartup(gdipToken.buffer, gdipStartupInput.buffer, 0);
if(status != 0) {
    MessageBox(0, "gdipStartup failed - "+gdipStatus[status], "Error", MB_OK | MB_ICONEXCLAMATION);
    exit(-1);
}

// Load Image.....i wish
var imageHandle = new DataView(new ArrayBuffer(4));

status = gdipImageLoadFromFile("C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg",imageHandle.buffer);
var handle=imageHandle.getUint32(0); // Think that might actually be Unit16......oh to have a handle!

if(status >= 0) {  // Do these use the same status codes as the above?...if so, what do you mean out of memory??????
    MessageBox(0, "gdipImageLoadFromFile!! error code="+status+" handle="+handle, "Error", MB_OK | MB_ICONEXCLAMATION);
    gdipShutdown(gdipTokenPTR);
    exit(-1);
}

MessageBox(0, "gdipImageLoadFromFile = "+imageHandle.getUint32(0), "Error", MB_OK | MB_ICONEXCLAMATION);

// Dispose Image
var result = gdipImageDispose(imageHandle.getUint32(0));
MessageBox(0, "gdipImageDispose = "+result, "Error", MB_OK | MB_ICONEXCLAMATION);

gdipShutdown(gdipTokenPTR);  // great....this returns nothing, so how do I know if I did the token stuff right?!?!?

exit(0);

....plus some of your code above it, to declare what you declared. Playing with images in this could be fun, IF I could get this to work ;)

Oh, and very cool what you've done;

proog128 commented 11 years ago

I have never used the GDI+ Flat API before, but it looks very interesting. I have created a small GDI+ example based on your code that displays an image in a window. Besides the docs in MSDN, I looked up the function signatures in the C header files of the Windows SDK. As the GDI+ functions only seem to support wide character strings, I have added automatic conversion of JS strings to wide character strings.

PAEz commented 11 years ago

Thank You!
Im going to have so much fun with this :P

I wondered if it was the string format thing, but like I said I'm no good at this stuff.....cant do C at all for instance. Talking of which, if you didn't supply compiled bins on your blog I wouldn't have been able to play with this (thank god I went to your blog to check out your motion blur demo, cool), you should put a link to that article, or maybe put the bins on github.

Thanks again, Im sure I can convert a bunch more of the header stuff now and have some fun!