1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import { imageUrlToBase64 } from '../../../ClientUtils';
import { Doc } from '../../../fields/Doc';
import { DocData } from '../../../fields/DocSymbols';
import { ImageCast } from '../../../fields/Types';
import { Upload } from '../../../server/SharedMediaTypes';
import { gptDescribeImage } from '../../apis/gpt/GPT';
import { Docs } from '../../documents/Documents';
import { Networking } from '../../Network';
import { DocumentView, DocumentViewInternal } from '../nodes/DocumentView';
import { OpenWhere } from '../nodes/OpenWhere';
import { AspectRatioLimits, FireflyDimensionsMap, FireflyImageDimensions, FireflyStylePresets } from './FireflyConstants';
export class DrawingFillHandler {
static drawingToImage = (drawing: Doc, strength: number, prompt: string) => {
const docData = drawing[DocData];
const tags: string[] = ((docData?.tags as unknown as string[]) ?? []).map(tag => tag.slice(1)) ?? [];
const styles = tags.filter(tag => FireflyStylePresets.has(tag));
DocumentView.GetDocImage(drawing)?.then(imageField => {
if (imageField) {
const aspectRatio = (drawing.width as number) / (drawing.height as number);
let dims: { width: number; height: number };
if (aspectRatio > AspectRatioLimits[FireflyImageDimensions.Widescreen]) {
dims = FireflyDimensionsMap[FireflyImageDimensions.Widescreen];
} else if (aspectRatio > AspectRatioLimits[FireflyImageDimensions.Landscape]) {
dims = FireflyDimensionsMap[FireflyImageDimensions.Landscape];
} else if (aspectRatio < AspectRatioLimits[FireflyImageDimensions.Portrait]) {
dims = FireflyDimensionsMap[FireflyImageDimensions.Portrait];
} else {
dims = FireflyDimensionsMap[FireflyImageDimensions.Square];
}
const { href } = ImageCast(imageField).url;
const hrefParts = href.split('.');
const structureUrl = `${hrefParts.slice(0, -1).join('.')}_o.${hrefParts.lastElement()}`;
imageUrlToBase64(structureUrl)
.then((hrefBase64: string) => gptDescribeImage(hrefBase64))
.then((prompt: string) => {
Networking.PostToServer('/queryFireflyImageFromStructure', { prompt: prompt, width: dims.width, height: dims.height, structureUrl: structureUrl, strength: strength, styles: styles }).then((info: Upload.ImageInformation) =>
DocumentViewInternal.addDocTabFunc(Docs.Create.ImageDocument(info.accessPaths.agnostic.client, { ai: 'firefly', ai_firefly_prompt: prompt, nativeWidth: dims.width, nativeHeight: dims.height }), OpenWhere.addRight)
); // prettier-ignore
});
}
return false;
});
};
}
|