changeset 805:56a813a4714d

drawing dicomizer sample
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 07 May 2014 13:23:08 +0200
parents a017d1a89b4f
children 557575fd93e9
files Resources/Samples/WebApplications/DrawingDicomizer.js Resources/Samples/WebApplications/DrawingDicomizer/drawing.js Resources/Samples/WebApplications/DrawingDicomizer/index.html Resources/Samples/WebApplications/DrawingDicomizer/orthanc.js Resources/Samples/WebApplications/NodeToolbox.js
diffstat 5 files changed, 384 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/Samples/WebApplications/DrawingDicomizer.js	Wed May 07 13:23:08 2014 +0200
@@ -0,0 +1,106 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
+ * Belgium
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ **/
+
+
+
+/**
+ * Parameters of the HTTP server.
+ **/
+
+var orthanc = { 
+  host: 'localhost',
+  port: 8042 
+};
+
+var port = 8000;
+
+
+
+/**
+ * The Web application.
+ **/
+
+var http = require('http');
+var querystring = require('querystring');
+var toolbox = require('./NodeToolbox.js');
+
+var server = http.createServer(function(req, response) {
+  switch (req.method)
+  {
+  case 'GET':
+    {
+      if (req.url == '/') {
+        toolbox.Redirect('/index.html', response);
+      }
+      else if (req.url == '/index.html') {
+        toolbox.ServeFile('DrawingDicomizer/index.html', response);
+      }
+      else if (req.url == '/drawing.js') {
+        toolbox.ServeFile('DrawingDicomizer/drawing.js', response);
+      }
+      else if (req.url == '/orthanc.js') {
+        toolbox.ServeFile('DrawingDicomizer/orthanc.js', response);
+      }
+      else if (req.url == '/jquery.js') {
+        toolbox.ServeFile('../../../OrthancExplorer/libs/jquery-1.7.2.min.js', response);
+      }
+      else if (req.url.startsWith('/orthanc')) {
+        toolbox.ForwardGetRequest(orthanc, req.url.substr(8), response);
+      }
+      else {
+        toolbox.NotFound(response);
+      }
+
+      break;
+    }
+
+  case 'POST':
+    {
+      var body = '';
+
+      req.on('data', function (data) {
+        body += data;
+      });
+
+      req.on('end', function () {
+        if (req.url == '/orthanc/tools/create-dicom') {
+          body = JSON.stringify(querystring.parse(body));
+          toolbox.ForwardPostRequest(orthanc, '/tools/create-dicom', body, response);
+        }
+        else {
+          toolbox.NotFound(response);
+        }
+      });
+
+      break;
+    }
+
+  default:
+    toolbox.NotFound(response);
+  }
+});
+
+server.listen(port);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/Samples/WebApplications/DrawingDicomizer/drawing.js	Wed May 07 13:23:08 2014 +0200
@@ -0,0 +1,100 @@
+/**
+ * Copyright 2010 William Malone (www.williammalone.com)
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ **/
+
+
+/**
+ * This code comes from the blog entry "Create a Drawing App with
+ * HTML5 Canvas and JavaScript" by William Malone. It is the "simple
+ * demo" of a pure HTML5 drawing application.
+ *
+ * http://www.williammalone.com/articles/create-html5-canvas-javascript-drawing-app/
+ *
+ * To keep this sample code as simple as possible, we do not implement
+ * hacks for the canvas of Microsoft Internet Explorer.
+ **/
+
+
+if ($.browser.msie) {
+  alert('Please use Mozilla Firefox or Google Chrome. Microsoft Internet Explorer is not supported.');
+}
+
+
+var context;
+var clickX = new Array();
+var clickY = new Array();
+var clickDrag = new Array();
+var paint;
+
+
+function addClick(x, y, dragging)
+{
+  clickX.push(x);
+  clickY.push(y);
+  clickDrag.push(dragging);
+}
+
+
+function Redraw() 
+{
+  context.fillStyle = '#ffffff';
+  context.fillRect(0, 0, context.canvas.width, context.canvas.height); // Clears the canvas
+  
+  context.strokeStyle = '#df4b26';
+  context.lineJoin = 'round';
+  context.lineWidth = 5;
+  
+  for (var i=0; i < clickX.length; i++) {		
+    context.beginPath();
+    if (clickDrag[i] && i) {
+      context.moveTo(clickX[i - 1], clickY[i - 1]);
+    } else {
+      context.moveTo(clickX[i] - 1, clickY[i]);
+    }
+    context.lineTo(clickX[i], clickY[i]);
+    context.closePath();
+    context.stroke();
+  }
+}
+
+
+$(document).ready(function() {
+  context = document.getElementById('canvas').getContext('2d');
+  Redraw();
+
+  $('#canvas').mousedown(function(e) {
+    var mouseX = e.pageX - this.offsetLeft;
+    var mouseY = e.pageY - this.offsetTop;
+    
+    paint = true;
+    addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
+    Redraw();
+  });
+
+  $('#canvas').mousemove(function(e) {
+    if(paint) {
+      addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
+      Redraw();
+    }
+  });
+
+  $('#canvas').mouseup(function(e) {
+    paint = false;
+  });
+
+  $('#canvas').mouseleave(function(e) {
+    paint = false;
+  });
+});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/Samples/WebApplications/DrawingDicomizer/index.html	Wed May 07 13:23:08 2014 +0200
@@ -0,0 +1,24 @@
+<!doctype html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8">
+    <title>HTML5 Drawing Dicomizer</title>
+    <style media="screen" type="text/css">
+      canvas {
+        border: 1px inset brown;
+      }
+    </style>
+    <script src="jquery.js"></script>
+    <script src="drawing.js"></script>
+    <script src="orthanc.js"></script>
+  </head>
+  <body>
+    <canvas id="canvas" width="490" height="220"></canvas>
+    <p>
+      Patient Name: <input type="text" id="patientName"></input>
+    </p>
+    <p>
+      <button id="submit">Submit</button>
+    </p>
+  </body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/Samples/WebApplications/DrawingDicomizer/orthanc.js	Wed May 07 13:23:08 2014 +0200
@@ -0,0 +1,47 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
+ * Belgium
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ **/
+
+
+$(document).ready(function() {
+  $('#submit').click(function(event) {
+    var png = context.canvas.toDataURL();
+
+    $.ajax({
+      type: 'POST',
+      url: '/orthanc/tools/create-dicom',
+      data: { 
+        PatientName: $('#patientName').val(),
+        PixelData: png,
+        Modality: 'RX' 
+      }
+    })
+      .success(function( msg ) {
+        alert('The image has been dicomized! ' + msg);
+      });
+
+    return false;
+  });
+});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/Samples/WebApplications/NodeToolbox.js	Wed May 07 13:23:08 2014 +0200
@@ -0,0 +1,107 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
+ * Belgium
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ **/
+
+
+var fs = require('fs');
+var http = require('http');
+
+
+function ForwardGetRequest(orthanc, path, res) {
+  var opts = orthanc;
+  opts.path = path;
+  opts.method = 'GET';
+
+  http.get(opts, function(response) {
+    response.setEncoding('utf-8');
+    response.on('data', function(chunk) {
+      res.write(chunk);
+    });
+    response.on('end', function() {
+      res.end();
+    });
+  }).on('error', function(e) {
+    console.log('Got error on GET forwarding: ' + e.message + ' (' + path + ')');
+  });
+}
+
+
+function ForwardPostRequest(orthanc, path, body, res) {
+  var opts = orthanc;
+  opts.path = path;
+  opts.method = 'POST';
+  opts.headers = {
+    'Content-Length': body.length
+  }
+
+  var req = http.request(opts, function(response) {
+    response.setEncoding('utf-8');
+    response.on('data', function(chunk) {
+      res.write(chunk);
+    });
+    response.on('end', function() {
+      res.end();
+    });
+  }).on('error', function(e) {
+    console.log('Got error on POST forwarding: ' + e.message + ' (' + path + ')');
+  });
+
+  req.write(body);
+  req.end();
+}
+
+
+function ServeFile(filename, res) {
+  fs.readFile(filename, function(r, c) {
+    res.end(c.toString());
+  });
+}
+
+
+function NotFound(res) {
+  res.writeHead(404, {'Content-Type': 'text/plain'});
+  res.end();
+}
+
+
+function Redirect(path, res) {
+  res.writeHead(301, {
+    'Content-Type': 'text/plain',
+    'Location': path
+  });
+  res.end();
+}
+
+
+String.prototype.startsWith = function(prefix) {
+  return this.indexOf(prefix) === 0;
+}
+
+
+module.exports.ForwardGetRequest = ForwardGetRequest;
+module.exports.ForwardPostRequest = ForwardPostRequest;
+module.exports.NotFound = NotFound;
+module.exports.Redirect = Redirect;
+module.exports.ServeFile = ServeFile;