/src/wrappers/cairo/library/cairo_image_surface.e

http://github.com/tybor/Liberty · Specman e · 158 lines · 83 code · 21 blank · 54 comment · 2 complexity · 9b4a907c83838fa65f2bcf1a5dee60bb MD5 · raw file

  1. note
  2. description: "Cairo Image Surface: Cairo surfaces that represent data in memory."
  3. copyright: "[
  4. Copyright (C) 2007-2017: Soluciones Informaticas Libres S.A. (Except),
  5. Cairo team
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public License
  8. as published by the Free Software Foundation; either version 2.1 of
  9. the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. 02110-1301 USA
  18. ]"
  19. date: "$Date:$"
  20. revision: "$Revision:$"
  21. wrapped_version: "1.2.4"
  22. class CAIRO_IMAGE_SURFACE
  23. -- Image surfaces provide the ability to render to memory buffers either
  24. -- allocated by cairo or by the calling code. The supported image formats
  25. -- are those defined in CAIRO_FORMAT.
  26. inherit
  27. CAIRO_SURFACE
  28. insert
  29. CAIRO_FORMAT
  30. CAIRO_IMAGE_SURFACE_EXTERNALS
  31. create {ANY}
  32. make, for_data, from_png
  33. feature {} -- Creation
  34. make (a_format, a_width, a_height: INTEGER)
  35. -- Creates an image surface of the specified format and dimensions.
  36. -- Initially the surface contents are all 0. (Specifically, within
  37. -- each pixel, each color or alpha channel belonging to format will
  38. -- be 0. The contents of bits within a pixel, but not belonging to
  39. -- the given format are undefined).
  40. -- a_format: format of pixels in the surface to create
  41. -- a_width: width of the surface, in pixels
  42. -- a_height: height of the surface, in pixels
  43. -- Returns: A pointer to the newly created surface. The caller owns
  44. -- the surface and should call cairo_surface_destroy when done with
  45. -- it. This function always returns a valid pointer, but it will
  46. -- return a pointer to a "nil" surface if an error such as out of
  47. -- memory occurs. Always check 'status' to check for this.
  48. require
  49. is_valid_cairo_format (a_format)
  50. do
  51. from_external_pointer (cairo_image_surface_create (a_format, a_width, a_height))
  52. end
  53. for_data (some_data: POINTER; a_format, a_width, a_height, a_stride: INTEGER)
  54. -- Creates an image surface for the provided pixel data.
  55. -- The output buffer must be kept around until the CAIRO_SURFACE is
  56. -- destroyed or 'finish' is called on the surface. The initial
  57. -- contents of buffer will be used as the initial image contents;
  58. -- you must explicitly clear the buffer, using, for example,
  59. -- 'rectangle()' and 'fill()' if you want it cleared.
  60. --
  61. -- data: a pointer to a buffer supplied by the application in which
  62. -- to write contents.
  63. -- format: the format of pixels in the buffer
  64. -- width: the width of the image to be stored in the buffer
  65. -- height: the height of the image to be stored in the buffer
  66. -- stride: the number of bytes between the start of rows in the
  67. -- buffer. Having this be specified separate from width
  68. -- allows for padding at the end of rows, or for writing to
  69. -- a subportion of a larger image.
  70. require
  71. is_valid_cairo_format (a_format)
  72. do
  73. from_external_pointer (cairo_image_surface_create_for_data (some_data,
  74. a_format, a_width, a_height, a_stride))
  75. end
  76. from_png (a_filename: STRING)
  77. -- Creates a new image surface and initializes the contents to the
  78. -- given PNG file.
  79. -- Creates a CAIRO_SURFACE initialized with the contents of the
  80. -- PNG file, or a "nil" surface if any error occurred. A nil surface
  81. -- can be checked for with 'status' which may return one of the
  82. -- following values: cairo_status_no_memory,
  83. -- cairo_status_file_not_found,
  84. -- cairo_status_read_error
  85. require
  86. a_filename /= Void
  87. do
  88. from_external_pointer (cairo_image_surface_create_from_png (
  89. a_filename.to_external))
  90. end
  91. feature {ANY} -- Access
  92. data: POINTER
  93. --Get a pointer to the data of the image surface, for direct
  94. -- inspection or modification.
  95. do
  96. Result := cairo_image_surface_get_data (handle)
  97. end
  98. format: INTEGER
  99. --Get the format of the surface.
  100. do
  101. Result := cairo_image_surface_get_format (handle)
  102. ensure
  103. is_valid_cairo_format (Result)
  104. end
  105. width: INTEGER
  106. --Get the width of the image surface in pixels.
  107. do
  108. Result := cairo_image_surface_get_width (handle)
  109. end
  110. height: INTEGER
  111. --Get the height of the image surface in pixels.
  112. do
  113. Result := cairo_image_surface_get_height (handle)
  114. end
  115. stride: INTEGER
  116. --Get the stride of the image surface in bytes
  117. do
  118. Result := cairo_image_surface_get_stride (handle)
  119. end
  120. feature {ANY} -- Operations
  121. write_to_png (a_filename: STRING): INTEGER
  122. -- Writes the contents of surface to a new file filename as a PNG
  123. -- image.
  124. -- filename: the name of a file to write to
  125. -- Returns: cairo_status_success if the PNG file was written successfully.
  126. -- Otherwise, cairo_status_no_memory if memory could not be allocated
  127. -- for the operation or cairo_status_surface_type_mismatch if the
  128. -- surface does not have pixel contents, or cairo_status_write_error
  129. -- if an I/O error occurs while attempting to write the file.
  130. require
  131. a_filename /= Void
  132. do
  133. Result := cairo_surface_write_to_png (handle, a_filename.to_external)
  134. ensure
  135. is_valid_cairo_status (Result)
  136. end
  137. end -- class CAIRO_IMAGE_SURFACE