Advertisement
Guest User

Untitled

a guest
Sep 8th, 2012
110
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. # openscad_surface_export.py
  4. # by Rob Antonishen
  5. # http://ffaat.pointclark.net
  6. #
  7. # Version 1.0 (20120907)
  8. #
  9. # Description
  10. # File save handler for openscad dat format
  11. # Borrows heavily from colorxhtml.py
  12. #
  13. # Changes
  14. #
  15. # License:
  16. #
  17. # This program is free software; you can redistribute it and/or modify
  18. # it under the terms of the GNU General Public License as published by
  19. # the Free Software Foundation; either version 2 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. # GNU General Public License for more details.
  26. #
  27. # The GNU Public License is available at
  28. # http://www.gnu.org/copyleft/gpl.datfile
  29.  
  30. import struct
  31. import datetime
  32. import gimp
  33. from gimpfu import *
  34.  
  35. gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
  36.  
  37. def save_openscad_dat(img, drawable, filename, raw_filename, minval, maxval):
  38.     drawable = pdb.gimp_layer_new_from_visible(img, img, "merged")
  39.     width = drawable.width
  40.     height = drawable.height
  41.     bpp = drawable.bpp
  42.     span = maxval-minval
  43.    
  44.     if span == 0:
  45.         return
  46.  
  47.     gimp.tile_cache_ntiles(width / gimp.tile_width() + 1)
  48.    
  49.     fileOut = file(filename,"w")
  50.    
  51.     pr = drawable.get_pixel_rgn(0, 0, width, height, False, False)
  52.    
  53.     gimp.progress_init(_("Saving as OpenSCAD .dat file"))
  54.    
  55.     fileOut.write("# Created by gimp plugin openscad_surface_export.py, %s\n" % datetime.datetime.strftime(datetime.datetime.now(), "%c"))
  56.     fileOut.write("# Name: %s\n" % img.name)
  57.     fileOut.write("# type: matrix\n")
  58.     fileOut.write("# Rows: %s\n" % height)
  59.     fileOut.write("# Columns: %s\n" % width)
  60.  
  61.     for y in range(height-1, -1, -1):
  62.         row = pr[0:width, y]
  63.         for pixel in RowIterator(row, bpp):
  64.             pixval = pixel[0]
  65.             fileOut.write(" %s" % str((minval+span*(pixval/255.0))))
  66.         fileOut.write("\n")
  67.         gimp.progress_update((height-y) / float(height))
  68.  
  69.     fileOut.close()
  70.     pdb.gimp_drawable_delete(drawable)
  71.    
  72. class RowIterator:
  73.     def __init__(self, row, bpp):
  74.         self.row = row
  75.         self.bpp = bpp
  76.  
  77.         self.start = 0
  78.         self.stop = bpp
  79.  
  80.         self.length = len(row)
  81.         self.fmt = 'B' * bpp
  82.  
  83.     def __iter__(self):
  84.         return iter(self.get_pixel, None)
  85.  
  86.     def get_pixel(self):
  87.         if self.stop > self.length:
  88.             return None
  89.  
  90.         pixel = struct.unpack(self.fmt, self.row[self.start:self.stop])
  91.  
  92.         self.start += self.bpp
  93.         self.stop += self.bpp
  94.  
  95.         return pixel
  96.        
  97. def register_save_dat():
  98.     gimp.register_save_handler("file-openscad-save", "dat", "")
  99.  
  100. register(
  101.         "file-openscad-save",
  102.         N_("Saves an OpenSCAD surface dat file."),
  103.         "Saves an OpenSCAD surface dat file.",
  104.         "Rob Antonishen",
  105.         "Rob Antonishen",
  106.         "2012",
  107.         N_("OpenSCAD Surface"),
  108.         "RGB, GRAY",
  109.         [
  110.             (PF_IMAGE, "img", "Input image", None),
  111.             (PF_DRAWABLE, "drawable", "Input drawable", None),
  112.             (PF_STRING, "filename", "The name of the file", None),
  113.             (PF_STRING, "raw-filename", "The name of the file", None),
  114.             (PF_FLOAT, "minval", "Black (0) Value", 0.0),
  115.             (PF_FLOAT, "maxval", "White (255) Value", 100.0),
  116.         ],
  117.         [],
  118.         save_openscad_dat, on_query=register_save_dat,
  119.         menu="<Save>", domain=("gimp20-python", gimp.locale_directory))
  120. main()
Advertisement
RAW Paste Data Copied
Advertisement