OSDN Git Service

16bit compositing (step 1/2)
[mypaint-anime/master.git] / lib / document.py
index ce1d697..c86e89d 100644 (file)
@@ -2,67 +2,47 @@
 # Copyright (C) 2007-2008 by Martin Renold <martinxyz@gmx.ch>
 #
 # This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License.
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY. See the COPYING file for more details.
-
-"""
-Design thoughts:
-A stroke:
-- is a list of motion events
-- knows everything needed to draw itself (brush settings / initial brush state)
-- has fixed brush settings (only brush states can change during a stroke)
-
-A layer:
-- is a container of several strokes (strokes can be removed)
-- can be rendered as a whole
-- can contain cache bitmaps, so it doesn't have to retrace all strokes all the time
-
-A document:
-- contains several layers
-- knows the active layer and the current brush
-- manages the undo history
-- must be altered via undo/redo commands (except painting)
-"""
-
-import mypaintlib, helpers, tiledsurface, pixbufsurface
-import command, stroke, layer, serialize
-import brush # FIXME: the brush module depends on gtk and everything, but we only need brush_lowlevel
-import gzip, os, zipfile, tempfile
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+
+import os, zipfile, tempfile, time
 join = os.path.join
 import xml.etree.ElementTree as ET
 from gtk import gdk
+import gobject, numpy
+
+import helpers, tiledsurface, pixbufsurface, backgroundsurface, mypaintlib
+import command, stroke, layer
+import brush # FIXME: the brush module depends on gtk and everything, but we only need brush_lowlevel
+N = tiledsurface.N
+
+class SaveLoadError(Exception):
+    """Expected errors on loading or saving, like missing permissions or non-existing files."""
+    pass
 
 class Document():
     """
     This is the "model" in the Model-View-Controller design.
     (The "view" would be ../gui/tileddrawwidget.py.)
-    It represenst everything that the user would want to save.
+    It represents everything that the user would want to save.
 
 
     The "controller" mostly in drawwindow.py.
-    It should be possible to use it without any GUI attached.
-    
-    Undo/redo is part of the model. The whole undo/redo stack can be
-    saved to disk (planned) and can be used to reconstruct
-    everything else.
+    It is possible to use it without any GUI attached (see ../tests/)
     """
-    # Please note the following difficulty:
+    # Please note the following difficulty with the undo stack:
     #
     #   Most of the time there is an unfinished (but already rendered)
     #   stroke pending, which has to be turned into a command.Action
     #   or discarded as empty before any other action is possible.
-    #
-    # TODO: the document should allow to "playback" (redo) a stroke
-    # partially and examine its timing (realtime playback / calculate
-    # total painting time) ?using half-done commands?
+    #   (split_stroke)
 
     def __init__(self):
         self.brush = brush.Brush_Lowlevel()
         self.stroke = None
         self.canvas_observers = []
-        self.layer_observers = []
-
+        self.stroke_observers = [] # callback arguments: stroke, brush (brush is a temporary read-only convenience object)
         self.clear(True)
 
     def clear(self, init=False):
@@ -71,12 +51,13 @@ class Document():
             bbox = self.get_bbox()
         # throw everything away, including undo stack
         self.command_stack = command.CommandStack()
-        self.background = (255, 255, 255)
+        self.set_background((255, 255, 255))
         self.layers = []
         self.layer_idx = None
         self.add_layer(0)
-        # disallow undo of the first layer (TODO: deleting the last layer should clear it instead)
-        self.command_stack = command.CommandStack()
+        # disallow undo of the first layer
+        self.command_stack.clear()
+        self.unsaved_painting_time = 0.0
 
         if not init:
             for f in self.canvas_observers:
@@ -90,8 +71,11 @@ class Document():
         if not self.stroke: return
         self.stroke.stop_recording()
         if not self.stroke.empty:
-            self.layer.new_stroke_rendered_on_surface(self.stroke)
-            self.command_stack.do(command.Stroke(self, self.stroke))
+            self.command_stack.do(command.Stroke(self, self.stroke, self.snapshot_before_stroke))
+            del self.snapshot_before_stroke
+            self.unsaved_painting_time += self.stroke.total_painting_time
+            for f in self.stroke_observers:
+                f(self.stroke, self.brush)
         self.stroke = None
 
     def select_layer(self, idx):
@@ -104,6 +88,7 @@ class Document():
         if not self.stroke:
             self.stroke = stroke.Stroke()
             self.stroke.start_recording(self.brush)
+            self.snapshot_before_stroke = self.layer.save_snapshot()
         self.stroke.record_event(dtime, x, y, pressure)
 
         l = self.layer
@@ -114,6 +99,28 @@ class Document():
         if split:
             self.split_stroke()
 
+    def straight_line(self, src, dst):
+        self.split_stroke()
+        # TODO: undo last stroke if it was very short... (but not at document level?)
+        real_brush = self.brush
+        self.brush = brush.Brush_Lowlevel()
+        self.brush.copy_settings_from(real_brush)
+
+        duration = 3.0
+        pressure = 0.3
+        N = 1000
+        x = numpy.linspace(src[0], dst[0], N)
+        y = numpy.linspace(src[1], dst[1], N)
+        # rest the brush in src for a minute, to avoid interpolation
+        # from the upper left corner (states are zero) (FIXME: the
+        # brush should handle this on its own, maybe?)
+        self.stroke_to(60.0, x[0], y[0], 0.0)
+        for i in xrange(N):
+            self.stroke_to(duration/N, x[i], y[i], pressure)
+        self.split_stroke()
+        self.brush = real_brush
+
+
     def layer_modified_cb(self, *args):
         # for now, any layer modification is assumed to be visible
         for f in self.canvas_observers:
@@ -141,6 +148,9 @@ class Document():
         self.split_stroke()
         self.command_stack.do(cmd)
 
+    def get_last_command(self):
+        self.split_stroke()
+        return self.command_stack.get_last_command()
 
     def set_brush(self, brush):
         self.split_stroke()
@@ -155,127 +165,220 @@ class Document():
             res.expandToIncludeRect(bbox)
         return res
 
-    def blit_tile_into(self, dst, tx, ty, layers=None):
+    def blit_tile_into(self, dst, tx, ty, mipmap=0, layers=None, background=None):
         if layers is None:
             layers = self.layers
+        if background is None:
+            background = self.background
+
+        background.blit_tile_into(dst, tx, ty, mipmap)
 
-        # render solid white background (planned: something like self.background.blit_tile())
-        assert dst.shape[2] == 3, 'RGB destination expected'
-        N = tiledsurface.N
-        dst[:N,:N,:] = self.background
+        assert dst.dtype == 'uint8' # OPTIMIZE: rewrite background to hold 16bit directly
+        dst_16bit = (dst.astype('uint32') * (1<<15) / 255).astype('uint16')
 
         for layer in layers:
             surface = layer.surface
-            surface.composite_tile_over(dst, tx, ty)
-            
-    def get_total_painting_time(self):
-        t = 0.0
-        for cmd in self.command_stack.undo_stack:
-            if isinstance(cmd, command.Stroke):
-                t += cmd.stroke.total_painting_time
-        return t
+            surface.composite_tile_over(dst_16bit, tx, ty, mipmap_level=mipmap, opacity=layer.opacity)
 
+        if dst_16bit is not dst:
+            mypaintlib.tile_convert_rgb16_to_rgb8(dst_16bit, dst)
+            
     def add_layer(self, insert_idx):
         self.do(command.AddLayer(self, insert_idx))
 
     def remove_layer(self):
         self.do(command.RemoveLayer(self))
 
+    def merge_layer(self, dst_idx):
+        self.do(command.MergeLayer(self, dst_idx))
+
     def load_layer_from_pixbuf(self, pixbuf, x=0, y=0):
-        arr = pixbuf.get_pixels_array()
-        arr = mypaintlib.gdkpixbuf2numpy(arr)
+        arr = helpers.gdkpixbuf2numpy(pixbuf)
         self.do(command.LoadLayer(self, arr, x, y))
 
+    def set_layer_opacity(self, opacity):
+        cmd = self.get_last_command()
+        if isinstance(cmd, command.SetLayerOpacity):
+            self.undo()
+        self.do(command.SetLayerOpacity(self, opacity))
+
     def set_background(self, obj):
-        try:
-            obj = obj.get_pixels_array()
-            obj = mypaintlib.gdkpixbuf2numpy(obj)
-        except:
-            # it was already an array
-            pass
-        self.do(command.SetBackground(self, obj))
+        # This is not an undoable action. One reason is that dragging
+        # on the color chooser would get tons of undo steps.
+
+        if not isinstance(obj, backgroundsurface.Background):
+            obj = backgroundsurface.Background(obj)
+        self.background = obj
+
+        self.invalidate_all()
 
     def load_from_pixbuf(self, pixbuf):
         self.clear()
         self.load_layer_from_pixbuf(pixbuf)
 
-    def save(self, filename):
+    def is_layered(self):
+        count = 0
+        for l in self.layers:
+            if not l.surface.is_empty():
+                count += 1
+        return count > 1
+
+    def save(self, filename, **kwargs):
         trash, ext = os.path.splitext(filename)
         ext = ext.lower().replace('.', '')
-        print ext
         save = getattr(self, 'save_' + ext, self.unsupported)
-        save(filename)
+        try:        
+            save(filename, **kwargs)
+        except gobject.GError, e:
+                if  e.code == 5:
+                    #add a hint due to a very consfusing error message when there is no space left on device
+                    raise SaveLoadError, 'Unable to save: ' + e.message +  '\nDo you have enough space left on the device?'
+                else:
+                    raise SaveLoadError, 'Unable to save: ' + e.message
+        except IOError, e:
+            raise SaveLoadError, 'Unable to save: ' + e.strerror
+        self.unsaved_painting_time = 0.0
 
     def load(self, filename):
+        if not os.path.isfile(filename):
+            raise SaveLoadError, 'File does not exist: ' + repr(filename)
+        if not os.access(filename,os.R_OK):
+            raise SaveLoadError, 'You do not have the necessary permissions to open file: ' + repr(filename)
         trash, ext = os.path.splitext(filename)
         ext = ext.lower().replace('.', '')
         load = getattr(self, 'load_' + ext, self.unsupported)
         load(filename)
+        self.command_stack.clear()
+        self.unsaved_painting_time = 0.0
 
     def unsupported(self, filename):
-        raise ValueError, 'Unkwnown file format extension: ' + repr(filename)
+        raise SaveLoadError, 'Unknown file format extension: ' + repr(filename)
 
     def render_as_pixbuf(self, *args):
         return pixbufsurface.render_as_pixbuf(self, *args)
 
-    def save_png(self, filename):
-        pixbuf = self.render_as_pixbuf()
-        pixbuf.save(filename, 'png')
+    def save_png(self, filename, compression=2, alpha=False, multifile=False):
+        if multifile:
+            self.save_multifile_png(filename, compression)
+        else:
+            if alpha:
+                tmp_layer = layer.Layer()
+                for l in self.layers:
+                    l.merge_into(tmp_layer)
+                pixbuf = tmp_layer.surface.render_as_pixbuf()
+            else:
+                pixbuf = self.render_as_pixbuf()
+            pixbuf.save(filename, 'png', {'compression':str(compression)})
+
+    def save_multifile_png(self, filename, compression=2, alpha=False):
+        prefix, ext = os.path.splitext(filename)
+        # if we have a number already, strip it
+        l = prefix.rsplit('.', 1)
+        if l[-1].isdigit():
+            prefix = l[0]
+        doc_bbox = self.get_bbox()
+        for i, l in enumerate(self.layers):
+            filename = '%s.%03d%s' % (prefix, i+1, ext)
+            l.surface.save(filename, *doc_bbox)
 
     def load_png(self, filename):
         self.load_from_pixbuf(gdk.pixbuf_new_from_file(filename))
 
-    def save_ora(self, filename):
+    def load_jpg(self, filename):
+        self.load_from_pixbuf(gdk.pixbuf_new_from_file(filename))
+    load_jpeg = load_jpg
+
+    def save_jpg(self, filename, quality=90):
+        pixbuf = self.render_as_pixbuf()
+        pixbuf.save(filename, 'jpeg', options={'quality':str(quality)})
+    save_jpeg = save_jpg
+
+    def save_ora(self, filename, options=None):
+        print 'save_ora:'
+        t0 = time.time()
         tempdir = tempfile.mkdtemp('mypaint')
-        z = zipfile.ZipFile(filename, 'w', compression=zipfile.ZIP_STORED)
+        # use .tmp extension, so we don't overwrite a valid file if there is an exception
+        z = zipfile.ZipFile(filename + '.tmpsave', 'w', compression=zipfile.ZIP_STORED)
         # work around a permission bug in the zipfile library: http://bugs.python.org/issue3394
         def write_file_str(filename, data):
             zi = zipfile.ZipInfo(filename)
             zi.external_attr = 0100644 << 16
             z.writestr(zi, data)
-        write_file_str('mimetype', 'image/openraster') # Mime type must be the first object stored. FIXME: what should go here?
+        write_file_str('mimetype', 'image/openraster') # must be the first file
         image = ET.Element('image')
         stack = ET.SubElement(image, 'stack')
         x0, y0, w0, h0 = self.get_bbox()
         a = image.attrib
-        a['x'] = str(0)
-        a['y'] = str(0)
         a['w'] = str(w0)
         a['h'] = str(h0)
 
-        def add_layer(x, y, pixbuf, name):
-            layer = ET.Element('layer')
-            stack.append(layer)
-
+        def store_pixbuf(pixbuf, name):
             tmp = join(tempdir, 'tmp.png')
-            pixbuf.save(tmp, 'png')
+            t1 = time.time()
+            pixbuf.save(tmp, 'png', {'compression':'2'})
+            print '  %.3fs saving %s compression 2' % (time.time() - t1, name)
             z.write(tmp, name)
             os.remove(tmp)
 
+        def add_layer(x, y, opac, pixbuf, name):
+            layer = ET.Element('layer')
+            stack.append(layer)
+            store_pixbuf(pixbuf, name)
             a = layer.attrib
             a['src'] = name
             a['x'] = str(x)
             a['y'] = str(y)
+            a['opacity'] = str(opac)
+            return layer
 
         for idx, l in enumerate(reversed(self.layers)):
             if l.surface.is_empty():
                 continue
+            opac = l.opacity
             x, y, w, h = l.surface.get_bbox()
             pixbuf = l.surface.render_as_pixbuf()
-            add_layer(x-x0, y-y0, pixbuf, 'data/layer%03d.png' % idx)
+            add_layer(x-x0, y-y0, opac, pixbuf, 'data/layer%03d.png' % idx)
 
         # save background as layer (solid color or tiled)
-        s = pixbufsurface.Surface(0, 0, w0, h0)
+        s = pixbufsurface.Surface(x0, y0, w0, h0)
         s.fill(self.background)
-        add_layer(0, 0, s.pixbuf, 'data/background.png')
-
+        l = add_layer(0, 0, 1.0, s.pixbuf, 'data/background.png')
+        bg = self.background
+        x, y, w, h = bg.get_pattern_bbox()
+        pixbuf = pixbufsurface.render_as_pixbuf(bg, x, y, w, h, alpha=False)
+        store_pixbuf(pixbuf, 'data/background_tile.png')
+        l.attrib['background_tile'] = 'data/background_tile.png'
+
+        # preview
+        t2 = time.time()
+        print '  starting to render image for thumbnail...'
+        pixbuf = self.render_as_pixbuf()
+        w, h = pixbuf.get_width(), pixbuf.get_height()
+        if w > h:
+            w, h = 256, max(h*256/w, 1)
+        else:
+            w, h = max(w*256/h, 1), 256
+        t1 = time.time()
+        pixbuf = pixbuf.scale_simple(w, h, gdk.INTERP_BILINEAR)
+        print '  %.3fs scaling thumbnail' % (time.time() - t1)
+        store_pixbuf(pixbuf, 'Thumbnails/thumbnail.png')
+        print '  total %.3fs spent on thumbnail' % (time.time() - t2)
+
+        helpers.indent_etree(image)
         xml = ET.tostring(image, encoding='UTF-8')
 
         write_file_str('stack.xml', xml)
         z.close()
         os.rmdir(tempdir)
+        if os.path.exists(filename):
+            os.remove(filename) # windows needs that
+        os.rename(filename + '.tmpsave', filename)
+
+        print '%.3fs save_ora total' % (time.time() - t0)
 
     def load_ora(self, filename):
+        print 'load_ora:'
+        t0 = time.time()
         tempdir = tempfile.mkdtemp('mypaint')
         z = zipfile.ZipFile(filename)
         print 'mimetype:', z.read('mimetype').strip()
@@ -283,57 +386,80 @@ class Document():
         image = ET.fromstring(xml)
         stack = image.find('stack')
 
-        self.clear()
+        def get_pixbuf(filename):
+            t1 = time.time()
+            tmp = join(tempdir, 'tmp.png')
+            f = open(tmp, 'wb')
+            f.write(z.read(filename))
+            f.close()
+            res = gdk.pixbuf_new_from_file(tmp)
+            os.remove(tmp)
+            print '  %.3fs loading %s' % (time.time() - t1, filename)
+            return res
+
+        self.clear() # this leaves one empty layer
+        no_background = True
         for layer in stack:
             if layer.tag != 'layer':
                 print 'Warning: ignoring unsupported tag:', layer.tag
                 continue
             a = layer.attrib
+
+            if 'background_tile' in a:
+                assert no_background
+                try:
+                    print a['background_tile']
+                    self.set_background(get_pixbuf(a['background_tile']))
+                    no_background = False
+                    continue
+                except backgroundsurface.BackgroundError, e:
+                    print 'ORA background tile not usable:', e
+
             src = a.get('src', '')
             if not src.lower().endswith('.png'):
                 print 'Warning: ignoring non-png layer'
                 continue
-
-            tmp = join(tempdir, 'tmp.png')
-            f = open(tmp, 'w')
-            f.write(z.read(src))
-            f.close()
-            pixbuf = gdk.pixbuf_new_from_file(tmp)
-            os.remove(tmp)
+            pixbuf = get_pixbuf(src)
 
             x = int(a.get('x', '0'))
             y = int(a.get('y', '0'))
+            opac = float(a.get('opacity', '1.0'))
             self.add_layer(insert_idx=0)
             last_pixbuf = pixbuf
+            t1 = time.time()
             self.load_layer_from_pixbuf(pixbuf, x, y)
+            self.layers[0].opacity = helpers.clamp(opac, 0.0, 1.0)
+            print '  %.3fs converting pixbuf to layer format' % (time.time() - t1)
 
         os.rmdir(tempdir)
 
         if len(self.layers) == 1:
             raise ValueError, 'Could not load any layer.'
 
-        # recognize solid or tiled background layers (at least those that mypaint saves)
-        # (OpenRaster will probably get generator layers for this some day)
-        N = tiledsurface.N
-        p = last_pixbuf
-        if not p.get_has_alpha() and p.get_width() % N == 0 and p.get_height() % N == 0:
-            tiles = self.layers[0].surface.tiledict.values()
-            if len(tiles) > 1:
-                all_equal = True
-                for tile in tiles[1:]:
-                    if (tile.rgba != tiles[0].rgba).any():
-                        all_equal = False
-                        break
-                if all_equal:
-                    arr = p.get_pixels_array()
-                    arr = mypaintlib.gdkpixbuf2numpy(arr)
-                    tile = arr[0:N,0:N,:]
-                    self.set_background(tile.copy())
-                    self.select_layer(0)
-                    self.remove_layer()
+        if no_background:
+            # recognize solid or tiled background layers, at least those that mypaint <= 0.7.1 saves
+            t1 = time.time()
+            p = last_pixbuf
+            if not p.get_has_alpha() and p.get_width() % N == 0 and p.get_height() % N == 0:
+                tiles = self.layers[0].surface.tiledict.values()
+                if len(tiles) > 1:
+                    all_equal = True
+                    for tile in tiles[1:]:
+                        if (tile.rgba != tiles[0].rgba).any():
+                            all_equal = False
+                            break
+                    if all_equal:
+                        arr = helpers.gdkpixbuf2numpy(p)
+                        tile = arr[0:N,0:N,:]
+                        self.set_background(tile.copy())
+                        self.select_layer(0)
+                        self.remove_layer()
+            print '  %.3fs recognizing tiled background' % (time.time() - t1)
 
         if len(self.layers) > 1:
-            # select the still present initial empty top layer
-            # hm, should this better be removed?
+            # remove the still present initial empty top layer
             self.select_layer(len(self.layers)-1)
+            self.remove_layer()
+            # this leaves the topmost layer selected
 
+        print '%.3fs load_ora total' % (time.time() - t0)