Commits

Brendan Howell committed 27cb564

some experiments with cairo osx color mapping palaver

  • Participants
  • Parent commits 6ed83db

Comments (0)

Files changed (3)

File drawing_cairo.py

 import pango
 import numpy
 import math
-#import Image
 import array
 
 
         self.sdl_surface = sdl_surface
         width = sdl_surface.get_width()
         height = sdl_surface.get_height()
+        subsurf = sdl_surface.subsurface((0,0,width,height))
         
-        self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
+        r,g,b,a = subsurf.get_shifts()
+        print r,g,b,a
+        rm,gm,bm,am = subsurf.get_masks()
+        print rm,gm,bm,am
+        subsurf.set_shifts((a, r, g, b))
+        subsurf.set_masks((am, rm, gm, bm))
+        
+        
+        buf = sdl_surface.get_view("2")
+        
+        stride = width * 4
+        self.surface = cairo.ImageSurface.create_for_data(buf, cairo.FORMAT_RGB24, width, height, stride)
+        
+        #self.surface = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height)
         self.ctx = cairo.Context(self.surface)
         self.pangoctx = pangocairo.CairoContext(self.ctx)
         self.font_map = pangocairo.cairo_font_map_get_default()
     	
 
     def _blitToScreen(self):  
-        dest = pygame.surfarray.pixels2d(self.sdl_surface)
-        dest.data[:] = self.surface.get_data()
+        pass
+        #dest = pygame.surfarray.pixels2d(self.sdl_surface)
+        #dest.data[:] = self.surface.get_data()
+        #destR = pygame.surfarray.pixels_red(self.sdl_surface)
+        #destR.data[:] = self.surface.get_data()[1::4]
+        #destG = pygame.surfarray.pixels_green(self.sdl_surface)
+        #destG.data[:] = self.surface.get_data()[2::4]
+        #destB = pygame.surfarray.pixels_blue(self.sdl_surface)
+        #destB.data[:] = self.surface.get_data()[3::4]
+        #destA = pygame.surfarray.pixels_alpha(self.sdl_surface)
+        #destA.data[:] = self.surface.get_data()[::4]
+        
     	
     def __init__(self, file_name):
         self.file_name = file_name
         self.loaded = False
-        self.transformed = False
-        self.matrix = cairo.Matrix()
         self.rotation = 0
-        self.scalex = 1
-        self.scaley = 1
+        self.scalex = None
+        self.scaley = None
         
         
     def _loadSurface_(self):
             
         CTX.save()
 
-        self.matrix.translate(x/self.width, y/self.height)
-        CTX.set_matrix(self.matrix)
+        CTX.translate(x, y)
+        CTX.rotate(self.rotation)
+        CTX.scale(self.scalex, self.scaley)
+
         CTX.set_source_surface(self.surface, 0, 0)
         CTX.paint()
         CTX.restore()
         
     def rotate(self, angle):
-        if(not(self.loaded)):
-            self._loadSurface_()
-        radians = math.pi * angle / 180.0
-        self.matrix.rotate(-1 * self.rotation)
-        self.rotation = radians
-        self.matrix.rotate(radians)
+        self.rotation = math.pi * angle / 180.0
         
     def scale(self, pixelsx, pixelsy):
+        if (pixelsx < 1) or (pixelsy < 1):
+            print "image must be at least 1px wide and 1px high!"
+            return
         if(not(self.loaded)):
             self._loadSurface_()
-        oldx = float(self.width) / self.scalex
-        oldy = float(self.height) / self.scaley
-        self.matrix.scale(oldx, oldy)
-        sx = float(pixelsx) / self.width
-        sy = float(pixelsy) / self.height
-        self.matrix.scale(sx, sy)
+        self.scalex = float(pixelsx) / self.width
+        self.scaley = float(pixelsy) / self.height
         
     def get_width(self):
         if(not(self.loaded)):
             self._loadSurface_()
-        dx, dy = self.matrix.transform_distance(self.width, 0)
-        return dx
+        if self.scalex:
+            return self.scalex
+        else:
+            return self.width
     
     def get_height(self):
         if(not(self.loaded)):
             self._loadSurface_()
         dx, dy = self.matrix.transform_distance(0, self.height)
-        return dy
+        if self.scaley:
+            return self.scaley
+        else:
+            return self.height
 
 class Image:
     """All-purpose class for pixel images in pycessing"""
         self.key = None
         self.filename = filename
         
-    def size(self, x=320, y=240, fullscreen=False, flags=0):
+    def size(self, x=320, y=240, fullscreen=False, flags=pygame.OPENGL):
         print "setting display size"
         self.display_w = x
         self.display_h = y
         self.screen = pygame.display.set_mode((x,y), flags, 32)
         pygame.display.set_caption('PyCessing - ' + self.filename, "PyCessing")
         module_location = os.path.dirname(__file__)
+        
+        r,g,b,a = self.screen.get_shifts()
+        print r,g,b,a
+        rm,gm,bm,am = self.screen.get_masks()
+        print rm,gm,bm,am
+
+
+        self.screen.set_shifts((1,2,3,4))
+        self.screen.set_masks((am,rm,gm,bm)) 
 
         plat = platform.system()
         if plat == "Linux":