Author: jwilcox
Date: 2006-06-29 21:14:40 -0400 (Thu, 29 Jun 2006)
New Revision: 62149

Modified:
   trunk/ipod-sharp/ChangeLog
   trunk/ipod-sharp/tools/PhotoAdder.cs
   trunk/ipod-sharp/ui/ArtworkHelpers.cs
Log:
2006-06-29  James Willcox  <[EMAIL PROTECTED]>

        * ui/ArtworkHelpers.cs: preserve aspect ratio when scaling



Modified: trunk/ipod-sharp/ChangeLog
===================================================================
--- trunk/ipod-sharp/ChangeLog  2006-06-29 23:03:59 UTC (rev 62148)
+++ trunk/ipod-sharp/ChangeLog  2006-06-30 01:14:40 UTC (rev 62149)
@@ -1,5 +1,9 @@
 2006-06-29  James Willcox  <[EMAIL PROTECTED]>
 
+       * ui/ArtworkHelpers.cs: preserve aspect ratio when scaling
+
+2006-06-29  James Willcox  <[EMAIL PROTECTED]>
+
        * ui/ArtworkHelpers.cs: remove the SetCoverArt/SetThumbnail methods,
        and just have ToBytes/ToPixbuf.  Also, do a better job of figuring
        out the endianess needed for the RGB565 images and handle rotation.

Modified: trunk/ipod-sharp/tools/PhotoAdder.cs
===================================================================
--- trunk/ipod-sharp/tools/PhotoAdder.cs        2006-06-29 23:03:59 UTC (rev 
62148)
+++ trunk/ipod-sharp/tools/PhotoAdder.cs        2006-06-30 01:14:40 UTC (rev 
62149)
@@ -14,13 +14,20 @@
             if (format.ArtworkType != ArtworkType.PhotoSmall && 
format.ArtworkType != ArtworkType.PhotoLarge &&
                 format.ArtworkType != ArtworkType.PhotoFullScreen && 
format.ArtworkType != ArtworkType.PhotoTvScreen)
                 continue;
-            
+
+            byte[] bytes;
+            short padX, padY;
+
+            bytes = ArtworkHelpers.ToBytes (format, pixbuf, out padX, out 
padY);
+
             Thumbnail thumbnail = photo.CreateThumbnail ();
             thumbnail.Format = format;
-            thumbnail.Width = format.Width;
-            thumbnail.Height = format.Height;
+            thumbnail.Width = (short) pixbuf.Width;
+            thumbnail.Height = (short) pixbuf.Height;
+            thumbnail.HorizontalPadding = padX;
+            thumbnail.VerticalPadding = padY;
 
-            thumbnail.SetData (ArtworkHelpers.ToBytes (thumbnail.Format, 
pixbuf));
+            thumbnail.SetData (bytes);
         }
     }
 
@@ -36,8 +43,9 @@
 
                 photo.FullSizeFileName = file;
                 album.Add (photo);
+            } catch (GLib.GException e) {
             } catch (Exception e) {
-                Console.Error.WriteLine (e);
+                Console.WriteLine (e);
             }
         }
 

Modified: trunk/ipod-sharp/ui/ArtworkHelpers.cs
===================================================================
--- trunk/ipod-sharp/ui/ArtworkHelpers.cs       2006-06-29 23:03:59 UTC (rev 
62148)
+++ trunk/ipod-sharp/ui/ArtworkHelpers.cs       2006-06-30 01:14:40 UTC (rev 
62149)
@@ -194,6 +194,31 @@
             }
         }
 
+        private static Gdk.Pixbuf Scale (Gdk.Pixbuf pixbuf, int width, int 
height, out int widthPadding,
+                                         out int heightPadding) {
+            if (pixbuf.Width == width && pixbuf.Height == height) {
+                widthPadding = 0;
+                heightPadding = 0;
+                return pixbuf;
+            }
+
+            double scale = Math.Min  (width / (double) pixbuf.Width, height / 
(double) pixbuf.Height);
+
+            int scaleWidth = (int) (scale * pixbuf.Width);
+            int scaleHeight = (int) (scale * pixbuf.Height);
+
+            Gdk.Pixbuf scaled = new Gdk.Pixbuf (Gdk.Colorspace.Rgb, false, 8, 
width, height);
+            scaled.Fill (0);
+
+            widthPadding = width - scaleWidth;
+            heightPadding = height - scaleHeight;
+
+            pixbuf.Scale (scaled, widthPadding / 2, heightPadding / 2, 
scaleWidth, scaleHeight,
+                          widthPadding / 2, heightPadding / 2, scale, scale, 
Gdk.InterpType.Bilinear);
+
+            return scaled;
+        }
+
         public static Gdk.Pixbuf ToPixbuf (ArtworkFormat format, byte[] data) {
             Gdk.Pixbuf pixbuf = new Gdk.Pixbuf (Gdk.Colorspace.Rgb, false, 8, 
format.Width, format.Height);
             Gdk.Pixbuf rotated = null;
@@ -228,6 +253,15 @@
         }
 
         public static byte[] ToBytes (ArtworkFormat format, Gdk.Pixbuf pixbuf) 
{
+            short a, b;
+            return ToBytes (format, pixbuf, out a, out b);
+        }
+
+        public static byte[] ToBytes (ArtworkFormat format, Gdk.Pixbuf pixbuf, 
out short horizontalPadding,
+                                      out short verticalPadding) {
+            horizontalPadding = 0;
+            verticalPadding = 0;
+            
             bool disposePixbuf = false;
 
             if (format.ImageType == ImageType.Rgb56590 || format.ImageType == 
ImageType.Rgb565BE90) {
@@ -235,17 +269,18 @@
                 disposePixbuf = true;
             }
             
-            // FIXME: preserve aspect ratio
-            if (pixbuf.Height > format.Height || pixbuf.Width > format.Width) {
-                Gdk.Pixbuf scaled = pixbuf.ScaleSimple (format.Width, 
format.Height,
-                                                        
Gdk.InterpType.Bilinear);
+            if (pixbuf.Height != format.Height || pixbuf.Width != 
format.Width) {
+                int padX, padY;
+                Gdk.Pixbuf scaled = Scale (pixbuf, format.Width, 
format.Height, out padX, out padY);
 
+                horizontalPadding = (short) padX;
+                verticalPadding = (short) padY;
+
                 if (disposePixbuf) {
                     pixbuf.Dispose ();
                 }
 
                 pixbuf = scaled;
-                
                 disposePixbuf = true;
             }
             

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to