Skip to content

Commit

Permalink
there is now both , for the raw file data, and for the raw pixel data…
Browse files Browse the repository at this point in the history
… (with the width and height)
  • Loading branch information
mjrlowe committed Nov 22, 2020
1 parent 28f0d4b commit 0801bd9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ This should output something like this:
| Property | Type | Description | Default Value | CLI Flag |
|-|-|-|-|-|
| `path` | string | The path or URL of the input image. | No default | `--file` or `-f` |
| `raw` | UintArray | The raw data of the input image. (You can use this instead of `path`) | No default | [No CLI flag] |
| `rawFile` | UintArray | The raw data of the input image. (You can use this instead of `path`) | No default | [No CLI flag] |
| `rawPixels` | {data: UintArray, width: number, height: number} | The rgb(a) data (as well as the height and width) of the input image. (You can use this instead of `path` or `rawFile`) | No default | [No CLI flag] |
| `color` | boolean | Whether the output should be in color. | `false` | `--color` or `-c` |
| `characterMap` | string \| string[] | See the section on character maps for more information. | By default a character map isn't used, and high-res mode is used instead. | `--character-map` or `-m` |
| `inverted` | boolean | Whether the character map should be mapped from light to dark instead of dark to light. Normally you will want to set this to true if your terminal is in a dark theme. | `false` | `--inverted` or `-i` |
Expand Down Expand Up @@ -111,7 +112,7 @@ If you are using the module, you can set the character map to an array of string
| `--allow-net` | _ | To fetch images from the web |
| `--allow-read` | _ | To use images stored locally |

Either `--allow-net` or `--allow-read` is needed when using `path` (`--file` in the CLI tool). When using `raw`, only the `--unstable` flag is needed.
Either `--allow-net` or `--allow-read` is needed when using `path` (`--file` in the CLI tool). When using `rawFile` or `rawPixels`, only the `--unstable` flag is needed.

## Examples

Expand Down
21 changes: 16 additions & 5 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ interface imageSettings {
/** The local file path or URL of the image */
path?: string;
/** The raw data of a PNG or JPG image */
raw?: Uint8Array;
rawFile?: Uint8Array;
/** The raw data for the image: the rgb(a) array as well as the height and width */
rawPixels?: {data: Uint8Array, width: number, height: number};
/** The character map to use when outputting the image */
characterMap?: string | string[];
/** The number of characters wide the output image is */
Expand Down Expand Up @@ -47,13 +49,16 @@ async function getImageString(settings: imageSettings): Promise<string> {
raw = await Deno.readFile(path);
}

}else if(typeof settings.raw !== "undefined"){
raw = settings.raw;
}else if(typeof settings.rawFile !== "undefined"){
raw = settings.rawFile;
}else if(typeof settings.rawPixels !== "undefined"){
raw = settings.rawPixels;
} else{
throw new Error("No file path or raw data specified.")
}

const imageFileType = getFileType(raw);
//@ts-ignore
let imageFileType = typeof settings.rawPixels !== "undefined" ? "raw" : getFileType(raw);
if (imageFileType === "unknown") {
if(settings.path){
const fileExtension = settings.path.substr(
Expand Down Expand Up @@ -285,16 +290,22 @@ async function printImageString(settings: imageSettings): Promise<void> {
console.log(await getImageString(settings));
}

function decodeImage(raw: Uint8Array, format: string) {
function decodeImage(raw: Uint8Array | {data: Uint8Array, width: number, height: number}, format: string) {
let decodedImage;
switch (format) {
case "jpg":
//@ts-ignore
decodedImage = decodeJpeg(raw);
break;

case "png":
//@ts-ignore
decodedImage = decodePng(raw);
break;

case "raw":
decodedImage = raw;
break;

default:
throw `Image format ${format} not supported. Also, this error message should be unreachable. :/`;
Expand Down

0 comments on commit 0801bd9

Please sign in to comment.