master 0082d79eb9a4 cached
16 files
190.6 KB
50.1k tokens
131 symbols
1 requests
Download .txt
Repository: mcmonkeyprojects/sd-infinity-grid-generator-script
Branch: master
Commit: 0082d79eb9a4
Files: 16
Total size: 190.6 KB

Directory structure:
gitextract_jp1goxq0/

├── .github/
│   └── FUNDING.yml
├── .gitignore
├── LICENSE.txt
├── README.md
├── assets/
│   ├── a1111webui.js
│   ├── grid.schema.json
│   ├── images/
│   │   └── put-usable-images-here.txt
│   ├── jsgif.js
│   ├── page.html
│   ├── proc.js
│   └── styles.css
├── gridgencore.py
├── install.py
├── javascript/
│   └── infinity_grid.js
├── scripts/
│   └── infinity_grid.py
└── style.css

================================================
FILE CONTENTS
================================================

================================================
FILE: .github/FUNDING.yml
================================================
github: mcmonkey4eva


================================================
FILE: .gitignore
================================================
.vscode/
assets/*.yml
assets/styles-user.css
__pycache__/
assets/images/**/*.png
assets/images/**/*.jpg
assets/images/**/*.webp


================================================
FILE: LICENSE.txt
================================================
The MIT License (MIT)

Copyright (c) 2022-2023 Alex "mcmonkey" Goodwin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


================================================
FILE: README.md
================================================
# Stable Diffusion Infinity Grid Generator

![img](github/megagrid_ref.png)

### Concept

Extension for the [AUTOMATIC1111 Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui) that generates infinite-dimensional grids.

An "infinite axis grid" is like an X/Y plot grid, but with, well, more axes on it. Of course, monitors are 2D, so this is implemented in practice by generating a webpage that lets you select which two primary axes to display, and then choose the current value for each of the other axes.

### Goals and Use Cases

The primary goal is to let people generate their own fancy grids to explore how different settings affect their renders in a convenient form.

Another goal of this system is to develop educational charts, to provide a universal answer to the classic question of "what does (X) setting do? But what about with (Y)?" - [The MegaGrid](https://sd.mcmonkey.org/megagrid/). There is a built in ability to add description text to fields, for the specific purpose of enhancing educational page output.

### Pros/Cons

The advantage of this design is it allows you to rapidly compare the results of different combinations of settings, without having to wait to for generation times for each specific sub-grid as-you-go - you just run it all once in advance (perhaps overnight for a large run), and then after that browse through it in realtime.

The disadvantage is that time to generate a grid is exponential - if you have 5 samplers, 5 seeds, 5 step counts, 5 CFG scales... that's 5^4, or 625 images. Add another variable with 5 options and now it's 3125 images. You can see how this quickly jumps from a two minute render to a two hour render.

--------------

### Table of Contents

- [Examples](#Examples)
- [Status](#Status)
- [Installation](#Installation)
- [Basic Usage](#Basic-Usage)
- [Advanced Usage](#Advanced-Usage)
    - [1: Grid Definition File](#1-grid-definition-file)
    - [Supported Extensions](#supported-extensions)
        - [Dynamic Thresholding (CFG Scale Fix)](#dynamic-thresholding-cfg-scale-fix)
        - [ControlNet](#controlnet)
    - [2: Grid Content Generation via WebUI](#2-grid-content-generation-via-webui)
    - [3: Using The Output](#3-using-the-output)
    - [4: Expanding Later](#4-expanding-later)
- [Credits](#credits)
- [Common Issues](#common-issues)
- [License](#License)

--------------

### Examples

Here's a big MegaGrid using almost every mode option in one, with detailed educational descriptions on every part: https://sd.mcmonkey.org/megagrid/

Here's a very small web demo you can try to test how the output looks and works: https://mcmonkeyprojects.github.io/short_example and you can view the generated asset files for that demo [here](https://github.com/mcmonkeyprojects/mcmonkeyprojects.github.io/tree/master/short_example).

![img](github/simple_ref.png)

--------------

### Status

Current overall project status (as of December 2023): **Works well, actively maintained**. Has been generally well tested. The core has been ported to other environments and stress-tested for multiple real projects that depend on large grids and rapid grid analysis.

A version of this project is also available for [StableSwarmUI, here](https://github.com/Stability-AI/StableSwarmUI/blob/master/src/BuiltinExtensions/GridGenerator/README.md) which also lets you use it with Comfy and other backends

--------------

### Installation

- You must have the [AUTOMATIC1111 Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui) already installed and working. Refer to that project's readme for help with that.
- Open the WebUI, go to to the `Extensions` tab
- -EITHER- Option **A**:
    - go to the `Available` tab with
    - click `Load from` (with the default list)
    - Scroll down to find `Infinity Grid Generator`, or use `CTRL+F` to find it
- -OR- Option **B**:
    - Click on `Install from URL`
    - Copy/paste this project's URL into the `URL for extension's git repository` textbox: `https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script`
- Click `Install`
- Restart or reload the WebUI

--------------

### Basic Usage

![img](github/create_in_ui_demo.png)

- Basic usage is very simple!
    - select the script under `Scripts`
    - Leave file as `Create in UI`
    - Select `Axis 1 Mode` as whatever mode you want, like `Seed` or `Sampler`
    - In the `Axis 1 Value` box, you can type a comma-separated list like `1, 2, 3`, or a double-pipe separated list like `1girl, booru style, prompt || 1boy, has commas, so pipes avoid problems`
    - Repeat for as many axes as you want. When you configure axis #4, it will automatically add 4 more rows of axes to fill in. Leave any extras empty.
    - Click your main `Generate` button and wait for it to finish.
    - When it's done, click the button labeled `Page will be at (Click me) outputs/...` to view the full page.
    - Note that a `config.yml` is saved in the output directory, which you can copy to your `assets` folder if you want to reuse it.

--------------

### Advanced Usage

Usage comes in three main steps:
- [1: Build a grid definition](#1-grid-definition-file)
- [2: Generate its contents](#2-grid-content-generation-via-webui)
- [3: View/use the generated output page](#3-using-the-output)
- You can also [expand your grid later](#4-expanding-later)

--------------

### 1: Grid Definition File

![img](github/yaml_settings_blur.png)

- Grid information is defined by **YAML files**, in the extension folder under `assets`. Find the `assets/short_example.yml` file to see an example of the full format.

**If you do not want to follow an example file:**
- You can **create new files** in the assets directory (as long as the `.yml` extension stays), or copy/paste an example file and edit it. I recommend you do not edit the actual example file directly to avoid git issues.
- I recommend editing with a **good text editor**, such as *VS Code* or *Notepad++*. Don't use *MS Word* or *Windows Notepad* as those might cause trouble.
- All text inputs allow for **raw HTML**, so, be careful. You can use `&lt;` for `<`, and `&gt;` for `>`, `&#58;` for `:`, and `&amp;` for `&`.
- The file must have key `grid`, with subkey `title` and `description` to define the file data.
    - It must also have `format` as `jpg` or `png`
    - It can optionally also have `params` to specify any default parameters.
    - It can optionally define `show descriptions`, `autoscale`, and `sticky` as `true` or `false` to change default web-viewer settings.
    - It can optionally define `x axis`, `y axis`, `x super axis`, and `y super axis` as axis IDs to change the default web-viewer axes.
- The file can optionally have key `variables` with subkey/value pairs as replacements, for example `(type): waffle` - then later in a param value you can use `a picture of a (type)` to automatically fill the variable. These can be in any format you desire, as they are simple text replacements. They apply to all values, including titles, descriptions, and params (this was added for [valconius in issue #16](https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script/issues/16)).
- The file must have key `axes` to define the list of axes. This is a map-list key - meaning, add subkeys to create a list of each axis.
    - The simplest format for an axis is a setting name as the key and a comma-separated list of values inside.
        - For example, `seed: 1, 2, 3` is a valid and complete axis.
        - If commas may be problematic, you many instead use two pipes `||` to separate values - for example `prompt: 1girl, booru style, commas || 1boy, more commas, etc`. If `||` is used, commas will be ignored for splitting
        - For numeric inputs, you can use ellipses notation to quickly shorthand long lists. For example: `seed: 1, 2, ..., 10` automatically fills to `1, 2, 3, 4, 5, 6, 7, 8, 9, 10`. Or, `cfg scale: 7.0, 7.1, ..., 7.8` automatically fills to `7.0, 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8`.
            - Note that you must have two entries before and one after - the step rate is defined as the space between the two prior and the total range is defined as between the one prior and the one after.
            - You can use ellipses multiple times in one set, for example `1, 2, ..., 6, 8, ... 14` will fill to `1, 2, 3, 4, 5, 6, 8, 10, 12, 14` (note how 1-6 are spaced 1 apart, but 6-14 are spaced two apart).
    - Each axis must have a `title`, and `values`. It can optionally have a `description`.
        - You can also optionally have `default: (value_id)` to set the default selected tab.
        - There are two ways to do a value in the value list:
            - Option 1: just do like `steps=10` ... this will set title to `10`, and param `steps` to value `10`, with no description.
            - Option 2: Add a submapping with key `title`, and optional `description`, and then `params` as a sub map of parameters like `steps: 10`
                - A value with a full format can be set to skip rendering via adding `skip: true`. This is useful for eg if some values are only valid in certain combinations, you can do runs with some skipped. Skipped values will be kept in the HTML output.
                - A value may specify `show: false` to default uncheck the 'Show value' advanced option.

Micro example:
```yml
grid:
    title: Tiny example
    author: someone
    description: This is just to show core format. View the example `.yml` files in assets for better examples.
    format: jpg
axes:
    sampler: Euler, DDIM
    seed: 1, 2, 3
```

- Names and descriptions can always be whatever you want, as HTML text.


#### Settings supported for parameters

| Name | Type | Example | Notes |
| --- | --- | --- | ----------- |
| `Sampler` | Named | `DDIM`, `euler`, ... | |
| `Model` | Filename | `sd-v1-5` | Note that `Model` and `VAE` are **global settings**, and as such you should not have an axis where some values specify one of those params but others don't, as this will cause an unpredictable model selection for the values that lack specificity. |
| `VAE` | Filename | `kl-f8-anime2` | See note on `Model` above |
| `Prompt` | Text | `a cat` | |
| `Negative Prompt` | Text | `gross, weird, bad` | |
| `Prompt Replace` | Text-Pair | `some_tag = new text here` | Note the `=` symbol to separate the original text with the new text. That will change a prompt of for example `my prompt with some_tag stuff` to `my prompt with new text here stuff`.<br>Unlike other modes, the PromptReplace is case-sensitive - if you use capitals in your prompt, you need capitals in your replace matcher.<br>If you want multiple replacements in one value, you can number them, like `Prompt Replace 1` and `Prompt Replace 2` and etc.<br>When building a simple list in UI, you can just do eg `cat, dog, waffle` and it will understand to replace `cat` in the base prompt first with `cat`, then `dog`, then `waffle` (ie you can skip the `=` for simple usages). |
| `Styles` | Text | `photo`, `cartoon`, ... | Automatically includes UI styles to your prompt. Can use comma-separated list to have multiple. Note this means in the UI you can do for example `photo, cinematic ||| anime, cartoon` to compare multiple combos easily. |
| `Seed` | Integer | `1`, `2`, `3`, ... | |
| `Steps` | Integer | `20`, `50`, ... | |
| `CFG Scale` | Decimal | `5`, `7.5`, `12`, ... | |
| `Width` | Integer | `512`, `768`, ... | Initial generation width. |
| `Height` | Integer | `512`, `768`, ... | Initial generation height. |
| `Out Width` | Integer | `512`, `768`, ... | What resolution to save the image as (if unspecified, uses `Width`). Useful to save filespace. |
| `Out Height` | Integer | `512`, `768`, ... | Refer to `Out Width`. |
| `Clip Skip` | Integer | `1`, `2` | Use `2` for NAI-like models, `1` for the rest. |
| `Var Seed` | Integer | `0`, `1`, ... | Variation seed, use with `Var Strength`. |
| `Var Strength` | Decimal | `0`, `0.5`, ..., `1` | Variation seed strength. |
| `Restore Faces` | Named | `true`, `false`, `GFPGan`, `CodeFormer` | Limited to the given example inputs only. |
| `CodeFormer Weight` | Decimal | `0`, `0.5`, ..., `1` | Only applicable if `Restore Faces` is set to `CodeFormer`. |
| `Denoising` | Decimal | `0`, `0.5`, ..., `1` | Denoising strength for img2img or HR fix. |
| `ETA` | Decimal | `0`, `0.5`, ..., `1` | ? |
| `ETA Noise Seed Delta` | Integer |  `0`, `31337` | use `31337` to replicate NovelAI results, use `0` for anything else. Not very useful. |
| `Sigma Churn` | Decimal | `0`, `0.5`, ..., `1` | Sampler parameter, rarely used. |
| `Sigma Tmin` | Decimal | `0`, `0.5`, ..., `1` | Sampler parameter, rarely used. |
| `Sigma Tmax` | Decimal | `0`, `0.5`, ..., `1` | Sampler parameter, rarely used. |
| `Sigma Noise` | Decimal | `0`, `0.5`, ..., `1` | Sampler parameter, rarely used. |
| `Tiling` | Boolean | `true`, `false` | Useful for textures. |
| `Image Mask Weight` | Decimal | `0`, `0.5`, ..., `1` | Conditional image mask weight. Only applies to img2img or HR fix. |
| `Enable Highres Fix` | Boolean | `true`, `false` | Required for other HR settings to work. Defaults denoising strength to `0.75` if not specified. Only valid in txt2img. |
| `Highres Scale` | Decimal | `2`, `2.5`, ..., `16` | How much to scale by for HR fix. |
| `Highres Steps` | Integer | `20`, `50`, ... | Secondary steps for HR fix. |
| `Highres Upscaler` | Named | `None`, `Latent`, ... | Upscaler mode to use prior to running Highres Fix. |
| `Highres Resize Width` | Integer | `512`, `768`, ... | Resolution to target as final output size for Highres Fix, overrides `Highres Scale`. |
| `Highres Resize Height` | Integer | `512`, `768`, ... | See `Highres Resize Width` above. |
| `Highres Upscale To Width` | Integer | `512`, `768`, ... | Resolution to upscale to prior to running Highres Fix. |
| `Highres Upscale To Height` | Integer | `512`, `768`, ... | See `Highres Upscale To Height` above. |
| `Image CFG Scale` | Decimal | `5`, `7.5`, `12`, ... | Image CFG Scale, for Instruct pix2pix usage. |
| `Use Result Index` | Integer | `0`, `1`, ... | Special trick to get a non-zero result image index, eg for ControlNet secondary output image. |

- All setting names are **case insensitive and spacing insensitive**. That means `CFG scale`, `cfgscale`, `CFGSCALE`, etc. are all read as the same.
    - Inputs where possible also similarly insensitive, including model names.
    - Inputs have error checking at the start, to avoid the risk of it working fine until 3 hours into a very big grid run.
- Note that it will be processed from bottom to top
    - so if you have  first `samplers` DDIM and Euler, then `steps` 20 and 10, then `seeds` 1 and 2, it will go in this order:
        - Sampler=DDIM, Steps=20, Seed=1
        - Sampler=DDIM, Steps=20, Seed=**2**
        - Sampler=DDIM, Steps=**10**, Seed=1
        - Sampler=DDIM, Steps=10, Seed=2
        - Sampler=**Euler**, Steps=20, Seed=1
        - Sampler=Euler, Steps=20, Seed=2
        - Sampler=Euler, Steps=10, Seed=1
        - Sampler=Euler, Steps=10, Seed=2
    - So, things that take time to load, like `Model`, should be put near the top, so they don't have to be loaded repeatedly.

--------------

### Supported Extensions

#### Dynamic Thresholding (CFG Scale Fix)

Extension docs: <https://github.com/mcmonkeyprojects/sd-dynamic-thresholding>

| Name | Type | Example | Notes |
| --- | --- | --- | ----------- |
| `DynamicThreshold Enable` | Boolean | `true`, `false` | |
| `DynamicThreshold Mimic Scale` | Decimal | `5`, `7.5`, `12`, ... | |
| `DynamicThreshold Threshold Percentile` | Decimal | `0`, `0.5`, ..., `1.0` | |
| `DynamicThreshold Mimic Mode` | Named | `Constant`, `Linear Down`, `Cosine Down`, `Half Cosine Down`, `Linear Up`, `Cosine Up`, `Half Cosine Up`, `Power Up` | |
| `DynamicThreshold CFG Mode` | Named | `Constant`, `Linear Down`, `Cosine Down`, `Half Cosine Down`, `Linear Up`, `Cosine Up`, `Half Cosine Up`, `Power Up` | |
| `DynamicThreshold Mimic Scale Minimum` | Decimal | `5`, `7.5`, `12`, ... | |
| `DynamicThreshold CFG Scale Minimum` | Decimal | `5`, `7.5`, `12`, ... | |
| `DynamicThreshold Power Value` | Decimal | `2`, `4`, ... | For `Power Up` mode only. |
| `DynamicThreshold Scaling Startpoint` | Named | `ZERO`, `MEAN` | |
| `DynamicThreshold Variability Measure` | Named | `STD`, `AD` | |
| `DynamicThreshold Interpolate Phi` | Decimal | `0`, `0.5`, ..., `1.0` | |
| `DynamicThreshold Separate Feature Channels` | Boolean | `true`, `false` | |

#### ControlNet

Extension docs: <https://github.com/Mikubill/sd-webui-controlnet>

Note: must enable `Allow other script to control this extension` in `Settings` -> `ControlNet`

| Name | Type | Example | Notes |
| --- | --- | --- | ----------- |
| `ControlNet Enable` | Boolean | `true`, `false` | |
| `ControlNet Preprocessor` | Named | `none`, `canny`, `depth`, `hed`, `mlsd`, `normal_map`, `openpose`, `openpose_hand`, `pidinet`, `scribble`, `fake_scribble`, `segmentation` | |
| `ControlNet Model` | Named | `diff_control_sd15_canny_fp16`, ... | |
| `ControlNet Weight` | Decimal | `0.0`, `0.5`, ..., `2.0` | |
| `ControlNet Guidance Strength` | Decimal | `0.0`, `0.5`, ..., `1.0` | |
| `ControlNet Annotator Resolution` | Integer | `64`, `512`, ..., `2048` | |
| `ControlNet Threshold A` | Integer | `64`, `512`, ..., `256` | |
| `ControlNet Threshold B` | Integer | `64`, `512`, ..., `256` | |
| `ControlNet Image` | Text | `pose.png`, `otherpose.jpg`, ... | Put image files in `(EXTENSION FOLDER)/assets/images/`, as `png`, `jpg`, or `webp`. Subfolders allowed. |

#### Other Extensions

- Any extension has the ability to add its own modes with the following code:
```py
# Verify grid extension is present
import importlib
if importlib.util.find_spec("gridgencore") is not None:
    import gridgencore
    from gridgencore import GridSettingMode
    # p is the SD processing object, v is the value
    def apply(p, v):
        p.some_setting_here = v
    # dry: bool, type: str, apply: callable, min: float = None, max: float = None, clean: callable = None
    gridgencore.registerMode("mySettingNameHere", GridSettingMode(True, "text", apply))
    # for apply if the param is a 'p' field, you can use gridgencore.apply_field("fieldname")
```

--------------

### 2: Grid Content Generation via WebUI

![img](github/webui_ref.png)

- Open the WebUI
- Go to the `txt2img` or `img2img` tab
- At the bottom of the page, find the `Script` selection box, and select `Generate Infinite-Axis Grid`
- Select options at will. You can hover your mouse over each option for extra usage information.
- Select your grid definition file from earlier.
    - If it's not there, you might just need to hit the Refresh button on the right side.
    - If it's still not, there double check that your file is in the `assets/` folder of the extension, and that it has a proper `.yml` extension.
- Hit your `Generate` button (the usual big orange one at the top), and wait.
- The output folder will be named based on your `.yml` file's name.

--------------

### 3: Using The Output

![img](github/files_ref.png)

- Find the `index.html` file
    - It's normally in `(your grid output directory)/(filename)/index.html`
        - The example file might output to `outputs/grids/short_example/index.html`
- Open the HTML file in a browser. Enjoy.
- If you want to share the content, just copy/paste the whole folder into a webserver somewhere.
    - Or upload to github and make GitHub.io pages host it for you. [See example here](https://github.com/mcmonkeyprojects/mcmonkeyprojects.github.io)
- You have a few different clickable options:
    - `Show descriptions of axes and values`: if you used descriptions, you can uncheck this box to hide them. Helps save space for direct viewing.
    - `Auto-scale images to viewport width`: this is handy for a few different scenarios
        - A: if your images are small and your screen is big, checking this option makes them bigger
        - B: if your images are so big they're going off the edge, checking this option makes them smaller
        - C: if checked, you can zoom in/out of the page using your browser zoom (CTRL + Mouse Wheel) to change the UI size without affecting the image size
            - if unchecked, you can zoom in/out to change the size of the images.
    - `Sticky navigation`: when checked, the navigation will stick to the top of the screen as you scroll down. Helps you quickly change axes while scrolling around without losing your place.
    - `Advanced Settings`: when clicked, it will open a dropdown with more advanced settings
        - ![img](github/advanced_settings_ref.png)
        - `Auto cycle every (x) seconds`: you can set these to non-zero values to the grid automatically change settings over time. For example, if you set your "Seed" option to "3 seconds", then every 3 seconds the seed will change (cycling between the options you have in order). This was suggested by [itswhateverman in issue #2](https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script/issues/2).
        - `Show value`: you can uncheck any box to hide a value from the grid. Helps if you want to ignore some of the values and get a clean grid of just the ones you care about. This was suggested by [piyarsquare in issue #4](https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script/issues/4).
    - You can also click on any image to view it fullscreen and see its metadata (if included in output). While in this mode, you can use arrow keys to quick navigate between images, or press Escape to close (This suggested by [itswhateverman in issue #5](https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script/issues/5) and [issue #17](https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script/issues/17)).
    - You can also set the `X Super-axis` and `Y Super-axis` to unique axes to get a grid-of-grids!
        - ![img](github/super_axis_demo.png)
    - You can also quicksave preview images of the grid, or even generate GIFs that autoscroll through an axis, all with a few easy buttons on the viewer page!

--------------

### 4: Expanding Later

If you want to add more content to a grid you already made, you can do that:

- Use the same `yml` file.
- You can add new values to an axis freely.
- If you remove values, they will be excluded from the output but pre-existing generated images won't be removed.
- You can add axes, but you'll have to regenerate all images if so.
    - Probably save as a new filename in that case.
- If you're just adding a new value, make sure to leave `overwriting existing images` off.

----------------------

### Credits

- This design was partially inspired by the "XYZ Plot" script by "xrypgame" (not to be confused with the "XYZ Plot" script in Auto WebUI which is actually just the "X/Y Plot" script but they added a "Z" lol)
- Sections of code are referenced from the WebUI itself, and its default "X/Y Plot" script (since renamed to labeled "XYZ Plot").
- Some sections of code were referenced from various other relevant sources, for example the Dreambooth extension by d8ahazard was referenced for a JavaScript code trick (titles override).
- Some sections of code were referenced from, well, random StackOverflow answers and a variety of other googled up documentation and answer sites. I haven't kept track of them, but I'm glad to live in a world where so many developers are happy and eager to help each other learn and grow. So, thank you to all members of the FOSS community!
- Thanks to the authors of all [merged PRs](https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script/pulls?q=is%3Apr+is%3Aclosed+is%3Amerged).
- Thanks to the authors of all issues labeled as [Completed](https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script/issues?q=label%3ACompleted).
- Thanks to StabilityAI, RunwayML, CompVis for Stable Diffusion, and the researchers whose work was incorporated.
- Thanks to AUTOMATIC1111 and the long list of contributors for the WebUI.

----------------------

### Common Issues

```
  File "stable-diffusion-webui\modules\images.py", line 508, in _atomically_save_image
    image_format = Image.registered_extensions()[extension]
KeyError: '.jpg'
```
If you have this error, just hit generate again. I'm not sure why it happens, it just does at random sometimes on the first time the WebUI starts up. It seems to happen when you use `OutWidth`/`OutHeight` settings and is prevented by running any generation without a custom out-resolution. Might be some required initialization is getting skipped when an image is rescaled?

----------------------

### Licensing pre-note:

This is an open source project, provided entirely freely, for everyone to use and contribute to.

If you make any changes that could benefit the community as a whole, please contribute upstream.

### The short of the license is:

You can do basically whatever you want, except you may not hold any developer liable for what you do with the software.

### The long version of the license follows:

The MIT License (MIT)

Copyright (c) 2022-2023 Alex "mcmonkey" Goodwin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


================================================
FILE: assets/a1111webui.js
================================================
/**
 * This file is part of Infinity Grid Generator, view the README.md at https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script for more information.
 */

function genParamQuote(text) {
    // Referenced to match generation_parameters_copypaste.py - quote(text)
    if (!text.includes(',')) {
        return text;
    }
    return '"' + text.toString().replaceAll('\\', '\\\\').replaceAll('"', '\\"') + '"';
}

function formatMet(name, val, bad) {
    if (val == null) {
        return '';
    }
    val = val.toString();
    if (bad != undefined && val == bad) {
        return '';
    }
    return name + ': ' + genParamQuote(val) + ', ';
}

function formatMetadata(valSet) {
    var count = Object.keys(valSet).length;
    if (count == 0) {
        return '';
    }
    else if (count == 1) {
        return valSet['error'];
    }
    // Referenced to match processing.py - create_infotext(p)
    var negative = valSet['negativeprompt'];
    if (negative.length > 0) {
        negative = '\nNegative prompt: ' + negative;
    }
    const handled = ['steps', 'sampler', 'cfgscale', 'seed', 'restorefaces', 'width', 'height', 'model', 'varseed', 'varstrength', 'denoising', 'eta', 'clipskip', 'vae', 'sigmachurn', 'sigmatmin', 'sigmatmax', 'sigmanoise', 'prompt', 'negativeprompt', 'codeformerweight'];
    var keyData = formatMet('Steps', valSet['steps'])
        + formatMet('Sampler', valSet['sampler'])
        + formatMet('CFG scale', valSet['cfgscale'])
        + formatMet('Seed', valSet['seed'])
        + formatMet('Face restoration', valSet['restorefaces'], 'false')
        + formatMet('Size', valSet['width'] + 'x' + valSet['height'])
        // model hash
        + formatMet('Model', valSet['model'])
        // Batch size, batch pos
        + formatMet('Variation seed', valSet['varseed'], '0')
        + formatMet('Variation seed strength', valSet['varstrength'], '0')
        // Seed resize from
        + formatMet('Denoising strength', valSet['denoising'])
        // Conditional mask weight
        + formatMet('Eta', valSet['eta'])
        + formatMet('Clip skip', valSet['clipskip'], '1')
        // ENSD
        ;
        // Not part of normal gen-params
    var extraData = formatMet('VAE', valSet['vae'])
        + formatMet('Sigma Churn', valSet['sigmachurn'], '0')
        + formatMet('Sigma T-Min', valSet['sigmatmin'], '0')
        + formatMet('Sigma T-Max', valSet['sigmatmax'], '1')
        + formatMet('Sigma Noise', valSet['sigmanoise'], '1')
        + (valSet['restorefaces'] == 'CodeFormer' ? formatMet('CodeFormer Weight', valSet['codeformerweight']) : '');
    var lastData = '';
    for (const [key, value] of Object.entries(valSet)) {
        if (!handled.includes(key) && value != null) {
            lastData += `${key}: ${value}, `;
        }
    }
    if (lastData.length > 2) {
        lastData = '\n(Other): ' + lastData.substring(0, lastData.length - 2);
    }
    keyData = keyData.substring(0, keyData.length - 2);
    if (extraData.length > 2) {
        extraData = extraData.substring(0, extraData.length - 2);
    }
    return valSet['prompt'] + negative + '\n' + keyData + '\n' + extraData + lastData;
}

function crunchParamHook(data, key, value) {
    if (key == 'promptreplace') {
        var replacers = value.split('=', 2);
        var match = replacers[0].trim();
        var replace = replacers[1].trim();
        data['prompt'] = data['prompt'].replaceAll(match, replace);
        data['negativeprompt'] = data['negativeprompt'].replaceAll(match, replace);
        return true;
    }
    return false;
}


================================================
FILE: assets/grid.schema.json
================================================
{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "type": "object",
    "properties": {
        "grid": {
            "$ref": "#/definitions/Grid"
        },
        "axes": {
            "$ref": "#/definitions/Axes"
        },
        "variables": {
            "$ref": "#/definitions/Variables"
        }
    },
    "required": [
        "axes",
        "grid"
    ],
    "title": "InfinityGrid",
    "definitions": {
        "Variables": {
            "type": "object",
            "patternProperties": {
                "\\(.*\\)": {
                    "oneOf": [
                        {"type": "string"},
                        {"type": "number"}
                    ]
                }
            }
        },
        "Axes": {
            "type": "object",
            "patternProperties": {
                ".*": {
                    "$ref": "#/definitions/Axis" 
                }
            },
            "title": "Axes"
        },
        "Axis": {
            "oneOf": [{
                "type": "string"
            },
            {
                "type": "object",
                "properties": {
                    "title": {
                        "oneOf": [
                            {"type": "string"},
                            {"type": "number"}
                        ]
                    },
                    "default": {
                        "oneOf": [{
                                "type": "string"
                            },
                            {
                                "type": "number"
                            }
                        ]
                    },
                    "description": {
                        "type": "string"
                    },
                    "values": {
                        "patternProperties": {
                            ".*": {
                                "$ref": "#/definitions/Value"
                            }
                        }
                    }
                },
                "required": ["title", "values"]
            }]
        },
        "Value": {
            "oneOf": [{
                "type": "string",
                "pattern": "(Sampler|sampler|Model|model|VAE|vae|Prompt|prompt|NegativePrompt|negativeprompt|Negative Prompt|negative prompt|PromptReplace|promptreplace|Prompt Replace|prompt replace|Prompt Replace|prompt replace|Seed|seed|Steps|steps|CFGScale|cfgscale|CFG Scale|cfg scale|CFGscale|CFG scale|Width|width|Height|height|OutWidth|outwidth|Out Width|out width|OutHeight|outheight|Out Height|out height|ClipSkip|clipskip|Clip Skip|clip skip|VarSeed|varseed|Var Seed|var seed|VarStrength|varstrength|Var Strength|var strength|CodeFormerWeight|codeformerweight|CodeFormer Weight|codeformer weight|Denoising|denoising|ETA|eta|ETANoiseSeedDelta|etanoiseseeddelta|ETA Noise Seed Delta|eta noise seed delta|SigmaChurn|sigmachurn|Sigma Churn|sigma churn|SigmaTmin|sigmatmin|Sigma Tmin|sigma tmin|SigmaTmax|sigmatmax|Sigma Tmax|sigma tmax|SigmaNoise|sigmanoise|Sigma Noise|sigma noise|Tiling|tiling|ImageMaskWeight|imagemaskweight|Image Mask Weight|image mask weight|EnableHighresFix|enablehighresfix|Enable Highres Fix|enable highres fix|HighresScale|highresscale|Highres Scale|highres scale|HighresSteps|highressteps|Highres Steps|highres steps|HighresUpscaler|highresupscaler|Highres Upscaler|highres upscaler|HighresResizeWidth|highresresizewidth|Highres Resize Width|highres resize width|HighresResizeHeight|highresresizeheight|Highres Resize Height|highres resize height|HighresUpscaleToWidth|highresupscaletowidth|Highres Upscale To Width|highres upscale to width|HighresUpscaleToHeight|highresupscaletoheight|Highres Upscale To Height|highres upscale to height|RestoreFaces|Restore Faces|restorefaces|restore faces|([dD]ynamic\\s?[Tt]hreshold.*)|)\\=.*"
            },
            {
                "type": "object",
                "properties": {
                    "title": {
                        "oneOf": [
                            {"type": "string"},
                            {"type": "number"}
                        ]
                    },
                    "skip": {
                        "type": "boolean"
                    },
                    "show": {
                        "type": "boolean"
                    },
                    "description": {
                        "type": "string"
                    },
                    "params": {
                        "$ref": "#/definitions/GridParams"
                    }
                },
                "required": ["title", "params"]
            }
        ]
        },
        "Grid": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "title": {
                    "type": "string"
                },
                "author": {
                    "type": "string"
                },
                "description": {
                    "type": "string"
                },
                "params": {
                    "$ref": "#/definitions/GridParams"
                },
                "format": {
                    "type": "string",
                    "enum": ["png", "jpg", "webp"]
                }
            },
            "required": [
                "author",
                "description",
                "format",
                "title"
            ],
            "title": "Grid"
        },
        "GridParams": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "Sampler": { "type": "string" }, "sampler": { "type": "string" },
                "Model": { "type": "string" }, "model": { "type": "string" },
                "VAE": { "type": "string" }, "vae": { "type": "string" },
                "Prompt": { "type": "string" }, "prompt": { "type": "string" },
                "NegativePrompt": { "type": "string" }, "negativeprompt": { "type": "string" }, "Negative Prompt": { "type": "string" }, "negative prompt": { "type": "string" },
                "PromptReplace": { "type": "string" }, "promptreplace": { "type": "string" }, "Prompt Replace": { "type": "string" }, "prompt replace": { "type": "string" }, "Prompt  Replace": { "type": "string" }, "prompt  replace": { "type": "string" },
                "Seed": { "type": "integer" }, "seed": { "type": "integer" },
                "Steps": { "type": "integer" }, "steps": { "type": "integer" },
                "CFGScale": { "type": "number" }, "cfgscale": { "type": "number" }, "CFG Scale": { "type": "number" }, "cfg scale": { "type": "number" }, "CFGscale": { "type": "number" }, "CFG scale": { "type": "number" },
                "Width": { "type": "integer" }, "width": { "type": "integer" },
                "Height": { "type": "integer" }, "height": { "type": "integer" },
                "OutWidth": { "type": "integer" }, "outwidth": { "type": "integer" }, "Out Width": { "type": "integer" }, "out width": { "type": "integer" },
                "OutHeight": { "type": "integer" }, "outheight": { "type": "integer" }, "Out Height": { "type": "integer" }, "out height": { "type": "integer" },
                "ClipSkip": { "type": "integer" }, "clipskip": { "type": "integer" }, "Clip Skip": { "type": "integer" }, "clip skip": { "type": "integer" },
                "VarSeed": { "type": "integer" }, "varseed": { "type": "integer" }, "Var Seed": { "type": "integer" }, "var seed": { "type": "integer" },
                "VarStrength": { "type": "number" }, "varstrength": { "type": "number" }, "Var Strength": { "type": "number" }, "var strength": { "type": "number" },
                "CodeFormerWeight": { "type": "number" }, "codeformerweight": { "type": "number" }, "CodeFormer Weight": { "type": "number" }, "codeformer weight": { "type": "number" },
                "Denoising": { "type": "number" }, "denoising": { "type": "number" },
                "ETA": { "type": "number" }, "eta": { "type": "number" },
                "ETANoiseSeedDelta": { "type": "integer" }, "etanoiseseeddelta": { "type": "integer" }, "ETA Noise Seed Delta": { "type": "integer" }, "eta noise seed delta": { "type": "integer" },
                "SigmaChurn": { "type": "number" }, "sigmachurn": { "type": "number" }, "Sigma Churn": { "type": "number" }, "sigma churn": { "type": "number" },
                "SigmaTmin": { "type": "number" }, "sigmatmin": { "type": "number" }, "Sigma Tmin": { "type": "number" }, "sigma tmin": { "type": "number" },
                "SigmaTmax": { "type": "number" }, "sigmatmax": { "type": "number" }, "Sigma Tmax": { "type": "number" }, "sigma tmax": { "type": "number" },
                "SigmaNoise": { "type": "number" }, "sigmanoise": { "type": "number" }, "Sigma Noise": { "type": "number" }, "sigma noise": { "type": "number" },
                "Tiling": { "type": "boolean" }, "tiling": { "type": "boolean" },
                "ImageMaskWeight": { "type": "number" }, "imagemaskweight": { "type": "number" }, "Image Mask Weight": { "type": "number" }, "image mask weight": { "type": "number" },
                "EnableHighresFix": { "type": "boolean" }, "enablehighresfix": { "type": "boolean" }, "Enable Highres Fix": { "type": "boolean" }, "enable highres fix": { "type": "boolean" },
                "HighresScale": { "type": "number" }, "highresscale": { "type": "number" }, "Highres Scale": { "type": "number" }, "highres scale": { "type": "number" },
                "HighresSteps": { "type": "integer" }, "highressteps": { "type": "integer" }, "Highres Steps": { "type": "integer" }, "highres steps": { "type": "integer" },
                "HighresUpscaler": { "type": "string" }, "highresupscaler": { "type": "string" }, "Highres Upscaler": { "type": "string" }, "highres upscaler": { "type": "string" },
                "HighresResizeWidth": { "type": "integer" }, "highresresizewidth": { "type": "integer" }, "Highres Resize Width": { "type": "integer" }, "highres resize width": { "type": "integer" },
                "HighresResizeHeight": { "type": "integer" }, "highresresizeheight": { "type": "integer" }, "Highres Resize Height": { "type": "integer" }, "highres resize height": { "type": "integer" },
                "HighresUpscaleToWidth": { "type": "integer" }, "highresupscaletowidth": { "type": "integer" }, "Highres Upscale To Width": { "type": "integer" }, "highres upscale to width": { "type": "integer" },
                "HighresUpscaleToHeight": { "type": "integer" }, "highresupscaletoheight": { "type": "integer" }, "Highres Upscale To Height": { "type": "integer" }, "highres upscale to height": { "type": "integer" },
                "RestoreFaces": { "oneOf": [{ "type" : "boolean"}, { "type": "string", "enum": ["GFPGan", "CodeFormer", "true", "false", "gfpgan", "codeformer"] }]},
                "Restore Faces": { "oneOf": [{ "type" : "boolean"}, { "type": "string", "enum": ["GFPGan", "CodeFormer", "true", "false", "gfpgan", "codeformer"] }]},
                "restorefaces": { "oneOf": [{ "type" : "boolean"}, { "type": "string", "enum": ["GFPGan", "CodeFormer", "true", "false", "gfpgan", "codeformer"] }]},
                "restore faces": { "oneOf": [{ "type" : "boolean"}, { "type": "string", "enum": ["GFPGan", "CodeFormer", "true", "false", "gfpgan", "codeformer"] }]}
            },
            "title": "GridParams"
        }
    }
}


================================================
FILE: assets/images/put-usable-images-here.txt
================================================


================================================
FILE: assets/jsgif.js
================================================
/**
 * This class lets you encode animated GIF files
 * Base class :  http://www.java2s.com/Code/Java/2D-Graphics-GUI/AnimatedGifEncoder.htm
 * @author Kevin Weiner (original Java version - kweiner@fmsware.com)
 * @author Thibault Imbert (AS3 version - bytearray.org)
 * @author Kevin Kwok (JavaScript version - https://github.com/antimatter15/jsgif)
 * @version 0.1 AS3 implementation
 */

GIFEncoder = function() {

	for (var i = 0, chr = {}; i < 256; i++)
		chr[i] = String.fromCharCode(i);

	function ByteArray() {
		this.bin = [];
	}

	ByteArray.prototype.getData = function() {
		for (var v = '', l = this.bin.length, i = 0; i < l; i++)
			v += chr[this.bin[i]];
		return v;
	};

	ByteArray.prototype.writeByte = function(val) {
		this.bin.push(val);
	};

	ByteArray.prototype.writeUTFBytes = function(string) {
		for (var l = string.length, i = 0; i < l; i++)
			this.writeByte(string.charCodeAt(i));
	};

	ByteArray.prototype.writeBytes = function(array, offset, length) {
		for (var l = length || array.length, i = offset || 0; i < l; i++)
			this.writeByte(array[i]);
	};

	var exports = {};
	var width; // image size
	var height;
	var transparent = null; // transparent color if given
	var transIndex; // transparent index in color table
	var repeat = -1; // no repeat
	var delay = 0; // frame delay (hundredths)
	var started = false; // ready to output frames
	var out;
	var image; // current frame
	var pixels; // BGR byte array from frame
	var indexedPixels; // converted frame indexed to palette
	var colorDepth; // number of bit planes
	var colorTab; // RGB palette
	var usedEntry = []; // active palette entries
	var palSize = 7; // color table size (bits-1)
	var dispose = -1; // disposal code (-1 = use default)
	var closeStream = false; // close stream when finished
	var firstFrame = true;
	var sizeSet = false; // if false, get size from first frame
	var sample = 10; // default sample interval for quantizer
	var comment = "Generated by jsgif (https://github.com/antimatter15/jsgif/)"; // default comment for generated gif

	/**
	 * Sets the delay time between each frame, or changes it for subsequent frames
	 * (applies to last frame added)
	 * int delay time in milliseconds
	 * @param ms
	 */

	var setDelay = exports.setDelay = function setDelay(ms) {
		delay = Math.round(ms / 10);
	};

	/**
	 * Sets the GIF frame disposal code for the last added frame and any
	 *
	 * subsequent frames. Default is 0 if no transparent color has been set,
	 * otherwise 2.
	 * @param code
	 * int disposal code.
	 */

	var setDispose = exports.setDispose = function setDispose(code) {
		if (code >= 0) dispose = code;
	};

	/**
	 * Sets the number of times the set of GIF frames should be played. Default is
	 * 1; 0 means play indefinitely. Must be invoked before the first image is
	 * added.
	 *
	 * @param iter
	 * int number of iterations.
	 * @return
	 */

	var setRepeat = exports.setRepeat = function setRepeat(iter) {
		if (iter >= 0) repeat = iter;
	};

	/**
	 * Sets the transparent color for the last added frame and any subsequent
	 * frames. Since all colors are subject to modification in the quantization
	 * process, the color in the final palette for each frame closest to the given
	 * color becomes the transparent color for that frame. May be set to null to
	 * indicate no transparent color.
	 * @param
	 * Color to be treated as transparent on display.
	 */

	var setTransparent = exports.setTransparent = function setTransparent(c) {
		transparent = c;
	};


	/**
	 * Sets the comment for the block comment
	 * @param
	 * string to be insterted as comment
	 */

	var setComment = exports.setComment = function setComment(c) {
		comment = c;
	};



	/**
	 * The addFrame method takes an incoming BitmapData object to create each frames
	 * @param
	 * BitmapData object to be treated as a GIF's frame
	 */

	var addFrame = exports.addFrame = function addFrame(im, is_imageData) {

		if ((im === null) || !started || out === null) {
			throw new Error("Please call start method before calling addFrame");
		}

		var ok = true;

		try {
			if (!is_imageData) {
				image = im.getImageData(0, 0, im.canvas.width, im.canvas.height).data;
				if (!sizeSet) setSize(im.canvas.width, im.canvas.height);
			} else {
				if(im instanceof ImageData) {
					image = im.data;
					if(!sizeset || width!=im.width || height!=im.height) {
						setSize(im.width,im.height);
					} else {
						
					}
				} else if(im instanceof Uint8ClampedArray) {
					if(im.length==(width*height*4)) {
						image=im;
					} else {
						console.log("Please set the correct size: ImageData length mismatch");
						ok=false;
					}
				} else {
					console.log("Please provide correct input");
					ok=false;
				}
			}
			getImagePixels(); // convert to correct format if necessary
			analyzePixels(); // build color table & map pixels

			if (firstFrame) {
				writeLSD(); // logical screen descriptior
				writePalette(); // global color table
				if (repeat >= 0) {
					// use NS app extension to indicate reps
					writeNetscapeExt();
				}
			}

			writeGraphicCtrlExt(); // write graphic control extension
			if (comment !== '') {
				writeCommentExt(); // write comment extension
			}
			writeImageDesc(); // image descriptor
			if (!firstFrame) writePalette(); // local color table
			writePixels(); // encode and write pixel data
			firstFrame = false;
		} catch (e) {
			ok = false;
		}

		return ok;
	};
	
	/**
	* @description: Downloads the encoded gif with the given name
	* No need of any conversion from the stream data (out) to base64
	* Solves the issue of large file sizes when there are more frames
	* and does not involve in creation of any temporary data in the process
	* so no wastage of memory, and speeds up the process of downloading
	* to just calling this function.
	* @parameter {String} filename filename used for downloading the gif
	*/
	
	var download = exports.download = function download(filename) {
		if(out===null || closeStream==false) {
			console.log("Please call start method and add frames and call finish method before calling download"); 
		} else {
			filename= filename !== undefined ? ( filename.endsWith(".gif")? filename: filename+".gif" ): "download.gif";
			var templink = document.createElement("a");
			templink.download=filename;
			templink.href= URL.createObjectURL(new Blob([new Uint8Array(out.bin)], {type : "image/gif" } ));
			templink.click();
		}
	}

	/**
	 * Adds final trailer to the GIF stream, if you don't call the finish method
	 * the GIF stream will not be valid.
	 */

	var finish = exports.finish = function finish() {

		if (!started) return false;

		var ok = true;
		started = false;

		try {
			out.writeByte(0x3b); // gif trailer
			closeStream=true;
		} catch (e) {
			ok = false;
		}

		return ok;
	};

	/**
	 * Resets some members so that a new stream can be started.
	 * This method is actually called by the start method
	 */

	var reset = function reset() {

		// reset for subsequent use
		transIndex = 0;
		image = null;
		pixels = null;
		indexedPixels = null;
		colorTab = null;
		closeStream = false;
		firstFrame = true;
	};

	/**
	 * * Sets frame rate in frames per second. Equivalent to
	 * <code>setDelay(1000/fps)</code>.
	 * @param fps
	 * float frame rate (frames per second)
	 */

	var setFrameRate = exports.setFrameRate = function setFrameRate(fps) {
		if (fps != 0xf) delay = Math.round(100 / fps);
	};

	/**
	 * Sets quality of color quantization (conversion of images to the maximum 256
	 * colors allowed by the GIF specification). Lower values (minimum = 1)
	 * produce better colors, but slow processing significantly. 10 is the
	 * default, and produces good color mapping at reasonable speeds. Values
	 * greater than 20 do not yield significant improvements in speed.
	 * @param quality
	 * int greater than 0.
	 * @return
	 */

	var setQuality = exports.setQuality = function setQuality(quality) {
		if (quality < 1) quality = 1;
		sample = quality;
	};

	/**
	 * Sets the GIF frame size. The default size is the size of the first frame
	 * added if this method is not invoked.
	 * @param w
	 * int frame width.
	 * @param h
	 * int frame width.
	 */

	var setSize = exports.setSize = function setSize(w, h) {

		if (started && !firstFrame) return;
		width = w;
		height = h;
		if (width < 1) width = 320;
		if (height < 1) height = 240;
		sizeSet = true;
	};

	/**
	 * Initiates GIF file creation on the given stream.
	 * @param os
	 * OutputStream on which GIF images are written.
	 * @return false if initial write failed.
	 */

	var start = exports.start = function start() {

		reset();
		var ok = true;
		closeStream = false;
		out = new ByteArray();
		try {
			out.writeUTFBytes("GIF89a"); // header
		} catch (e) {
			ok = false;
		}

		return started = ok;
	};

	var cont = exports.cont = function cont() {

		reset();
		var ok = true;
		closeStream = false;
		out = new ByteArray();

		return started = ok;
	};

	/**
	 * Analyzes image colors and creates color map.
	 */

	var analyzePixels = function analyzePixels() {

		var len = pixels.length;
		var nPix = len / 3;
		indexedPixels = [];
		var nq = new NeuQuant(pixels, len, sample);

		// initialize quantizer
		colorTab = nq.process(); // create reduced palette

		// map image pixels to new palette
		var k = 0;
		for (var j = 0; j < nPix; j++) {
			var index = nq.map(pixels[k++] & 0xff, pixels[k++] & 0xff, pixels[k++] & 0xff);
			usedEntry[index] = true;
			indexedPixels[j] = index;
		}

		pixels = null;
		colorDepth = 8;
		palSize = 7;

		// get closest match to transparent color if specified
		if (transparent !== null) {
			transIndex = findClosest(transparent);
		}
	};

	/**
	 * Returns index of palette color closest to c
	 */

	var findClosest = function findClosest(c) {

		if (colorTab === null) return -1;
		var r = (c & 0xFF0000) >> 16;
		var g = (c & 0x00FF00) >> 8;
		var b = (c & 0x0000FF);
		var minpos = 0;
		var dmin = 256 * 256 * 256;
		var len = colorTab.length;

		for (var i = 0; i < len;) {
			var dr = r - (colorTab[i++] & 0xff);
			var dg = g - (colorTab[i++] & 0xff);
			var db = b - (colorTab[i] & 0xff);
			var d = dr * dr + dg * dg + db * db;
			var index = i / 3;
			if (usedEntry[index] && (d < dmin)) {
				dmin = d;
				minpos = index;
			}
			i++;
		}
		return minpos;
	};

	/**
	 * Extracts image pixels into byte array "pixels
	 */

	var getImagePixels = function getImagePixels() {
		var w = width;
		var h = height;
		pixels = [];
		var data = image;
		var count = 0;

		for (var i = 0; i < h; i++) {

			for (var j = 0; j < w; j++) {

				var b = (i * w * 4) + j * 4;
				pixels[count++] = data[b];
				pixels[count++] = data[b + 1];
				pixels[count++] = data[b + 2];

			}

		}
	};

	/**
	 * Writes Graphic Control Extension
	 */

	var writeGraphicCtrlExt = function writeGraphicCtrlExt() {
		out.writeByte(0x21); // extension introducer
		out.writeByte(0xf9); // GCE label
		out.writeByte(4); // data block size
		var transp;
		var disp;
		if (transparent === null) {
			transp = 0;
			disp = 0; // dispose = no action
		} else {
			transp = 1;
			disp = 2; // force clear if using transparent color
		}
		if (dispose >= 0) {
			disp = dispose & 7; // user override
		}
		disp <<= 2;
		// packed fields
		out.writeByte(0 | // 1:3 reserved
			disp | // 4:6 disposal
			0 | // 7 user input - 0 = none
			transp); // 8 transparency flag

		WriteShort(delay); // delay x 1/100 sec
		out.writeByte(transIndex); // transparent color index
		out.writeByte(0); // block terminator
	};

	/**
	 * Writes Comment Extention
	 */

	var writeCommentExt = function writeCommentExt() {
		out.writeByte(0x21); // extension introducer
		out.writeByte(0xfe); // comment label
		out.writeByte(comment.length); // Block Size (s)
		out.writeUTFBytes(comment);
		out.writeByte(0); // block terminator
	};


	/**
	 * Writes Image Descriptor
	 */

	var writeImageDesc = function writeImageDesc() {

		out.writeByte(0x2c); // image separator
		WriteShort(0); // image position x,y = 0,0
		WriteShort(0);
		WriteShort(width); // image size
		WriteShort(height);

		// packed fields
		if (firstFrame) {
			// no LCT - GCT is used for first (or only) frame
			out.writeByte(0);
		} else {
			// specify normal LCT
			out.writeByte(0x80 | // 1 local color table 1=yes
				0 | // 2 interlace - 0=no
				0 | // 3 sorted - 0=no
				0 | // 4-5 reserved
				palSize); // 6-8 size of color table
		}
	};

	/**
	 * Writes Logical Screen Descriptor
	 */

	var writeLSD = function writeLSD() {

		// logical screen size
		WriteShort(width);
		WriteShort(height);
		// packed fields
		out.writeByte((0x80 | // 1 : global color table flag = 1 (gct used)
			0x70 | // 2-4 : color resolution = 7
			0x00 | // 5 : gct sort flag = 0
			palSize)); // 6-8 : gct size

		out.writeByte(0); // background color index
		out.writeByte(0); // pixel aspect ratio - assume 1:1
	};

	/**
	 * Writes Netscape application extension to define repeat count.
	 */

	var writeNetscapeExt = function writeNetscapeExt() {
		out.writeByte(0x21); // extension introducer
		out.writeByte(0xff); // app extension label
		out.writeByte(11); // block size
		out.writeUTFBytes("NETSCAPE" + "2.0"); // app id + auth code
		out.writeByte(3); // sub-block size
		out.writeByte(1); // loop sub-block id
		WriteShort(repeat); // loop count (extra iterations, 0=repeat forever)
		out.writeByte(0); // block terminator
	};

	/**
	 * Writes color table
	 */

	var writePalette = function writePalette() {
		out.writeBytes(colorTab);
		var n = (3 * 256) - colorTab.length;
		for (var i = 0; i < n; i++) out.writeByte(0);
	};

	var WriteShort = function WriteShort(pValue) {
		out.writeByte(pValue & 0xFF);
		out.writeByte((pValue >> 8) & 0xFF);
	};

	/**
	 * Encodes and writes pixel data
	 */

	var writePixels = function writePixels() {
		var myencoder = new LZWEncoder(width, height, indexedPixels, colorDepth);
		myencoder.encode(out);
	};

	/**
	 * Retrieves the GIF stream
	 */

	var stream = exports.stream = function stream() {
		return out;
	};

	var setProperties = exports.setProperties = function setProperties(has_start, is_first) {
		started = has_start;
		firstFrame = is_first;
	};

	return exports;

};
/**
 * This class handles LZW encoding
 * Adapted from Jef Poskanzer's Java port by way of J. M. G. Elliott.
 * @author Kevin Weiner (original Java version - kweiner@fmsware.com)
 * @author Thibault Imbert (AS3 version - bytearray.org)
 * @author Kevin Kwok (JavaScript version - https://github.com/antimatter15/jsgif)
 * @version 0.1 AS3 implementation
 */

LZWEncoder = function() {

	var exports = {};
	var EOF = -1;
	var imgW;
	var imgH;
	var pixAry;
	var initCodeSize;
	var remaining;
	var curPixel;

	// GIFCOMPR.C - GIF Image compression routines
	// Lempel-Ziv compression based on 'compress'. GIF modifications by
	// David Rowley (mgardi@watdcsu.waterloo.edu)
	// General DEFINEs

	var BITS = 12;
	var HSIZE = 5003; // 80% occupancy

	// GIF Image compression - modified 'compress'
	// Based on: compress.c - File compression ala IEEE Computer, June 1984.
	// By Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
	// Jim McKie (decvax!mcvax!jim)
	// Steve Davies (decvax!vax135!petsd!peora!srd)
	// Ken Turkowski (decvax!decwrl!turtlevax!ken)
	// James A. Woods (decvax!ihnp4!ames!jaw)
	// Joe Orost (decvax!vax135!petsd!joe)

	var n_bits; // number of bits/code
	var maxbits = BITS; // user settable max # bits/code
	var maxcode; // maximum code, given n_bits
	var maxmaxcode = 1 << BITS; // should NEVER generate this code
	var htab = [];
	var codetab = [];
	var hsize = HSIZE; // for dynamic table sizing
	var free_ent = 0; // first unused entry

	// block compression parameters -- after all codes are used up,
	// and compression rate changes, start over.

	var clear_flg = false;

	// Algorithm: use open addressing double hashing (no chaining) on the
	// prefix code / next character combination. We do a variant of Knuth's
	// algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
	// secondary probe. Here, the modular division first probe is gives way
	// to a faster exclusive-or manipulation. Also do block compression with
	// an adaptive reset, whereby the code table is cleared when the compression
	// ratio decreases, but after the table fills. The variable-length output
	// codes are re-sized at this point, and a special CLEAR code is generated
	// for the decompressor. Late addition: construct the table according to
	// file size for noticeable speed improvement on small files. Please direct
	// questions about this implementation to ames!jaw.

	var g_init_bits;
	var ClearCode;
	var EOFCode;

	// output
	// Output the given code.
	// Inputs:
	// code: A n_bits-bit integer. If == -1, then EOF. This assumes
	// that n_bits =< wordsize - 1.
	// Outputs:
	// Outputs code to the file.
	// Assumptions:
	// Chars are 8 bits long.
	// Algorithm:
	// Maintain a BITS character long buffer (so that 8 codes will
	// fit in it exactly). Use the VAX insv instruction to insert each
	// code in turn. When the buffer fills up empty it and start over.

	var cur_accum = 0;
	var cur_bits = 0;
	var masks = [0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF];

	// Number of characters so far in this 'packet'
	var a_count;

	// Define the storage for the packet accumulator
	var accum = [];

	var LZWEncoder = exports.LZWEncoder = function LZWEncoder(width, height, pixels, color_depth) {
		imgW = width;
		imgH = height;
		pixAry = pixels;
		initCodeSize = Math.max(2, color_depth);
	};

	// Add a character to the end of the current packet, and if it is 254
	// characters, flush the packet to disk.
	var char_out = function char_out(c, outs) {
		accum[a_count++] = c;
		if (a_count >= 254) flush_char(outs);
	};

	// Clear out the hash table
	// table clear for block compress

	var cl_block = function cl_block(outs) {
		cl_hash(hsize);
		free_ent = ClearCode + 2;
		clear_flg = true;
		output(ClearCode, outs);
	};

	// reset code table
	var cl_hash = function cl_hash(hsize) {
		for (var i = 0; i < hsize; ++i) htab[i] = -1;
	};

	var compress = exports.compress = function compress(init_bits, outs) {

		var fcode;
		var i; /* = 0 */
		var c;
		var ent;
		var disp;
		var hsize_reg;
		var hshift;

		// Set up the globals: g_init_bits - initial number of bits
		g_init_bits = init_bits;

		// Set up the necessary values
		clear_flg = false;
		n_bits = g_init_bits;
		maxcode = MAXCODE(n_bits);

		ClearCode = 1 << (init_bits - 1);
		EOFCode = ClearCode + 1;
		free_ent = ClearCode + 2;

		a_count = 0; // clear packet

		ent = nextPixel();

		hshift = 0;
		for (fcode = hsize; fcode < 65536; fcode *= 2)
			++hshift;
		hshift = 8 - hshift; // set hash code range bound

		hsize_reg = hsize;
		cl_hash(hsize_reg); // clear hash table

		output(ClearCode, outs);

		outer_loop: while ((c = nextPixel()) != EOF) {
			fcode = (c << maxbits) + ent;
			i = (c << hshift) ^ ent; // xor hashing

			if (htab[i] == fcode) {
				ent = codetab[i];
				continue;
			}

			else if (htab[i] >= 0) { // non-empty slot

				disp = hsize_reg - i; // secondary hash (after G. Knott)
				if (i === 0) disp = 1;

				do {
					if ((i -= disp) < 0)
						i += hsize_reg;

					if (htab[i] == fcode) {
						ent = codetab[i];
						continue outer_loop;
					}
				} while (htab[i] >= 0);
			}

			output(ent, outs);
			ent = c;
			if (free_ent < maxmaxcode) {
				codetab[i] = free_ent++; // code -> hashtable
				htab[i] = fcode;
			}
			else cl_block(outs);
		}

		// Put out the final code.
		output(ent, outs);
		output(EOFCode, outs);
	};

	// ----------------------------------------------------------------------------
	var encode = exports.encode = function encode(os) {
		os.writeByte(initCodeSize); // write "initial code size" byte
		remaining = imgW * imgH; // reset navigation variables
		curPixel = 0;
		compress(initCodeSize + 1, os); // compress and write the pixel data
		os.writeByte(0); // write block terminator
	};

	// Flush the packet to disk, and reset the accumulator
	var flush_char = function flush_char(outs) {
		if (a_count > 0) {
			outs.writeByte(a_count);
			outs.writeBytes(accum, 0, a_count);
			a_count = 0;
		}
	};

	var MAXCODE = function MAXCODE(n_bits) {
		return (1 << n_bits) - 1;
	};

	// ----------------------------------------------------------------------------
	// Return the next pixel from the image
	// ----------------------------------------------------------------------------

	var nextPixel = function nextPixel() {
		if (remaining === 0) return EOF;
		--remaining;
		var pix = pixAry[curPixel++];
		return pix & 0xff;
	};

	var output = function output(code, outs) {

		cur_accum &= masks[cur_bits];

		if (cur_bits > 0) cur_accum |= (code << cur_bits);
		else cur_accum = code;

		cur_bits += n_bits;

		while (cur_bits >= 8) {
			char_out((cur_accum & 0xff), outs);
			cur_accum >>= 8;
			cur_bits -= 8;
		}

		// If the next entry is going to be too big for the code size,
		// then increase it, if possible.

		if (free_ent > maxcode || clear_flg) {

			if (clear_flg) {

				maxcode = MAXCODE(n_bits = g_init_bits);
				clear_flg = false;

			} else {

				++n_bits;
				if (n_bits == maxbits) maxcode = maxmaxcode;
				else maxcode = MAXCODE(n_bits);
			}
		}

		if (code == EOFCode) {

			// At EOF, write the rest of the buffer.
			while (cur_bits > 0) {
				char_out((cur_accum & 0xff), outs);
				cur_accum >>= 8;
				cur_bits -= 8;
			}

			flush_char(outs);
		}
	};

	LZWEncoder.apply(this, arguments);
	return exports;
};
/*
 * NeuQuant Neural-Net Quantization Algorithm
 * ------------------------------------------
 *
 * Copyright (c) 1994 Anthony Dekker
 *
 * NEUQUANT Neural-Net quantization algorithm by Anthony Dekker, 1994. See
 * "Kohonen neural networks for optimal colour quantization" in "Network:
 * Computation in Neural Systems" Vol. 5 (1994) pp 351-367. for a discussion of
 * the algorithm.
 *
 * Any party obtaining a copy of these files from the author, directly or
 * indirectly, is granted, free of charge, a full and unrestricted irrevocable,
 * world-wide, paid up, royalty-free, nonexclusive right and license to deal in
 * this software and documentation files (the "Software"), including without
 * limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons who
 * receive copies from any such party to do so, with the only requirement being
 * that this copyright notice remain intact.
 */

/*
 * This class handles Neural-Net quantization algorithm
 * @author Kevin Weiner (original Java version - kweiner@fmsware.com)
 * @author Thibault Imbert (AS3 version - bytearray.org)
 * @author Kevin Kwok (JavaScript version - https://github.com/antimatter15/jsgif)
 * @version 0.1 AS3 implementation
 */

NeuQuant = function() {

	var exports = {};
	var netsize = 256; /* number of colours used */

	/* four primes near 500 - assume no image has a length so large */
	/* that it is divisible by all four primes */

	var prime1 = 499;
	var prime2 = 491;
	var prime3 = 487;
	var prime4 = 503;
	var minpicturebytes = (3 * prime4); /* minimum size for input image */

	/*
	 * Program Skeleton ---------------- [select samplefac in range 1..30] [read
	 * image from input file] pic = (unsigned char*) malloc(3*width*height);
	 * initnet(pic,3*width*height,samplefac); learn(); unbiasnet(); [write output
	 * image header, using writecolourmap(f)] inxbuild(); write output image using
	 * inxsearch(b,g,r)
	 */

	/*
	 * Network Definitions -------------------
	 */

	var maxnetpos = (netsize - 1);
	var netbiasshift = 4; /* bias for colour values */
	var ncycles = 100; /* no. of learning cycles */

	/* defs for freq and bias */
	var intbiasshift = 16; /* bias for fractions */
	var intbias = (1 << intbiasshift);
	var gammashift = 10; /* gamma = 1024 */
	var gamma = (1 << gammashift);
	var betashift = 10;
	var beta = (intbias >> betashift); /* beta = 1/1024 */
	var betagamma = (intbias << (gammashift - betashift));

	/* defs for decreasing radius factor */
	var initrad = (netsize >> 3); /* for 256 cols, radius starts */
	var radiusbiasshift = 6; /* at 32.0 biased by 6 bits */
	var radiusbias = (1 << radiusbiasshift);
	var initradius = (initrad * radiusbias); /* and decreases by a */
	var radiusdec = 30; /* factor of 1/30 each cycle */

	/* defs for decreasing alpha factor */
	var alphabiasshift = 10; /* alpha starts at 1.0 */
	var initalpha = (1 << alphabiasshift);
	var alphadec; /* biased by 10 bits */

	/* radbias and alpharadbias used for radpower calculation */
	var radbiasshift = 8;
	var radbias = (1 << radbiasshift);
	var alpharadbshift = (alphabiasshift + radbiasshift);
	var alpharadbias = (1 << alpharadbshift);

	/*
	 * Types and Global Variables --------------------------
	 */

	var thepicture; /* the input image itself */
	var lengthcount; /* lengthcount = H*W*3 */
	var samplefac; /* sampling factor 1..30 */

	// typedef int pixel[4]; /* BGRc */
	var network; /* the network itself - [netsize][4] */
	var netindex = [];

	/* for network lookup - really 256 */
	var bias = [];

	/* bias and freq arrays for learning */
	var freq = [];
	var radpower = [];

	var NeuQuant = exports.NeuQuant = function NeuQuant(thepic, len, sample) {

		var i;
		var p;

		thepicture = thepic;
		lengthcount = len;
		samplefac = sample;

		network = new Array(netsize);

		for (i = 0; i < netsize; i++) {

			network[i] = new Array(4);
			p = network[i];
			p[0] = p[1] = p[2] = (i << (netbiasshift + 8)) / netsize;
			freq[i] = intbias / netsize; /* 1/netsize */
			bias[i] = 0;
		}
	};

	var colorMap = function colorMap() {

		var map = [];
		var index = new Array(netsize);

		for (var i = 0; i < netsize; i++)
			index[network[i][3]] = i;

		var k = 0;
		for (var l = 0; l < netsize; l++) {
			var j = index[l];
			map[k++] = (network[j][0]);
			map[k++] = (network[j][1]);
			map[k++] = (network[j][2]);
		}

		return map;
	};

	/*
	 * Insertion sort of network and building of netindex[0..255] (to do after
	 * unbias)
	 * -------------------------------------------------------------------------------
	 */

	var inxbuild = function inxbuild() {

		var i;
		var j;
		var smallpos;
		var smallval;
		var p;
		var q;
		var previouscol;
		var startpos;

		previouscol = 0;
		startpos = 0;
		for (i = 0; i < netsize; i++) {

			p = network[i];
			smallpos = i;
			smallval = p[1]; /* index on g */

			/* find smallest in i..netsize-1 */
			for (j = i + 1; j < netsize; j++) {

				q = network[j];
				if (q[1] < smallval) { /* index on g */
					smallpos = j;
					smallval = q[1]; /* index on g */
				}
			}
			q = network[smallpos];

			/* swap p (i) and q (smallpos) entries */
			if (i != smallpos) {
				j = q[0];
				q[0] = p[0];
				p[0] = j;
				j = q[1];
				q[1] = p[1];
				p[1] = j;
				j = q[2];
				q[2] = p[2];
				p[2] = j;
				j = q[3];
				q[3] = p[3];
				p[3] = j;
			}

			/* smallval entry is now in position i */

			if (smallval != previouscol) {

				netindex[previouscol] = (startpos + i) >> 1;

				for (j = previouscol + 1; j < smallval; j++) netindex[j] = i;

				previouscol = smallval;
				startpos = i;
			}
		}

		netindex[previouscol] = (startpos + maxnetpos) >> 1;
		for (j = previouscol + 1; j < 256; j++) netindex[j] = maxnetpos; /* really 256 */
	};

	/*
	 * Main Learning Loop ------------------
	 */

	var learn = function learn() {

		var i;
		var j;
		var b;
		var g;
		var r;
		var radius;
		var rad;
		var alpha;
		var step;
		var delta;
		var samplepixels;
		var p;
		var pix;
		var lim;

		if (lengthcount < minpicturebytes) samplefac = 1;

		alphadec = 30 + ((samplefac - 1) / 3);
		p = thepicture;
		pix = 0;
		lim = lengthcount;
		samplepixels = lengthcount / (3 * samplefac);
		delta = (samplepixels / ncycles) | 0;
		alpha = initalpha;
		radius = initradius;

		rad = radius >> radiusbiasshift;
		if (rad <= 1) rad = 0;

		for (i = 0; i < rad; i++) radpower[i] = alpha * (((rad * rad - i * i) * radbias) / (rad * rad));

		if (lengthcount < minpicturebytes) step = 3;

		else if ((lengthcount % prime1) !== 0) step = 3 * prime1;

		else {

			if ((lengthcount % prime2) !== 0) step = 3 * prime2;
			else {
				if ((lengthcount % prime3) !== 0) step = 3 * prime3;
				else step = 3 * prime4;
			}
		}

		i = 0;
		while (i < samplepixels) {

			b = (p[pix + 0] & 0xff) << netbiasshift;
			g = (p[pix + 1] & 0xff) << netbiasshift;
			r = (p[pix + 2] & 0xff) << netbiasshift;
			j = contest(b, g, r);

			altersingle(alpha, j, b, g, r);
			if (rad !== 0) alterneigh(rad, j, b, g, r); /* alter neighbours */

			pix += step;
			if (pix >= lim) pix -= lengthcount;

			i++;

			if (delta === 0) delta = 1;

			if (i % delta === 0) {
				alpha -= alpha / alphadec;
				radius -= radius / radiusdec;
				rad = radius >> radiusbiasshift;

				if (rad <= 1) rad = 0;

				for (j = 0; j < rad; j++) radpower[j] = alpha * (((rad * rad - j * j) * radbias) / (rad * rad));
			}
		}
	};

	/*
	 ** Search for BGR values 0..255 (after net is unbiased) and return colour
	 * index
	 * ----------------------------------------------------------------------------
	 */

	var map = exports.map = function map(b, g, r) {

		var i;
		var j;
		var dist;
		var a;
		var bestd;
		var p;
		var best;

		bestd = 1000; /* biggest possible dist is 256*3 */
		best = -1;
		i = netindex[g]; /* index on g */
		j = i - 1; /* start at netindex[g] and work outwards */

		while ((i < netsize) || (j >= 0)) {

			if (i < netsize) {
				p = network[i];
				dist = p[1] - g; /* inx key */

				if (dist >= bestd) i = netsize; /* stop iter */

				else {

					i++;
					if (dist < 0) dist = -dist;
					a = p[0] - b;
					if (a < 0) a = -a;
					dist += a;

					if (dist < bestd) {
						a = p[2] - r;
						if (a < 0) a = -a;
						dist += a;

						if (dist < bestd) {
							bestd = dist;
							best = p[3];
						}
					}
				}
			}

			if (j >= 0) {

				p = network[j];
				dist = g - p[1]; /* inx key - reverse dif */

				if (dist >= bestd) j = -1; /* stop iter */

				else {

					j--;
					if (dist < 0) dist = -dist;
					a = p[0] - b;
					if (a < 0) a = -a;
					dist += a;

					if (dist < bestd) {
						a = p[2] - r;
						if (a < 0) a = -a;
						dist += a;
						if (dist < bestd) {
							bestd = dist;
							best = p[3];
						}
					}
				}
			}
		}

		return (best);
	};

	var process = exports.process = function process() {
		learn();
		unbiasnet();
		inxbuild();
		return colorMap();
	};

	/*
	 * Unbias network to give byte values 0..255 and record position i to prepare
	 * for sort
	 * -----------------------------------------------------------------------------------
	 */

	var unbiasnet = function unbiasnet() {

		var i;
		var j;

		for (i = 0; i < netsize; i++) {
			network[i][0] >>= netbiasshift;
			network[i][1] >>= netbiasshift;
			network[i][2] >>= netbiasshift;
			network[i][3] = i; /* record colour no */
		}
	};

	/*
	 * Move adjacent neurons by precomputed alpha*(1-((i-j)^2/[r]^2)) in
	 * radpower[|i-j|]
	 * ---------------------------------------------------------------------------------
	 */

	var alterneigh = function alterneigh(rad, i, b, g, r) {

		var j;
		var k;
		var lo;
		var hi;
		var a;
		var m;
		var p;

		lo = i - rad;
		if (lo < -1) lo = -1;

		hi = i + rad;
		if (hi > netsize) hi = netsize;

		j = i + 1;
		k = i - 1;
		m = 1;

		while ((j < hi) || (k > lo)) {
			a = radpower[m++];

			if (j < hi) {
				p = network[j++];

				try {
					p[0] -= (a * (p[0] - b)) / alpharadbias;
					p[1] -= (a * (p[1] - g)) / alpharadbias;
					p[2] -= (a * (p[2] - r)) / alpharadbias;
				} catch (e) {} // prevents 1.3 miscompilation
			}

			if (k > lo) {
				p = network[k--];

				try {
					p[0] -= (a * (p[0] - b)) / alpharadbias;
					p[1] -= (a * (p[1] - g)) / alpharadbias;
					p[2] -= (a * (p[2] - r)) / alpharadbias;
				} catch (e) {}
			}
		}
	};

	/*
	 * Move neuron i towards biased (b,g,r) by factor alpha
	 * ----------------------------------------------------
	 */

	var altersingle = function altersingle(alpha, i, b, g, r) {

		/* alter hit neuron */
		var n = network[i];
		n[0] -= (alpha * (n[0] - b)) / initalpha;
		n[1] -= (alpha * (n[1] - g)) / initalpha;
		n[2] -= (alpha * (n[2] - r)) / initalpha;
	};

	/*
	 * Search for biased BGR values ----------------------------
	 */

	var contest = function contest(b, g, r) {

		/* finds closest neuron (min dist) and updates freq */
		/* finds best neuron (min dist-bias) and returns position */
		/* for frequently chosen neurons, freq[i] is high and bias[i] is negative */
		/* bias[i] = gamma*((1/netsize)-freq[i]) */

		var i;
		var dist;
		var a;
		var biasdist;
		var betafreq;
		var bestpos;
		var bestbiaspos;
		var bestd;
		var bestbiasd;
		var n;

		bestd = ~ (1 << 31);
		bestbiasd = bestd;
		bestpos = -1;
		bestbiaspos = bestpos;

		for (i = 0; i < netsize; i++) {
			n = network[i];
			dist = n[0] - b;
			if (dist < 0) dist = -dist;
			a = n[1] - g;
			if (a < 0) a = -a;
			dist += a;
			a = n[2] - r;
			if (a < 0) a = -a;
			dist += a;

			if (dist < bestd) {
				bestd = dist;
				bestpos = i;
			}

			biasdist = dist - ((bias[i]) >> (intbiasshift - netbiasshift));

			if (biasdist < bestbiasd) {
				bestbiasd = biasdist;
				bestbiaspos = i;
			}

			betafreq = (freq[i] >> betashift);
			freq[i] -= betafreq;
			bias[i] += (betafreq << gammashift);
		}

		freq[bestpos] += beta;
		bias[bestpos] -= betagamma;
		return (bestbiaspos);
	};

	NeuQuant.apply(this, arguments);
	return exports;
};
function encode64(input) {
	var output = "", i = 0, l = input.length,
	key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", 
	chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	while (i < l) {
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;
		if (isNaN(chr2)) enc3 = enc4 = 64;
		else if (isNaN(chr3)) enc4 = 64;
		output = output + key.charAt(enc1) + key.charAt(enc2) + key.charAt(enc3) + key.charAt(enc4);
	}
	return output;
}


================================================
FILE: assets/page.html
================================================
<!DOCTYPE html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="bootstrap.min.css" media="screen">
    <link rel="stylesheet" href="styles.css?vary={VERSION}" media="screen">
    <link rel="stylesheet" href="styles-user.css?vary={VERSION}" media="screen">
    <title>{TITLE}</title>
    <meta name="description" content="{CLEAN_DESCRIPTION}" />
    <script src="jquery.min.js"></script>
</head>
<body>
    <div class="content_box">
        <h1>{TITLE}</h1>
        <h4>{DESCRIPTION}</h4>
        <noscript>This page requires JavaScript to work. Don't worry, it's all local to the current page, and open source on GitHub.</noscript>
        <hr>
        <div class="top_nav_bar" id="top_nav_bar">
            <div class="accordion navigation_accordion" id="navigation_accordion">
                <div class="accordion-item">
                    <h2 class="accordion-header" id="navigation_accordion_heading">
                        <button class="accordion-button" type="button" id="toggle_nav_button" data-bs-toggle="collapse" data-bs-target="#navigation_accordion_collapse" aria-expanded="true" aria-controls="navigation_accordion_collapse">Navigation</button>
                    </h2>
                    <div id="navigation_accordion_collapse" class="accordion-collapse collapse show" aria-labelledby="navigation_accordion_heading" data-bs-parent="navigation_accordion">
                        <div class="accordion-body">
                            <div>
                                <div class="accordion advanced_settings_section" id="settings_accordion">
                                    <div class="accordion-item">
                                        <h2 class="accordion-header" id="setting_accordion_heading">
                                            <button class="accordion-button collapsed" id="toggle_adv_button" type="button" data-bs-toggle="collapse" data-bs-target="#settings_accordion_collapse" aria-expanded="false" aria-controls="settings_accordion_collapse">Advanced Settings</button>
                                        </h2>
                                        <div id="settings_accordion_collapse" class="accordion-collapse collapse" aria-labelledby="setting_accordion_heading" data-bs-parent="#settings_accordion">
                                            <div class="accordion-body">
                                                {ADVANCED_SETTINGS}
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <br>
                            {CONTENT}
                            <center>
                                <input class="form-check-input" type="checkbox" id="showDescriptions" checked="true" autocomplete="off" onchange="javascript:toggleDescriptions()"> <label class="form-check-label" for="showDescriptions" title="Uncheck this to focus on the grid. Check it to see the full descriptions of each option.">Show descriptions of axes and values</label>
                                &emsp;&emsp;<input class="form-check-input" type="checkbox" autocomplete="off" value="" id="autoScaleImages"> <label class="form-check-label" for="autoScaleImages">Auto-scale images to viewport width</label>
                                &emsp;&emsp;<input class="form-check-input" type="checkbox" autocomplete="off" value="" id="stickyNavigation"> <label class="form-check-label" for="stickyNavigation">Sticky navigation</label>
                                &emsp;&emsp;<input class="form-check-input" type="checkbox" autocomplete="off" checked id="stickyLabels"> <label class="form-check-label" for="stickyLabels">Sticky labels</label>
                                &emsp;&emsp;<span id="score_setting">Score Display: <select id="score_display"><option>None</option><option>Thin Outline</option><option>Thick Bars</option><option>Heatmap</option></select></span>
                            </center>
                        </div>
                    </div>
                </div>
            </div>
            <div id="save_image_helper" class="save_image_area">
                <button onclick="makeImage()">Save Image Of Current View</button> <select title="Image Type" id="makeimage_type"><option>jpeg</option><option>png</option></select> <select title="Image Size" id="makeimage_size"><option selected>1x</option><option>0.75x</option><option>0.5x</option><option>0.25x</option></select>
                &emsp;<button onclick="makeGif()">Create Axis GIF Animation</button> <select title="Axis" id="makegif_axis"><option value="x-axis">X Axis</option></select> <select title="GIF Size" id="makegif_size"><option>1x</option><option>0.75x</option><option selected>0.5x</option><option>0.25x</option></select> <select title="GIF Speed" id="makegif_speed"><option>1/s</option><option>2/s</option><option>3/s</option><option selected>4/s</option><option>5/s</option><option>10/s</option></select><select title="GIF Direction" id="makegif_direction"><option>Forwards</option><option>Backwards</option></select>
                <div id="save_image_info" style="display: none;">Here's your image! To share it, just use right click -> Copy image or Save Image As</div>
            </div>
        </div>
        <div class="image_table_box">
            <table class="image_table" id="image_table"></table>
            <table id="image_script_dump"></table>
        </div>
    </div>
    <div class="modal modal-fullscreen popup_modal_background" id="image_info_modal"></div>
    <div class="modal" tabindex="-1" id="save_image_output_modal">
        <div class="modal-dialog modal-xl">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Here's your image!</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <p>To share it, just use right click -> Copy image or Save Image As.</p>
                    <div id="save_image_output" class="save_image_output"></div>
                </div>
            </div>
        </div>
    </div>
    <br><div style="width:256px;height:512px;"></div> <!-- Spacer to reduce screen-jumping if images load while you're scrolled down -->
    <footer>
        <center>
            <hr>
            Created by: {AUTHOR}
            <br>The technology that powers this page is <a href="https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script">Infinity Grid Generator</a>, published under the MIT license by Alex 'mcmonkey' Goodwin.
            <br>This software allows users to generate pages with any content they desire. Therefore, content on this page (images, text, etc.) is the property of whoever generated this specific page.
            <br>{EXTRA_FOOTER}
            <br>Made using the <a href="https://bootswatch.com/darkly/">Darkly Bootstrap Theme</a> by Thomas Park, which was released under the <a href="https://github.com/thomaspark/bootswatch/blob/95df99d76147797cbcb1014b639805add2327f65/LICENSE">MIT License</a>.
            <br>Gifs are generated using <a href="https://github.com/antimatter15/jsgif">JSGif</a>, which was released under the <a href="https://github.com/antimatter15/jsgif/blob/b46429c50a53d23b762d6ebb00b375aece3ed843/LICENSE">MIT License</a>.
            <br>
            <br><div style="width:256px;height:512px;"></div>
        </center>
    </footer>
    <script src="bootstrap.bundle.min.js"></script>
    <script src="jsgif.js"></script>
    <script src="data.js?vary={VERSION}"></script>
    <script src="proc.js?vary={VERSION}"></script>
</body>


================================================
FILE: assets/proc.js
================================================
/*
 * This file is part of Infinity Grid Generator, view the README.md at https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script for more information.
*/

let suppressUpdate = true;
let file_extensions_alt = {};

function loadData() {
    let rawHash = window.location.hash;
    document.getElementById('x_' + rawData.axes[0].id).click();
    document.getElementById('x2_none').click();
    document.getElementById('y2_none').click();
    let makegif_axis = document.getElementById('makegif_axis');
    // rawData.ext/title/description
    for (var axis of rawData.axes) {
        // axis.id/title/description
        for (var val of axis.values) {
            // val.key/title/description/show
            let clicktab = getNavValTab(axis.id, val.key);
            clicktab.addEventListener('click', fillTable);
            if (!val.show) {
                setShowVal(axis.id, val.key, false);
            }
        }
        for (var prefix of ['x_', 'y_', 'x2_', 'y2_']) {
            document.getElementById(prefix + axis.id).addEventListener('click', fillTable);
        }
        for (var label of ['x2_none', 'y2_none']) {
            document.getElementById(label).addEventListener('click', fillTable);
        }
        makegif_axis.appendChild(new Option(axis.title, axis.id));
    }
    console.log(`Loaded data for '${rawData.title}'`);
    document.getElementById('autoScaleImages').addEventListener('change', updateScaling);
    document.getElementById('stickyNavigation').addEventListener('change', toggleTopSticky);
    document.getElementById('stickyLabels').addEventListener('change', toggleLabelSticky);
    document.getElementById('toggle_nav_button').addEventListener('click', updateTitleSticky);
    document.getElementById('toggle_adv_button').addEventListener('click', updateTitleSticky);
    document.getElementById('showDescriptions').checked = rawData.defaults.show_descriptions;
    document.getElementById('autoScaleImages').checked = rawData.defaults.autoscale;
    document.getElementById('stickyNavigation').checked = rawData.defaults.sticky;
    document.getElementById('stickyLabels').checked = rawData.defaults.sticky_labels;
    document.getElementById('score_display').addEventListener('click', fillTable);
    document.getElementById('score_setting').style.display = typeof getScoreFor == 'undefined' ? 'none' : 'inline-block';
    updateStylesToMatchInputs();
    for (var axis of ['x', 'y', 'x2', 'y2']) {
        if (rawData.defaults[axis] != '') {
            document.getElementById(axis + '_' + rawData.defaults[axis]).click();
        }
    }
    applyHash(rawHash);
    suppressUpdate = false;
    fillTable();
    startAutoScroll();
    if (rawData.will_run) {
        setTimeout(checkForUpdates, 5000);
    }
}

function getExtension(filePath) {
    if (filePath in file_extensions_alt) {
        return file_extensions_alt[filePath];
    }
    return rawData.ext;
}

/** External callable. */
function fix_video(path) {
    let ext = getExtension(path);
    let matches = document.querySelectorAll(`img[data-img_path="${path}"]`);
    for (let match of matches) {
        if (ext != 'webm' && ext != 'mp4') {
            if (!match.src.endsWith(ext)) {
                match.src = `${path}.${ext}`;
            }
            continue;
        }
        if (match.tagName == 'VIDEO') {
            continue;
        }
        let video = document.createElement('video');
        video.loop = true;
        video.muted = true;
        video.autoplay = true;
        video.classList = match.classList;
        video.dataset.img_path = match.dataset.img_path;
        video.onclick = "doPopupFor(this)";
        video.onerror = "setImgPlaceholder(this)";
        let source = document.createElement('source');
        source.src = `${path}.${ext}`;
        source.type = `video/${ext}`;
        video.appendChild(source);
        match.parentElement.replaceChild(video, match);
    }
}

function updateStylesToMatchInputs() {
    toggleTopSticky();
    toggleLabelSticky();
}

function getAxisById(id) {
    return rawData.axes.find(axis => axis.id == id);
}

function getNextAxis(axes, startId) {
    var next = false;
    for (var subAxis of axes) {
        if (subAxis.id == startId) {
            next = true;
        }
        else if (next) {
            return subAxis;
        }
    }
    return null;
}

function getSelectedValKey(axis) {
    for (var subVal of axis.values) {
        if (window.getComputedStyle(document.getElementById('tab_' + axis.id + '__' + subVal.key)).display != 'none') {
            return subVal.path;
        }
    }
    return null;
}

var popoverLastImg = null;

function clickRowImage(rows, x, y) {
    $('#image_info_modal').modal('hide');
    var columns = rows[y].getElementsByTagName('td');
    columns[x].getElementsByTagName('img')[0].click();
}

window.addEventListener('keydown', function(kbevent) {
    if ($('#image_info_modal').is(':visible')) {
        if (kbevent.key == 'Escape') {
            $('#image_info_modal').modal('toggle');
            kbevent.preventDefault();
            kbevent.stopPropagation();
            return false;
        }
        var tableElem = document.getElementById('image_table');
        var rows = tableElem.getElementsByTagName('tr');
        var matchedRow = null;
        var x = 0, y = 0;
        for (var row of rows) {
            var columns = row.getElementsByTagName('td');
            for (var column of columns) {
                var images = column.getElementsByTagName('img');
                if (images.length == 1 && images[0] == popoverLastImg) {
                    matchedRow = row;
                    break;
                }
                x++;
            }
            if (matchedRow != null) {
                break;
            }
            x = 0;
            y++;
        }
        if (matchedRow == null) {
            return;
        }
        if (kbevent.key == 'ArrowLeft') {
            if (x > 1) {
                x--;
                clickRowImage(rows, x, y);
            }
        }
        else if (kbevent.key == 'ArrowRight') {
            x++;
            var columns = matchedRow.getElementsByTagName('td');
            if (columns.length > x) {
                clickRowImage(rows, x, y);
            }
        }
        else if (kbevent.key == 'ArrowUp') {
            if (y > 1) {
                y--;
                clickRowImage(rows, x, y);
            }
        }
        else if (kbevent.key == 'ArrowDown') {
            y++;
            if (rows.length > y) {
                clickRowImage(rows, x, y);
            }
        }
        else {
            return;
        }
        kbevent.preventDefault();
        kbevent.stopPropagation();
        return false;
    }
    var elem = document.activeElement;
    if (!elem.id.startsWith('clicktab_')) {
        return;
    }
    var axisId = elem.id.substring('clicktab_'.length);
    var splitIndex = axisId.lastIndexOf('__');
    axisId = axisId.substring(0, splitIndex);
    var axis = getAxisById(axisId);
    if (kbevent.key == 'ArrowLeft') {
        var tabPage = document.getElementById('tablist_' + axis.id);
        var tabs = tabPage.getElementsByClassName('nav-link');
        var newTab = clickTabAfterActiveTab(Array.from(tabs).reverse());
        newTab.focus({ preventScroll: true });
    }
    else if (kbevent.key == 'ArrowRight') {
        var tabPage = document.getElementById('tablist_' + axis.id);
        var tabs = tabPage.getElementsByClassName('nav-link');
        var newTab = clickTabAfterActiveTab(tabs);
        newTab.focus({ preventScroll: true });
    }
    else if (kbevent.key == 'ArrowUp') {
        var next = getNextAxis(Array.from(rawData.axes).reverse(), axisId);
        if (next != null) {
            var selectedKey = getSelectedValKey(next);
            var swapToTab = getNavValTab(next.id, selectedKey);
            swapToTab.focus({ preventScroll: true });
        }
    }
    else if (kbevent.key == 'ArrowDown') {
        var next = getNextAxis(rawData.axes, axisId);
        if (next != null) {
            var selectedKey = getSelectedValKey(next);
            var swapToTab = getNavValTab(next.id, selectedKey);
            swapToTab.focus({ preventScroll: true });
        }
    }
    else {
        return;
    }
    kbevent.preventDefault();
    kbevent.stopPropagation();
    return false;
}, true);

function escapeHtml(text) {
    if (typeof text != 'string') {
        return text;
    }
    return text.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;').replaceAll('"', '&quot;').replaceAll("'", '&#039;');
}

function unescapeHtml(text) {
    return text.replaceAll('&lt;', '<').replaceAll('&gt;', '>').replaceAll('&quot;', '"').replaceAll('&#039;', "'").replaceAll('&amp;', '&');
}

function canShowVal(axis, val) {
    return document.getElementById(`showval_${axis}__${val}`).checked;
}

function setShowVal(axis, val, show) {
    document.getElementById(`showval_${axis}__${val}`).checked = show;
    getNavValTab(axis, val).classList.toggle('tab_hidden', !show);
}

function getNavValTab(axis, val) {
    return document.getElementById(`clicktab_${axis}__${val}`);
}

function percentToRedGreen(percent) {
    return `color-mix(in srgb, red, green ${percent}%)`;
}

let scoreTrackCounter = 0;
let scoreUpdates = [];
let lastScoreBump = Date.now();
let scoreBumpTracker = null;
let scoreMin = 0, scoreMax = 1;

function getXAxisContent(x, y, xAxis, yval, x2Axis, x2val, y2Axis, y2val) {
    let scriptDump = document.getElementById('image_script_dump');
    let imgPath = [];
    let index = 0;
    for (let subAxis of rawData.axes) {
        if (subAxis.id == x) {
            index = imgPath.length;
            imgPath.push(null);
        }
        else if (subAxis.id == y) {
            imgPath.push(yval.path);
        }
        else if (x2Axis != null && subAxis.id == x2Axis.id) {
            imgPath.push(x2val.path);
        }
        else if (y2Axis != null && subAxis.id == y2Axis.id) {
            imgPath.push(y2val.path);
        }
        else {
            imgPath.push(getSelectedValKey(subAxis));
        }
    }
    let newContent = '';
    let subInd = 0;
    let scoreDisplay = document.getElementById('score_display').value;
    for (let xVal of xAxis.values) {
        subInd++;
        if (!canShowVal(xAxis.id, xVal.key)) {
            continue;
        }
        imgPath[index] = xVal.path;
        let slashed = imgPath.join('/');
        let ext = getExtension(slashed);
        let actualUrl = slashed + '.' + ext;
        let id = scoreTrackCounter++;
        newContent += `<td id="td-img-${id}"><span></span>`;
        if (ext == 'mp4' || ext == 'webm') {
            newContent += `<video loop autoplay muted class="table_img" data-img_path="${slashed}" onclick="doPopupFor(this)" onerror="setImgPlaceholder(this)" alt="${actualUrl}"><source src="${actualUrl}" type="video/${ext}"></source></video>`;
        }
        else {
            newContent += `<img class="table_img" data-img_path="${slashed}" onclick="doPopupFor(this)" onerror="setImgPlaceholder(this)" src="${actualUrl}" alt="${actualUrl}" />`;
        }
        newContent += '</td>';
        let newScr = null;
        if (typeof getMetadataScriptFor != 'undefined') {
            newScr = document.createElement('script');
            newScr.src = getMetadataScriptFor(slashed);
        }
        let doScores = scoreDisplay != 'None' && typeof getScoreFor != 'undefined';
        if (doScores) {
            scoreUpdates.push(() => {
                let score = getScoreFor(slashed);
                if (score) {
                    score = (score - scoreMin) / (scoreMax - scoreMin);
                    let elem = document.getElementById(`td-img-${id}`);
                    let color = percentToRedGreen(score * 100);
                    let blockColor = '';
                    if (scoreDisplay == 'Thin Outline')
                    {
                        let xborder = `border-top: 2px solid ${color}; border-bottom: 2px solid ${color};`;
                        let yborder = `border-left: 2px solid ${color}; border-right: 2px solid ${color};`;
                        elem.getElementsByTagName('img')[0].style = `${xborder} ${yborder}`;
                    }
                    else if (scoreDisplay == 'Thick Bars')
                    {
                        elem.getElementsByTagName('img')[0].style = `border-top: 10px solid ${color}; border-left: 10px solid ${color};`;
                    }
                    else if (scoreDisplay == 'Heatmap')
                    {
                        blockColor = `color-mix(in srgb, ${color} 50%, transparent)`;
                    }
                    elem.firstChild.innerHTML = `<div style="position: relative; width: 0; height: 0"><div style="position: absolute; left: 0; z-index: 20;">${Math.round(score * 100)}%</div><div class="heatmapper" style="position: absolute; left: 0; width: 100px; height: 100px; z-index: 10; background-color: ${blockColor}"></div></div>`;
                }
            });
        }
        if (newScr != null) {
            newScr.onload = () => {
                setTimeout(() => {
                    lastScoreBump = Date.now();
                    let ext = file_extensions_alt[slashed];
                    if (ext && !actualUrl.endsWith(ext)) {
                        fix_video(slashed);
                    }
                }, 1);
                if (scoreBumpTracker == null) {
                    scoreBumpTracker = setInterval(() => {
                        if (Date.now() - lastScoreBump > 300) {
                            clearInterval(scoreBumpTracker);
                            scoreBumpTracker = null;
                            if (doScores) {
                                scoreMin = 1;
                                scoreMax = 0;
                                for (let image of document.getElementsByClassName('table_img')) {
                                    let score = getScoreFor(image.dataset.img_path);
                                    if (score) {
                                        scoreMin = Math.min(scoreMin, score);
                                        scoreMax = Math.max(scoreMax, score);
                                    }
                                }
                                let upds = scoreUpdates;
                                scoreUpdates = [];
                                for (let update of upds) {
                                    update();
                                }
                                updateScaling();
                            }
                        }
                    }, 100);
                }
            };
        }
        if (newScr) {
            scriptDump.appendChild(newScr);
        }
    }
    return newContent;
}

function setImgPlaceholder(img) {
    if (!img.parentElement) {
        return;
    }
    img.onerror = undefined;
    img.dataset.errored_src = img.src;
    img.src = 'placeholder.png';
    if (rawData.min_width) {
        img.width = rawData.min_width;
        img.height = rawData.min_height;
    }
    setImageScale(img, getWantedScaling());
}

function optDescribe(isFirst, val) {
    return isFirst && val != null ? '<span title="' + escapeHtml(val.description) + '"><b>' + escapeHtml(val.title) + '</b></span><br>' : (val != null ? '<br>' : '');
}

function fillTable() {
    if (suppressUpdate) {
        return;
    }
    var x = getCurrentSelectedAxis('x');
    var y = getCurrentSelectedAxis('y');
    var x2 = getCurrentSelectedAxis('x2');
    var y2 = getCurrentSelectedAxis('y2');
    console.log('Do fill table, x=' + x + ', y=' + y + ', x2=' + x2 + ', y2=' + y2);
    var xAxis = getAxisById(x);
    var yAxis = getAxisById(y);
    var x2Axis = x2 == 'None' || x2 == x || x2 == y ? null : getAxisById(x2);
    var y2Axis = y2 == 'None' || y2 == x2 || y2 == x || y2 == y ? null : getAxisById(y2);
    var table = document.getElementById('image_table');
    var newContent = '<tr id="image_table_header" class="sticky_top"><th></th>';
    var superFirst = true;
    document.getElementById('image_script_dump').innerHTML = '';
    for (var x2val of (x2Axis == null ? [null] : x2Axis.values)) {
        if (x2val != null && !canShowVal(x2Axis.id, x2val.key)) {
            continue;
        }
        var x2first = true;
        for (var val of xAxis.values) {
            if (!canShowVal(xAxis.id, val.key)) {
                continue;
            }
            newContent += `<th${(superFirst ? '' : ' class="superaxis_second"')} title="${val.description.replaceAll('"', '&quot;')}">${optDescribe(x2first, x2val)}<b>${escapeHtml(val.title)}</b></th>`;
            x2first = false;
        }
        superFirst = !superFirst;
    }
    newContent += '</tr>';
    superFirst = true;
    for (var y2val of (y2Axis == null ? [null] : y2Axis.values)) {
        if (y2val != null && !canShowVal(y2Axis.id, y2val.key)) {
            continue;
        }
        var y2first = true;
        for (var val of yAxis.values) {
            if (!canShowVal(yAxis.id, val.key)) {
                continue;
            }
            newContent += `<tr><td class="axis_label_td${(superFirst ? '' : ' superaxis_second')}" title="${escapeHtml(val.description)}">${optDescribe(y2first, y2val)}<b>${escapeHtml(val.title)}</b></td>`;
            y2first = false;
            for (var x2val of (x2Axis == null ? [null] : x2Axis.values)) {
                if (x2val != null && !canShowVal(x2Axis.id, x2val.key)) {
                    continue;
                }
                newContent += getXAxisContent(x, y, xAxis, val, x2Axis, x2val, y2Axis, y2val);
            }
            newContent += '</tr>';
            if (x == y) {
                break;
            }
        }
        superFirst = !superFirst;
    }
    table.innerHTML = newContent;
    updateScaling();
}

function getCurrentSelectedAxis(axisPrefix) {
    var id = document.querySelector(`input[name="${axisPrefix}_axis_selector"]:checked`).id;
    var index = id.indexOf('_');
    return id.substring(index + 1);
}

function getShownItemsOfAxis(axis) {
    return axis.values.filter(val => canShowVal(axis.id, val.key));
}

function getWantedScaling() {
    if (!document.getElementById('autoScaleImages').checked) {
        return 0;
    }
    var x = getCurrentSelectedAxis('x');
    var xAxis = getAxisById(x);
    var count = getShownItemsOfAxis(xAxis).length;
    var x2 = getCurrentSelectedAxis('x2');
    if (x2 != 'none') {
        var x2Axis = getAxisById(x2);
        count *= getShownItemsOfAxis(x2Axis).length;
    }
    return (90 / count);
}

function setImageScale(image, percent) {
    let heatmapper = image.parentElement.getElementsByClassName('heatmapper')[0];
    if (percent == 0) {
        image.style.width = '';
        image.style.height = '';
        if (heatmapper) {
            heatmapper.style.width = `${image.clientWidth}px`;
            heatmapper.style.height = `${image.clientWidth}px`;
        }
    }
    else {
        image.style.width = percent + 'vw';
        if (heatmapper) {
            heatmapper.style.width = percent + 'vw';
            heatmapper.style.height = percent * (parseFloat(image.clientWidth) / parseFloat(image.clientHeight)) + 'vw';
        }
        let width = image.getAttribute('width');
        let height = image.getAttribute('height');
        if (width != null && height != null) { // Rescale placeholders cleanly
            image.style.height = (percent * (parseFloat(height) / parseFloat(width))) + 'vw';
        }
        else {
            image.style.height = '';
        }
    }
}

function updateScaling() {
    let percent = getWantedScaling();
    for (var image of document.getElementById('image_table').getElementsByClassName('table_img')) {
        setImageScale(image, percent);
    }
    updateTitleSticky();
}

function toggleDescriptions() {
    var show = document.getElementById('showDescriptions').checked;
    for (var cName of ['tabval_subdiv', 'axis_table_cell']) {
        for (var elem of document.getElementsByClassName(cName)) {
            elem.classList.toggle('tab_hidden', !show);
        }
    }
    updateTitleSticky();
}

function toggleShowAllAxis(axisId) {
    var axis = getAxisById(axisId);
    var hide = axis.values.some(val => {
        return canShowVal(axisId, val.key);
    });
    for (var val of axis.values) {
        setShowVal(axisId, val.key, !hide);
    }
    fillTable();
}

function toggleShowVal(axis, val) {
    var show = canShowVal(axis, val);
    let element = getNavValTab(axis, val);
    element.classList.toggle('tab_hidden', !show);
    if (!show && element.classList.contains('active')) {
        var next = [...element.parentElement.parentElement.getElementsByClassName('nav-link')].filter(e => !e.classList.contains('tab_hidden'));
        if (next.length > 0) {
            next[0].click();
        }
    }
    fillTable();
}

var anyRangeActive = false;

function enableRange(id) {
    var range = document.getElementById('range_tablist_' + id);
    var label = document.getElementById('label_range_tablist_' + id);
    range.oninput = function() {
        anyRangeActive = true;
        label.innerText = (range.value / 2) + ' seconds';
    };
    var tabPage = document.getElementById('tablist_' + id);
    return {
        range,
        counter: 0,
        tabs: tabPage.getElementsByClassName('nav-link')
    };
}

function clickTabAfterActiveTab(tabs) {
    var firstTab = null;
    var foundActive = false;
    var nextTab = Array.from(tabs).find(tab => {
        var isActive = tab.classList.contains('active');
        var isHidden = tab.classList.contains('tab_hidden');
        if (!isHidden && !isActive && !firstTab) {
            firstTab = tab;
        }
        if (isActive) {
            foundActive = true;
            return false;
        }
        return (foundActive && !isHidden);
    }) || firstTab;

    if (nextTab) {
        nextTab.click();
    }
    return nextTab;
}

const timer = ms => new Promise(res => setTimeout(res, ms));

async function startAutoScroll() {
    var rangeSet = [];
    for (var axis of rawData.axes) {
        rangeSet.push(enableRange(axis.id));
    }
    while (true) {
        await timer(500);
        if (!anyRangeActive) {
            continue;
        }
        for (var data of rangeSet) {
            if (data.range.value <= 0) {
                continue;
            }
            data.counter++;
            if (data.counter < data.range.value) {
                continue;
            }
            data.counter = 0;
            clickTabAfterActiveTab(data.tabs);
        }
    }
}

function crunchMetadata(parts) {
    if (!('metadata' in rawData)) {
        return {};
    }
    var initialData = structuredClone(rawData.metadata);
    if (!initialData) {
        return {};
    }
    for (var index = 0; index < parts.length; index++) {
        var part = parts[index];
        var axis = rawData.axes[index];
        var actualVal = axis.values.find(val => val.key == part);
        if (actualVal == null) {
            return { 'error': `metadata parsing failed for part ${index}: ${part}` };
        }
        for (var [key, value] of Object.entries(actualVal.params)) {
            key = key.replaceAll(' ', '');
            if (typeof(crunchParamHook) == 'undefined' || !crunchParamHook(initialData, key, value)) {
                initialData[key] = value;
            }
        }
    }
    return initialData;
}

function doPopupFor(img) {
    popoverLastImg = img;
    let modalElem = document.getElementById('image_info_modal');
    let metaText;
    if (typeof getMetadataForImage != 'undefined') {
        metaText = getMetadataForImage(img);
    }
    else {
        let imgPath = img.dataset.img_path.split('/');
        let metaData = crunchMetadata(imgPath);
        metaText = typeof(formatMetadata) == 'undefined' ? JSON.stringify(metaData) : formatMetadata(metaData);
    }
    let params = escapeHtml(metaText).replaceAll('\n', '\n<br>');
    let text = 'Image: ' + img.alt + (params.length > 1 ? ', parameters: <br>' + params : '<br>(parameters hidden)');
    modalElem.innerHTML = `<div class="modal-dialog" style="display:none">(click outside image to close)</div><div class="modal_inner_div"><img onclick="$('#image_info_modal').modal('hide')" class="popup_modal_img" src="${img.src}"><br><div class="popup_modal_undertext">${text}</div>`;
    $('#image_info_modal').modal('toggle');
}

function updateTitleStickyDirect() {
    var height = Math.round(document.getElementById('top_nav_bar').getBoundingClientRect().height);
    var header = document.getElementById('image_table_header');
    if (header.style.top != height + 'px') { // This check is to reduce the odds of the browser yelling at us
        header.style.top = height + 'px';
    }
}

function updateTitleSticky() {
    var header = document.getElementById('image_table_header');
    if (!header) {
        return;
    }
    updateHash();
    var topBar = document.getElementById('top_nav_bar');
    if (!topBar.classList.contains('sticky_top')) {
        header.style.top = '0';
        return;
    }
    // client rect is dynamically animated, so, uh, just hack it for now.
    // TODO: Actually smooth attachment.
    var rate = 50;
    for (var time = 0; time <= 500; time += rate) {
        setTimeout(updateTitleStickyDirect, time);
    }
}

function toggleTopSticky() {
    var topBar = document.getElementById('top_nav_bar');
    topBar.classList.remove('sticky_top');
    if (document.getElementById('stickyNavigation').checked) {
        topBar.classList.add('sticky_top');
    }
    updateTitleSticky();
}

function toggleLabelSticky() {
    updateHash();
    var table = document.getElementById('image_table');
    table.classList.remove('nostickytable');
    if (!document.getElementById('stickyLabels').checked) {
        table.classList.add('nostickytable');
    }
}

function removeGeneratedImages() {
    document.getElementById('save_image_output').innerHTML = '';
}

function makeImage(minRow = 0, doClear = true) {
    // Preprocess data
    var imageTable = document.getElementById('image_table');
    var rows = Array.from(imageTable.getElementsByTagName('tr')).filter(e => e.getElementsByTagName('img').length > 0);
    var header = document.getElementById('image_table_header');
    var headers = Array.from(header.getElementsByTagName('th')).slice(1);
    var widest_width = 0;
    var total_height = 0;
    var columns = 0;
    var rowData = [];
    var pad_x = 64, pad_y = 64;
    let count = 0;
    let sizeMult = parseFloat(document.getElementById('makeimage_size').value.replaceAll('x', ''));
    for (var row of rows) {
        count++;
        if (count < minRow) {
            continue;
        }
        var images = Array.from(row.getElementsByTagName('img'));
        var real_images = images.filter(i => i.src != 'placeholder.png');
        widest_width = Math.max(widest_width, ...real_images.map(i => i.naturalWidth * sizeMult));
        var height = Math.max(...real_images.map(i => i.naturalHeight * sizeMult));
        var y = pad_y + total_height;
        if (total_height + height > 30000) { // 32,767 is max canvas size
            setTimeout(() => makeImage(count, false), 100);
            break;
        }
        total_height += height + 1;
        columns = Math.max(columns, images.length);
        var label = row.getElementsByClassName('axis_label_td')[0];
        rowData.push({ row, images, real_images, height, label, y });
    }
    var holder = document.getElementById('save_image_output');
    if (doClear) {
        removeGeneratedImages();
    }
    // Temporary canvas to measure what padding we need
    var canvas = new OffscreenCanvas(256, 256);
    var ctx = canvas.getContext('2d');
    ctx.beginPath();
    ctx.rect(0, 0, canvas.width, canvas.height);
    ctx.font = '16px sans';
    ctx.textBaseline = 'top';
    for (var row of rowData) {
        var blocks = row.label.getElementsByTagName('b');
        pad_x = Math.max(pad_x, ctx.measureText(blocks[0].textContent).width);
        if (blocks.length == 2) {
            pad_x = Math.max(pad_x, ctx.measureText(blocks[1].textContent).width);
        }
    }
    pad_x = Math.min(pad_x, widest_width / 2);
    pad_x += 5;
    canvas = document.createElement('canvas');
    canvas.width = (widest_width + 1) * columns + pad_x;
    canvas.height = total_height + pad_y;
    ctx = canvas.getContext('2d');
    // Background
    ctx.beginPath();
    ctx.rect(0, 0, canvas.width, canvas.height);
    ctx.fillStyle = '#202020';
    ctx.fill();
    // Secondary color toggling
    var doColor = false;
    ctx.fillStyle = '#303030';
    var grid_x = pad_x;
    for (var part of headers) {
        if (part.getElementsByTagName('b').length == 2) {
            doColor = !doColor;
        }
        if (doColor) {
            ctx.beginPath();
            ctx.rect(grid_x, 0, widest_width, pad_y);
            ctx.fill();
        }
        grid_x += widest_width + 1;
    }
    doColor = false;
    for (var row of rowData) {
        if (row.label.getElementsByTagName('b').length == 2) {
            doColor = !doColor;
        }
        if (doColor) {
            ctx.beginPath();
            ctx.rect(0, row.y, pad_x, row.height);
            ctx.fill();
        }
    }
    // Grid lines
    ctx.fillStyle = '#000000';
    for (var row of rowData) {
        ctx.beginPath();
        ctx.rect(0, row.y, canvas.width, 1);
        ctx.fill();
    }
    grid_x = pad_x - 1;
    for (var i = 0; i < columns; i++) {
        ctx.beginPath();
        ctx.rect(grid_x, 0, 1, canvas.height);
        ctx.fill();
        grid_x += widest_width + 1;
    }
    // Text Labels
    ctx.font = '16px sans';
    ctx.textBaseline = 'top';
    ctx.fillStyle = '#ffffff';
    ctx.beginPath();
    ctx.rect(0, 0, canvas.width, canvas.height);
    grid_x = pad_x + 5;
    for (var part of headers) {
        var blocks = part.getElementsByTagName('b');
        if (blocks.length == 2) {
            ctx.fillText(blocks[0].textContent, grid_x, 5, widest_width);
            ctx.fillText(blocks[1].textContent, grid_x, 25, widest_width);
        }
        else {
            ctx.fillText(blocks[0].textContent, grid_x, 25, widest_width);
        }
        grid_x += widest_width + 1;
    }
    function wrap(text, width) {
        var words = text.split(' ');
        var lines = [];
        var line = '';
        for (var word of words) {
            var newLine = line + word + ' ';
            if (ctx.measureText(newLine).width > width) {
                lines.push(line);
                line = word + ' ';
            }
            else {
                line = newLine;
            }
        }
        lines.push(line);
        return lines.join('\n');
    }
    function writeMultiline(ctx, text, x, y) {
        for (var line of text.split('\n')) {
            ctx.fillText(line, x, y);
            y += 16;
        }
    }
    for (var row of rowData) {
        var blocks = row.label.getElementsByTagName('b');
        if (blocks.length == 2) {
            writeMultiline(ctx, wrap(blocks[0].textContent + "\n" + blocks[1].textContent, widest_width / 2), 5, row.y + 4);
        }
        else {
            writeMultiline(ctx, wrap(blocks[0].textContent, widest_width / 2), 5, row.y + 25);
        }
    }
    // Images
    for (var row of rowData) {
        var x = pad_x;
        for (var image of row.images) {
            if (image.src != 'placeholder.png') {
                ctx.drawImage(image, x, row.y, image.naturalWidth * sizeMult, image.naturalHeight * sizeMult);
                x += widest_width + 1;
            }
        }
    }
    var imageType = document.getElementById('makeimage_type').value;
    try {
        var data = canvas.toDataURL(`image/${imageType}`);
        canvas.remove();
        var img = new Image();
        img.className = 'save_image_output_img';
        img.src = data;
        holder.appendChild(img);
    }
    catch (e) {
        holder.appendChild(canvas);
        canvas.className = 'save_image_output_img';
        canvas.style.width = "200px";
        canvas.style.height = "200px";
    }
    $('#save_image_output_modal').modal('show');
}

function makeGif() {
    let holder = document.getElementById('save_image_output');
    removeGeneratedImages();
    let axisId = document.getElementById('makegif_axis').value;
    if (axisId == 'x-axis') {
        axisId = getCurrentSelectedAxis('x');
    }
    let sizeMult = parseFloat(document.getElementById('makegif_size').value.replaceAll('x', ''));
    let speed = parseFloat(document.getElementById('makegif_speed').value.replaceAll('/s', ''));
    let axis = getAxisById(axisId);
    let images = [];
    let imgPath = [];
    let index = 0;
    for (let subAxis of rawData.axes) {
        if (subAxis.id == axisId) {
            index = imgPath.length;
            imgPath.push(null);
        }
        else {
            imgPath.push(getSelectedValKey(subAxis));
        }
    }
    for (let val of axis.values) {
        if (!canShowVal(axis.id, val.key)) {
            continue;
        }
        imgPath[index] = val.path;
        let slashed = imgPath.join('/');
        let actualUrl = slashed + '.' + getExtension(slashed);
        images.push(actualUrl);
    }
    if (document.getElementById('makegif_direction').value == 'Backwards') {
        images.reverse();
    }
    let encoder = new GIFEncoder();
    encoder.setRepeat(0);
    encoder.setDelay(1000 / speed);
    encoder.start();
    let image1 = new Image();
    image1.src = images[0];
    image1.decode().then(() => {
        let canvas = document.createElement('canvas');
        canvas.width = image1.naturalWidth * sizeMult;
        canvas.height = image1.naturalHeight * sizeMult;
        ctx = canvas.getContext('2d');
        ctx.beginPath();
        let id = 1;
        let image2 = new Image();
        let callback = () => {
            ctx.drawImage(image2, 0, 0, canvas.width, canvas.height);
            encoder.addFrame(ctx);
            if (id >= images.length) {
                encoder.finish();
                let binary_gif = encoder.stream().getData();
                let data_url = 'data:image/gif;base64,' + encode64(binary_gif);
                let animatedImage = document.createElement('img');
                animatedImage.className = 'save_image_output_img';
                animatedImage.src = data_url;
                image1.remove();
                image2.remove();
                holder.appendChild(animatedImage);
                $('#save_image_output_modal').modal('show');
            }
            else {
                image2 = new Image();
                image2.src = images[id];
                id++;
                image2.decode().then(callback);
            }
        };
        image2.src = images[0];
        image2.decode().then(callback);
    });

}

function updateHash() {
    var hash = `#auto-loc`;
    for (let elem of ['showDescriptions', 'autoScaleImages', 'stickyNavigation', 'stickyLabels']) {
        hash += `,${document.getElementById(elem).checked}`;
    }
    for (let val of ['x', 'y', 'x2', 'y2']) {
        hash += `,${encodeURIComponent(getCurrentSelectedAxis(val))}`;
    }
    for (let subAxis of rawData.axes) {
        hash += `,${encodeURIComponent(getSelectedValKey(subAxis))}`;
    }
    for (let axis of rawData.axes) {
        for (let value of axis.values) {
            if (!canShowVal(axis.id, value.key)) {
                hash += `&hide=${encodeURIComponent(axis.id)},${encodeURIComponent(value.key)}`;
            }
        }
    }
    history.pushState(null, null, hash);
}

function applyHash(hash) {
    if (!hash) {
        return;
    }
    let params = hash.substring(1).split('&');
    for (let hidden of params.slice(1)) {
        let [action, value] = hidden.split('=');
        if (action == 'hide') {
            let [axis, val] = value.split(',');
            setShowVal(axis, val, false);
        }
    }
    let hashInputs = params[0].split(',');
    let expectedLen = 1 + 4 + 4 + rawData.axes.length;
    if (hashInputs.length != expectedLen) {
        console.log(`Hash length mismatch: ${hashInputs.length} != ${expectedLen}, skipping value reload.`);
        return;
    }
    if (hashInputs[0] != 'auto-loc') {
        console.log(`Hash prefix mismatch: ${hashInputs[0]} != auto-loc, skipping value reload.`);
        return;
    }
    let index = 1;
    for (let elem of ['showDescriptions', 'autoScaleImages', 'stickyNavigation', 'stickyLabels']) {
        document.getElementById(elem).checked = hashInputs[index++] == 'true';
    }
    for (let axis of ['x', 'y', 'x2', 'y2']) {
        let id = axis + '_' + decodeURIComponent(hashInputs[index++]);
        let target = document.getElementById(id);
        if (!target) {
            console.log(`Axis element not found: ${id}, skipping value reload.`);
            return;
        }
        target.click();
    }
    for (let subAxis of rawData.axes) {
        let val = decodeURIComponent(hashInputs[index++]);
        let target = getNavValTab(subAxis.id, val);
        if (!target) {
            console.log(`Axis-value element not found: ${id}, skipping value reload.`);
            return;
        }
        target.click();
    }
    updateStylesToMatchInputs();
}

let lastUpdateObj = null;
let updateCheckCount = 0;
let updatesWithoutData = 0;

function tryReloadImg(img) {
    let target = img.dataset.errored_src;
    delete img.dataset.errored_src;
    img.removeAttribute('width');
    img.removeAttribute('height');
    img.addEventListener('error', function() {
        setImgPlaceholder(img);
    });
    img.src = target;
    if (typeof getMetadataScriptFor != 'undefined') {
        let newScr = document.createElement('script');
        newScr.src = getMetadataScriptFor(img.dataset.img_path);
        document.getElementById('image_script_dump').appendChild(newScr);
    }
}

function checkForUpdates() {
    if (!window.lastUpdated) {
        if (updatesWithoutData++ > 2) {
            console.log('Update-checker has no more updates.');
            for (let img of document.querySelectorAll(`img[data-errored_src]`)) {
                tryReloadImg(img);
            }
            return;
        }
    }
    else {
        console.log(`Update-checker found ${window.lastUpdated.length} updates.`);
        for (let url of window.lastUpdated) {
            for (let img of document.querySelectorAll(`img[data-errored_src]`)) {
                if (img.dataset.errored_src.endsWith(url)) {
                    tryReloadImg(img);
                }
            }
        }
        updateScaling();
        window.lastUpdated = null;
    }
    if (lastUpdateObj != null) {
        lastUpdateObj.remove();
    }
    lastUpdateObj = document.createElement('script');
    lastUpdateObj.src = `last.js?vary=${updateCheckCount++}`;
    document.body.appendChild(lastUpdateObj);
    setTimeout(checkForUpdates, 5 * 1000);
}

loadData();


================================================
FILE: assets/styles.css
================================================
/*
 If you want to customize your stylesheet, please edit 'styles-user.css' instead of this.
*/

.sel_table tr td {
    border: 1px solid black;
    padding-right: 16px;
    padding-left: 16px;
}
.sel_table {
    min-width: 60em;
    width: 100%;
}
.secondary {
    background-color: #202020;
}
.emptytab {
    width: 0px;
}
.primary {
    background-color: #303030;
}
.axis_table_cell {
    width: calc(min(40em, max(10em, 100vw - 40em)));
    max-height: 8em;
    overflow: auto;
}
.tabval_subdiv {
    min-width: calc(100vw - 5em - min(40em, max(10em, 100vw - 40em)));
    max-height: 8em;
    overflow: auto;
}
table tr td,
table tr th {
    vertical-align: top;
    border: 1px solid black;
    word-break: break-all;
}
th {
    position: sticky;
    top: -1px;
    background-color: #202020;
    background-clip: padding-box;
}
table tr td:first-child {
    position: sticky;
    left: -1px;
}
.nostickytable tr:first-child {
    position: static !important;
}
.nostickytable tr th {
    position: static !important;
}
.nostickytable tr td:first-child {
    position: static !important;
}
.popup_modal_background {
    font-family: monospace, monospace;
    background-color: rgb(50, 50, 50, 0.7);
    text-align: center;
}
.popup_modal_img {
    height: 100%;
    width: 100%;
    max-height: calc(min(100vw, 100vh - 8em));
    max-width: 90vw;
    position: relative;
    margin: auto;
    object-fit: contain;
}
.popup_modal_undertext {
    background-color: #404060;
    overflow-wrap: break-word;
}
.modal_inner_div {
    max-width: 90vw;
    margin: auto;
}
.advanced_settings_section {
    max-width: calc(max(1500px, 90em));
    text-align: left;
    margin: auto;
}
.timer_range {
    vertical-align: bottom;
}
.tab_hidden {
    display: none;
}
.nav-link:focus {
    background-color: #909050 !important;
}
.axis_label_td {
    left: 0px;
    background-color: #202020;
    background-clip: padding-box;
    word-break: break-all;
    min-width: 10em;
}
.superaxis_second {
    background-color: #303030;
}
.axis_selectors {
    text-align: right;
    margin: auto;
    width: fit-content;
}
.sticky_top {
    position: sticky;
    top: 0;
}
.accordion-button {
    padding: 0.5rem;
}
.save_image_area {
    text-align: center;
}
.save_image_area img {
    margin-right: 1rem;
    margin-left: 1rem;
    border: 2px solid orange;
    border-radius: 0.5rem;
}
.content_box {
    display: inline-block;
}
.top_nav_bar {
    position: sticky;
    left: 2.5vw;
    width: 95vw;
}
.image_table_box {
    display: inline-block;
    min-width: 100%;
}
.image_table {
    margin: auto;
} 
.save_image_output {
    text-align: center;
}
.save_image_output_img {
    max-width: 100%;
    max-height: 70vh;
}
.advanced-checkbox {
    display: inline-block;
    border-radius: 0.2rem;
    border: 1px dashed rgba(255, 255, 255, 0.25);
    margin: 0.1rem;
}
label {
    display: inline;
}


================================================
FILE: gridgencore.py
================================================
# This file is part of Infinity Grid Generator, view the README.md at https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script for more information.

import os, glob, yaml, json, shutil, math, re, time
from copy import copy
from PIL import Image
from git import Repo

######################### Core Variables #########################

ASSET_DIR = os.path.dirname(__file__) + "/assets"
EXTRA_FOOTER = "..."
EXTRA_ASSETS = []
VERSION = None
valid_modes = {}
IMAGES_CACHE = None

######################### Hooks #########################

# hook(SingleGridCall)
grid_call_init_hook: callable = None
# hook(SingleGridCall, param_name: str, value: any) -> bool
grid_call_param_add_hook: callable = None
# hook(SingleGridCall, param_name: str, dry: bool)
grid_call_apply_hook: callable = None
# hook(GridRunner)
grid_runner_pre_run_hook: callable = None
# hook(GridRunner)
grid_runner_pre_dry_hook: callable = None
# hook(GridRunner, PassThroughObject, set: SingleGridCall) -> ResultObject
grid_runner_post_dry_hook: callable = None
# hook(GridRunner, SingleGridCall) -> int
grid_runner_count_steps: callable = None
# hook(PassThroughObject) -> dict
webdata_get_base_param_data: callable = None

######################### Utilities #########################

def escape_html(text: str):
    return str(text).replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;')

def clean_file_path(fn: str):
    fn = fn.replace('\\', '/')
    while '//' in fn:
        fn = fn.replace('//', '/')
    return fn

def get_version():
    global VERSION
    if VERSION is not None:
        return VERSION
    try:
        repo = Repo(os.path.dirname(__file__))
        VERSION = repo.head.commit.hexsha[:8]
    except Exception:
        VERSION = "Unknown"
    return VERSION

def list_image_files():
    global IMAGES_CACHE
    if IMAGES_CACHE is not None:
        return IMAGES_CACHE
    image_dir = clean_file_path(ASSET_DIR + "/images")
    IMAGES_CACHE = list()
    for path, _, files in os.walk(image_dir):
        for name in files:
            _, ext = os.path.splitext(name)
            if ext in [".jpg", ".png", ".webp"]:
                fn = path + "/" + name
                fn = clean_file_path(fn).replace(image_dir, '')
                while fn.startswith('/'):
                    fn = fn[1:]
                IMAGES_CACHE.append(fn)
    return IMAGES_CACHE

def clear_caches():
    global IMAGES_CACHE
    IMAGES_CACHE = None

def get_name_list():
    file_list = glob.glob(ASSET_DIR + "/*.yml")
    just_file_names = sorted(list(map(lambda f: os.path.relpath(f, ASSET_DIR), file_list)))
    return just_file_names

def fix_dict(d: dict):
    if d is None:
        return None
    if type(d) is not dict:
        raise RuntimeError(f"Value '{d}' is supposed to be submapping but isn't (it's plaintext, a list, or some other incorrect format). Did you typo the formatting?")
    return {str(k).lower(): v for k, v in d.items()}

def clean_for_web(text: str):
    if text is None:
        return None
    if type(text) is not str:
        raise RuntimeError(f"Value '{text}' is supposed to be text but isn't (it's a datamapping, list, or some other incorrect format). Did you typo the formatting?")
    return text.replace('"', '&quot;')

def clean_id(id: str):
    return re.sub("[^a-z0-9]", "_", id.lower().strip())

def clean_mode(id: str):
    return re.sub("[^a-z]", "", id.lower().strip())

def clean_name(name: str):
    return str(name).lower().replace(' ', '').replace('[', '').replace(']', '').strip()

def get_best_in_list(name: str, list: list):
    backup = None
    best_len = 999
    name = clean_name(name)
    for list_val in list:
        list_val_clean = clean_name(list_val)
        if list_val_clean == name:
            return list_val
        if name in list_val_clean:
            if len(list_val_clean) < best_len:
                backup = list_val
                best_len = len(list_val_clean)
    return backup

def choose_better_file_name(raw_name: str, full_name: str):
    partial_name = os.path.splitext(os.path.basename(full_name))[0]
    if '/' in raw_name or '\\' in raw_name or '.' in raw_name or len(raw_name) >= len(partial_name):
        return raw_name
    return partial_name

def fix_num(num):
    if num is None or math.isinf(num) or math.isnan(num):
        return None
    return num

def expand_numeric_list_ranges(in_list, num_type):
    out_list = list()
    for i in range(0, len(in_list)):
        raw_val = str(in_list[i]).strip()
        if raw_val in ["..", "...", "...."]:
            if i < 2 or i + 1 >= len(in_list):
                raise RuntimeError(f"Cannot use ellipses notation at index {i}/{len(in_list)} - must have at least 2 values before and 1 after.")
            prior = out_list[-1]
            double_prior = out_list[-2]
            after = num_type(in_list[i + 1])
            step = prior - double_prior
            if (step < 0) != ((after - prior) < 0):
                raise RuntimeError(f"Ellipses notation failed for step {step} between {prior} and {after} - steps backwards.")
            count = int((after - prior) / step)
            for x in range(1, count):
                out_list.append(prior + x * step)
        else:
            out_list.append(num_type(raw_val))
    return out_list

######################### Value Modes #########################

class GridSettingMode:
    """
    Defines a custom parameter input mode for an Infinity Grid Generator.
    'dry' is True if the mode should be processed in dry runs, or False if it should be skipped.
    'type' is 'text', 'integer', 'decimal', or 'boolean'
    'apply' is a function to call taking (passthroughObject, value)
    'min' is for integer/decimal type, optional minimum value
    'max' is for integer/decimal type, optional maximum value
    'valid_list' is for text type, an optional lambda that returns a list of valid values
    'clean' is an optional function to call that takes (passthroughObject, value) and returns a cleaned copy of the value, or raises an error if invalid
    'parse_list' is an optional function to call that takes a List and returns a List, to apply any special pre-processing for list-format inputs.
    """
    def __init__(self, dry: bool, type: str, apply: callable, min: float = None, max: float = None, valid_list: callable = None, clean: callable = None, parse_list: callable = None):
        self.dry = dry
        self.type = type
        self.apply = apply
        self.min = min
        self.max = max
        self.clean = clean
        self.valid_list = valid_list
        self.parse_list = parse_list

def registerMode(name: str, mode: GridSettingMode):
    mode.name = name
    valid_modes[clean_name(name)] = mode

######################### Validation #########################

def validate_params(grid, params: dict):
    for p,v in params.items():
        params[p] = validate_single_param(p, grid.proc_variables(v))

def apply_field(name: str):
    def applier(p, v):
        setattr(p, name, v)
    return applier

def apply_field_as_image_data(name: str):
    def applier(p, v):
        file_name = get_best_in_list(v, list_image_files())
        if file_name is None:
            raise RuntimeError("Invalid parameter '{p}' as '{v}': image file does not exist")
        path = ASSET_DIR + "/images/" + file_name
        image = Image.open(path)
        setattr(p, name, image)
    return applier

def validate_single_param(p: str, v):
    orig_v = v
    p = clean_mode(p)
    mode = valid_modes.get(p)
    if mode is None:
        raise RuntimeError(f"Invalid grid parameter '{p}': unknown mode")
    mode_type = mode.type
    if mode_type == "integer":
        v_int = int(v)
        if v_int is None:
            raise RuntimeError(f"Invalid parameter '{p}' as '{orig_v}': must be an integer number")
        min = mode.min
        max = mode.max
        if min is not None and v_int < min:
            raise RuntimeError(f"Invalid parameter '{p}' as '{orig_v}': must be at least {min}")
        if max is not None and v_int > max:
            raise RuntimeError(f"Invalid parameter '{p}' as '{orig_v}': must not exceed {max}")
        v = v_int
    elif mode_type == "decimal":
        v_float = float(v)
        if v_float is None:
            raise RuntimeError(f"Invalid parameter '{p}' as '{orig_v}': must be a decimal number")
        min = mode.min
        max = mode.max
        if min is not None and v_float < min:
            raise RuntimeError(f"Invalid parameter '{p}' as '{orig_v}': must be at least {min}")
        if max is not None and v_float > max:
            raise RuntimeError(f"Invalid parameter '{p}' as '{orig_v}': must not exceed {max}")
        v = v_float
    elif mode_type == "boolean":
        v_clean = str(v).lower().strip()
        if v_clean == "true":
            v = True
        elif v_clean == "false":
            v = False
        else:
            raise RuntimeError(f"Invalid parameter '{p}' as '{orig_v}': must be either 'true' or 'false'")
    elif mode_type == "text" and mode.valid_list is not None:
        valid_list = mode.valid_list()
        v = get_best_in_list(clean_name(v), valid_list)
        if v is None:
            raise RuntimeError(f"Invalid parameter '{p}' as '{orig_v}': not matched to any entry in list {list(valid_list)}")
    if mode.clean is not None:
        return mode.clean(p, v)
    return v

######################### YAML Parsing and Processing #########################

class GridYamlLoader(yaml.SafeLoader):
    pass

try:
    from yamlinclude import YamlIncludeConstructor
    YamlIncludeConstructor.add_to_loader_class(loader_class=GridYamlLoader, base_dir=ASSET_DIR)
except:
    from yaml_include import Constructor
    yaml.add_constructor("!include", Constructor(base_dir=ASSET_DIR), GridYamlLoader)

class AxisValue:
    def __init__(self, axis, grid, key: str, val):
        self.axis = axis
        self.key = clean_id(str(key))
        if any(x.key == self.key for x in axis.values):
            self.key += f"__{len(axis.values)}"
        self.params = list()
        if isinstance(val, str):
            halves = val.split('=', maxsplit=1)
            if len(halves) != 2:
                raise RuntimeError(f"Invalid value '{key}': '{val}': not expected format")
            self.skip = False
            halves[0] = grid.proc_variables(halves[0])
            halves[1] = grid.proc_variables(halves[1])
            try:
                halves[1] = validate_single_param(halves[0], halves[1])
            except RuntimeError:
                if grid.skip_invalid:
                    self.skip = True
                else:
                    raise
            self.title = halves[1]
            self.params = { clean_mode(halves[0]): halves[1] }
            self.description = None
            self.show = True
            self.path = clean_name(self.key)
        else:
            self.title = grid.proc_variables(val.get("title"))
            self.description = grid.proc_variables(val.get("description"))
            self.skip = (str(grid.proc_variables(val.get("skip")))).lower() == "true"
            self.params = fix_dict(val.get("params"))
            self.show = (str(grid.proc_variables(val.get("show")))).lower() != "false"
            self.path = str(val.get("path") or clean_name(self.key))
            if self.title is None or self.params is None:
                raise RuntimeError(f"Invalid value '{key}': '{val}': missing title or params")
            if not self.skip:
                try:
                    validate_params(grid, self.params)
                except RuntimeError:
                    if grid.skip_invalid:
                        self.skip = True
                    else:
                        raise
    
    def __str__(self):
        return f"(title={self.title}, description={self.description}, params={self.params})"
    def __unicode__(self):
        return self.__str__()

class Axis:
    def build_from_list_str(self, id, grid, list_str):
        is_split_by_double_pipe = "||" in list_str
        values_list = list_str.split("||" if is_split_by_double_pipe else ",")
        self.mode_name = clean_name(str(id))
        self.mode = valid_modes.get(clean_mode(self.mode_name))
        if self.mode is None:
            raise RuntimeError(f"Invalid axis mode '{self.mode}' from '{id}': unknown mode")
        if self.mode.type == "integer":
            values_list = expand_numeric_list_ranges(values_list, int)
        elif self.mode.type == "decimal":
            values_list = expand_numeric_list_ranges(values_list, float)
        index = 0
        if self.mode.parse_list is not None:
            values_list = self.mode.parse_list(values_list)
        for val in values_list:
            try:
                if isinstance(val, dict):
                    self.values.append(AxisValue(self, grid, str(index), val))
                    continue
                val = str(val).strip()
                index += 1
                if is_split_by_double_pipe and val == "" and index == len(values_list):
                    continue
                self.values.append(AxisValue(self, grid, str(index), f"{id}={val}"))
            except Exception as e:
                raise RuntimeError(f"value '{val}' errored: {e}")

    def __init__(self, grid, id: str, obj):
        self.raw_id = id
        self.values = list()
        self.id = clean_id(str(id))
        if any(x.id == self.id for x in grid.axes):
            self.id += f"__{len(grid.axes)}"
        if isinstance(obj, str):
            self.title = id
            self.default = None
            self.description = ""
            self.build_from_list_str(id, grid, obj)
        else:
            self.title = grid.proc_variables(obj.get("title"))
            self.default = grid.proc_variables(obj.get("default"))
            if self.title is None:
                raise RuntimeError("missing title")
            self.description = grid.proc_variables(obj.get("description"))
            values_obj = obj.get("values")
            if values_obj is None:
                raise RuntimeError("missing values")
            elif isinstance(values_obj, str):
                self.build_from_list_str(id, grid, values_obj)
            else:
                for key, val in values_obj.items():
                    try:
                        self.values.append(AxisValue(self, grid, key, val))
                    except Exception as e:
                        raise RuntimeError(f"value '{key}' errored: {e}")

class GridFileHelper:
    def proc_variables(self, text):
        if text is None:
            return None
        text = str(text)
        for key, val in self.variables.items():
            text = text.replace(key, val)
        return text
    
    def read_grid_direct(self, key: str):
        return self.grid_obj.get(key)
    
    def read_str_from_grid(self, key: str):
        return self.proc_variables(self.read_grid_direct(key))

    def parse_yaml(self, yaml_content: dict, grid_file: str):
        self.variables = dict()
        self.axes = list()
        yaml_content = fix_dict(yaml_content)
        vars_obj = fix_dict(yaml_content.get("variables"))
        if vars_obj is not None:
            for key, val in vars_obj.items():
                self.variables[str(key).lower()] = str(val)
        self.grid_obj = fix_dict(yaml_content.get("grid"))
        if self.grid_obj is None:
            raise RuntimeError(f"Invalid file {grid_file}: missing basic 'grid' root key")
        self.title = self.read_str_from_grid("title")
        self.description = self.read_str_from_grid("description")
        self.stylesheet = self.read_str_from_grid("stylesheet") or ''
        self.author = self.read_str_from_grid("author")
        self.format = self.read_str_from_grid("format")
        self.out_path = self.grid_obj.get("outpath")
        self.skip_invalid = self.read_grid_direct("skip_invalid") or getattr(self, 'skip_invalid', False)
        if self.title is None or self.description is None or self.author is None or self.format is None:
            raise RuntimeError(f"Invalid file {grid_file}: missing grid title, author, format, or description in grid obj {self.grid_obj}")
        self.params = fix_dict(self.grid_obj.get("params"))
        if self.params is not None:
            validate_params(self, self.params)
        axes_obj = fix_dict(yaml_content.get("axes"))
        if axes_obj is None:
            raise RuntimeError(f"Invalid file {grid_file}: missing basic 'axes' root key")
        for id, axis_obj in axes_obj.items():
            try:
                self.axes.append(Axis(self, id, axis_obj if isinstance(axis_obj, str) else fix_dict(axis_obj)))
            except Exception as e:
                raise RuntimeError(f"Invalid axis '{id}': errored: {e}")
        total_count = 1
        for axis in self.axes:
            total_count *= len(axis.values)
        if total_count <= 0:
            raise RuntimeError(f"Invalid file {grid_file}: something went wrong ... is an axis empty? total count is {total_count} for {len(self.axes)} axes")
        clean_desc = self.description.replace('\n', ' ')
        print(f"Loaded grid file, title '{self.title}', description '{clean_desc}', with {len(self.axes)} axes... combines to {total_count} total images")
        return self

######################### Actual Execution Logic #########################

class SingleGridCall:
    def __init__(self, values: list):
        self.values = values
        self.skip = False
        for val in values:
            if val.skip:
                self.skip = True
        if grid_call_init_hook is not None:
            grid_call_init_hook(self)

    def flatten_params(self, grid: GridFileHelper):
        self.grid = grid
        self.params = grid.params.copy() if grid.params is not None else dict()
        for val in self.values:
            for p, v in val.params.items():
                if grid_call_param_add_hook is None or not grid_call_param_add_hook(self, p, v):
                    self.params[p] = v

    def apply_to(self, p, dry: bool):
        for name, val in self.params.items():
            mode = valid_modes[clean_mode(name)]
            if not dry or mode.dry:
                mode.apply(p, val)
        if grid_call_apply_hook is not None:
            grid_call_apply_hook(self, p, dry)

class GridRunner:
    def __init__(self, grid: GridFileHelper, do_overwrite: bool, base_path: str, p, fast_skip: bool):
        self.grid = grid
        self.total_run = 0
        self.total_skip = 0
        self.total_steps = 0
        self.do_overwrite = do_overwrite
        self.base_path = base_path
        self.fast_skip = fast_skip
        self.p = p
        grid.min_width = None
        grid.min_height = None
        grid.initial_p = p
        self.last_update = []

    def update_live_file(self, new_file: str):
        t_now = time.time()
        self.last_update = [x for x in self.last_update if t_now - x['t'] < 20]
        self.last_update.append({'f': new_file, 't': t_now})
        with open(self.base_path + '/last.js', 'w', encoding="utf-8") as f:
            update_str = '", "'.join([x['f'] for x in self.last_update])
            f.write(f'window.lastUpdated = ["{update_str}"]')

    def build_value_set_list(self, axis_list: list):
        result = list()
        if len(axis_list) == 0:
            return result
        cur_axis = axis_list[0]
        if len(axis_list) == 1:
            for val in cur_axis.values:
                if not val.skip or not self.fast_skip:
                    new_list = list()
                    new_list.append(val)
                    result.append(SingleGridCall(new_list))
            return result
        next_axis_list = axis_list[1::]
        for obj in self.build_value_set_list(next_axis_list):
            for val in cur_axis.values:
                if not val.skip or not self.fast_skip:
                    new_list = obj.values.copy()
                    new_list.append(val)
                    result.append(SingleGridCall(new_list))
        return result

    def preprocess(self):
        self.value_sets = self.build_value_set_list(list(reversed(self.grid.axes)))
        print(f'Have {len(self.value_sets)} unique value sets, will go into {self.base_path}')
        for set in self.value_sets:
            set.filepath = self.base_path + '/' + '/'.join(list(map(lambda v: v.path, set.values)))
            set.data = ', '.join(list(map(lambda v: f"{v.axis.title}={v.title}", set.values)))
            set.flatten_params(self.grid)
            set.do_skip = set.skip or (not self.do_overwrite and os.path.exists(set.filepath + "." + self.grid.format))
            if set.do_skip:
                self.total_skip += 1
            else:
                self.total_run += 1
                self.total_steps += grid_runner_count_steps(self, set) if grid_runner_count_steps is not None else 1
        print(f"Skipped {self.total_skip} files, will run {self.total_run} files, for {self.total_steps} total steps")

    def run(self, dry: bool):
        if grid_runner_pre_run_hook is not None:
            grid_runner_pre_run_hook(self)
        iteration = 0
        last = None
        for set in self.value_sets:
            if set.do_skip:
                continue
            iteration += 1
            if not dry:
                print(f'On {iteration}/{self.total_run} ... Set: {set.data}, file {set.filepath}')
            p = copy(self.p)
            if grid_runner_pre_dry_hook is not None:
                grid_runner_pre_dry_hook(self)
            set.apply_to(p, dry)
            if dry:
                continue
            try:
                last = grid_runner_post_dry_hook(self, p, set)
            except FileNotFoundError as e:
                if e.strerror == 'The filename or extension is too long' and hasattr(e, 'winerror') and e.winerror == 206:
                    print(f"\n\n\nOS Error: {e.strerror} - see this article to fix that: https://www.autodesk.com/support/technical/article/caas/sfdcarticles/sfdcarticles/The-Windows-10-default-path-length-limitation-MAX-PATH-is-256-characters.html \n\n\n")
                raise e
            self.update_live_file(set.filepath + "." + self.grid.format)
        return last

######################### Web Data Builders #########################

class WebDataBuilder():
    def build_json(grid: GridFileHelper, publish_gen_metadata: bool, p, dry_run: bool):
        def get_axis(axis: str):
            id = grid.read_str_from_grid(axis)
            if id is None:
                return ''
            id = str(id).lower()
            if id == 'none':
                return 'none'
            possible = [x.id for x in grid.axes if x.raw_id == id]
            if len(possible) == 0:
                raise RuntimeError(f"Cannot find axis '{id}' for axis default '{axis}'... valid: {[x.raw_id for x in grid.axes]}")
            return possible[0]
        show_descrip = grid.read_grid_direct('show descriptions')
        result = {
            'title': grid.title,
            'description': grid.description,
            'ext': grid.format,
            'min_width': grid.min_width,
            'min_height': grid.min_height,
            'defaults': {
                'show_descriptions': True if show_descrip is None else show_descrip,
                'autoscale': grid.read_grid_direct('autoscale') or False,
                'sticky': grid.read_grid_direct('sticky') or False,
                'sticky_labels': grid.read_grid_direct('sticky labels') or False,
                'x': get_axis('x axis'),
                'y': get_axis('y axis'),
                'x2': get_axis('x super axis'),
                'y2': get_axis('y super axis')
            }
        }
        if not dry_run:
            result['will_run'] = True
        if publish_gen_metadata:
            result['metadata'] = None if webdata_get_base_param_data is None else webdata_get_base_param_data(p)
        axes = list()
        for axis in grid.axes:
            j_axis = {
                'id': str(axis.id).lower(),
                'title': axis.title,
                'description': axis.description or ""
            }
            exported_paths = {}
            values = list()
            for val in axis.values:
                if val.path in exported_paths:
                    continue
                exported_paths[val.path] = val
                j_val = {
                    'key': str(val.key).lower(),
                    'path': str(val.path),
                    'title': val.title,
                    'description': val.description or "",
                    'show': val.show
                }
                if publish_gen_metadata:
                    j_val['params'] = val.params
                values.append(j_val)
            j_axis['values'] = values
            axes.append(j_axis)
        result['axes'] = axes
        return json.dumps(result)

    def radio_button_html(name, id, descrip, label):
        return f'<input type="radio" class="btn-check" name="{name}" id="{str(id).lower()}" autocomplete="off" checked=""><label class="btn btn-outline-primary" for="{str(id).lower()}" title="{descrip}">{escape_html(label)}</label>\n'

    def axis_bar(label, content):
        return f'<br><div class="btn-group" role="group" aria-label="Basic radio toggle button group">{label}:&nbsp;\n{content}</div>\n'

    def build_html(grid):
        with open(ASSET_DIR + "/page.html", 'r', encoding="utf-8") as reference_html:
            html = reference_html.read()
        x_select = ""
        y_select = ""
        x2Select = WebDataBuilder.radio_button_html('x2_axis_selector', f'x2_none', 'None', 'None')
        y2Select = WebDataBuilder.radio_button_html('y2_axis_selector', f'y2_none', 'None', 'None')
        content = '<div style="margin: auto; width: fit-content;"><table class="sel_table">\n'
        advanced_settings = ''
        primary = True
        for axis in grid.axes:
            try:
                axis_descrip = clean_for_web(axis.description or '')
                tr_class = "primary" if primary else "secondary"
                content += f'<tr class="{tr_class}">\n<td>\n<h4>{escape_html(axis.title)}</h4>\n'
                advanced_settings += f'\n<h4>{axis.title}</h4><div class="timer_box">Auto cycle every <input style="width:30em;" autocomplete="off" type="range" min="0" max="360" value="0" class="form-range timer_range" id="range_tablist_{axis.id}"><label class="form-check-label" for="range_tablist_{axis.id}" id="label_range_tablist_{axis.id}">0 seconds</label></div>\nShow value: '
                axis_class = "axis_table_cell"
                if len(axis_descrip.strip()) == 0:
                    axis_class += " emptytab"
                content += f'<div class="{axis_class}">{axis_descrip}</div></td>\n<td><ul class="nav nav-tabs" role="tablist" id="tablist_{axis.id}">\n'
                primary = not primary
                is_first = axis.default is None
                exported_paths = {}
                for val in axis.values:
                    if val.path in exported_paths:
                        continue
                    exported_paths[val.path] = val
                    if axis.default is not None:
                        is_first = str(axis.default) == str(val.key)
                    selected = "true" if is_first else "false"
                    active = " active" if is_first else ""
                    is_first = False
                    descrip = clean_for_web(val.description or '')
                    content += f'<li class="nav-item" role="presentation"><a class="nav-link{active}" data-bs-toggle="tab" href="#tab_{axis.id}__{val.key}" id="clicktab_{axis.id}__{val.key}" aria-selected="{selected}" role="tab" title="{escape_html(val.title)}: {descrip}">{escape_html(val.title)}</a></li>\n'
                    advanced_settings += f'&nbsp;<input class="form-check-input" type="checkbox" autocomplete="off" id="showval_{axis.id}__{val.key}" checked="true" onchange="javascript:toggleShowVal(\'{axis.id}\', \'{val.key}\')"> <label class="form-check-label" for="showval_{axis.id}__{val.key}" title="Uncheck this to hide \'{escape_html(val.title)}\' from the page.">{escape_html(val.title)}</label>'
                advanced_settings += f'&nbsp;&nbsp;<button class="submit" onclick="javascript:toggleShowAllAxis(\'{axis.id}\')">Toggle All</button>'
                content += '</ul>\n<div class="tab-content">\n'
                is_first = axis.default is None
                for val in axis.values:
                    if axis.default is not None:
                        is_first = str(axis.default) == str(val.key)
                    active = " active show" if is_first else ""
                    is_first = False
                    descrip = clean_for_web(val.description or '')
                    if len(descrip.strip()) == 0:
                        active += " emptytab"
                    content += f'<div class="tab-pane{active}" id="tab_{axis.id}__{val.key}" role="tabpanel"><div class="tabval_subdiv">{descrip}</div></div>\n'
            except Exception as e:
                raise RuntimeError(f"Failed to build HTML for axis '{axis.id}': {e}")
            content += '</div></td></tr>\n'
            x_select += WebDataBuilder.radio_button_html('x_axis_selector', f'x_{axis.id}', axis_descrip, axis.title)
            y_select += WebDataBuilder.radio_button_html('y_axis_selector', f'y_{axis.id}', axis_descrip, axis.title)
            x2Select += WebDataBuilder.radio_button_html('x2_axis_selector', f'x2_{axis.id}', axis_descrip, axis.title)
            y2Select += WebDataBuilder.radio_button_html('y2_axis_selector', f'y2_{axis.id}', axis_descrip, axis.title)
        content += '</table>\n<div class="axis_selectors">'
        content += WebDataBuilder.axis_bar('X Axis', x_select)
        content += WebDataBuilder.axis_bar('Y Axis', y_select)
        content += WebDataBuilder.axis_bar('X Super-Axis', x2Select)
        content += WebDataBuilder.axis_bar('Y Super-Axis', y2Select)
        content += '</div></div>\n'
        html = html.replace("{TITLE}", grid.title).replace("{CLEAN_DESCRIPTION}", clean_for_web(grid.description)).replace("{DESCRIPTION}", grid.description).replace("{CONTENT}", content).replace("{ADVANCED_SETTINGS}", advanced_settings).replace("{AUTHOR}", grid.author).replace("{EXTRA_FOOTER}", EXTRA_FOOTER).replace("{VERSION}", get_version())
        return html

    def emit_web_data(path: str, grid, publish_gen_metadata: bool, p, yaml_content: dict, dry_run: bool):
        print("Building final web data...")
        os.makedirs(path, exist_ok=True)
        json = WebDataBuilder.build_json(grid, publish_gen_metadata, p, dry_run)
        if not dry_run:
            with open(path + '/last.js', 'w', encoding="utf-8") as f:
                f.write("window.lastUpdated = []")
        with open(path + "/data.js", 'w', encoding="utf-8") as f:
            f.write("rawData = " + json)
        with open(path + "/config.yml", 'w', encoding="utf-8") as f:
            yaml.dump(yaml_content, f, sort_keys=False, default_flow_style=False, width=1000)
        for f in ["bootstrap.min.css", "jsgif.js", "bootstrap.bundle.min.js", "proc.js", "jquery.min.js", "styles.css", "placeholder.png"] + EXTRA_ASSETS:
            shutil.copyfile(ASSET_DIR + "/" + f, path + "/" + f)
        with open(ASSET_DIR + "/styles-user.css", 'r', encoding="utf-8") as style:
            with open(path + "/styles-user.css", 'w', encoding="utf-8") as f:
                f.write(style.read() + '\n' + (grid.stylesheet or ''))
        html = WebDataBuilder.build_html(grid)
        with open(path + "/index.html", 'w', encoding="utf-8") as f:
            f.write(html)
        print(f"Web file is now at {path}/index.html")
        return json

######################### Main Runner Function #########################

def run_grid_gen(pass_through_obj, input_file: str, output_folder_base: str, output_folder_name: str = None, do_overwrite: bool = False,
               fast_skip: bool = False, generate_page: bool = True, publish_gen_metadata: bool = True, dry_run: bool = False, manual_pairs: list = None, allow_includes: bool = True, skip_invalid: bool = False):
    grid = GridFileHelper()
    grid.stylesheet = ''
    grid.skip_invalid = skip_invalid
    yaml_content = None
    if manual_pairs is None:
        full_input_path = ASSET_DIR + "/" + input_file
        if not os.path.exists(full_input_path):
            raise RuntimeError(f"Non-existent file '{input_file}'")
        # Parse and verify
        with open(full_input_path, 'r', encoding="utf-8") as yaml_content_text:
            try:
                if allow_includes:
                    yaml_content = yaml.load(yaml_content_text, Loader=GridYamlLoader)
                else:
                    yaml_content = yaml.safe_load(yaml_content_text)
            except yaml.YAMLError as exc:
                raise RuntimeError(f"Invalid YAML in file '{input_file}': {exc}")
        grid.parse_yaml(yaml_content, input_file)
    else:
        grid.title = output_folder_name
        grid.description = ""
        grid.variables = dict()
        grid.author = "Unspecified"
        grid.format = "png"
        grid.axes = list()
        grid.params = None
        grid.grid_obj = {}
        yaml_content = {
            'grid': {
                'title': grid.title,
                'description': grid.description,
                'format': grid.format,
                'author': grid.author
            },
            'axes': {}
        }
        for i in range(0, int(len(manual_pairs) / 2)):
            key = manual_pairs[i * 2]
            if isinstance(key, str) and key.strip() != "":
                try:
                    val = manual_pairs[i * 2 + 1]
                    grid.axes.append(Axis(grid, key, val))
                    yaml_key = key
                    duplicates = 1
                    while yaml_key in yaml_content['axes']:
                        duplicates += 1
                        yaml_key = f"{key} {duplicates}"
                    yaml_content['axes'][yaml_key] = val
                except Exception as e:
                    raise RuntimeError(f"Invalid axis {(i + 1)} '{key}': errored: {e}")
    # Now start using it
    if output_folder_name.strip() == "":
        if grid.out_path is None:
            output_folder_name = input_file.replace(".yml", "")
        else:
            output_folder_name = grid.out_path.strip()
    if os.path.isabs(output_folder_name):
        folder = output_folder_name
    else:
        folder = output_folder_base + "/" + output_folder_name
    runner = GridRunner(grid, do_overwrite, folder, pass_through_obj, fast_skip)
    runner.preprocess()
    if generate_page:
        json = WebDataBuilder.emit_web_data(folder, grid, publish_gen_metadata, pass_through_obj, yaml_content, dry_run)
    result = runner.run(dry_run)
    if dry_run:
        print("Infinite Grid dry run succeeded without error")
    else:
        json = json.replace('"will_run": true, ', '')
        with open(folder + "/data.js", 'w', encoding="utf-8") as f:
            f.write("rawData = " + json)
        os.remove(folder + "/last.js")
    return result


================================================
FILE: install.py
================================================
import launch

if not launch.is_installed("yaml"):
    launch.run_pip("install pyyaml", "pyyaml for Infinity Grid Script")
if not launch.is_installed("yamlinclude"):
    launch.run_pip("install pyyaml-include", "pyyaml-include for Infinity Grid Script")


================================================
FILE: javascript/infinity_grid.js
================================================
/**
 * Stable Diffusion Infinity Grid Generator
 *
 * Author: Alex 'mcmonkey' Goodwin
 *
 * GitHub URL: https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script
 * Created: 2022/12/08
 * Last updated: 2023/02/19
 *
 * For usage help, view the README.md file in the extension root, or via the GitHub page.
 *
 */

// Title injection code referenced from d8ahazard's Dreambooth extension
ex_titles = titles;

new_titles = {
    "Select grid definition file": "Select the grid definition yaml file, in your '(extension)/assets' folder. Refer to the README for info.",
    "Overwrite existing images (for updating grids)": "If checked, any existing image files will be overwritten - this is useful if you want to redo the grid with completely different base settings. If unchecked, if an image already exists, it will be skipped - this is useful for adding new options to an existing grid.",
    "Generate infinite-grid webviewer page": "If checked, generate the webviewer page. If unchecked, won't generate. You can uncheck this for dryruns that don't need it, or to avoid overwriting customized pages.",
    "Do a dry run to validate your grid file": "If checked, no images will be rendered - it will just validate your YAML and all its content. Check the WebUI's console for any messages.",
    "Publish full generation metadata for viewing on-page": "If checked, any/all image metadata will be stored in the webpage's files, and the internal values of each axis. This is useful for viewing, but if you're sharing a generation where some details are private (eg exact prompt text) you'll want to uncheck this. Note that this doesn't change whether metadata gets stored in images or not, edit your Settings tab to configure that.",
    "Use more-performant skipping": "Only matters if you have 'skip: true' on any values - if checked, uses a method of skipping that improves performance but prevents validation of the skipped options.",
    "Validate PromptReplace input": "If unchecked, will allow useless PromptReplace settings to be ignored. If checked, will error if the replace won't do anything."
}

for (var i = 1; i <= 16; i++) {
    new_titles[`Axis ${i} Mode`] = "Select the desired mode / setting - ie what value it is that should be changing, for this axis. You have as many axes as you need, just select modes and more will be added as you go.";
    new_titles[`Axis ${i} Value`] = "Fill in values applicable to the mode, separated by commas. If it's a numeric mode, you can do for example '1, 2, 3'.";
}

ex_titles = Object.assign({}, ex_titles, new_titles);
titles = ex_titles;


================================================
FILE: scripts/infinity_grid.py
================================================
##################
# Stable Diffusion Infinity Grid Generator
#
# Author: Alex 'mcmonkey' Goodwin
# GitHub URL: https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script
# Created: 2022/12/08
# Last updated: 2023/02/19
# License: MIT
#
# For usage help, view the README.md file in the extension root, or via the GitHub page.
#
##################

import gradio as gr
import os, numpy, threading
from copy import copy
from datetime import datetime
from modules import images, shared, sd_models, sd_vae, sd_samplers, scripts, processing, ui_components
from modules.processing import process_images, Processed
from modules.shared import opts, state
from PIL import Image
import gridgencore as core
from gridgencore import clean_name, clean_mode, get_best_in_list, choose_better_file_name, GridSettingMode, fix_num, apply_field, registerMode

######################### Constants #########################
refresh_symbol = '\U0001f504'  # 🔄
fill_values_symbol = "\U0001f4d2"  # 📒
INF_GRID_README = "https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script"
core.EXTRA_FOOTER = 'Images area auto-generated by an AI (Stable Diffusion) and so may not have been reviewed by the page author before publishing.\n<script src="a1111webui.js?vary=9"></script>'
core.EXTRA_ASSETS = ["a1111webui.js"]

######################### Value Mode Helpers #########################

def get_model_for(name):
    return get_best_in_list(name, map(lambda m: m.title, sd_models.checkpoints_list.values()))

def apply_model(p, v):
    opts.sd_model_checkpoint = get_model_for(v)
    sd_models.reload_model_weights()

def clean_model(p, v):
    actual_model = get_model_for(v)
    if actual_model is None:
        raise RuntimeError(f"Invalid parameter '{p}' as '{v}': model name unrecognized - valid {list(map(lambda m: m.title, sd_models.checkpoints_list.values()))}")
    return choose_better_file_name(v, actual_model)

def get_vae_for(name):
    return get_best_in_list(name, sd_vae.vae_dict.keys())

def apply_vae(p, v):
    vae_name = clean_name(v)
    if vae_name == "none":
        vae_name = "None"
    elif vae_name in ["auto", "automatic"]:
        vae_name = "Automatic"
    else:
        vae_name = get_vae_for(vae_name)
    opts.sd_vae = vae_name
    sd_vae.reload_vae_weights(None)

def clean_vae(p, v):
    vae_name = clean_name(v)
    if vae_name in ["none", "auto", "automatic"]:
        return vae_name
    actual_vae = get_vae_for(vae_name)
    if actual_vae is None:
        raise RuntimeError(f"Invalid parameter '{p}' as '{v}': VAE name unrecognized - valid: {list(sd_vae.vae_dict.keys())}")
    return choose_better_file_name(v, actual_vae)

def apply_codeformer_weight(p, v):
    opts.code_former_weight = float(v)

def apply_restore_faces(p, v):
    input = str(v).lower().strip()
    if input == "false":
        p.restore_faces = False
        return
    p.restore_faces = True
    restorer = get_best_in_list(input, map(lambda m: m.name(), shared.face_restorers))
    if restorer is not None:
        opts.face_restoration_model = restorer

def prompt_replace_parse_list(in_list):
    if not any(('=' in x) for x in in_list):
        first_val = in_list[0]
        for x in range(0, len(in_list)):
            in_list[x] = {
                "title": in_list[x],
                "params": {
                    "promptreplace": f"{first_val}={in_list[x]}"
                }
            }
    return in_list

def apply_prompt_replace(p, v):
    val = v.split('=', maxsplit=1)
    if len(val) != 2:
        raise RuntimeError(f"Invalid prompt replace, missing '=' symbol, for '{v}'")
    match = val[0].strip()
    replace = val[1].strip()
    if Script.VALIDATE_REPLACE and match not in p.prompt and match not in p.negative_prompt:
        raise RuntimeError(f"Invalid prompt replace, '{match}' is not in prompt '{p.prompt}' nor negative prompt '{p.negative_prompt}'")
    p.prompt = p.prompt.replace(match, replace)
    p.negative_prompt = p.negative_prompt.replace(match, replace)

def apply_enable_hr(p, v):
    p.enable_hr = v
    if v:
        if p.denoising_strength is None:
            p.denoising_strength = 0.75

def apply_styles(p, v: str):
    p.styles = list(v.split(','))

def apply_setting_override(name: str):
    def applier(p, v):
        p.override_settings[name] = v
    return applier

######################### Value Modes #########################
has_inited = False

def try_init():
    global has_inited
    if has_inited:
        return
    has_inited = True
    core.grid_call_init_hook = a1111_grid_call_init_hook
    core.grid_call_param_add_hook = a1111_grid_call_param_add_hook
    core.grid_call_apply_hook = a1111_grid_call_apply_hook
    core.grid_runner_pre_run_hook = a1111_grid_runner_pre_run_hook
    core.grid_runner_pre_dry_hook = a1111_grid_runner_pre_dry_hook
    core.grid_runner_post_dry_hook = a1111_grid_runner_post_dry_hook
    core.grid_runner_count_steps = a1111_grid_runner_count_steps
    core.webdata_get_base_param_data = a1111_webdata_get_base_param_data
    registerMode("Model", GridSettingMode(dry=False, type="text", apply=apply_model, clean=clean_model, valid_list=lambda: list(map(lambda m: m.title, sd_models.checkpoints_list.values()))))
    registerMode("VAE", GridSettingMode(dry=False, type="text", apply=apply_vae, clean=clean_vae, valid_list=lambda: list(sd_vae.vae_dict.keys()) + ['none', 'auto', 'automatic']))
    registerMode("Sampler", GridSettingMode(dry=True, type="text", apply=apply_field("sampler_name"), valid_list=lambda: list(sd_samplers.all_samplers_map.keys())))
    registerMode("Seed", GridSettingMode(dry=True, type="integer", apply=apply_field("seed")))
    registerMode("Steps", GridSettingMode(dry=True, type="integer", min=0, max=200, apply=apply_field("steps")))
    registerMode("CFG Scale", GridSettingMode(dry=True, type="decimal", min=0, max=500, apply=apply_field("cfg_scale")))
    registerMode("Width", GridSettingMode(dry=True, type="integer", apply=apply_field("width")))
    registerMode("Height", GridSettingMode(dry=True, type="integer", apply=apply_field("height")))
    registerMode("Prompt", GridSettingMode(dry=True, type="text", apply=apply_field("prompt")))
    registerMode("Negative Prompt", GridSettingMode(dry=True, type="text", apply=apply_field("negative_prompt")))
    registerMode("Prompt Replace", GridSettingMode(dry=True, type="text", apply=apply_prompt_replace, parse_list=prompt_replace_parse_list))
    registerMode("Styles", GridSettingMode(dry=True, type="text", apply=apply_styles, valid_list=lambda: list(shared.prompt_styles.styles)))
    registerMode("Var Seed", GridSettingMode(dry=True, type="integer", apply=apply_field("subseed")))
    registerMode("Var Strength", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("subseed_strength")))
    registerMode("ClipSkip", GridSettingMode(dry=False, type="integer", min=1, max=12, apply=apply_setting_override("CLIP_stop_at_last_layers")))
    registerMode("Denoising", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("denoising_strength")))
    registerMode("ETA", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("eta")))
    registerMode("Sigma Churn", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("s_churn")))
    registerMode("Sigma TMin", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("s_tmin")))
    registerMode("Sigma TMax", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("s_tmax")))
    registerMode("Sigma Noise", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("s_noise")))
    registerMode("Out Width", GridSettingMode(dry=True, type="integer", min=0, apply=apply_field("inf_grid_out_width")))
    registerMode("Out Height", GridSettingMode(dry=True, type="integer", min=0, apply=apply_field("inf_grid_out_height")))
    registerMode("Restore Faces", GridSettingMode(dry=True, type="text", apply=apply_restore_faces, valid_list=lambda: list(map(lambda m: m.name(), shared.face_restorers)) + ["true", "false"]))
    registerMode("CodeFormer Weight", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_codeformer_weight))
    registerMode("Tiling", GridSettingMode(dry=True, type="boolean", apply=apply_field("tiling")))
    registerMode("Image Mask Weight", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("inpainting_mask_weight")))
    registerMode("ETA Noise Seed Delta", GridSettingMode(dry=True, type="integer", apply=apply_setting_override("eta_noise_seed_delta")))
    registerMode("Enable HighRes Fix", GridSettingMode(dry=True, type="boolean", apply=apply_enable_hr))
    registerMode("HighRes Scale", GridSettingMode(dry=True, type="decimal", min=1, max=16, apply=apply_field("hr_scale")))
    registerMode("HighRes Steps", GridSettingMode(dry=True, type="integer", min=0, max=200, apply=apply_field("hr_second_pass_steps")))
    registerMode("HighRes Resize Width", GridSettingMode(dry=True, type="integer", apply=apply_field("hr_resize_x")))
    registerMode("HighRes Resize Height", GridSettingMode(dry=True, type="integer", apply=apply_field("hr_resize_y")))
    registerMode("HighRes Upscale to Width", GridSettingMode(dry=True, type="integer", apply=apply_field("hr_upscale_to_x")))
    registerMode("HighRes Upscale to Height", GridSettingMode(dry=True, type="integer", apply=apply_field("hr_upscale_to_y")))
    registerMode("HighRes Upscaler", GridSettingMode(dry=True, type="text", apply=apply_field("hr_upscaler"), valid_list=lambda: list(map(lambda u: u.name, shared.sd_upscalers)) + list(shared.latent_upscale_modes.keys())))
    registerMode("HighRes Sampler", GridSettingMode(dry=True, type="text", apply=apply_field("hr_sampler_name"), valid_list=lambda: list(sd_samplers.all_samplers_map.keys())))
    registerMode("HighRes Checkpoint", GridSettingMode(dry=False, type="text", apply=apply_field("hr_checkpoint_name"), clean=clean_model, valid_list=lambda: list(map(lambda m: m.title, sd_models.checkpoints_list.values()))))
    registerMode("Image CFG Scale", GridSettingMode(dry=True, type="decimal", min=0, max=500, apply=apply_field("image_cfg_scale")))
    registerMode("Use Result Index", GridSettingMode(dry=True, type="integer", min=0, max=500, apply=apply_field("inf_grid_use_result_index")))
    try:
        script_list = [x for x in scripts.scripts_data if x.script_class.__module__ == "dynamic_thresholding.py"][:1]
        if len(script_list) == 1:
            dynamic_thresholding = script_list[0].module
            registerMode("[DynamicThreshold] Enable", GridSettingMode(dry=True, type="boolean", apply=apply_field("dynthres_enabled")))
            registerMode("[DynamicThreshold] Mimic Scale", GridSettingMode(dry=True, type="decimal", min=0, max=500, apply=apply_field("dynthres_mimic_scale")))
            registerMode("[DynamicThreshold] Threshold Percentile", GridSettingMode(dry=True, type="decimal", min=0.0, max=100.0, apply=apply_field("dynthres_threshold_percentile")))
            registerMode("[DynamicThreshold] Mimic Mode", GridSettingMode(dry=True, type="text", apply=apply_field("dynthres_mimic_mode"), valid_list=lambda: list(dynamic_thresholding.VALID_MODES)))
            registerMode("[DynamicThreshold] CFG Mode", GridSettingMode(dry=True, type="text", apply=apply_field("dynthres_cfg_mode"), valid_list=lambda: list(dynamic_thresholding.VALID_MODES)))
            registerMode("[DynamicThreshold] Mimic Scale Minimum", GridSettingMode(dry=True, type="decimal", min=0.0, max=100.0, apply=apply_field("dynthres_mimic_scale_min")))
            registerMode("[DynamicThreshold] CFG Scale Minimum", GridSettingMode(dry=True, type="decimal", min=0.0, max=100.0, apply=apply_field("dynthres_cfg_scale_min")))
            registerMode("[DynamicThreshold] Experiment Mode", GridSettingMode(dry=True, type="decimal", min=0, max=100000, apply=apply_field("dynthres_experiment_mode")))
            registerMode("[DynamicThreshold] Scheduler Value", GridSettingMode(dry=True, type="decimal", min=0, max=100, apply=apply_field("dynthres_scheduler_val")))
            registerMode("[DynamicThreshold] Scaling Startpoint", GridSettingMode(dry=True, type="text", apply=apply_field("dynthres_scaling_startpoint"), valid_list=lambda: list(['ZERO', 'MEAN'])))
            registerMode("[DynamicThreshold] Variability Measure", GridSettingMode(dry=True, type="text", apply=apply_field("dynthres_variability_measure"), valid_list=lambda: list(['STD', 'AD'])))
            registerMode("[DynamicThreshold] Interpolate Phi", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("dynthres_interpolate_phi")))
            registerMode("[DynamicThreshold] Separate Feature Channels", GridSettingMode(dry=True, type="boolean", apply=apply_field("dynthres_separate_feature_channels")))
        script_list = [x for x in scripts.scripts_data if x.script_class.__module__ == "controlnet.py"][:1]
        if len(script_list) == 1:
            # Hacky but works
            module = script_list[0].module
            preprocessors_list = list(p.name for p in module.Preprocessor.get_sorted_preprocessors())
            def validate_param(p, v):
                if not shared.opts.data.get("control_net_allow_script_control", False):
                    raise RuntimeError("ControlNet options cannot currently work, you must enable 'Allow other script to control this extension' in Settings -> ControlNet first")
                return v
            registerMode("[ControlNet] Enable", GridSettingMode(dry=True, type="boolean", apply=apply_field("control_net_enabled"), clean=validate_param))
            registerMode("[ControlNet] Preprocessor", GridSettingMode(dry=True, type="text", apply=apply_field("control_net_module"), clean=validate_param, valid_list=lambda: list(preprocessors_list)))
            registerMode("[ControlNet] Model", GridSettingMode(dry=True, type="text", apply=apply_field("control_net_model"), clean=validate_param, valid_list=lambda: list(list(module.global_state.cn_models.keys()))))
            registerMode("[ControlNet] Weight", GridSettingMode(dry=True, type="decimal", min=0.0, max=2.0, apply=apply_field("control_net_weight"), clean=validate_param))
            registerMode("[ControlNet] Guidance Strength", GridSettingMode(dry=True, type="decimal", min=0.0, max=1.0, apply=apply_field("control_net_guidance_strength"), clean=validate_param))
            registerMode("[ControlNet] Annotator Resolution", GridSettingMode(dry=True, type="integer", min=0, max=2048, apply=apply_field("control_net_pres"), clean=validate_param))
            registerMode("[ControlNet] Threshold A", GridSettingMode(dry=True, type="integer", min=0, max=256, apply=apply_field("control_net_pthr_a"), clean=validate_param))
            registerMode("[ControlNet] Threshold B", GridSettingMode(dry=True, type="integer", min=0, max=256, apply=apply_field("control_net_pthr_b"), clean=validate_param))
            registerMode("[ControlNet] Image", GridSettingMode(dry=True, type="text", apply=core.apply_field_as_image_data("control_net_input_image"), clean=validate_param, valid_list=lambda: core.list_image_files()))
    except Exception as e:
        print(f"Infinity Grid Generator failed to import a dependency module: {e}")
        pass

######################### Actual Execution Logic #########################

def a1111_grid_call_init_hook(grid_call: core.SingleGridCall):
    grid_call.replacements = list()

def a1111_grid_call_param_add_hook(grid_call: core.SingleGridCall, param: str, value):
    if grid_call.grid.min_width is None:
        grid_call.grid.min_width = grid_call.grid.initial_p.width
    if grid_call.grid.min_height is None:
        grid_call.grid.min_height = grid_call.grid.initial_p.height
    cleaned = clean_mode(param)
    if cleaned == "promptreplace":
        grid_call.replacements.append(value)
        return True
    elif cleaned in ["width", "outwidth"]:
        grid_call.grid.min_width = min(grid_call.grid.min_width or 99999, int(value))
    elif cleaned in ["height", "outheight"]:
        grid_call.grid.min_height = min(grid_call.grid.min_height or 99999, int(value))
    return False

def a1111_grid_call_apply_hook(grid_call: core.SingleGridCall, param: str, dry: bool):
    for replace in grid_call.replacements:
        apply_prompt_replace(param, replace)
    
def a1111_grid_runner_pre_run_hook(grid_runner: core.GridRunner):
    state.job_count = grid_runner.total_run
    shared.total_tqdm.updateTotal(grid_runner.total_steps)
    # prevents the steps from from being recalculated by Auto1 using the current value of hires steps
    state.processing_has_refined_job_count = True

class TempHolder: pass

def a1111_grid_runner_pre_dry_hook(grid_runner: core.GridRunner):
    grid_runner.temp = TempHolder()
    grid_runner.temp.old_codeformer_weight = opts.code_former_weight
    grid_runner.temp.old_face_restorer = opts.face_restoration_model
    grid_runner.temp.old_vae = opts.sd_vae
    grid_runner.temp.old_model = opts.sd_model_checkpoint

def a1111_grid_runner_post_dry_hook(grid_runner: core.GridRunner, p, set):
    p.seed = processing.get_fixed_seed(p.seed)
    p.subseed = processing.get_fixed_seed(p.subseed)
    processed = process_images(p)
    if len(processed.images) < 1:
        raise RuntimeError(f"Something went wrong! Image gen '{set.data}' produced {len(processed.images)} images, which is wrong")
    os.makedirs(os.path.dirname(set.filepath), exist_ok=True)
    result_index = getattr(p, 'inf_grid_use_result_index', 0)
    if result_index >= len(processed.images):
        result_index = len(processed.images) - 1
    img = processed.images[result_index]
    if type(img) == numpy.ndarray:
        img = Image.fromarray(img)
    if hasattr(p, 'inf_grid_out_width') and hasattr(p, 'inf_grid_out_height'):
        img = img.resize((p.inf_grid_out_width, p.inf_grid_out_height), resample=images.LANCZOS)
    processed.images[result_index] = img
    info = processing.create_infotext(p, [p.prompt], [p.seed], [p.subseed], [])
    ext = grid_runner.grid.format
    prompt = p.prompt
    seed = processed.seed
    def save_offthread():
        images.save_image(img, path=os.path.dirname(set.filepath), basename="", forced_filename=os.path.basename(set.filepath), save_to_dirs=False, info=info, extension=ext, p=p, prompt=prompt, seed=seed)
    threading.Thread(target=save_offthread).start()
    opts.code_former_weight = grid_runner.temp.old_codeformer_weight
    opts.face_restoration_model = grid_runner.temp.old_face_restorer
    opts.sd_vae = grid_runner.temp.old_vae
    opts.sd_model_checkpoint = grid_runner.temp.old_model
    grid_runner.temp = None
    return processed

def a1111_grid_runner_count_steps(grid_runner: core.GridRunner, set):
    step_count = set.params.get("steps")
    step_count = int(step_count) if step_count is not None else grid_runner.p.steps
    total_steps = step_count
    enable_hr = set.params.get("enable highres fix")
    if enable_hr is None:
        enable_hr = grid_runner.p.enable_hr if hasattr(grid_runner.p, 'enable_hr') else False
    if enable_hr:
        highres_steps = set.params.get("highres steps")
        highres_steps = int(highres_steps) if highres_steps is not None else (grid_runner.p.hr_second_pass_steps or step_count)
        total_steps += highres_steps
    return total_steps

def a1111_webdata_get_base_param_data(p):
    return {
        "sampler": p.sampler_name,
        "seed": p.seed,
        "restorefaces": (opts.face_restoration_model if p.restore_faces else None),
        "steps": p.steps,
        "cfgscale": p.cfg_scale,
        "model": choose_better_file_name('', shared.sd_model.sd_checkpoint_info.model_name).replace(',', '').replace(':', ''),
        "vae": (None if sd_vae.loaded_vae_file is None else (choose_better_file_name('', sd_vae.loaded_vae_file).replace(',', '').replace(':', ''))),
        "width": p.width,
        "height": p.height,
        "prompt": p.prompt,
        "negativeprompt": p.negative_prompt,
        "varseed": (None if p.subseed_strength == 0 else p.subseed),
        "varstrength": (None if p.subseed_strength == 0 else p.subseed_strength),
        "clipskip": opts.CLIP_stop_at_last_layers,
        "codeformerweight": opts.code_former_weight,
        "denoising": getattr(p, 'denoising_strength', None),
        "eta": fix_num(p.eta),
        "sigmachurn": fix_num(p.s_churn),
        "sigmatmin": fix_num(p.s_tmin),
        "sigmatmax": fix_num(p.s_tmax),
        "sigmanoise": fix_num(p.s_noise),
        "ENSD": None if opts.eta_noise_seed_delta == 0 else opts.eta_noise_seed_delta
    }

class SettingsFixer():
    def __enter__(self):
        self.model = opts.sd_model_checkpoint
        self.code_former_weight = opts.code_former_weight
        self.face_restoration_model = opts.face_restoration_model
        self.vae = opts.sd_vae

    def __exit__(self, exc_type, exc_value, tb):
        opts.code_former_weight = self.code_former_weight
        opts.face_restoration_model = self.face_restoration_model
        opts.sd_vae = self.vae
        opts.sd_model_checkpoint = self.model
        sd_models.reload_model_weights()
        sd_vae.reload_vae_weights()

######################### Script class entrypoint #########################
class Script(scripts.Script):
    BASEDIR = scripts.basedir()
    VALIDATE_REPLACE = True

    def title(self):
        return "Generate Infinite-Axis Grid"

    def show(self, is_img2img):
        return True

    def ui(self, is_img2img):
        core.list_image_files()
        try_init()
        gr.HTML(value=f"<br>Confused/new? View <a style=\"border-bottom: 1px #00ffff dotted;\" href=\"{INF_GRID_README}\" target=\"_blank\" rel=\"noopener noreferrer\">the README</a> for usage instructions.<br><br>")
        with gr.Row():
            grid_file = gr.Dropdown(value="Create in UI",label="Select grid definition file", choices=["Create in UI"] + core.get_name_list())
            def refresh():
                new_choices = ["Create in UI"] + core.get_name_list()
                grid_file.choices = new_choices
                return gr.update(choices=new_choices)
            refresh_button = ui_components.ToolButton(value=refresh_symbol, elem_id="infinity_grid_refresh_button")
            refresh_button.click(fn=refresh, inputs=[], outputs=[grid_file])
        output_file_path = gr.Textbox(value="", label="Output folder name (if blank uses yaml's 'outpath' parameter, filename, or current date)")
        page_will_be = gr.HTML(value="(...)<br><br>")
        manual_group = gr.Group(visible=True)
        manual_axes = list()
        sets = list()
        def get_page_url_text(file):
            if file is None:
                return "(...)"
            notice = ""
            if not os.path.isabs(file):
                out_path = opts.outdir_grids or (opts.outdir_img2img_grids if is_img2img else opts.outdir_txt2img_grids)
                full_out_path = out_path + "/" + file
                url = "/file=" + full_out_path
            else:
                full_out_path = file
                url = "file://" + ("" if file.startswith("/") else "/") + file
                notice = "<br><span style=\"color: red;\">This is a raw file path, not within the WebUI output directory. You may need to open the output file manually.</span>"
            if os.path.exists(full_out_path):
                notice += "<br><span style=\"color: red;\">NOTICE: There is already something saved there! This will overwrite prior data.</span>"
            return f"Page will be at <a style=\"border-bottom: 1px #00ffff dotted;\" href=\"{url}/index.html\" target=\"_blank\" rel=\"noopener noreferrer\">(Click me) <code>{full_out_path}</code></a>{notice}<br><br>"
        def update_page_url(file_path, selected_file):
            out_file_update = gr.Textbox.update()
            if file_path == "" and selected_file == "Create in UI":
                file_path = f"autonamed_inf_grid_{datetime.now().strftime('%Y_%m_%d_%H_%M_%S')}"
                out_file_update = gr.Textbox.update(value=file_path)
            info_update = gr.update(value=get_page_url_text(file_path or (selected_file.replace(".yml", "") if selected_file is not None else None)))
            return [out_file_update, info_update]
        def update_page_url_single(file_path, selected_file):
            (_, info_update) = update_page_url(file_path, selected_file)
            return info_update
        with manual_group:
            with gr.Row():
                with gr.Column():
                    axis_count = 0
                    for group in range(0, 4):
                        group_obj = gr.Group(visible=group == 0)
                        with group_obj:
                            rows = list()
                            for i in range(0, 4):
                                with gr.Row():
                                    axis_count += 1
                                    row_mode = gr.Dropdown(value="", label=f"Axis {axis_count} Mode", choices=[" "] + [x.name for x in core.valid_modes.values()])
                                    row_value = gr.Textbox(label=f"Axis {axis_count} Value", lines=1)
                                    fill_row_button = ui_components.ToolButton(value=fill_values_symbol, visible=False)
                                    def fill_axis(mode_name):
                                        core.clear_caches()
                                        mode = core.valid_modes.get(clean_mode(mode_name))
                                        if mode is None:
                                            return gr.update()
                                        elif mode.type == "boolean":
                                            return "true, false"
                                        elif mode.valid_list is not None:
                                            return ", ".join(list(mode.valid_list()))
                                        raise RuntimeError(f"Can't fill axis for {mode_name}")
                                    fill_row_button.click(fn=fill_axis, inputs=[row_mode], outputs=[row_value])
                                    def on_axis_change(mode_name, out_file):
                                        mode = core.valid_modes.get(clean_mode(mode_name))
                                        button_update = gr.Button.update(visible=mode is not None and (mode.valid_list is not None or mode.type == "boolean"))
                                        (out_file_update, info_update) = update_page_url(out_file, "Create in UI")
                                        return [button_update, out_file_update, info_update]
                                    row_mode.change(fn=on_axis_change, inputs=[row_mode, output_file_path], outputs=[fill_row_button, output_file_path, page_will_be])
                                    manual_axes += list([row_mode, row_value])
                                    rows.append(row_mode)
                            sets.append([group_obj, rows])
        for group in range(0, 3):
            row_mode = sets[group][1][3]
            group_obj = sets[group + 1][0]
            next_rows = sets[group + 1][1]
            def make_vis(prior, r1, r2, r3, r4):
                return gr.Group.update(visible=(prior+r1+r2+r3+r4).strip() != "")
            row_mode.change(fn=make_vis, inputs=[row_mode] + next_rows, outputs=[group_obj])
        gr.HTML('<span style="opacity:0.5;">(More input rows will be automatically added after you select modes above.)</span>')
        grid_file.change(
            fn=lambda x: {"visible": x == "Create in UI", "__type__": "update"},
            inputs=[grid_file],
            outputs=[manual_group],
            show_progress = False)
        output_file_path.change(fn=update_page_url_single, inputs=[output_file_path, grid_file], outputs=[page_will_be])
        grid_file.change(fn=update_page_url, inputs=[output_file_path, grid_file], outputs=[output_file_path, page_will_be])
        with gr.Row():
            do_overwrite = gr.Checkbox(value=False, label="Overwrite existing images (for updating grids)")
            dry_run = gr.Checkbox(value=False, label="Do a dry run to validate your grid file")
            fast_skip = gr.Checkbox(value=False, label="Use more-performant skipping")
            skip_invalid = gr.Checkbox(value=False, label="Skip invalid entries")
        with gr.Row():
            generate_page = gr.Checkbox(value=True, label="Generate infinite-grid webviewer page")
            validate_replace = gr.Checkbox(value=True, label="Validate PromptReplace input")
            publish_gen_metadata = gr.Checkbox(value=True, label="Publish full generation metadata for viewing on-page")
        return [do_overwrite, generate_page, dry_run, validate_replace, publish_gen_metadata, grid_file, fast_skip, output_file_path, skip_invalid] + manual_axes

    def run(self, p, do_overwrite, generate_page, dry_run, validate_replace, publish_gen_metadata, grid_file, fast_skip, output_file_path, skip_invalid, *manual_axes):
        core.clear_caches()
        try_init()
        # Clean up default params
        p = copy(p)
        p.n_iter = 1
        p.batch_size = 1
        p.do_not_save_samples = True
        p.do_not_save_grid = True
        p.seed = processing.get_fixed_seed(p.seed)
        # Store extra variable
        Script.VALIDATE_REPLACE = validate_replace
        # Validate to avoid abuse
        if '..' in grid_file or grid_file == "":
            raise RuntimeError(f"Unacceptable filename '{grid_file}'")
        if '..' in output_file_path:
            raise RuntimeError(f"Unacceptable alt file path '{output_file_path}'")
        if grid_file == "Create in UI":
            if output_file_path is None or output_file_path == "":
                raise RuntimeError(f"Must specify the output file path")
            manual_axes = list(manual_axes)
        else:
            manual_axes = None
        with SettingsFixer():
            result = core.run_grid_gen(p, grid_file, p.outpath_grids, output_file_path, do_overwrite, fast_skip, generate_page, publish_gen_metadata, dry_run, manual_axes, skip_invalid=skip_invalid)
        if result is None:
            return Processed(p, list())
        return result


================================================
FILE: style.css
================================================
/**
 * This file is part of Stable Diffusion Infinity Grid Generator, view the README.md for more information.
 */

#infinity_grid_refresh_button {
    max-width: 2.5em;
    min-width: 2.5em;
    height: 2.4em;
}
Download .txt
gitextract_jp1goxq0/

├── .github/
│   └── FUNDING.yml
├── .gitignore
├── LICENSE.txt
├── README.md
├── assets/
│   ├── a1111webui.js
│   ├── grid.schema.json
│   ├── images/
│   │   └── put-usable-images-here.txt
│   ├── jsgif.js
│   ├── page.html
│   ├── proc.js
│   └── styles.css
├── gridgencore.py
├── install.py
├── javascript/
│   └── infinity_grid.js
├── scripts/
│   └── infinity_grid.py
└── style.css
Download .txt
SYMBOL INDEX (131 symbols across 5 files)

FILE: assets/a1111webui.js
  function genParamQuote (line 5) | function genParamQuote(text) {
  function formatMet (line 13) | function formatMet(name, val, bad) {
  function formatMetadata (line 24) | function formatMetadata(valSet) {
  function crunchParamHook (line 79) | function crunchParamHook(data, key, value) {

FILE: assets/jsgif.js
  function ByteArray (line 15) | function ByteArray() {
  function encode64 (line 1368) | function encode64(input) {

FILE: assets/proc.js
  function loadData (line 8) | function loadData() {
  function getExtension (line 60) | function getExtension(filePath) {
  function fix_video (line 68) | function fix_video(path) {
  function updateStylesToMatchInputs (line 97) | function updateStylesToMatchInputs() {
  function getAxisById (line 102) | function getAxisById(id) {
  function getNextAxis (line 106) | function getNextAxis(axes, startId) {
  function getSelectedValKey (line 119) | function getSelectedValKey(axis) {
  function clickRowImage (line 130) | function clickRowImage(rows, x, y) {
  function escapeHtml (line 243) | function escapeHtml(text) {
  function unescapeHtml (line 250) | function unescapeHtml(text) {
  function canShowVal (line 254) | function canShowVal(axis, val) {
  function setShowVal (line 258) | function setShowVal(axis, val, show) {
  function getNavValTab (line 263) | function getNavValTab(axis, val) {
  function percentToRedGreen (line 267) | function percentToRedGreen(percent) {
  function getXAxisContent (line 277) | function getXAxisContent(x, y, xAxis, yval, x2Axis, x2val, y2Axis, y2val) {
  function setImgPlaceholder (line 395) | function setImgPlaceholder(img) {
  function optDescribe (line 409) | function optDescribe(isFirst, val) {
  function fillTable (line 413) | function fillTable() {
  function getCurrentSelectedAxis (line 474) | function getCurrentSelectedAxis(axisPrefix) {
  function getShownItemsOfAxis (line 480) | function getShownItemsOfAxis(axis) {
  function getWantedScaling (line 484) | function getWantedScaling() {
  function setImageScale (line 499) | function setImageScale(image, percent) {
  function updateScaling (line 526) | function updateScaling() {
  function toggleDescriptions (line 534) | function toggleDescriptions() {
  function toggleShowAllAxis (line 544) | function toggleShowAllAxis(axisId) {
  function toggleShowVal (line 555) | function toggleShowVal(axis, val) {
  function enableRange (line 570) | function enableRange(id) {
  function clickTabAfterActiveTab (line 585) | function clickTabAfterActiveTab(tabs) {
  function startAutoScroll (line 609) | async function startAutoScroll() {
  function crunchMetadata (line 633) | function crunchMetadata(parts) {
  function doPopupFor (line 658) | function doPopupFor(img) {
  function updateTitleStickyDirect (line 676) | function updateTitleStickyDirect() {
  function updateTitleSticky (line 684) | function updateTitleSticky() {
  function toggleTopSticky (line 703) | function toggleTopSticky() {
  function toggleLabelSticky (line 712) | function toggleLabelSticky() {
  function removeGeneratedImages (line 721) | function removeGeneratedImages() {
  function makeImage (line 725) | function makeImage(minRow = 0, doClear = true) {
  function makeGif (line 904) | function makeGif() {
  function updateHash (line 980) | function updateHash() {
  function applyHash (line 1001) | function applyHash(hash) {
  function tryReloadImg (line 1052) | function tryReloadImg(img) {
  function checkForUpdates (line 1068) | function checkForUpdates() {

FILE: gridgencore.py
  function escape_html (line 38) | def escape_html(text: str):
  function clean_file_path (line 41) | def clean_file_path(fn: str):
  function get_version (line 47) | def get_version():
  function list_image_files (line 58) | def list_image_files():
  function clear_caches (line 75) | def clear_caches():
  function get_name_list (line 79) | def get_name_list():
  function fix_dict (line 84) | def fix_dict(d: dict):
  function clean_for_web (line 91) | def clean_for_web(text: str):
  function clean_id (line 98) | def clean_id(id: str):
  function clean_mode (line 101) | def clean_mode(id: str):
  function clean_name (line 104) | def clean_name(name: str):
  function get_best_in_list (line 107) | def get_best_in_list(name: str, list: list):
  function choose_better_file_name (line 121) | def choose_better_file_name(raw_name: str, full_name: str):
  function fix_num (line 127) | def fix_num(num):
  function expand_numeric_list_ranges (line 132) | def expand_numeric_list_ranges(in_list, num_type):
  class GridSettingMode (line 154) | class GridSettingMode:
    method __init__ (line 166) | def __init__(self, dry: bool, type: str, apply: callable, min: float =...
  function registerMode (line 176) | def registerMode(name: str, mode: GridSettingMode):
  function validate_params (line 182) | def validate_params(grid, params: dict):
  function apply_field (line 186) | def apply_field(name: str):
  function apply_field_as_image_data (line 191) | def apply_field_as_image_data(name: str):
  function validate_single_param (line 201) | def validate_single_param(p: str, v):
  class GridYamlLoader (line 249) | class GridYamlLoader(yaml.SafeLoader):
  class AxisValue (line 259) | class AxisValue:
    method __init__ (line 260) | def __init__(self, axis, grid, key: str, val):
    method __str__ (line 303) | def __str__(self):
    method __unicode__ (line 305) | def __unicode__(self):
  class Axis (line 308) | class Axis:
    method build_from_list_str (line 309) | def build_from_list_str(self, id, grid, list_str):
    method __init__ (line 336) | def __init__(self, grid, id: str, obj):
  class GridFileHelper (line 365) | class GridFileHelper:
    method proc_variables (line 366) | def proc_variables(self, text):
    method read_grid_direct (line 374) | def read_grid_direct(self, key: str):
    method read_str_from_grid (line 377) | def read_str_from_grid(self, key: str):
    method parse_yaml (line 380) | def parse_yaml(self, yaml_content: dict, grid_file: str):
  class SingleGridCall (line 422) | class SingleGridCall:
    method __init__ (line 423) | def __init__(self, values: list):
    method flatten_params (line 432) | def flatten_params(self, grid: GridFileHelper):
    method apply_to (line 440) | def apply_to(self, p, dry: bool):
  class GridRunner (line 448) | class GridRunner:
    method __init__ (line 449) | def __init__(self, grid: GridFileHelper, do_overwrite: bool, base_path...
    method update_live_file (line 463) | def update_live_file(self, new_file: str):
    method build_value_set_list (line 471) | def build_value_set_list(self, axis_list: list):
    method preprocess (line 492) | def preprocess(self):
    method run (line 507) | def run(self, dry: bool):
  class WebDataBuilder (line 535) | class WebDataBuilder():
    method build_json (line 536) | def build_json(grid: GridFileHelper, publish_gen_metadata: bool, p, dr...
    method radio_button_html (line 598) | def radio_button_html(name, id, descrip, label):
    method axis_bar (line 601) | def axis_bar(label, content):
    method build_html (line 604) | def build_html(grid):
    method emit_web_data (line 667) | def emit_web_data(path: str, grid, publish_gen_metadata: bool, p, yaml...
  function run_grid_gen (line 691) | def run_grid_gen(pass_through_obj, input_file: str, output_folder_base: ...

FILE: scripts/infinity_grid.py
  function get_model_for (line 34) | def get_model_for(name):
  function apply_model (line 37) | def apply_model(p, v):
  function clean_model (line 41) | def clean_model(p, v):
  function get_vae_for (line 47) | def get_vae_for(name):
  function apply_vae (line 50) | def apply_vae(p, v):
  function clean_vae (line 61) | def clean_vae(p, v):
  function apply_codeformer_weight (line 70) | def apply_codeformer_weight(p, v):
  function apply_restore_faces (line 73) | def apply_restore_faces(p, v):
  function prompt_replace_parse_list (line 83) | def prompt_replace_parse_list(in_list):
  function apply_prompt_replace (line 95) | def apply_prompt_replace(p, v):
  function apply_enable_hr (line 106) | def apply_enable_hr(p, v):
  function apply_styles (line 112) | def apply_styles(p, v: str):
  function apply_setting_override (line 115) | def apply_setting_override(name: str):
  function try_init (line 123) | def try_init():
  function a1111_grid_call_init_hook (line 217) | def a1111_grid_call_init_hook(grid_call: core.SingleGridCall):
  function a1111_grid_call_param_add_hook (line 220) | def a1111_grid_call_param_add_hook(grid_call: core.SingleGridCall, param...
  function a1111_grid_call_apply_hook (line 235) | def a1111_grid_call_apply_hook(grid_call: core.SingleGridCall, param: st...
  function a1111_grid_runner_pre_run_hook (line 239) | def a1111_grid_runner_pre_run_hook(grid_runner: core.GridRunner):
  class TempHolder (line 245) | class TempHolder: pass
  function a1111_grid_runner_pre_dry_hook (line 247) | def a1111_grid_runner_pre_dry_hook(grid_runner: core.GridRunner):
  function a1111_grid_runner_post_dry_hook (line 254) | def a1111_grid_runner_post_dry_hook(grid_runner: core.GridRunner, p, set):
  function a1111_grid_runner_count_steps (line 284) | def a1111_grid_runner_count_steps(grid_runner: core.GridRunner, set):
  function a1111_webdata_get_base_param_data (line 297) | def a1111_webdata_get_base_param_data(p):
  class SettingsFixer (line 323) | class SettingsFixer():
    method __enter__ (line 324) | def __enter__(self):
    method __exit__ (line 330) | def __exit__(self, exc_type, exc_value, tb):
  class Script (line 339) | class Script(scripts.Script):
    method title (line 343) | def title(self):
    method show (line 346) | def show(self, is_img2img):
    method ui (line 349) | def ui(self, is_img2img):
    method run (line 451) | def run(self, p, do_overwrite, generate_page, dry_run, validate_replac...
Condensed preview — 16 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (206K chars).
[
  {
    "path": ".github/FUNDING.yml",
    "chars": 21,
    "preview": "github: mcmonkey4eva\n"
  },
  {
    "path": ".gitignore",
    "chars": 128,
    "preview": ".vscode/\nassets/*.yml\nassets/styles-user.css\n__pycache__/\nassets/images/**/*.png\nassets/images/**/*.jpg\nassets/images/**"
  },
  {
    "path": "LICENSE.txt",
    "chars": 1095,
    "preview": "The MIT License (MIT)\n\nCopyright (c) 2022-2023 Alex \"mcmonkey\" Goodwin\n\nPermission is hereby granted, free of charge, to"
  },
  {
    "path": "README.md",
    "chars": 26175,
    "preview": "# Stable Diffusion Infinity Grid Generator\n\n![img](github/megagrid_ref.png)\n\n### Concept\n\nExtension for the [AUTOMATIC11"
  },
  {
    "path": "assets/a1111webui.js",
    "chars": 3582,
    "preview": "/**\n * This file is part of Infinity Grid Generator, view the README.md at https://github.com/mcmonkeyprojects/sd-infini"
  },
  {
    "path": "assets/grid.schema.json",
    "chars": 11417,
    "preview": "{\n    \"$schema\": \"http://json-schema.org/draft-06/schema#\",\n    \"type\": \"object\",\n    \"properties\": {\n        \"grid\": {\n"
  },
  {
    "path": "assets/images/put-usable-images-here.txt",
    "chars": 0,
    "preview": ""
  },
  {
    "path": "assets/jsgif.js",
    "chars": 34166,
    "preview": "/**\n * This class lets you encode animated GIF files\n * Base class :  http://www.java2s.com/Code/Java/2D-Graphics-GUI/An"
  },
  {
    "path": "assets/page.html",
    "chars": 7898,
    "preview": "<!DOCTYPE html>\n<head>\n    <meta charset=\"utf-8\" />\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale"
  },
  {
    "path": "assets/proc.js",
    "chars": 39115,
    "preview": "/*\n * This file is part of Infinity Grid Generator, view the README.md at https://github.com/mcmonkeyprojects/sd-infinit"
  },
  {
    "path": "assets/styles.css",
    "chars": 2892,
    "preview": "/*\n If you want to customize your stylesheet, please edit 'styles-user.css' instead of this.\n*/\n\n.sel_table tr td {\n    "
  },
  {
    "path": "gridgencore.py",
    "chars": 35300,
    "preview": "# This file is part of Infinity Grid Generator, view the README.md at https://github.com/mcmonkeyprojects/sd-infinity-gr"
  },
  {
    "path": "install.py",
    "chars": 254,
    "preview": "import launch\n\nif not launch.is_installed(\"yaml\"):\n    launch.run_pip(\"install pyyaml\", \"pyyaml for Infinity Grid Script"
  },
  {
    "path": "javascript/infinity_grid.js",
    "chars": 2604,
    "preview": "/**\n * Stable Diffusion Infinity Grid Generator\n *\n * Author: Alex 'mcmonkey' Goodwin\n *\n * GitHub URL: https://github.c"
  },
  {
    "path": "scripts/infinity_grid.py",
    "chars": 30344,
    "preview": "##################\n# Stable Diffusion Infinity Grid Generator\n#\n# Author: Alex 'mcmonkey' Goodwin\n# GitHub URL: https://"
  },
  {
    "path": "style.css",
    "chars": 213,
    "preview": "/**\n * This file is part of Stable Diffusion Infinity Grid Generator, view the README.md for more information.\n */\n\n#inf"
  }
]

About this extraction

This page contains the full source code of the mcmonkeyprojects/sd-infinity-grid-generator-script GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 16 files (190.6 KB), approximately 50.1k tokens, and a symbol index with 131 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!