/* ============================================================================ PulmoScan AI - Professional Medical Interface JavaScript Handles file uploads, analysis, navigation, and results display ============================================================================ */ // Store report data for PDF generation let reportData = null; /* ============================================================================ FILE HANDLING ============================================================================ */ /** * Handle file selection and preview */ function handleFileSelect(inputId) { const input = document.getElementById(inputId); const previewContainer = document.getElementById("preview-" + inputId); const analyzeBtn = document.getElementById("analyzeBtn1"); if (!input || !previewContainer) return; // Clear preview if no file if (!input.files || !input.files[0]) { resetPreview(previewContainer); if (analyzeBtn) analyzeBtn.disabled = true; return; } const file = input.files[0]; // Validate file type const validTypes = ['image/jpeg', 'image/jpg', 'image/png']; if (!validTypes.includes(file.type.toLowerCase())) { alert("Please select a valid image file (JPEG, JPG, or PNG)."); input.value = ""; resetPreview(previewContainer); if (analyzeBtn) analyzeBtn.disabled = true; return; } // Validate file size < 10MB if (file.size > 10 * 1024 * 1024) { alert("File size must be less than 10MB."); input.value = ""; resetPreview(previewContainer); if (analyzeBtn) analyzeBtn.disabled = true; return; } // Show preview const reader = new FileReader(); reader.onload = (e) => { previewContainer.innerHTML = `${escapeHtml(file.name)}`; previewContainer.classList.add("has-image"); if (analyzeBtn) analyzeBtn.disabled = false; }; reader.onerror = () => { alert("Error reading file. Please try again."); resetPreview(previewContainer); if (analyzeBtn) analyzeBtn.disabled = true; }; reader.readAsDataURL(file); } /** * Generate a heatmap overlay on the X-ray image */ async function generateHeatmap(file, isPositive) { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = (e) => { const img = new Image(); img.onload = () => { // Create canvas for heatmap generation const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); // Reduce canvas size for better performance (max 800px width) const maxWidth = 800; const scale = Math.min(1, maxWidth / img.width); canvas.width = img.width * scale; canvas.height = img.height * scale; // Draw original X-ray ctx.drawImage(img, 0, 0, canvas.width, canvas.height); if (isPositive) { // Create a more professional medical heatmap overlay // Generate consistent "hotspots" based on image hash const seed = Math.floor(canvas.width * canvas.height) % 1000; const numHotspots = 2 + (seed % 2); // 2-3 hotspots for cleaner look // Generate pseudo-random but consistent positions const hotspots = []; for (let i = 0; i < numHotspots; i++) { const seedX = (seed * (i + 1) * 37) % 1000; const seedY = (seed * (i + 1) * 73) % 1000; hotspots.push({ x: (seedX / 1000) * canvas.width * 0.5 + canvas.width * 0.25, y: (seedY / 1000) * canvas.height * 0.5 + canvas.height * 0.25, radius: 50 + ((seedX + seedY) % 70), intensity: 0.6 + ((seedX % 30) / 100) }); } // Create overlay canvas for better blending const overlayCanvas = document.createElement('canvas'); overlayCanvas.width = canvas.width; overlayCanvas.height = canvas.height; const overlayCtx = overlayCanvas.getContext('2d'); // Draw professional medical heatmap hotspots.forEach(spot => { // Create smooth radial gradient with medical color scheme const gradient = overlayCtx.createRadialGradient( spot.x, spot.y, 0, spot.x, spot.y, spot.radius ); // Professional medical heatmap colors (red core, yellow-orange edge) gradient.addColorStop(0, `rgba(220, 38, 38, ${spot.intensity * 0.8})`); gradient.addColorStop(0.3, `rgba(239, 68, 68, ${spot.intensity * 0.65})`); gradient.addColorStop(0.5, `rgba(251, 146, 60, ${spot.intensity * 0.5})`); gradient.addColorStop(0.7, `rgba(252, 211, 77, ${spot.intensity * 0.35})`); gradient.addColorStop(1, 'rgba(254, 240, 138, 0)'); overlayCtx.fillStyle = gradient; overlayCtx.beginPath(); overlayCtx.arc(spot.x, spot.y, spot.radius, 0, Math.PI * 2); overlayCtx.fill(); }); // Apply overlay with multiply blend mode for professional look ctx.globalAlpha = 0.6; ctx.globalCompositeOperation = 'multiply'; ctx.drawImage(overlayCanvas, 0, 0); ctx.globalCompositeOperation = 'source-over'; // Add subtle highlighting overlay ctx.globalAlpha = 0.3; hotspots.forEach(spot => { const highlightGradient = ctx.createRadialGradient( spot.x, spot.y, 0, spot.x, spot.y, spot.radius * 0.7 ); highlightGradient.addColorStop(0, 'rgba(255, 200, 0, 0.4)'); highlightGradient.addColorStop(1, 'rgba(255, 200, 0, 0)'); ctx.fillStyle = highlightGradient; ctx.beginPath(); ctx.arc(spot.x, spot.y, spot.radius * 0.7, 0, Math.PI * 2); ctx.fill(); }); ctx.globalAlpha = 1.0; // Add professional boundary markers ctx.strokeStyle = 'rgba(234, 179, 8, 0.8)'; ctx.lineWidth = 2; ctx.setLineDash([5, 5]); hotspots.forEach(spot => { ctx.beginPath(); ctx.arc(spot.x, spot.y, spot.radius * 0.85, 0, Math.PI * 2); ctx.stroke(); }); ctx.setLineDash([]); } else { // For negative cases, add professional clean overlay // Add subtle blue-green medical tint const gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height); gradient.addColorStop(0, 'rgba(5, 150, 105, 0.15)'); gradient.addColorStop(0.5, 'rgba(6, 182, 212, 0.12)'); gradient.addColorStop(1, 'rgba(34, 211, 238, 0.15)'); ctx.globalAlpha = 0.5; ctx.fillStyle = gradient; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.globalAlpha = 1.0; // Add professional "clear" indicators in corners const iconSize = Math.min(canvas.width, canvas.height) * 0.08; const margin = iconSize * 0.5; // Draw checkmarks in corners const drawCheckmark = (x, y) => { ctx.strokeStyle = 'rgba(16, 185, 129, 0.7)'; ctx.lineWidth = 3; ctx.lineCap = 'round'; ctx.lineJoin = 'round'; // Draw circle background ctx.fillStyle = 'rgba(16, 185, 129, 0.2)'; ctx.beginPath(); ctx.arc(x, y, iconSize * 0.6, 0, Math.PI * 2); ctx.fill(); // Draw checkmark ctx.beginPath(); ctx.moveTo(x - iconSize * 0.3, y); ctx.lineTo(x - iconSize * 0.1, y + iconSize * 0.2); ctx.lineTo(x + iconSize * 0.3, y - iconSize * 0.3); ctx.stroke(); }; // Top-left drawCheckmark(margin + iconSize, margin + iconSize); // Top-right drawCheckmark(canvas.width - margin - iconSize, margin + iconSize); // Add subtle border highlight ctx.strokeStyle = 'rgba(16, 185, 129, 0.3)'; ctx.lineWidth = 4; ctx.strokeRect(2, 2, canvas.width - 4, canvas.height - 4); } // Convert canvas to data URL with compression resolve(canvas.toDataURL('image/jpeg', 0.85)); // Use JPEG with 85% quality for smaller size }; img.onerror = () => { reject(new Error('Failed to load image for heatmap generation')); }; img.src = e.target.result; }; reader.onerror = () => { reject(new Error('Failed to read file for heatmap generation')); }; reader.readAsDataURL(file); }); } /** * Generate a consistent hash from file for deterministic predictions */ async function generateFileHash(file) { return new Promise((resolve) => { const reader = new FileReader(); reader.onload = (e) => { const arrayBuffer = e.target.result; const uint8Array = new Uint8Array(arrayBuffer); // Simple hash function - sum of bytes modulo large prime let hash = 0; const step = Math.max(1, Math.floor(uint8Array.length / 1000)); // Sample every Nth byte for (let i = 0; i < uint8Array.length; i += step) { hash = (hash * 31 + uint8Array[i]) % 10007; // Use prime number for better distribution } resolve(hash); }; reader.onerror = () => { // Fallback to file size + name based hash const nameHash = file.name.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0); resolve((file.size + nameHash) % 10007); }; // Read first 100KB for hash generation (faster) const blob = file.slice(0, Math.min(100000, file.size)); reader.readAsArrayBuffer(blob); }); } /** * Reset preview area */ function resetPreview(previewContainer) { previewContainer.innerHTML = `

Click to upload X-ray image

PNG, JPG up to 10MB

`; previewContainer.classList.remove("has-image"); } /* ============================================================================ ANALYSIS ============================================================================ */ async function analyzeScan(inputId) { const input = document.getElementById(inputId); const resultBox = document.getElementById("result1"); if (!input || !resultBox) return; if (!input.files || !input.files[0]) { alert("Please select an X-ray image first."); return; } const file = input.files[0]; // Show loading animation showLoading(resultBox); resultBox.classList.add("active"); try { // Simulate API call with timeout (replace with actual API endpoint) await new Promise(resolve => setTimeout(resolve, 2500)); // IMPORTANT: For production, replace this with actual API call: // const formData = new FormData(); // formData.append('file', file); // const response = await fetch('/api/analyze', { // method: 'POST', // body: formData // }); // const mockData = await response.json(); // Generate consistent hash from file for deterministic results const fileHash = await generateFileHash(file); const isPositive = fileHash % 10 < 3; // 30% positive rate, but consistent per file // Generate heatmap from uploaded image const heatmapUrl = await generateHeatmap(file, isPositive); // Generate consistent confidence based on file hash const confidenceBase = 70 + (fileHash % 30); const mockData = { success: true, raw_label: isPositive ? "TB_POSITIVE" : "TB_NEGATIVE", confidence: confidenceBase.toFixed(1), severity_score: isPositive ? (0.4 + (fileHash % 60) / 100) : null, heatmap_url: heatmapUrl, analyzed_at: new Date().toISOString(), file_hash: fileHash // Store for consistency }; if (mockData.success) { displayResults(mockData, resultBox); reportData = { ...mockData, filename: file.name, filesize: formatFileSize(file.size), timestamp: new Date().toLocaleString() }; } else { throw new Error(mockData.error || "Analysis failed"); } } catch (err) { console.error("Analysis error:", err); showError(resultBox, err.message || "An unexpected error occurred", inputId); } } /* Loading UI */ function showLoading(resultBox) { resultBox.innerHTML = `

Analyzing X-ray image, please wait...

This usually takes 10-30 seconds

`; } /* Error UI */ function showError(resultBox, message, inputId) { resultBox.innerHTML = `

⚠️ Analysis Failed

${escapeHtml(message)}

`; } /* ============================================================================ RESULTS RENDERING ============================================================================ */ function displayResults(data, resultBox) { const isPositive = data.raw_label === "TB_POSITIVE"; const statusIcon = isPositive ? "⚠️" : "✅"; const statusText = isPositive ? "TB Features Detected" : "No TB Features Detected"; let html = `
${statusIcon}

${statusText}

Confidence: ${data.confidence}%
`; // Severity bar for positive cases if (isPositive && data.severity_score !== null) { const percent = Math.round(data.severity_score * 100); let severityClass = "severity-moderate"; let severityLabel = "Moderate"; if (percent < 50) { severityClass = "severity-mild"; severityLabel = "Mild"; } else if (percent > 80) { severityClass = "severity-severe"; severityLabel = "Severe"; } html += `
Mild Moderate Severe
${severityLabel} Suspicion
Score: ${data.severity_score.toFixed(2)}
Level: ${percent}%
`; } // Recommendations section for both positive and negative cases html += `

${isPositive ? 'Recommended Next Steps' : 'Clinical Recommendations'}

`; if (isPositive) { html += `

Based on AI analysis, tuberculosis features were detected. Please consult with a healthcare professional for confirmatory testing and treatment guidance.

Immediate Actions:
  • ✓ Consult pulmonologist or infectious disease specialist immediately
  • ✓ Schedule sputum culture and PCR testing for confirmation
  • ✓ Consider chest CT scan for detailed assessment
  • ✓ Practice respiratory hygiene to prevent transmission
  • ✓ Inform close contacts and follow isolation protocols
  • ✓ Do not delay medical consultation
`; } else { html += `

The AI analysis did not detect TB features. However, this does not rule out all respiratory conditions. Please follow up with your healthcare provider.

Recommended Actions:
  • ✓ Share results with your healthcare provider
  • ✓ Continue monitoring symptoms if present
  • ✓ Follow up with clinical examination as scheduled
  • ✓ Consider additional tests if symptoms persist
  • ✓ Maintain regular health checkups
  • ✓ Practice good respiratory hygiene
`; } html += `
`; // Heatmap section if (data.heatmap_url) { html += `

AI Heatmap Visualization

${isPositive ? 'Red/orange/yellow areas indicate regions where the AI detected potential TB abnormalities. Brighter colors indicate higher confidence.' : 'Blue/cyan overlay indicates normal scan. Green checkmarks confirm no significant abnormalities detected.'}

AI Heatmap
`; } html += `
`; resultBox.innerHTML = html; } /* ============================================================================ PDF REPORT ============================================================================ */ async function downloadReport() { if (!reportData) { alert("Please analyze an X-ray first to generate a report."); return; } try { const downloadReportBtn = document.getElementById("downloadReportBtn"); const originalText = downloadReportBtn.textContent; downloadReportBtn.textContent = "Generating PDF..."; downloadReportBtn.disabled = true; const reportHTML = generateHTMLReport(reportData); const blob = new Blob([reportHTML], { type: "text/html" }); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = `PulmoScan_Report_${Date.now()}.html`; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); alert("Report downloaded successfully as HTML. Open in browser → Print → Save as PDF."); downloadReportBtn.textContent = originalText; downloadReportBtn.disabled = false; } catch (error) { console.error("Report generation failed:", error); alert("Failed to generate report. Please try again."); const downloadReportBtn = document.getElementById("downloadReportBtn"); downloadReportBtn.textContent = "Download Report"; downloadReportBtn.disabled = false; } } function generateHTMLReport(data) { const isPositive = data.raw_label === 'TB_POSITIVE'; const statusText = isPositive ? 'TB Positive' : 'TB Negative'; const statusColor = isPositive ? '#dc2626' : '#059669'; return ` PulmoScan AI - Medical Report

PulmoScan AI

Clinical-Grade TB Detection Report

Generated: ${data.timestamp}

${statusText}

Classification: ${statusText}
Confidence Level: ${data.confidence}%
${data.severity_score ? `
Severity Score: ${data.severity_score.toFixed(2)} (${Math.round(data.severity_score * 100)}%)
` : ''}
Filename: ${data.filename}
File Size: ${data.filesize}

${isPositive ? 'Recommended Next Steps' : 'Clinical Recommendations'}

${isPositive ? `

Based on AI analysis, tuberculosis features were detected. Please consult with a healthcare professional for confirmatory testing and treatment guidance.

Immediate Actions:

` : `

The AI analysis did not detect TB features. However, this does not rule out all respiratory conditions. Please follow up with your healthcare provider.

Recommended Actions:

`}
⚠️ Medical Disclaimer:
This AI tool assists healthcare professionals and is not a replacement for clinical diagnosis. Always consult with qualified medical professionals for diagnosis and treatment decisions. This report should be reviewed by a licensed healthcare provider before any medical decisions are made.
`; } /* ============================================================================ UTILITIES ============================================================================ */ function clearAll() { if (!confirm("Are you sure you want to clear all scans and results?")) { return; } // Clear file inputs document.querySelectorAll("input[type='file']").forEach((input) => { input.value = ""; }); // Reset preview containers document.querySelectorAll(".preview-container").forEach((container) => { resetPreview(container); }); // Clear result boxes document.querySelectorAll(".result-box").forEach((box) => { box.innerHTML = ""; box.classList.remove("active"); }); // Disable analyze buttons const analyzeBtn = document.getElementById("analyzeBtn1"); if (analyzeBtn) analyzeBtn.disabled = true; // Clear report data reportData = null; } function escapeHtml(text) { const div = document.createElement("div"); div.textContent = text; return div.innerHTML; } function formatFileSize(bytes) { if (bytes === 0) return '0 Bytes'; const k = 1024; const sizes = ['Bytes', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i]; } /* ============================================================================ NAVIGATION ============================================================================ */ function setActiveNav(event) { if (event && event.preventDefault) { event.preventDefault(); } const target = event ? event.target : null; // Update active nav link document.querySelectorAll(".nav-link").forEach((link) => { link.classList.remove("active"); }); if (target) { target.classList.add("active"); } // Smooth scroll to section if (target && target.getAttribute("href")) { const href = target.getAttribute("href"); if (href.startsWith("#")) { const id = href.substring(1); const section = document.getElementById(id); if (section) { section.scrollIntoView({ behavior: "smooth", block: "start" }); } } } } /* ============================================================================ UPLOAD ZONE CLICK HANDLING ============================================================================ */ function setupUploadZones() { const uploadZones = document.querySelectorAll(".upload-zone"); uploadZones.forEach((zone) => { const zoneId = zone.getAttribute("id"); const inputNumber = zoneId.replace("uploadZone", ""); const inputId = `fileInput${inputNumber}`; const fileInput = document.getElementById(inputId); if (!fileInput) return; // Click to upload zone.addEventListener("click", (e) => { // Don't trigger if clicking on the file input itself if (e.target !== fileInput) { fileInput.click(); } }); // Drag and drop support zone.addEventListener("dragover", (e) => { e.preventDefault(); e.stopPropagation(); zone.style.borderColor = "var(--primary, #0066cc)"; zone.style.background = "var(--primary-light, #e6f2ff)"; }); zone.addEventListener("dragleave", (e) => { e.preventDefault(); e.stopPropagation(); zone.style.borderColor = ""; zone.style.background = ""; }); zone.addEventListener("drop", (e) => { e.preventDefault(); e.stopPropagation(); zone.style.borderColor = ""; zone.style.background = ""; if (e.dataTransfer.files.length > 0) { fileInput.files = e.dataTransfer.files; handleFileSelect(inputId); } }); }); } /* ============================================================================ INITIALIZATION ============================================================================ */ document.addEventListener("DOMContentLoaded", () => { console.log("PulmoScan AI Ready"); // Setup file inputs const fileInput1 = document.getElementById("fileInput1"); if (fileInput1) { fileInput1.addEventListener("change", () => handleFileSelect("fileInput1")); } // Setup analyze buttons const analyzeBtn1 = document.getElementById("analyzeBtn1"); if (analyzeBtn1) { analyzeBtn1.addEventListener("click", () => analyzeScan("fileInput1")); } // Setup action buttons const clearAllBtn = document.getElementById("clearAllBtn"); const downloadReportBtn = document.getElementById("downloadReportBtn"); if (clearAllBtn) { clearAllBtn.addEventListener("click", clearAll); } if (downloadReportBtn) { downloadReportBtn.addEventListener("click", downloadReport); } // Setup navigation document.querySelectorAll(".nav-link").forEach(link => { link.addEventListener("click", setActiveNav); }); // Setup upload zones setupUploadZones(); // Set initial active nav based on URL hash or default to home const hash = window.location.hash || "#home"; const activeLink = document.querySelector(`.nav-link[href="${hash}"]`); if (activeLink) { activeLink.click(); } // Handle hash changes window.addEventListener("hashchange", () => { const newHash = window.location.hash || "#home"; const newActiveLink = document.querySelector(`.nav-link[href="${newHash}"]`); if (newActiveLink) { setActiveNav({ target: newActiveLink, preventDefault: () => {} }); } }); }); // Expose functions globally for inline event handlers window.setActiveNav = setActiveNav; window.analyzeScan = analyzeScan; window.handleFileSelect = handleFileSelect; window.clearAll = clearAll; window.downloadReport = downloadReport;