Color palette support in byos_laravel

This commit is contained in:
Dan van Kley 2025-11-17 20:48:06 -05:00
parent c157dcf3b6
commit 9ee7bc1aac
7 changed files with 236 additions and 4 deletions

View file

@ -6,6 +6,8 @@ use App\Enums\ImageFormat;
use App\Models\Device;
use App\Models\DeviceModel;
use App\Models\Plugin;
use Bnussbau\TrmnlPipeline\Data\ColorType;
use Bnussbau\TrmnlPipeline\Data\RgbColor;
use Bnussbau\TrmnlPipeline\Stages\BrowserStage;
use Bnussbau\TrmnlPipeline\Stages\ImageStage;
use Bnussbau\TrmnlPipeline\TrmnlPipeline;
@ -66,16 +68,21 @@ class ImageGenerationService
->width($imageSettings['width'])
->height($imageSettings['height'])
->colors($imageSettings['colors'])
->colorType($imageSettings['color_type'])
->bitDepth($imageSettings['bit_depth'])
->rotation($imageSettings['rotation'])
->offsetX($imageSettings['offset_x'])
->offsetY($imageSettings['offset_y'])
->outputPath($outputPath);
// Apply dithering if requested by markup
$shouldDither = self::markupContainsDitherImage($markup);
if ($shouldDither) {
$imageStage->dither();
// TODO: actually resolve this merge conflict
// // Apply dithering if requested by markup
// $shouldDither = self::markupContainsDitherImage($markup);
// if ($shouldDither) {
// $imageStage->dither();
// }
if ($imageSettings['palette']) {
$imageStage->palette($imageSettings['palette']);
}
(new TrmnlPipeline())->pipe($browserStage)
@ -110,11 +117,20 @@ class ImageGenerationService
if ($device->deviceModel) {
/** @var DeviceModel $model */
$model = $device->deviceModel;
$paletteRecord = $model->palette()->firstOr();
$palette = null;
if ($paletteRecord) {
$palette = array_map(
fn ($colorCode) => new RgbColor($colorCode),
json_decode($paletteRecord->palette)
);
}
return [
'width' => $model->width,
'height' => $model->height,
'colors' => $model->colors,
'color_type' => ColorType::fromString($model->color_type),
'bit_depth' => $model->bit_depth,
'scale_factor' => $model->scale_factor,
'rotation' => $model->rotation,
@ -123,6 +139,7 @@ class ImageGenerationService
'offset_y' => $model->offset_y,
'image_format' => self::determineImageFormatFromModel($model),
'use_model_settings' => true,
'palette' => $palette,
];
}
@ -153,6 +170,9 @@ class ImageGenerationService
private static function determineImageFormatFromModel(DeviceModel $model): string
{
// Map DeviceModel settings to ImageFormat
if ($model->mime_type === 'image/png' && $model->palette()) {
return ImageFormat::PNG_INDEXED->value;
}
if ($model->mime_type === 'image/bmp' && $model->bit_depth === 1) {
return ImageFormat::BMP3_1BIT_SRGB->value;
}