@@ -518,30 +518,43 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
518
518
msg = f"cannot write mode { im .mode } as DDS"
519
519
raise OSError (msg )
520
520
521
- alpha = im .mode [- 1 ] == "A"
522
- if im .mode [0 ] == "L" :
523
- pixel_flags = DDPF .LUMINANCE
524
- rawmode = im .mode
525
- if alpha :
526
- rgba_mask = [0x000000FF , 0x000000FF , 0x000000FF ]
521
+ flags = DDSD .CAPS | DDSD .HEIGHT | DDSD .WIDTH | DDSD .PIXELFORMAT
522
+ bitcount = len (im .getbands ()) * 8
523
+ raw = im .encoderinfo .get ("pixel_format" ) != "DXT1"
524
+ if raw :
525
+ codec_name = "raw"
526
+ flags |= DDSD .PITCH
527
+ pitch = (im .width * bitcount + 7 ) // 8
528
+
529
+ alpha = im .mode [- 1 ] == "A"
530
+ if im .mode [0 ] == "L" :
531
+ pixel_flags = DDPF .LUMINANCE
532
+ rawmode = im .mode
533
+ if alpha :
534
+ rgba_mask = [0x000000FF , 0x000000FF , 0x000000FF ]
535
+ else :
536
+ rgba_mask = [0xFF000000 , 0xFF000000 , 0xFF000000 ]
527
537
else :
528
- rgba_mask = [0xFF000000 , 0xFF000000 , 0xFF000000 ]
529
- else :
530
- pixel_flags = DDPF .RGB
531
- rawmode = im .mode [::- 1 ]
532
- rgba_mask = [0x00FF0000 , 0x0000FF00 , 0x000000FF ]
538
+ pixel_flags = DDPF .RGB
539
+ rawmode = im .mode [::- 1 ]
540
+ rgba_mask = [0x00FF0000 , 0x0000FF00 , 0x000000FF ]
533
541
542
+ if alpha :
543
+ r , g , b , a = im .split ()
544
+ im = Image .merge ("RGBA" , (a , r , g , b ))
534
545
if alpha :
535
- r , g , b , a = im .split ()
536
- im = Image .merge ("RGBA" , (a , r , g , b ))
537
- if alpha :
538
- pixel_flags |= DDPF .ALPHAPIXELS
539
- rgba_mask .append (0xFF000000 if alpha else 0 )
540
-
541
- flags = DDSD .CAPS | DDSD .HEIGHT | DDSD .WIDTH | DDSD .PITCH | DDSD .PIXELFORMAT
542
- bitcount = len (im .getbands ()) * 8
543
- pitch = (im .width * bitcount + 7 ) // 8
546
+ pixel_flags |= DDPF .ALPHAPIXELS
547
+ rgba_mask .append (0xFF000000 if alpha else 0 )
544
548
549
+ fourcc = 0
550
+ else :
551
+ codec_name = "bcn"
552
+ flags |= DDSD .LINEARSIZE
553
+ pitch = (im .width + 3 ) * 4
554
+ rawmode = None
555
+ rgba_mask = [0 , 0 , 0 , 0 ]
556
+ pixel_flags = DDPF .FOURCC
557
+ fourcc = D3DFMT .DXT1
545
558
fp .write (
546
559
o32 (DDS_MAGIC )
547
560
+ struct .pack (
@@ -556,11 +569,11 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
556
569
)
557
570
+ struct .pack ("11I" , * ((0 ,) * 11 )) # reserved
558
571
# pfsize, pfflags, fourcc, bitcount
559
- + struct .pack ("<4I" , 32 , pixel_flags , 0 , bitcount )
572
+ + struct .pack ("<4I" , 32 , pixel_flags , fourcc , bitcount )
560
573
+ struct .pack ("<4I" , * rgba_mask ) # dwRGBABitMask
561
574
+ struct .pack ("<5I" , DDSCAPS .TEXTURE , 0 , 0 , 0 , 0 )
562
575
)
563
- ImageFile ._save (im , fp , [ImageFile ._Tile ("raw" , (0 , 0 ) + im .size , 0 , rawmode )])
576
+ ImageFile ._save (im , fp , [ImageFile ._Tile (codec_name , (0 , 0 ) + im .size , 0 , rawmode )])
564
577
565
578
566
579
def _accept (prefix : bytes ) -> bool :
0 commit comments