├── .env.local ├── metadata.json ├── index.css ├── index.tsx ├── .gitignore ├── server ├── package.json ├── public │ ├── websocket-interceptor.js │ └── service-worker.js └── server.js ├── index.html ├── package.json ├── tsconfig.json ├── vite.config.ts ├── Dockerfile ├── README.md ├── LICENSE └── Home.tsx /.env.local: -------------------------------------------------------------------------------- 1 | GEMINI_API_KEY=PLACEHOLDER_API_KEY 2 | -------------------------------------------------------------------------------- /metadata.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Banana Zoom", 3 | "description": "lossless zoom app powered by nano banana.", 4 | "requestFramePermissions": [] 5 | } -------------------------------------------------------------------------------- /index.css: -------------------------------------------------------------------------------- 1 | html { 2 | color-scheme: dark; 3 | background-color: black; 4 | } 5 | .pixelated { 6 | image-rendering: pixelated; 7 | image-rendering: -moz-crisp-edges; 8 | image-rendering: crisp-edges; 9 | } -------------------------------------------------------------------------------- /index.tsx: -------------------------------------------------------------------------------- 1 | /** 2 | * @license 3 | * SPDX-License-Identifier: Apache-2.0 4 | */ 5 | import '@tailwindcss/browser'; 6 | 7 | import ReactDOM from 'react-dom/client'; 8 | import Home from './Home'; 9 | 10 | const root = ReactDOM.createRoot(document.getElementById('root')); 11 | root.render(); 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | pnpm-debug.log* 8 | lerna-debug.log* 9 | 10 | node_modules 11 | dist 12 | dist-ssr 13 | *.local 14 | 15 | # Editor directories and files 16 | .vscode/* 17 | !.vscode/extensions.json 18 | .idea 19 | .DS_Store 20 | *.suo 21 | *.ntvs* 22 | *.njsproj 23 | *.sln 24 | *.sw? 25 | -------------------------------------------------------------------------------- /server/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "appletserver", 3 | "private": true, 4 | "version": "0.0.0", 5 | "scripts": { 6 | "start": "node server.js", 7 | "dev": "nodemon server.js" 8 | }, 9 | "dependencies": { 10 | "axios": "^1.6.7", 11 | "dotenv": "^16.4.5", 12 | "express": "^4.18.2", 13 | "express-rate-limit": "^7.5.0", 14 | "ws": "^8.17.0" 15 | }, 16 | "devDependencies": { 17 | "@types/node": "^22.14.0", 18 | "nodemon": "^3.1.0" 19 | } 20 | } -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 12 |
13 | 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "banana-zoom", 3 | "private": true, 4 | "version": "0.0.0", 5 | "type": "module", 6 | "scripts": { 7 | "dev": "vite", 8 | "build": "vite build", 9 | "preview": "vite preview" 10 | }, 11 | "dependencies": { 12 | "@google/genai": "^0.7.0", 13 | "react": "^19.0.0", 14 | "react-dom": "^19.0.0", 15 | "@tailwindcss/browser": "^4.1.2" 16 | }, 17 | "devDependencies": { 18 | "@types/node": "^22.14.0", 19 | "@vitejs/plugin-react": "^5.0.0", 20 | "typescript": "~5.8.2", 21 | "vite": "^6.2.0" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ES2022", 4 | "experimentalDecorators": true, 5 | "useDefineForClassFields": false, 6 | "module": "ESNext", 7 | "lib": [ 8 | "ES2022", 9 | "DOM", 10 | "DOM.Iterable" 11 | ], 12 | "skipLibCheck": true, 13 | "types": [ 14 | "node" 15 | ], 16 | "moduleResolution": "bundler", 17 | "isolatedModules": true, 18 | "moduleDetection": "force", 19 | "allowJs": true, 20 | "jsx": "react-jsx", 21 | "paths": { 22 | "@/*": [ 23 | "./*" 24 | ] 25 | }, 26 | "allowImportingTsExtensions": true, 27 | "noEmit": true 28 | } 29 | } -------------------------------------------------------------------------------- /vite.config.ts: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import { defineConfig, loadEnv } from 'vite'; 3 | import react from '@vitejs/plugin-react'; 4 | 5 | export default defineConfig(({ mode }) => { 6 | const env = loadEnv(mode, '.', ''); 7 | return { 8 | server: { 9 | port: 3000, 10 | host: '0.0.0.0', 11 | }, 12 | plugins: [react()], 13 | define: { 14 | 'process.env.API_KEY': JSON.stringify(env.GEMINI_API_KEY), 15 | 'process.env.GEMINI_API_KEY': JSON.stringify(env.GEMINI_API_KEY) 16 | }, 17 | resolve: { 18 | alias: { 19 | '@': path.resolve(__dirname, '.'), 20 | } 21 | } 22 | }; 23 | }); 24 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Stage 1: Build the frontend, and install server dependencies 2 | FROM node:22 AS builder 3 | 4 | WORKDIR /app 5 | 6 | # Copy all files from the current directory 7 | COPY . ./ 8 | RUN echo "API_KEY=PLACEHOLDER" > ./.env 9 | RUN echo "GEMINI_API_KEY=PLACEHOLDER" >> ./.env 10 | 11 | # Install server dependencies 12 | WORKDIR /app/server 13 | RUN npm install 14 | 15 | # Install dependencies and build the frontend 16 | WORKDIR /app 17 | RUN mkdir dist 18 | RUN bash -c 'if [ -f package.json ]; then npm install && npm run build; fi' 19 | 20 | 21 | # Stage 2: Build the final server image 22 | FROM node:22 23 | 24 | WORKDIR /app 25 | 26 | #Copy server files 27 | COPY --from=builder /app/server . 28 | # Copy built frontend assets from the builder stage 29 | COPY --from=builder /app/dist ./dist 30 | 31 | EXPOSE 3000 32 | 33 | CMD ["node", "server.js"] 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # **Banana Zoom** 2 | 3 | > Banana Zoom an advanced image enhancement web app that lets users select regions of an image for AI-powered upscaling and detail refinement. Using Google’s (nano banana), it analyzes selections, generates context-aware enhancements, and produces high-resolution outputs. Simply drag-and-drop or upload images, make precise or fixed-size selections, and watch improvements in real-time with smooth zoom and pixel-dissolve effects. 4 | 5 | | Image | Preview | 6 | |-------|---------| 7 | | enhancement-zoom(1) | ![enhancement-zoom](https://github.com/user-attachments/assets/ef1d5e92-c502-4f0b-a225-0b56a006353b) | 8 | | enhancement-zoom(2) | ![enhancement-zoom(2)](https://github.com/user-attachments/assets/bf3834fe-5136-4e0c-ae8c-8ccc4fbc864e) | 9 | | enhancement-zoom(3) | ![enhancement-zoom(3)](https://github.com/user-attachments/assets/328ee22c-fe03-4a8c-8f27-67a15e844d3d) | 10 | | enhancement-zoom(4) | ![enhancement-zoom(4)](https://github.com/user-attachments/assets/1e4edb8a-c533-49f1-ad6a-b579c9e205eb) | 11 | | enhancement-zoom(5) | ![enhancement-zoom(5)](https://github.com/user-attachments/assets/4e4b341f-0706-43ea-bda8-dbfe13b2b183) | 12 | 13 | 14 | # Gemini App Proxy Server 15 | 16 | This nodejs proxy server lets you run your AI Studio Gemini application unmodified, without exposing your API key in the frontend code. 17 | 18 | 19 | ## Instructions 20 | 21 | **Prerequisites**: 22 | - [Google Cloud SDK / gcloud CLI](https://cloud.google.com/sdk/docs/install) 23 | - (Optional) Gemini API Key 24 | 25 | 1. Download or copy the files of your AI Studio app into this directory at the root level. 26 | 2. If your app calls the Gemini API, create a Secret for your API key: 27 | ``` 28 | echo -n "${GEMINI_API_KEY}" | gcloud secrets create gemini_api_key --data-file=- 29 | ``` 30 | 31 | 3. Deploy to Cloud Run (optionally including API key): 32 | ``` 33 | gcloud run deploy my-app --source=. --update-secrets=GEMINI_API_KEY=gemini_api_key:latest 34 | ``` 35 | -------------------------------------------------------------------------------- /server/public/websocket-interceptor.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | const TARGET_WS_HOST = 'generativelanguage.googleapis.com'; // Host to intercept 3 | const originalWebSocket = window.WebSocket; 4 | 5 | if (!originalWebSocket) { 6 | console.error('[WebSocketInterceptor] Original window.WebSocket not found. Cannot apply interceptor.'); 7 | return; 8 | } 9 | 10 | const handler = { 11 | construct(target, args) { 12 | let [url, protocols] = args; 13 | //stringify url's if necessary for parsing 14 | let newUrlString = typeof url === 'string' ? url : (url && typeof url.toString === 'function' ? url.toString() : null); 15 | //get ready to check for host to proxy 16 | let isTarget = false; 17 | 18 | if (newUrlString) { 19 | try { 20 | // For full URLs, parse string and check the host 21 | if (newUrlString.startsWith('ws://') || newUrlString.startsWith('wss://')) { 22 | //URL object again 23 | const parsedUrl = new URL(newUrlString); 24 | if (parsedUrl.host === TARGET_WS_HOST) { 25 | isTarget = true; 26 | //use wss if https, else ws 27 | const proxyScheme = window.location.protocol === 'https:' ? 'wss' : 'ws'; 28 | const proxyHost = window.location.host; 29 | newUrlString = `${proxyScheme}://${proxyHost}/api-proxy${parsedUrl.pathname}${parsedUrl.search}`; 30 | } 31 | } 32 | } catch (e) { 33 | console.warn('[WebSocketInterceptor-Proxy] Error parsing WebSocket URL, using original:', url, e); 34 | } 35 | } else { 36 | console.warn('[WebSocketInterceptor-Proxy] WebSocket URL is not a string or stringifiable. Using original.'); 37 | } 38 | 39 | if (isTarget) { 40 | console.log('[WebSocketInterceptor-Proxy] Original WebSocket URL:', url); 41 | console.log('[WebSocketInterceptor-Proxy] Redirecting to proxy URL:', newUrlString); 42 | } 43 | 44 | // Call the original constructor with potentially modified arguments 45 | // Reflect.construct ensures 'new target(...)' behavior and correct prototype chain 46 | if (protocols) { 47 | return Reflect.construct(target, [newUrlString, protocols]); 48 | } else { 49 | return Reflect.construct(target, [newUrlString]); 50 | } 51 | }, 52 | get(target, prop, receiver) { 53 | // Forward static property access (e.g., WebSocket.OPEN, WebSocket.CONNECTING) 54 | // and prototype access to the original WebSocket constructor/prototype 55 | if (prop === 'prototype') { 56 | return target.prototype; 57 | } 58 | return Reflect.get(target, prop, receiver); 59 | } 60 | }; 61 | 62 | window.WebSocket = new Proxy(originalWebSocket, handler); 63 | 64 | console.log('[WebSocketInterceptor-Proxy] Global WebSocket constructor has been wrapped using Proxy.'); 65 | })(); 66 | -------------------------------------------------------------------------------- /server/public/service-worker.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @license 3 | * Copyright 2025 Google LLC 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | // service-worker.js 7 | 8 | // Define the target URL that we want to intercept and proxy. 9 | const TARGET_URL_PREFIX = 'https://generativelanguage.googleapis.com'; 10 | 11 | // Installation event: 12 | self.addEventListener('install', (event) => { 13 | try { 14 | console.log('Service Worker: Installing...'); 15 | event.waitUntil(self.skipWaiting()); 16 | } catch (error) { 17 | console.error('Service Worker: Error during install event:', error); 18 | // If skipWaiting fails, the new SW might get stuck in a waiting state. 19 | } 20 | }); 21 | 22 | // Activation event: 23 | self.addEventListener('activate', (event) => { 24 | try { 25 | console.log('Service Worker: Activating...'); 26 | event.waitUntil(self.clients.claim()); 27 | } catch (error) { 28 | console.error('Service Worker: Error during activate event:', error); 29 | // If clients.claim() fails, the SW might not control existing pages until next nav. 30 | } 31 | }); 32 | 33 | // Fetch event: 34 | self.addEventListener('fetch', (event) => { 35 | try { 36 | const requestUrl = event.request.url; 37 | 38 | if (requestUrl.startsWith(TARGET_URL_PREFIX)) { 39 | console.log(`Service Worker: Intercepting request to ${requestUrl}`); 40 | 41 | const remainingPathAndQuery = requestUrl.substring(TARGET_URL_PREFIX.length); 42 | const proxyUrl = `${self.location.origin}/api-proxy${remainingPathAndQuery}`; 43 | 44 | console.log(`Service Worker: Proxying to ${proxyUrl}`); 45 | 46 | // Construct headers for the request to the proxy 47 | const newHeaders = new Headers(); 48 | // Copy essential headers from the original request 49 | // For OPTIONS (preflight) requests, Access-Control-Request-* are critical. 50 | // For actual requests (POST, GET), Content-Type, Accept etc. 51 | const headersToCopy = [ 52 | 'Content-Type', 53 | 'Accept', 54 | 'Access-Control-Request-Method', 55 | 'Access-Control-Request-Headers', 56 | ]; 57 | 58 | for (const headerName of headersToCopy) { 59 | if (event.request.headers.has(headerName)) { 60 | newHeaders.set(headerName, event.request.headers.get(headerName)); 61 | } 62 | } 63 | 64 | if (event.request.method === 'POST') { 65 | 66 | // Ensure Content-Type is set for POST requests to the proxy, defaulting to application/json 67 | if (!newHeaders.has('Content-Type')) { 68 | console.warn("Service Worker: POST request to proxy was missing Content-Type in newHeaders. Defaulting to application/json."); 69 | newHeaders.set('Content-Type', 'application/json'); 70 | } else { 71 | console.log(`Service Worker: POST request to proxy has Content-Type: ${newHeaders.get('Content-Type')}`); 72 | } 73 | } 74 | 75 | const requestOptions = { 76 | method: event.request.method, 77 | headers: newHeaders, // Use simplified headers 78 | body: event.request.body, // Still use the original body stream 79 | mode: event.request.mode, 80 | credentials: event.request.credentials, 81 | cache: event.request.cache, 82 | redirect: event.request.redirect, 83 | referrer: event.request.referrer, 84 | integrity: event.request.integrity, 85 | }; 86 | 87 | // Only set duplex if there's a body and it's a relevant method 88 | if (event.request.method !== 'GET' && event.request.method !== 'HEAD' && event.request.body ) { 89 | requestOptions.duplex = 'half'; 90 | } 91 | 92 | const promise = fetch(new Request(proxyUrl, requestOptions)) 93 | .then((response) => { 94 | console.log(`Service Worker: Successfully proxied request to ${proxyUrl}, Status: ${response.status}`); 95 | return response; 96 | }) 97 | .catch((error) => { 98 | // Log more error details 99 | console.error(`Service Worker: Error proxying request to ${proxyUrl}. Message: ${error.message}, Name: ${error.name}, Stack: ${error.stack}`); 100 | return new Response( 101 | JSON.stringify({ error: 'Proxying failed', details: error.message, name: error.name, proxiedUrl: proxyUrl }), 102 | { 103 | status: 502, // Bad Gateway is appropriate for proxy errors 104 | headers: { 'Content-Type': 'application/json' } 105 | } 106 | ); 107 | }); 108 | 109 | event.respondWith(promise); 110 | 111 | } else { 112 | // If the request URL doesn't match our target, let it proceed as normal. 113 | event.respondWith(fetch(event.request)); 114 | } 115 | } catch (error) { 116 | // Log more error details for unhandled errors too 117 | console.error('Service Worker: Unhandled error in fetch event handler. Message:', error.message, 'Name:', error.name, 'Stack:', error.stack); 118 | event.respondWith( 119 | new Response( 120 | JSON.stringify({ error: 'Service worker fetch handler failed', details: error.message, name: error.name }), 121 | { 122 | status: 500, 123 | headers: { 'Content-Type': 'application/json' } 124 | } 125 | ) 126 | ); 127 | } 128 | }); 129 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /server/server.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @license 3 | * Copyright 2025 Google LLC 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | 7 | require('dotenv').config(); 8 | const express = require('express'); 9 | const fs = require('fs'); 10 | const axios = require('axios'); 11 | const https = require('https'); 12 | const path = require('path'); 13 | const WebSocket = require('ws'); 14 | const { URLSearchParams, URL } = require('url'); 15 | const rateLimit = require('express-rate-limit'); 16 | 17 | const app = express(); 18 | const port = process.env.PORT || 3000; 19 | const externalApiBaseUrl = 'https://generativelanguage.googleapis.com'; 20 | const externalWsBaseUrl = 'wss://generativelanguage.googleapis.com'; 21 | // Support either API key env-var variant 22 | const apiKey = process.env.GEMINI_API_KEY || process.env.API_KEY; 23 | 24 | const staticPath = path.join(__dirname,'dist'); 25 | const publicPath = path.join(__dirname,'public'); 26 | 27 | 28 | if (!apiKey) { 29 | // Only log an error, don't exit. The server will serve apps without proxy functionality 30 | console.error("Warning: GEMINI_API_KEY or API_KEY environment variable is not set! Proxy functionality will be disabled."); 31 | } 32 | else { 33 | console.log("API KEY FOUND (proxy will use this)") 34 | } 35 | 36 | // Limit body size to 50mb 37 | app.use(express.json({ limit: '50mb' })); 38 | app.use(express.urlencoded({extended: true, limit: '50mb'})); 39 | app.set('trust proxy', 1 /* number of proxies between user and server */) 40 | 41 | // Rate limiter for the proxy 42 | const proxyLimiter = rateLimit({ 43 | windowMs: 15 * 60 * 1000, // Set ratelimit window at 15min (in ms) 44 | max: 100, // Limit each IP to 100 requests per window 45 | message: 'Too many requests from this IP, please try again after 15 minutes', 46 | standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers 47 | legacyHeaders: false, // no `X-RateLimit-*` headers 48 | handler: (req, res, next, options) => { 49 | console.warn(`Rate limit exceeded for IP: ${req.ip}. Path: ${req.path}`); 50 | res.status(options.statusCode).send(options.message); 51 | } 52 | }); 53 | 54 | // Apply the rate limiter to the /api-proxy route before the main proxy logic 55 | app.use('/api-proxy', proxyLimiter); 56 | 57 | // Proxy route for Gemini API calls (HTTP) 58 | app.use('/api-proxy', async (req, res, next) => { 59 | console.log(req.ip); 60 | // If the request is an upgrade request, it's for WebSockets, so pass to next middleware/handler 61 | if (req.headers.upgrade && req.headers.upgrade.toLowerCase() === 'websocket') { 62 | return next(); // Pass to the WebSocket upgrade handler 63 | } 64 | 65 | // Handle OPTIONS request for CORS preflight 66 | if (req.method === 'OPTIONS') { 67 | res.setHeader('Access-Control-Allow-Origin', '*'); // Adjust as needed for security 68 | res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); 69 | res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Goog-Api-Key'); 70 | res.setHeader('Access-Control-Max-Age', '86400'); // Cache preflight response for 1 day 71 | return res.sendStatus(200); 72 | } 73 | 74 | if (req.body) { // Only log body if it exists 75 | console.log(" Request Body (from frontend):", req.body); 76 | } 77 | try { 78 | // Construct the target URL by taking the part of the path after /api-proxy/ 79 | const targetPath = req.url.startsWith('/') ? req.url.substring(1) : req.url; 80 | const apiUrl = `${externalApiBaseUrl}/${targetPath}`; 81 | console.log(`HTTP Proxy: Forwarding request to ${apiUrl}`); 82 | 83 | // Prepare headers for the outgoing request 84 | const outgoingHeaders = {}; 85 | // Copy most headers from the incoming request 86 | for (const header in req.headers) { 87 | // Exclude host-specific headers and others that might cause issues upstream 88 | if (!['host', 'connection', 'content-length', 'transfer-encoding', 'upgrade', 'sec-websocket-key', 'sec-websocket-version', 'sec-websocket-extensions'].includes(header.toLowerCase())) { 89 | outgoingHeaders[header] = req.headers[header]; 90 | } 91 | } 92 | 93 | // Set the actual API key in the appropriate header 94 | outgoingHeaders['X-Goog-Api-Key'] = apiKey; 95 | 96 | // Set Content-Type from original request if present (for relevant methods) 97 | if (req.headers['content-type'] && ['POST', 'PUT', 'PATCH'].includes(req.method.toUpperCase())) { 98 | outgoingHeaders['Content-Type'] = req.headers['content-type']; 99 | } else if (['POST', 'PUT', 'PATCH'].includes(req.method.toUpperCase())) { 100 | // Default Content-Type to application/json if no content type for post/put/patch 101 | outgoingHeaders['Content-Type'] = 'application/json'; 102 | } 103 | 104 | // For GET or DELETE requests, ensure Content-Type is NOT sent, 105 | // even if the client erroneously included it. 106 | if (['GET', 'DELETE'].includes(req.method.toUpperCase())) { 107 | delete outgoingHeaders['Content-Type']; // Case-sensitive common practice 108 | delete outgoingHeaders['content-type']; // Just in case 109 | } 110 | 111 | // Ensure 'accept' is reasonable if not set 112 | if (!outgoingHeaders['accept']) { 113 | outgoingHeaders['accept'] = '*/*'; 114 | } 115 | 116 | 117 | const axiosConfig = { 118 | method: req.method, 119 | url: apiUrl, 120 | headers: outgoingHeaders, 121 | responseType: 'stream', 122 | validateStatus: function (status) { 123 | return true; // Accept any status code, we'll pipe it through 124 | }, 125 | }; 126 | 127 | if (['POST', 'PUT', 'PATCH'].includes(req.method.toUpperCase())) { 128 | axiosConfig.data = req.body; 129 | } 130 | // For GET, DELETE, etc., axiosConfig.data will remain undefined, 131 | // and axios will not send a request body. 132 | 133 | const apiResponse = await axios(axiosConfig); 134 | 135 | // Pass through response headers from Gemini API to the client 136 | for (const header in apiResponse.headers) { 137 | res.setHeader(header, apiResponse.headers[header]); 138 | } 139 | res.status(apiResponse.status); 140 | 141 | 142 | apiResponse.data.on('data', (chunk) => { 143 | res.write(chunk); 144 | }); 145 | 146 | apiResponse.data.on('end', () => { 147 | res.end(); 148 | }); 149 | 150 | apiResponse.data.on('error', (err) => { 151 | console.error('Error during streaming data from target API:', err); 152 | if (!res.headersSent) { 153 | res.status(500).json({ error: 'Proxy error during streaming from target' }); 154 | } else { 155 | // If headers already sent, we can't send a JSON error, just end the response. 156 | res.end(); 157 | } 158 | }); 159 | 160 | } catch (error) { 161 | console.error('Proxy error before request to target API:', error); 162 | if (!res.headersSent) { 163 | if (error.response) { 164 | const errorData = { 165 | status: error.response.status, 166 | message: error.response.data?.error?.message || 'Proxy error from upstream API', 167 | details: error.response.data?.error?.details || null 168 | }; 169 | res.status(error.response.status).json(errorData); 170 | } else { 171 | res.status(500).json({ error: 'Proxy setup error', message: error.message }); 172 | } 173 | } 174 | } 175 | }); 176 | 177 | const webSocketInterceptorScriptTag = ``; 178 | 179 | // Prepare service worker registration script content 180 | const serviceWorkerRegistrationScript = ` 181 | 196 | `; 197 | 198 | // Serve index.html or placeholder based on API key and file availability 199 | app.get('/', (req, res) => { 200 | const placeholderPath = path.join(publicPath, 'placeholder.html'); 201 | 202 | // Try to serve index.html 203 | console.log("LOG: Route '/' accessed. Attempting to serve index.html."); 204 | const indexPath = path.join(staticPath, 'index.html'); 205 | 206 | fs.readFile(indexPath, 'utf8', (err, indexHtmlData) => { 207 | if (err) { 208 | // index.html not found or unreadable, serve the original placeholder 209 | console.log('LOG: index.html not found or unreadable. Falling back to original placeholder.'); 210 | return res.sendFile(placeholderPath); 211 | } 212 | 213 | // If API key is not set, serve original HTML without injection 214 | if (!apiKey) { 215 | console.log("LOG: API key not set. Serving original index.html without script injections."); 216 | return res.sendFile(indexPath); 217 | } 218 | 219 | // index.html found and apiKey set, inject scripts 220 | console.log("LOG: index.html read successfully. Injecting scripts."); 221 | let injectedHtml = indexHtmlData; 222 | 223 | 224 | if (injectedHtml.includes('')) { 225 | // Inject WebSocket interceptor first, then service worker script 226 | injectedHtml = injectedHtml.replace( 227 | '', 228 | `${webSocketInterceptorScriptTag}${serviceWorkerRegistrationScript}` 229 | ); 230 | console.log("LOG: Scripts injected into ."); 231 | } else { 232 | console.warn("WARNING: tag not found in index.html. Prepending scripts to the beginning of the file as a fallback."); 233 | injectedHtml = `${webSocketInterceptorScriptTag}${serviceWorkerRegistrationScript}${indexHtmlData}`; 234 | } 235 | res.send(injectedHtml); 236 | }); 237 | }); 238 | 239 | app.get('/service-worker.js', (req, res) => { 240 | return res.sendFile(path.join(publicPath, 'service-worker.js')); 241 | }); 242 | 243 | app.use('/public', express.static(publicPath)); 244 | app.use(express.static(staticPath)); 245 | 246 | // Start the HTTP server 247 | const server = app.listen(port, () => { 248 | console.log(`Server listening on port ${port}`); 249 | console.log(`HTTP proxy active on /api-proxy/**`); 250 | console.log(`WebSocket proxy active on /api-proxy/**`); 251 | }); 252 | 253 | // Create WebSocket server and attach it to the HTTP server 254 | const wss = new WebSocket.Server({ noServer: true }); 255 | 256 | server.on('upgrade', (request, socket, head) => { 257 | const requestUrl = new URL(request.url, `http://${request.headers.host}`); 258 | const pathname = requestUrl.pathname; 259 | 260 | if (pathname.startsWith('/api-proxy/')) { 261 | if (!apiKey) { 262 | console.error("WebSocket proxy: API key not configured. Closing connection."); 263 | socket.destroy(); 264 | return; 265 | } 266 | 267 | wss.handleUpgrade(request, socket, head, (clientWs) => { 268 | console.log('Client WebSocket connected to proxy for path:', pathname); 269 | 270 | const targetPathSegment = pathname.substring('/api-proxy'.length); 271 | const clientQuery = new URLSearchParams(requestUrl.search); 272 | clientQuery.set('key', apiKey); 273 | const targetGeminiWsUrl = `${externalWsBaseUrl}${targetPathSegment}?${clientQuery.toString()}`; 274 | console.log(`Attempting to connect to target WebSocket: ${targetGeminiWsUrl}`); 275 | 276 | const geminiWs = new WebSocket(targetGeminiWsUrl, { 277 | protocol: request.headers['sec-websocket-protocol'], 278 | }); 279 | 280 | const messageQueue = []; 281 | 282 | geminiWs.on('open', () => { 283 | console.log('Proxy connected to Gemini WebSocket'); 284 | // Send any queued messages 285 | while (messageQueue.length > 0) { 286 | const message = messageQueue.shift(); 287 | if (geminiWs.readyState === WebSocket.OPEN) { 288 | // console.log('Sending queued message from client -> Gemini'); 289 | geminiWs.send(message); 290 | } else { 291 | // Should not happen if we are in 'open' event, but good for safety 292 | console.warn('Gemini WebSocket not open when trying to send queued message. Re-queuing.'); 293 | messageQueue.unshift(message); // Add it back to the front 294 | break; // Stop processing queue for now 295 | } 296 | } 297 | }); 298 | 299 | geminiWs.on('message', (message) => { 300 | // console.log('Message from Gemini -> client'); 301 | if (clientWs.readyState === WebSocket.OPEN) { 302 | clientWs.send(message); 303 | } 304 | }); 305 | 306 | geminiWs.on('close', (code, reason) => { 307 | console.log(`Gemini WebSocket closed: ${code} ${reason.toString()}`); 308 | if (clientWs.readyState === WebSocket.OPEN || clientWs.readyState === WebSocket.CONNECTING) { 309 | clientWs.close(code, reason.toString()); 310 | } 311 | }); 312 | 313 | geminiWs.on('error', (error) => { 314 | console.error('Error on Gemini WebSocket connection:', error); 315 | if (clientWs.readyState === WebSocket.OPEN || clientWs.readyState === WebSocket.CONNECTING) { 316 | clientWs.close(1011, 'Upstream WebSocket error'); 317 | } 318 | }); 319 | 320 | clientWs.on('message', (message) => { 321 | if (geminiWs.readyState === WebSocket.OPEN) { 322 | // console.log('Message from client -> Gemini'); 323 | geminiWs.send(message); 324 | } else if (geminiWs.readyState === WebSocket.CONNECTING) { 325 | // console.log('Queueing message from client -> Gemini (Gemini still connecting)'); 326 | messageQueue.push(message); 327 | } else { 328 | console.warn('Client sent message but Gemini WebSocket is not open or connecting. Message dropped.'); 329 | } 330 | }); 331 | 332 | clientWs.on('close', (code, reason) => { 333 | console.log(`Client WebSocket closed: ${code} ${reason.toString()}`); 334 | if (geminiWs.readyState === WebSocket.OPEN || geminiWs.readyState === WebSocket.CONNECTING) { 335 | geminiWs.close(code, reason.toString()); 336 | } 337 | }); 338 | 339 | clientWs.on('error', (error) => { 340 | console.error('Error on client WebSocket connection:', error); 341 | if (geminiWs.readyState === WebSocket.OPEN || geminiWs.readyState === WebSocket.CONNECTING) { 342 | geminiWs.close(1011, 'Client WebSocket error'); 343 | } 344 | }); 345 | }); 346 | } else { 347 | console.log(`WebSocket upgrade request for non-proxy path: ${pathname}. Closing connection.`); 348 | socket.destroy(); 349 | } 350 | }); 351 | -------------------------------------------------------------------------------- /Home.tsx: -------------------------------------------------------------------------------- 1 | /** 2 | * @license 3 | * SPDX-License-Identifier: Apache-2.0 4 | */ 5 | import React, { useState, useCallback, useEffect, useRef, MouseEvent } from 'react'; 6 | // FIX: Added GenerateContentResponse for proper typing of API responses. 7 | import { GoogleGenAI, Modality, GenerateContentResponse } from "@google/genai"; 8 | import {GIFEncoder, quantize, applyPalette} from 'https://unpkg.com/gifenc' 9 | 10 | // --- Type Definitions --- 11 | enum AppState { 12 | IDLE = 'IDLE', 13 | LOADING = 'LOADING', 14 | LOADED = 'LOADED', 15 | SELECTING = 'SELECTING', 16 | ENHANCING = 'ENHANCING', 17 | ENHANCED = 'ENHANCED', 18 | } 19 | 20 | interface Rect { 21 | x: number; 22 | y: number; 23 | w: number; 24 | h: number; 25 | } 26 | 27 | interface ImageDescription { 28 | selectionDescription:string; 29 | prompt?:string; 30 | } 31 | 32 | interface HistoryStep { 33 | imageSrc: string; 34 | description: ImageDescription | null; 35 | originalRect: Rect | null; 36 | } 37 | 38 | // --- Utility Functions --- 39 | 40 | // FIX: Changed to a standard function declaration to avoid issues with TypeScript generics in a .tsx file. 41 | // Also improved error handling to consistently return null. 42 | function extractJson(text: string): T | null { 43 | try { 44 | const data = JSON.parse(text) as T; 45 | return data; 46 | } catch { 47 | const match = text.match(/```json\s*([\s\S]*?)\s*```/s); 48 | if (!match) { 49 | console.error(`No JSON found in response: ${text}`); 50 | return null; 51 | } 52 | try { 53 | const data = JSON.parse(match[1]) as T; 54 | return data; 55 | } catch (error) { 56 | console.error(error); 57 | return null; 58 | } 59 | } 60 | } 61 | 62 | const cropImage = ( 63 | image: HTMLImageElement, 64 | cropRect: Rect, 65 | targetWidth: number, 66 | targetHeight: number, 67 | pixelated: boolean 68 | ): Promise => { 69 | return new Promise((resolve) => { 70 | const canvas = document.createElement('canvas'); 71 | canvas.width = targetWidth; 72 | canvas.height = targetHeight; 73 | const ctx = canvas.getContext('2d'); 74 | 75 | if (!ctx) { 76 | return resolve(''); 77 | } 78 | 79 | if (pixelated) { 80 | ctx.imageSmoothingEnabled = false; 81 | } 82 | 83 | ctx.drawImage( 84 | image, 85 | cropRect.x, 86 | cropRect.y, 87 | cropRect.w, 88 | cropRect.h, 89 | 0, 90 | 0, 91 | targetWidth, 92 | targetHeight 93 | ); 94 | 95 | resolve(canvas.toDataURL('image/png')); 96 | }); 97 | }; 98 | 99 | // FIX: Changed to an async function declaration to avoid JSX parsing issues with Promise return types. 100 | // FIX: Renamed `history` parameter to `descriptions` to avoid conflict with the browser's built-in `History` type. 101 | async function serviceDescribeImage(imageDataUrl: string, descriptions: ImageDescription[]): Promise { 102 | const ai = new GoogleGenAI({ apiKey: process.env.API_KEY }); 103 | const parts = imageDataUrl.split(','); 104 | const mimeType = parts[0].match(/:(.*?);/)?.[1] || 'image/png'; 105 | const base64Data = parts[1]; 106 | 107 | if (!base64Data) { 108 | console.error("Invalid image data URL provided to serviceDescribeImage."); 109 | return {selectionDescription:"user selected a region to enhance"}; 110 | } 111 | 112 | const imagePart = { 113 | inlineData: { 114 | mimeType, 115 | data: base64Data, 116 | }, 117 | }; 118 | 119 | const textPart = { 120 | text: `You are an advanced image enhancement system. Your two tasks are: 121 | 122 | 1. **Selection Description:** Provide a precise, internal-use description of what the user has selected. Format this as: "The user selected...". 123 | 124 | 2. **Enhancement Prompt:** Write a short, non-narrative prompt for an image enhancement model. The model is a 'black box' and only receives your prompt and the cropped image. It cannot access history. 125 | 126 | ### Enhancement Prompt Rules 127 | 128 | - **Camera Angle & Perspective:** Always provide a camera angle. Crucially, infer the most plausible perspective from the selection's context. For architectural features like windows, assume an **external perspective** (looking in) unless the image content or history clearly indicates an interior scene. 129 | 130 | - **Content & Detail:** 131 | - If the selection is clear, provide a concise, high-level description of the image type and angle (e.g., "microscopic photography, close-up"). Do not describe the content itself. 132 | - If the selection is blurry or too zoomed in, provide a creative, plausible, and imprecise description of what could be in the frame. Avoid details about color or shape, allowing the enhancement model to infer them from the image's pixels. Example: a blurry section of water could suggest "a contour of a fish beneath the surface," while a blurry sky could suggest "the faint glow of a distant nebula." 133 | 134 | - **Final Check:** Do not include a full narrative or describe anything outside the selection box. The prompt must be concise. 135 | 136 | ### Output 137 | Return a JSON object in the following format: 138 | 139 | \`\`\`json 140 | { 141 | "selectionDescription": "string", 142 | "prompt": "string" 143 | } 144 | \`\`\` 145 | 146 | Here's the selection history for your reference: 147 | 148 | ${descriptions.length ? descriptions.filter(Boolean).map((desc,index)=>`${index+1} - ${desc.selectionDescription}`).join('\n\n* ') : 'No current history, this is the first selection'} 149 | ` 150 | }; 151 | 152 | try { 153 | // FIX: Added GenerateContentResponse type for the response object. 154 | const response: GenerateContentResponse = await ai.models.generateContent({ 155 | model: 'gemini-2.5-flash', 156 | contents: { parts: [imagePart, textPart] }, 157 | }); 158 | // FIX: Added a safety check for the text property. 159 | const text = response.text?.trim(); 160 | if (!text) { 161 | console.error("No text in response from Gemini"); 162 | return {selectionDescription:"user selected a region to enhance"}; 163 | } 164 | 165 | const data = extractJson(text); 166 | 167 | if (!data) { 168 | return {selectionDescription:"user selected a region to enhance"}; 169 | } 170 | return data; 171 | } catch (error) { 172 | console.error("Error describing image with Gemini:", error); 173 | return {selectionDescription:"user selected a region to enhance"}; 174 | } 175 | } 176 | 177 | // FIX: Changed to an async function declaration to avoid JSX parsing issues with Promise return types. 178 | async function serviceEnhance(croppedImageDataUrl: string, history: string[]): Promise<{ imageSrc: string }> { 179 | const ai = new GoogleGenAI({ apiKey: process.env.API_KEY }); 180 | const base64Data = croppedImageDataUrl.split(',')[1] || ''; 181 | const imagePart = { 182 | inlineData: { 183 | mimeType: 'image/png', 184 | data: base64Data, 185 | }, 186 | }; 187 | 188 | if (!history || history.length === 0) { 189 | console.error("Enhancement history is empty."); 190 | return { imageSrc: croppedImageDataUrl }; 191 | } 192 | 193 | // FIX: Simplified the prompt to be less restrictive, which was likely causing the "No candidates" error. 194 | // REMOVED: Banana easter egg prompt text. 195 | const generationPrompt = `Enhance and upscale this image. Preserve the original content, shapes, and colors, but increase the resolution and detail. If the image is too blurry to determine content, use creative interpretation based on the existing shapes and colors.`; 196 | 197 | try { 198 | const response: GenerateContentResponse = await ai.models.generateContent({ 199 | model: 'gemini-2.5-flash-image-preview', 200 | contents: {parts:[imagePart, {text:generationPrompt}]}, 201 | config:{ 202 | // REMOVED: Modality.TEXT as we no longer expect a text response for the banana. 203 | responseModalities: [Modality.IMAGE], 204 | } 205 | }); 206 | 207 | // FIX: Added robust error handling to check for blocked prompts. 208 | if (response.promptFeedback?.blockReason) { 209 | const message = `Request was blocked: ${response.promptFeedback.blockReason} - ${response.promptFeedback.blockReasonMessage || 'No message.'}`; 210 | console.error(message, response.promptFeedback); 211 | throw new Error(message); 212 | } 213 | 214 | const candidates = response.candidates; 215 | // FIX: The original error. Check candidates and throw a more informative error. 216 | if (!candidates || candidates.length === 0) { 217 | console.error("No candidates returned from the API. Full response:", response); 218 | if (response.text) { 219 | throw new Error(`API returned no candidates, but provided a text response: ${response.text}`); 220 | } 221 | throw new Error("API returned no candidates and no block reason. The response may have been empty."); 222 | } 223 | 224 | const contents = candidates[0].content; 225 | if (!contents) throw new Error("No contents returned from the API."); 226 | const parts = contents.parts; 227 | if (!parts) throw new Error("No parts returned from the API."); 228 | 229 | let imageSrc = croppedImageDataUrl; 230 | 231 | // REMOVED: Logic to parse `foundTheBanana` 232 | for (const part of parts) { 233 | if (part.inlineData) { 234 | const imageData = part.inlineData.data; 235 | imageSrc = `data:${part.inlineData.mimeType};base64,${imageData}`; 236 | } 237 | } 238 | 239 | return { imageSrc }; 240 | 241 | } catch (error) { 242 | console.error("Error generating image with Gemini:", error); 243 | return { imageSrc: croppedImageDataUrl }; 244 | } 245 | } 246 | 247 | const easeInOutCubic = (t: number) => t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2; 248 | const interpolateRect = (start: Rect, end: Rect, t: number): Rect => ({ 249 | x: start.x + (end.x - start.x) * t, 250 | y: start.y + (end.y - start.y) * t, 251 | w: start.w + (end.w - start.w) * t, 252 | h: start.h + (end.h - start.h) * t, 253 | }); 254 | const addFrameToGif = (gif: any, ctx:CanvasRenderingContext2D, delay:number) => { 255 | const { data, width, height } = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height); 256 | const palette = quantize(data, 256) 257 | const indexed = applyPalette(data, palette) 258 | gif.writeFrame(indexed, width, height, { palette, delay }); 259 | } 260 | // FIX: Changed to an async function declaration to avoid JSX parsing issues with Promise return types. 261 | async function generateZoomGif(history: HistoryStep[]): Promise { 262 | if (history.length < 2) { 263 | throw new Error("History must contain at least two steps to generate a GIF."); 264 | } 265 | const images = await Promise.all( 266 | history.map(step => new Promise((resolve, reject) => { 267 | const img = new Image(); 268 | img.crossOrigin = "anonymous"; 269 | img.onload = () => resolve(img); 270 | img.onerror = reject; 271 | img.src = step.imageSrc; 272 | })) 273 | ); 274 | const firstSelectionRect = history[1].originalRect; 275 | if (!firstSelectionRect) { 276 | throw new Error("The second history step must have a selection rectangle."); 277 | } 278 | const gifAspectRatio = firstSelectionRect.h / firstSelectionRect.w; 279 | const gifWidth = 512; 280 | const gifHeight = Math.round(gifWidth * gifAspectRatio); 281 | 282 | const gif = GIFEncoder(); 283 | const canvas = document.createElement('canvas'); 284 | canvas.width = gifWidth; 285 | canvas.height = gifHeight; 286 | const ctx = canvas.getContext('2d', { willReadFrequently: true }); 287 | if (!ctx) throw new Error("Could not get canvas context"); 288 | 289 | const fps = 30; 290 | const zoomDuration = 1.0; 291 | const holdDuration = 0.5; 292 | const zoomFrames = zoomDuration * fps; 293 | const holdFrames = holdDuration * fps; 294 | const frameDelay = 1000 / fps; 295 | 296 | for (let i = 0; i < images.length - 1; i++) { 297 | const sourceImageForZoom = images[i]; 298 | const nextEnhancedImage = images[i + 1]; 299 | const startRect: Rect = { x: 0, y: 0, w: sourceImageForZoom.naturalWidth, h: sourceImageForZoom.naturalHeight }; 300 | const endRect = history[i + 1].originalRect; 301 | if (!endRect) continue; 302 | 303 | for (let f = 0; f < zoomFrames; f++) { 304 | const t = easeInOutCubic(f / zoomFrames); 305 | const currentRect = interpolateRect(startRect, endRect, t); 306 | ctx.fillStyle = 'black'; 307 | ctx.fillRect(0, 0, gifWidth, gifHeight); 308 | ctx.drawImage(sourceImageForZoom, currentRect.x, currentRect.y, currentRect.w, currentRect.h, 0, 0, gifWidth, gifHeight); 309 | const scaleX = gifWidth / currentRect.w; 310 | const scaleY = gifHeight / currentRect.h; 311 | const rectOnCanvas = { 312 | x: (endRect.x - currentRect.x) * scaleX, 313 | y: (endRect.y - currentRect.y) * scaleY, 314 | w: endRect.w * scaleX, 315 | h: endRect.h * scaleY, 316 | }; 317 | ctx.strokeStyle = '#EEE'; 318 | ctx.lineWidth = 2; 319 | ctx.setLineDash([5, 5]); 320 | ctx.strokeRect(rectOnCanvas.x, rectOnCanvas.y, rectOnCanvas.w, rectOnCanvas.h); 321 | ctx.setLineDash([]); 322 | addFrameToGif(gif, ctx, frameDelay); 323 | } 324 | 325 | for (let f = 0; f < holdFrames; f++) { 326 | ctx.fillStyle = 'black'; 327 | ctx.fillRect(0, 0, gifWidth, gifHeight); 328 | ctx.drawImage(nextEnhancedImage, 0, 0, gifWidth, gifHeight); 329 | addFrameToGif(gif, ctx, frameDelay); 330 | } 331 | } 332 | gif.finish(); 333 | return new Blob([gif.bytesView()], { type: 'image/gif' }); 334 | } 335 | 336 | // --- React Components --- 337 | 338 | interface DropZoneProps { 339 | onUploadClick: () => void; 340 | } 341 | const DropZone: React.FC = ({ onUploadClick }) => { 342 | return ( 343 |
344 | 345 | 346 | 347 | 348 | 349 |

[ CSI Image Enhancer v2.5 ]

350 |

Awaiting Image Input...

351 |

Drag & Drop an image file or

352 | 358 |
359 | ); 360 | }; 361 | 362 | interface ImageDisplayProps { 363 | imageSrc: string; 364 | onStageSelection: (originalRect: Rect, screenRect: Rect, canvasDataUrl: string) => void; 365 | isEnhancing: boolean; 366 | historicalSelection?: Rect | null; 367 | stagedSelectionRect?: Rect | null; 368 | useFixedSelectionBox: boolean; 369 | fixedSelectionSizePercentage: number; 370 | } 371 | const ImageDisplay: React.FC = ({ imageSrc, onStageSelection, isEnhancing, historicalSelection, stagedSelectionRect, useFixedSelectionBox, fixedSelectionSizePercentage }) => { 372 | const canvasRef = useRef(null); 373 | const [image, setImage] = useState(null); 374 | const [selection, setSelection] = useState(null); 375 | const [startPoint, setStartPoint] = useState<{ x: number; y: number } | null>(null); 376 | 377 | useEffect(() => { 378 | const img = new Image(); 379 | img.src = imageSrc; 380 | img.onload = () => setImage(img); 381 | }, [imageSrc]); 382 | 383 | const getCanvasScale = useCallback(() => { 384 | const canvas = canvasRef.current; 385 | if (!canvas || !image) return { scale: 1, offsetX: 0, offsetY: 0, dWidth: 0, dHeight: 0 }; 386 | const { width: canvasWidth, height: canvasHeight } = canvas.getBoundingClientRect(); 387 | const canvasAspect = canvasWidth / canvasHeight; 388 | const imageAspect = image.naturalWidth / image.naturalHeight; 389 | let dWidth, dHeight, offsetX, offsetY; 390 | if (canvasAspect > imageAspect) { 391 | dHeight = canvasHeight; 392 | dWidth = dHeight * imageAspect; 393 | } else { 394 | dWidth = canvasWidth; 395 | dHeight = dWidth / imageAspect; 396 | } 397 | offsetX = (canvasWidth - dWidth) / 2; 398 | offsetY = (canvasHeight - dHeight) / 2; 399 | const scale = dWidth / image.naturalWidth; 400 | return { scale, offsetX, offsetY, dWidth, dHeight }; 401 | }, [image]); 402 | 403 | const draw = useCallback(() => { 404 | const canvas = canvasRef.current; 405 | const ctx = canvas?.getContext('2d'); 406 | if (!ctx || !canvas || !image) return; 407 | 408 | const { width: cssWidth, height: cssHeight } = canvas.getBoundingClientRect(); 409 | ctx.clearRect(0, 0, cssWidth, cssHeight); 410 | const { scale, offsetX, offsetY, dWidth, dHeight } = getCanvasScale(); 411 | ctx.drawImage(image, offsetX, offsetY, dWidth, dHeight); 412 | 413 | if (stagedSelectionRect) { 414 | ctx.strokeStyle = '#FFFFFF'; 415 | ctx.lineWidth = 2; 416 | ctx.setLineDash([]); // solid line 417 | ctx.strokeRect(stagedSelectionRect.x, stagedSelectionRect.y, stagedSelectionRect.w, stagedSelectionRect.h); 418 | } 419 | 420 | if (selection) { 421 | ctx.strokeStyle = '#FFFFFF'; 422 | ctx.lineWidth = 2; 423 | ctx.setLineDash([5, 5]); 424 | ctx.strokeRect(selection.x, selection.y, selection.w, selection.h); 425 | ctx.setLineDash([]); 426 | ctx.font = '10px "Fira Code", monospace'; 427 | const info = `x:${Math.round(selection.x)} y:${Math.round(selection.y)} w:${Math.round(selection.w)} h:${Math.round(selection.h)}`; 428 | const textMetrics = ctx.measureText(info); 429 | ctx.fillStyle = 'rgba(0,0,0,0.7)'; 430 | ctx.fillRect(selection.x -1, selection.y - 14, textMetrics.width + 4, 12); 431 | ctx.fillStyle = '#FFFFFF'; 432 | ctx.fillText(info, selection.x + 1, selection.y - 4); 433 | } else if (historicalSelection && !stagedSelectionRect) { 434 | const screenRect = { 435 | x: historicalSelection.x * scale + offsetX, 436 | y: historicalSelection.y * scale + offsetY, 437 | w: historicalSelection.w * scale, 438 | h: historicalSelection.h * scale, 439 | }; 440 | ctx.strokeStyle = 'rgba(255, 255, 255, 0.7)'; 441 | ctx.lineWidth = 2; 442 | ctx.strokeRect(screenRect.x, screenRect.y, screenRect.w, screenRect.h); 443 | ctx.font = '10px "Fira Code", monospace'; 444 | const info = `PREV. CROP`; 445 | const textMetrics = ctx.measureText(info); 446 | ctx.fillStyle = 'rgba(0,0,0,0.7)'; 447 | ctx.fillRect(screenRect.x - 1, screenRect.y - 14, textMetrics.width + 4, 12); 448 | ctx.fillStyle = 'rgba(255, 255, 255, 0.7)'; 449 | ctx.fillText(info, screenRect.x + 1, screenRect.y - 4); 450 | } 451 | }, [image, selection, getCanvasScale, historicalSelection, stagedSelectionRect]); 452 | 453 | useEffect(() => { 454 | const canvas = canvasRef.current; 455 | if (!canvas) return; 456 | const resizeCanvas = () => { 457 | const parent = canvas.parentElement; 458 | if (parent) { 459 | const { width, height } = parent.getBoundingClientRect(); 460 | const dpr = window.devicePixelRatio || 1; 461 | canvas.width = width * dpr; 462 | canvas.height = height * dpr; 463 | canvas.style.width = `${width}px`; 464 | canvas.style.height = `${height}px`; 465 | const ctx = canvas.getContext('2d'); 466 | if (ctx) { 467 | ctx.scale(dpr, dpr); 468 | } 469 | draw(); 470 | } 471 | }; 472 | resizeCanvas(); 473 | window.addEventListener('resize', resizeCanvas); 474 | return () => window.removeEventListener('resize', resizeCanvas); 475 | }, [draw, image]); 476 | 477 | const getMousePos = (e: React.MouseEvent): { x: number; y: number } => { 478 | const canvas = canvasRef.current!; 479 | const rect = canvas.getBoundingClientRect(); 480 | return { x: e.clientX - rect.left, y: e.clientY - rect.top }; 481 | }; 482 | 483 | const handleMouseDown = (e: React.MouseEvent) => { 484 | if (isEnhancing) return; 485 | const pos = getMousePos(e); 486 | if (useFixedSelectionBox) { 487 | if (!image) return; 488 | const canvas = canvasRef.current; 489 | if (!canvas) return; 490 | const ctx = canvas.getContext('2d'); 491 | if (!ctx) return; 492 | const { scale, offsetX, offsetY, dWidth, dHeight } = getCanvasScale(); 493 | if (pos.x < offsetX || pos.x > offsetX + dWidth || pos.y < offsetY || pos.y > offsetY + dHeight) { 494 | return; 495 | } 496 | const originalClickX = (pos.x - offsetX) / scale; 497 | const originalClickY = (pos.y - offsetY) / scale; 498 | const boxWidth = image.naturalWidth * fixedSelectionSizePercentage; 499 | const boxHeight = image.naturalHeight * fixedSelectionSizePercentage; 500 | let originalX = originalClickX - boxWidth / 2; 501 | let originalY = originalClickY - boxHeight / 2; 502 | if (originalX < 0) originalX = 0; 503 | if (originalY < 0) originalY = 0; 504 | if (originalX + boxWidth > image.naturalWidth) originalX = image.naturalWidth - boxWidth; 505 | if (originalY + boxHeight > image.naturalHeight) originalY = image.naturalHeight - boxHeight; 506 | const originalRect: Rect = { x: originalX, y: originalY, w: boxWidth, h: boxHeight }; 507 | const screenRect: Rect = { 508 | x: originalRect.x * scale + offsetX, 509 | y: originalRect.y * scale + offsetY, 510 | w: originalRect.w * scale, 511 | h: originalRect.h * scale, 512 | }; 513 | ctx.strokeStyle = '#FFFFFF'; 514 | ctx.lineWidth = 2; 515 | ctx.setLineDash([5, 5]); 516 | ctx.strokeRect(screenRect.x, screenRect.y, screenRect.w, screenRect.h); 517 | ctx.setLineDash([]); 518 | const canvasDataUrl = canvas.toDataURL('image/png'); 519 | draw(); 520 | onStageSelection(originalRect, screenRect, canvasDataUrl); 521 | } else { 522 | setStartPoint(pos); 523 | setSelection({ ...pos, w: 0, h: 0 }); 524 | } 525 | }; 526 | 527 | const handleMouseMove = (e: React.MouseEvent) => { 528 | if (useFixedSelectionBox || !startPoint || isEnhancing) return; 529 | const pos = getMousePos(e); 530 | const x = Math.min(pos.x, startPoint.x); 531 | const y = Math.min(pos.y, startPoint.y); 532 | const w = Math.abs(pos.x - startPoint.x); 533 | const h = Math.abs(pos.y - startPoint.y); 534 | setSelection({ x, y, w, h }); 535 | }; 536 | 537 | const handleMouseUp = () => { 538 | if (useFixedSelectionBox) return; 539 | if (!selection || !image || selection.w < 10 || selection.h < 10 || isEnhancing) { 540 | setStartPoint(null); 541 | setSelection(null); 542 | return; 543 | } 544 | const canvas = canvasRef.current; 545 | if (!canvas) return; 546 | const { scale, offsetX, offsetY } = getCanvasScale(); 547 | const originalRect: Rect = { 548 | x: (selection.x - offsetX) / scale, 549 | y: (selection.y - offsetY) / scale, 550 | w: selection.w / scale, 551 | h: selection.h / scale 552 | }; 553 | const canvasDataUrl = canvas.toDataURL('image/png'); 554 | onStageSelection(originalRect, selection, canvasDataUrl); 555 | setStartPoint(null); 556 | setSelection(null); 557 | }; 558 | 559 | return ( 560 | 568 | ); 569 | }; 570 | 571 | interface PixelDissolveProps { 572 | lowResSrc: string; 573 | highResSrc: string; 574 | onComplete: () => void; 575 | } 576 | const PixelDissolve: React.FC = ({ lowResSrc, highResSrc, onComplete }) => { 577 | const canvasRef = useRef(null); 578 | const animationFrameId = useRef(null); 579 | 580 | const startAnimation = useCallback((lowResImg: HTMLImageElement, highResImg: HTMLImageElement) => { 581 | const canvas = canvasRef.current; 582 | if (!canvas) return; 583 | const parent = canvas.parentElement; 584 | if(!parent) return; 585 | const { width, height } = parent.getBoundingClientRect(); 586 | canvas.width = width; 587 | canvas.height = height; 588 | const offscreenLow = document.createElement('canvas'); 589 | const offscreenHigh = document.createElement('canvas'); 590 | offscreenLow.width = canvas.width; 591 | offscreenLow.height = canvas.height; 592 | offscreenHigh.width = canvas.width; 593 | offscreenHigh.height = canvas.height; 594 | const ctx = canvas.getContext('2d', { willReadFrequently: true }); 595 | const ctxLow = offscreenLow.getContext('2d', { willReadFrequently: true }); 596 | const ctxHigh = offscreenHigh.getContext('2d', { willReadFrequently: true }); 597 | if (!ctx || !ctxLow || !ctxHigh) return; 598 | ctxLow.imageSmoothingEnabled = false; 599 | ctxLow.drawImage(lowResImg, 0, 0, canvas.width, canvas.height); 600 | ctxHigh.imageSmoothingEnabled = true; 601 | ctxHigh.drawImage(highResImg, 0, 0, canvas.width, canvas.height); 602 | const lowData = ctxLow.getImageData(0, 0, canvas.width, canvas.height); 603 | const highData = ctxHigh.getImageData(0, 0, canvas.width, canvas.height); 604 | ctx.putImageData(lowData, 0, 0); 605 | const totalPixels = canvas.width * canvas.height; 606 | const pixelIndices = Array.from({ length: totalPixels }, (_, i) => i); 607 | for (let i = pixelIndices.length - 1; i > 0; i--) { 608 | const j = Math.floor(Math.random() * (i + 1)); 609 | [pixelIndices[i], pixelIndices[j]] = [pixelIndices[j], pixelIndices[i]]; 610 | } 611 | let currentPixel = 0; 612 | const pixelsPerFrame = Math.max(1, Math.ceil(totalPixels / 60)); 613 | const animate = () => { 614 | if (!canvasRef.current) return; 615 | if (currentPixel >= totalPixels) { 616 | ctx.putImageData(highData, 0, 0); 617 | onComplete(); 618 | return; 619 | } 620 | const endPixel = Math.min(currentPixel + pixelsPerFrame, totalPixels); 621 | for (let i = currentPixel; i < endPixel; i++) { 622 | const pIndex = pixelIndices[i] * 4; 623 | if (lowData.data.length > pIndex + 3 && highData.data.length > pIndex + 3) { 624 | lowData.data[pIndex] = highData.data[pIndex]; 625 | lowData.data[pIndex + 1] = highData.data[pIndex + 1]; 626 | lowData.data[pIndex + 2] = highData.data[pIndex + 2]; 627 | lowData.data[pIndex + 3] = highData.data[pIndex + 3]; 628 | } 629 | } 630 | ctx.putImageData(lowData, 0, 0); 631 | currentPixel = endPixel; 632 | animationFrameId.current = requestAnimationFrame(animate); 633 | }; 634 | animate(); 635 | }, [onComplete]); 636 | 637 | useEffect(() => { 638 | let lowResImg: HTMLImageElement; 639 | let highResImg: HTMLImageElement; 640 | const lowPromise = new Promise(resolve => { 641 | lowResImg = new Image(); 642 | lowResImg.crossOrigin = "anonymous"; 643 | lowResImg.src = lowResSrc; 644 | lowResImg.onload = () => resolve(lowResImg); 645 | }); 646 | const highPromise = new Promise(resolve => { 647 | highResImg = new Image(); 648 | highResImg.crossOrigin = "anonymous"; 649 | highResImg.src = highResSrc; 650 | highResImg.onload = () => resolve(highResImg); 651 | }); 652 | Promise.all([lowPromise, highPromise]).then(([loadedLow, loadedHigh]) => { 653 | startAnimation(loadedLow, loadedHigh); 654 | }); 655 | return () => { 656 | if (animationFrameId.current) { 657 | cancelAnimationFrame(animationFrameId.current); 658 | } 659 | }; 660 | }, [lowResSrc, highResSrc, startAnimation]); 661 | 662 | return ; 663 | }; 664 | 665 | interface SelectionAnimatorProps { 666 | rect: Rect; 667 | finalRect: Rect; 668 | src: string; 669 | onComplete: () => void; 670 | } 671 | const SelectionAnimator: React.FC = ({ rect, finalRect, src, onComplete }) => { 672 | const [isAnimating, setIsAnimating] = useState(false); 673 | const onCompleteCalled = useRef(false); 674 | 675 | useEffect(() => { 676 | const timer = setTimeout(() => setIsAnimating(true), 50); 677 | return () => clearTimeout(timer); 678 | }, []); 679 | 680 | const handleTransitionEnd = () => { 681 | if (!onCompleteCalled.current) { 682 | onCompleteCalled.current = true; 683 | onComplete(); 684 | } 685 | }; 686 | 687 | const initialStyle: React.CSSProperties = { 688 | top: `${rect.y}px`, 689 | left: `${rect.x}px`, 690 | width: `${rect.w}px`, 691 | height: `${rect.h}px`, 692 | }; 693 | 694 | const finalStyle: React.CSSProperties = { 695 | top: `${finalRect.y}px`, 696 | left: `${finalRect.x}px`, 697 | width: `${finalRect.w}px`, 698 | height: `${finalRect.h}px`, 699 | }; 700 | 701 | return ( 702 |
707 | Enhancing selection 712 |
713 | ); 714 | } 715 | 716 | interface StatusBarProps { 717 | state: AppState; 718 | useFixedSelectionBox: boolean; 719 | isInitialState: boolean; 720 | onUploadClick: () => void; 721 | } 722 | const getStatusMessage = (state: AppState, useFixedSelectionBox:boolean): string => { 723 | switch (state) { 724 | case AppState.IDLE: 725 | return 'SYSTEM IDLE. AWAITING INPUT.'; 726 | case AppState.LOADING: 727 | return 'LOADING INITIAL ASSETS... STANDBY...'; 728 | case AppState.LOADED: 729 | return 'IMAGE LOADED. '+ (useFixedSelectionBox ? 'CLICK TO SELECT AREA TO ENHANCE' : 'DRAW SELECTION TO ENHANCE.'); 730 | case AppState.SELECTING: 731 | return 'DEFINING SELECTION AREA...'; 732 | case AppState.ENHANCING: 733 | return 'ANALYZING SELECTION... ENHANCING...'; 734 | case AppState.ENHANCED: 735 | return 'APPLYING ENHANCEMENT...'; 736 | default: 737 | return '...'; 738 | } 739 | }; 740 | const StatusBar: React.FC = ({ state, useFixedSelectionBox, isInitialState, onUploadClick }) => { 741 | if (state === AppState.LOADED && isInitialState) { 742 | return ( 743 |
744 |

Drag and drop a new image or click on the current one to begin

745 | 751 |
752 | ); 753 | } 754 | const message = getStatusMessage(state, useFixedSelectionBox); 755 | return ( 756 |
757 |

{message}

758 |
759 | ); 760 | }; 761 | 762 | const ProcessingAnimation: React.FC = () => { 763 | return ( 764 |
765 |
766 |

PROCESSING...

767 |
768 | ) 769 | } 770 | 771 | // --- Main App Component --- 772 | 773 | interface EnhancementJob { 774 | originalRect: Rect; 775 | canvasWithSelectionDataUrl: string; 776 | pixelatedSrc: string; 777 | screenRect: Rect; 778 | } 779 | 780 | type StagedSelection = { 781 | originalRect: Rect; 782 | screenRect: Rect; 783 | canvasDataUrl: string; 784 | }; 785 | 786 | export default function Home() { 787 | const useFixedSelectionBox = true; 788 | const fixedSelectionSizePercentage = 0.125; 789 | const [appState, setAppState] = useState(AppState.LOADING); 790 | const [image, setImage] = useState(null); 791 | const [pixelatedImageSrc, setPixelatedImageSrc] = useState(null); 792 | const [enhancedImageSrc, setEnhancedImageSrc] = useState(null); 793 | const [finalImageSrc, setFinalImageSrc] = useState(null); 794 | const [history, setHistory] = useState([]); 795 | const [historyIndex, setHistoryIndex] = useState(-1); 796 | const [newHistoryEntryData, setNewHistoryEntryData] = useState<{description: ImageDescription, originalRect: Rect} | null>(null); 797 | const [enhancementJob, setEnhancementJob] = useState(null); 798 | const [finalEnhancementRect, setFinalEnhancementRect] = useState(null); 799 | const [displaySelection, setDisplaySelection] = useState(null); 800 | const [isGeneratingGif, setIsGeneratingGif] = useState(false); 801 | const [stagedSelection, setStagedSelection] = useState(null); 802 | const [isProcessing, setIsProcessing] = useState(false); 803 | 804 | const containerRef = useRef(null); 805 | const imageObjectURLRef = useRef(null); 806 | const fileInputRef = useRef(null); 807 | 808 | const loadInitialImage = useCallback(async () => { 809 | if (imageObjectURLRef.current) { 810 | URL.revokeObjectURL(imageObjectURLRef.current); 811 | imageObjectURLRef.current = null; 812 | } 813 | setAppState(AppState.LOADING); 814 | try { 815 | const response = await fetch('https://cdn-uploads.huggingface.co/production/uploads/65bb837dbfb878f46c77de4c/xlCiAw2IirDxEbryce_YI.jpeg'); 816 | if (!response.ok) throw new Error(`Failed to fetch initial image: ${response.statusText}`); 817 | const blob = await response.blob(); 818 | const objectURL = URL.createObjectURL(blob); 819 | imageObjectURLRef.current = objectURL; 820 | const img = new Image(); 821 | img.onload = () => { 822 | const newStep: HistoryStep = { imageSrc: objectURL, description: null, originalRect: null }; 823 | setHistory([newStep]); 824 | setHistoryIndex(0); 825 | setImage(img); 826 | setFinalImageSrc(objectURL); 827 | setDisplaySelection(null); 828 | setAppState(AppState.LOADED); 829 | }; 830 | img.onerror = () => { 831 | console.error("Image failed to load from object URL."); 832 | setAppState(AppState.IDLE); 833 | if (imageObjectURLRef.current) { 834 | URL.revokeObjectURL(imageObjectURLRef.current); 835 | imageObjectURLRef.current = null; 836 | } 837 | }; 838 | img.src = objectURL; 839 | } catch (error) { 840 | console.error("Failed to load initial image:", error); 841 | setAppState(AppState.IDLE); 842 | } 843 | }, []); 844 | 845 | const resetState = useCallback(() => { 846 | setEnhancementJob(null); 847 | setFinalEnhancementRect(null); 848 | setHistory([]); 849 | setHistoryIndex(-1); 850 | setNewHistoryEntryData(null); 851 | setDisplaySelection(null); 852 | setStagedSelection(null); 853 | setIsProcessing(false); 854 | loadInitialImage(); 855 | }, [loadInitialImage]); 856 | 857 | useEffect(() => { 858 | loadInitialImage(); 859 | return () => { 860 | if (imageObjectURLRef.current) { 861 | URL.revokeObjectURL(imageObjectURLRef.current); 862 | } 863 | }; 864 | }, [loadInitialImage]); 865 | 866 | const handleFileDrop = useCallback((file: File) => { 867 | if (imageObjectURLRef.current) { 868 | URL.revokeObjectURL(imageObjectURLRef.current); 869 | imageObjectURLRef.current = null; 870 | } 871 | if (file && file.type.startsWith('image/')) { 872 | const reader = new FileReader(); 873 | reader.onload = (e) => { 874 | const img = new Image(); 875 | img.onload = () => { 876 | const newImageSrc = e.target?.result as string; 877 | const newStep: HistoryStep = { imageSrc: newImageSrc, description: null, originalRect: null }; 878 | setHistory([newStep]); 879 | setHistoryIndex(0); 880 | setImage(img); 881 | setFinalImageSrc(newImageSrc); 882 | setEnhancementJob(null); 883 | setFinalEnhancementRect(null); 884 | setDisplaySelection(null); 885 | setStagedSelection(null); 886 | setAppState(AppState.LOADED); 887 | }; 888 | img.src = e.target?.result as string; 889 | }; 890 | reader.readAsDataURL(file); 891 | } 892 | }, []); 893 | 894 | const handleFileSelect = (e: React.ChangeEvent) => { 895 | if (e.target.files && e.target.files.length > 0) { 896 | handleFileDrop(e.target.files[0]); 897 | } 898 | }; 899 | 900 | const handleUploadClick = () => { fileInputRef.current?.click(); }; 901 | 902 | const handleStageSelection = useCallback((originalRect: Rect, screenRect: Rect, canvasDataUrl: string) => { 903 | setStagedSelection({ originalRect, screenRect, canvasDataUrl }); 904 | }, []); 905 | 906 | const startEnhancementProcess = useCallback((originalRect: Rect, screenRect: Rect, canvasWithSelectionDataUrl: string) => { 907 | if (!image) return; 908 | if (historyIndex < history.length - 1) { 909 | const newHistory = history.slice(0, historyIndex + 1); 910 | setHistory(newHistory); 911 | } 912 | setAppState(AppState.ENHANCING); 913 | const aspectRatio = originalRect.w / originalRect.h; 914 | const padding = 0.05; 915 | const maxWidth = window.innerWidth * (1 - padding); 916 | const maxHeight = window.innerHeight * (1 - padding); 917 | let targetWidth = maxWidth; 918 | let targetHeight = targetWidth / aspectRatio; 919 | if (targetHeight > maxHeight) { 920 | targetHeight = maxHeight; 921 | targetWidth = targetHeight * aspectRatio; 922 | } 923 | setFinalEnhancementRect({ w: targetWidth, h: targetHeight, x: (window.innerWidth - targetWidth) / 2, y: (window.innerHeight - targetHeight) / 2 }); 924 | cropImage(image, originalRect, originalRect.w, originalRect.h, true).then(pixelatedSrc => { 925 | setEnhancementJob({ originalRect, canvasWithSelectionDataUrl, pixelatedSrc, screenRect }); 926 | }); 927 | }, [image, history, historyIndex]); 928 | 929 | const handleProcessClick = useCallback(() => { 930 | if (!stagedSelection) return; 931 | startEnhancementProcess(stagedSelection.originalRect, stagedSelection.screenRect, stagedSelection.canvasDataUrl); 932 | setStagedSelection(null); 933 | }, [stagedSelection, startEnhancementProcess]); 934 | 935 | const runEnhancementJob = useCallback(async () => { 936 | if (!enhancementJob || !image) return; 937 | setIsProcessing(true); 938 | try { 939 | const { originalRect, canvasWithSelectionDataUrl, pixelatedSrc } = enhancementJob; 940 | const descriptionHistory = history.slice(0, historyIndex + 1).map(h => h.description).filter((d): d is ImageDescription => d !== null); 941 | const description = await serviceDescribeImage(canvasWithSelectionDataUrl, descriptionHistory); 942 | setNewHistoryEntryData({ description, originalRect }); 943 | const sourceImageWidth = image.naturalWidth; 944 | const sourceImageHeight = image.naturalHeight; 945 | const padding = 0.25; 946 | const paddedX = originalRect.x - originalRect.w * padding; 947 | const paddedY = originalRect.y - originalRect.h * padding; 948 | const paddedW = originalRect.w * (1 + 2 * padding); 949 | const paddedH = originalRect.h * (1 + 2 * padding); 950 | const finalPaddedX = Math.max(0, paddedX); 951 | const finalPaddedY = Math.max(0, paddedY); 952 | const finalPaddedX2 = Math.min(sourceImageWidth, paddedX + paddedW); 953 | const finalPaddedY2 = Math.min(sourceImageHeight, paddedY + paddedH); 954 | const paddedRect = { x: finalPaddedX, y: finalPaddedY, w: finalPaddedX2 - finalPaddedX, h: finalPaddedY2 - finalPaddedY }; 955 | const aspect = paddedRect.h / paddedRect.w; 956 | const targetWidth = 512 * (1.+padding); 957 | const targetHeight = Math.round(targetWidth * aspect); 958 | const croppedForEnhancement = await cropImage(image, paddedRect, targetWidth, targetHeight, false); 959 | const prompts = [...descriptionHistory.map(d=>(d.prompt || '')), description.prompt || '']; 960 | const { imageSrc: enhancedPaddedSrc } = await serviceEnhance(croppedForEnhancement, prompts); 961 | 962 | const enhancedPaddedImage = await new Promise((resolve, reject) => { 963 | const img = new Image(); 964 | img.crossOrigin = "anonymous"; 965 | img.onload = () => resolve(img); 966 | img.onerror = reject; 967 | img.src = enhancedPaddedSrc; 968 | }); 969 | const finalCropRect = { 970 | x: enhancedPaddedImage.naturalWidth * ((originalRect.x - paddedRect.x) / paddedRect.w), 971 | y: enhancedPaddedImage.naturalHeight * ((originalRect.y - paddedRect.y) / paddedRect.h), 972 | w: enhancedPaddedImage.naturalWidth * (originalRect.w / paddedRect.w), 973 | h: enhancedPaddedImage.naturalHeight * (originalRect.h / paddedRect.h), 974 | }; 975 | const finalImageWidth = 1024; 976 | const finalImageHeight = Math.round(finalImageWidth * (originalRect.h / originalRect.w)); 977 | const enhancedSrc = await cropImage(enhancedPaddedImage, finalCropRect, finalImageWidth, finalImageHeight, false); 978 | setPixelatedImageSrc(pixelatedSrc); 979 | setEnhancedImageSrc(enhancedSrc); 980 | setAppState(AppState.ENHANCED); 981 | } catch (error) { 982 | console.error("Enhancement process failed:", error); 983 | const fallbackSrc = await cropImage(image, enhancementJob.originalRect, enhancementJob.originalRect.w * 2, enhancementJob.originalRect.h * 2, false); 984 | setPixelatedImageSrc(enhancementJob.pixelatedSrc); 985 | setEnhancedImageSrc(fallbackSrc); 986 | setAppState(AppState.ENHANCED); 987 | } finally { 988 | setEnhancementJob(null); 989 | setIsProcessing(false); 990 | } 991 | }, [enhancementJob, image, history, historyIndex]); 992 | 993 | const handleEnhancementComplete = useCallback(() => { 994 | if (enhancedImageSrc && newHistoryEntryData) { 995 | const newStep: HistoryStep = { imageSrc: enhancedImageSrc, description: newHistoryEntryData.description, originalRect: newHistoryEntryData.originalRect }; 996 | const newHistory = history.slice(0, historyIndex + 1); 997 | setHistory([...newHistory, newStep]); 998 | setHistoryIndex(newHistory.length); 999 | const newImage = new Image(); 1000 | newImage.onload = () => { 1001 | setImage(newImage); 1002 | setFinalImageSrc(enhancedImageSrc); 1003 | setEnhancedImageSrc(null); 1004 | setFinalEnhancementRect(null); 1005 | setNewHistoryEntryData(null); 1006 | setDisplaySelection(null); 1007 | setAppState(AppState.LOADED); 1008 | } 1009 | newImage.src = enhancedImageSrc; 1010 | } 1011 | }, [enhancedImageSrc, newHistoryEntryData, history, historyIndex]); 1012 | 1013 | const handleDragOver = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); }; 1014 | const handleDrop = (e: React.DragEvent) => { 1015 | e.preventDefault(); e.stopPropagation(); 1016 | if (e.dataTransfer.files && e.dataTransfer.files.length > 0) { 1017 | handleFileDrop(e.dataTransfer.files[0]); 1018 | e.dataTransfer.clearData(); 1019 | } 1020 | }; 1021 | 1022 | const handleUndo = useCallback(() => { 1023 | if (historyIndex <= 0 || appState === AppState.ENHANCING || isGeneratingGif) return; 1024 | const newIndex = historyIndex - 1; 1025 | setHistoryIndex(newIndex); 1026 | const nextStep = history[newIndex + 1]; 1027 | setDisplaySelection(nextStep?.originalRect || null); 1028 | const newImageSrc = history[newIndex].imageSrc; 1029 | const img = new Image(); 1030 | img.onload = () => { setImage(img); setFinalImageSrc(newImageSrc); }; 1031 | img.src = newImageSrc; 1032 | setStagedSelection(null); 1033 | }, [history, historyIndex, appState, isGeneratingGif]); 1034 | 1035 | const handleRedo = useCallback(() => { 1036 | if (historyIndex >= history.length - 1 || appState === AppState.ENHANCING || isGeneratingGif) return; 1037 | const newIndex = historyIndex + 1; 1038 | setHistoryIndex(newIndex); 1039 | const nextStep = history[newIndex + 1]; 1040 | setDisplaySelection(nextStep?.originalRect || null); 1041 | const newImageSrc = history[newIndex].imageSrc; 1042 | const img = new Image(); 1043 | img.onload = () => { setImage(img); setFinalImageSrc(newImageSrc); }; 1044 | img.src = newImageSrc; 1045 | setStagedSelection(null); 1046 | }, [history, historyIndex, appState, isGeneratingGif]); 1047 | 1048 | const handleRegenerate = useCallback(async () => { 1049 | if (historyIndex <= 0 || appState === AppState.ENHANCING || isGeneratingGif) return; 1050 | setAppState(AppState.ENHANCING); 1051 | setStagedSelection(null); 1052 | const previousStep = history[historyIndex - 1]; 1053 | const originalRect = history[historyIndex].originalRect; 1054 | if (!originalRect) { setAppState(AppState.LOADED); return; } 1055 | const sourceImage = new Image(); 1056 | sourceImage.crossOrigin = "anonymous"; 1057 | sourceImage.onload = async () => { 1058 | try { 1059 | const descriptionHistory = history.slice(0, historyIndex).map(h => h.description).filter((d): d is ImageDescription => d !== null); 1060 | const croppedForDescription = await cropImage(sourceImage, originalRect, originalRect.w, originalRect.h, false); 1061 | const description = await serviceDescribeImage(croppedForDescription, descriptionHistory); 1062 | const sourceImageWidth = sourceImage.naturalWidth; 1063 | const sourceImageHeight = sourceImage.naturalHeight; 1064 | const padding = 0.5; 1065 | const paddedX = originalRect.x - originalRect.w * padding; 1066 | const paddedY = originalRect.y - originalRect.h * padding; 1067 | const paddedW = originalRect.w * (1 + 2 * padding); 1068 | const paddedH = originalRect.h * (1 + 2 * padding); 1069 | const finalPaddedX = Math.max(0, paddedX); 1070 | const finalPaddedY = Math.max(0, paddedY); 1071 | const finalPaddedX2 = Math.min(sourceImageWidth, paddedX + paddedW); 1072 | const finalPaddedY2 = Math.min(sourceImageHeight, paddedY + paddedH); 1073 | const paddedRect = { x: finalPaddedX, y: finalPaddedY, w: finalPaddedX2 - finalPaddedX, h: finalPaddedY2 - finalPaddedY }; 1074 | const aspect = paddedRect.h / paddedRect.w; 1075 | const targetWidth = 512; 1076 | const targetHeight = Math.round(targetWidth * aspect); 1077 | const croppedForEnhancement = await cropImage(sourceImage, paddedRect, targetWidth, targetHeight, false); 1078 | const prompts = [...descriptionHistory.map(d=>(d.prompt || '')), description.prompt || '']; 1079 | const { imageSrc: enhancedPaddedSrc } = await serviceEnhance(croppedForEnhancement, prompts); 1080 | 1081 | const enhancedPaddedImage = await new Promise((resolve, reject) => { 1082 | const img = new Image(); img.crossOrigin = "anonymous"; img.onload = () => resolve(img); img.onerror = reject; img.src = enhancedPaddedSrc; 1083 | }); 1084 | const finalCropRect = { 1085 | x: enhancedPaddedImage.naturalWidth * ((originalRect.x - paddedRect.x) / paddedRect.w), 1086 | y: enhancedPaddedImage.naturalHeight * ((originalRect.y - paddedRect.y) / paddedRect.h), 1087 | w: enhancedPaddedImage.naturalWidth * (originalRect.w / paddedRect.w), 1088 | h: enhancedPaddedImage.naturalHeight * (originalRect.h / paddedRect.h), 1089 | }; 1090 | const finalImageWidth = 1024; 1091 | const finalImageHeight = Math.round(finalImageWidth * (originalRect.h / originalRect.w)); 1092 | const enhancedSrc = await cropImage(enhancedPaddedImage, finalCropRect, finalImageWidth, finalImageHeight, false); 1093 | const newStep: HistoryStep = { imageSrc: enhancedSrc, description, originalRect }; 1094 | const newHistory = [...history.slice(0, historyIndex), newStep]; 1095 | setHistory(newHistory); 1096 | setDisplaySelection(null); 1097 | const newImage = new Image(); 1098 | newImage.onload = () => { setImage(newImage); setFinalImageSrc(enhancedSrc); setAppState(AppState.LOADED); }; 1099 | newImage.src = enhancedSrc; 1100 | } catch (error) { console.error("Regeneration failed:", error); setAppState(AppState.LOADED); } 1101 | }; 1102 | sourceImage.src = previousStep.imageSrc; 1103 | }, [history, historyIndex, appState, isGeneratingGif]); 1104 | 1105 | const handleExportGif = useCallback(async () => { 1106 | if (historyIndex < 1) return; 1107 | setIsGeneratingGif(true); 1108 | try { 1109 | const blob = await generateZoomGif(history.slice(0, historyIndex + 1)); 1110 | const url = URL.createObjectURL(blob); 1111 | const a = document.createElement('a'); a.href = url; a.download = 'enhancement-zoom.gif'; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); 1112 | } catch (error) { console.error("Failed to generate GIF:", error); 1113 | } finally { setIsGeneratingGif(false); } 1114 | }, [history, historyIndex]); 1115 | 1116 | const stopPropagation = (ev:MouseEvent)=>{ ev.stopPropagation(); } 1117 | 1118 | return ( 1119 |
1120 | {isProcessing && } 1121 | {appState === AppState.IDLE && } 1122 | 1123 |
1124 | {finalImageSrc && ![AppState.ENHANCED, AppState.ENHANCING].includes(appState) && ( 1125 | 1126 | )} 1127 |
1128 | 1129 | {appState === AppState.LOADED && ( 1130 |
1131 | 1138 |
1139 | )} 1140 | 1141 | {enhancementJob && appState === AppState.ENHANCING && finalEnhancementRect && ( )} 1142 | {appState === AppState.ENHANCED && pixelatedImageSrc && enhancedImageSrc && finalEnhancementRect && ( 1143 |
1144 | 1145 |
1146 | )} 1147 | 1148 | {appState === AppState.LOADED && history.length >= 1 && ( 1149 |
1150 | {stagedSelection ? ( 1151 | <> 1152 | 1153 | 1154 | 1155 | ) : ( 1156 | <> 1157 | 1158 |
1159 | Step: {historyIndex + 1} / {history.length} 1160 | Zoom: {historyIndex + 1}x 1161 |
1162 | 1163 | 1164 | 1165 | 1166 | 1167 | )} 1168 |
1169 | )} 1170 | 1171 | 1172 |
1173 | ); 1174 | } --------------------------------------------------------------------------------