Win32 API 日本語リファレンス
ホームGraphics.OpenGL › glReadPixels

glReadPixels

関数
フレームバッファの矩形領域のピクセルを読み出す。
DLLOPENGL32.dll呼出規約winapi

シグネチャ

// OPENGL32.dll
#include <windows.h>

void glReadPixels(
    INT x,
    INT y,
    INT width,
    INT height,
    DWORD format,
    DWORD type,
    void* pixels
);

パラメーター

名前方向説明
xINTin読み取り矩形領域の左下隅のx座標(ウィンドウ座標)。
yINTin読み取り矩形領域の左下隅のy座標(ウィンドウ座標)。
widthINTin読み取るピクセル領域の幅。
heightINTin読み取るピクセル領域の高さ。
formatDWORDin返すピクセルデータの形式。GL_RGB、GL_RGBA、GL_DEPTH_COMPONENTなどを指定する。
typeDWORDin返すピクセルデータのデータ型。GL_UNSIGNED_BYTEやGL_FLOATなどを指定する。
pixelsvoid*inout読み取ったピクセルデータを格納する出力バッファへのポインタ。

戻り値の型: void

各言語での呼び出し定義

// OPENGL32.dll
#include <windows.h>

void glReadPixels(
    INT x,
    INT y,
    INT width,
    INT height,
    DWORD format,
    DWORD type,
    void* pixels
);
[DllImport("OPENGL32.dll", ExactSpelling = true)]
static extern void glReadPixels(
    int x,   // INT
    int y,   // INT
    int width,   // INT
    int height,   // INT
    uint format,   // DWORD
    uint type,   // DWORD
    IntPtr pixels   // void* in/out
);
<DllImport("OPENGL32.dll", ExactSpelling:=True)>
Public Shared Sub glReadPixels(
    x As Integer,   ' INT
    y As Integer,   ' INT
    width As Integer,   ' INT
    height As Integer,   ' INT
    format As UInteger,   ' DWORD
    type As UInteger,   ' DWORD
    pixels As IntPtr   ' void* in/out
)
End Sub
' x : INT
' y : INT
' width : INT
' height : INT
' format : DWORD
' type : DWORD
' pixels : void* in/out
Declare PtrSafe Sub glReadPixels Lib "opengl32" ( _
    ByVal x As Long, _
    ByVal y As Long, _
    ByVal width As Long, _
    ByVal height As Long, _
    ByVal format As Long, _
    ByVal type As Long, _
    ByVal pixels As LongPtr)
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

glReadPixels = ctypes.windll.opengl32.glReadPixels
glReadPixels.restype = None
glReadPixels.argtypes = [
    ctypes.c_int,  # x : INT
    ctypes.c_int,  # y : INT
    ctypes.c_int,  # width : INT
    ctypes.c_int,  # height : INT
    wintypes.DWORD,  # format : DWORD
    wintypes.DWORD,  # type : DWORD
    ctypes.POINTER(None),  # pixels : void* in/out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('OPENGL32.dll')
glReadPixels = Fiddle::Function.new(
  lib['glReadPixels'],
  [
    Fiddle::TYPE_INT,  # x : INT
    Fiddle::TYPE_INT,  # y : INT
    Fiddle::TYPE_INT,  # width : INT
    Fiddle::TYPE_INT,  # height : INT
    -Fiddle::TYPE_INT,  # format : DWORD
    -Fiddle::TYPE_INT,  # type : DWORD
    Fiddle::TYPE_VOIDP,  # pixels : void* in/out
  ],
  Fiddle::TYPE_VOID)
#[link(name = "opengl32")]
extern "system" {
    fn glReadPixels(
        x: i32,  // INT
        y: i32,  // INT
        width: i32,  // INT
        height: i32,  // INT
        format: u32,  // DWORD
        type: u32,  // DWORD
        pixels: *mut ()  // void* in/out
    );
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("OPENGL32.dll")]
public static extern void glReadPixels(int x, int y, int width, int height, uint format, uint type, IntPtr pixels);
"@
$api = Add-Type -MemberDefinition $sig -Name 'OPENGL32_glReadPixels' -Namespace Win32 -PassThru
# $api::glReadPixels(x, y, width, height, format, type, pixels)
#uselib "OPENGL32.dll"
#func global glReadPixels "glReadPixels" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; glReadPixels x, y, width, height, format, type, pixels
; x : INT -> "sptr"
; y : INT -> "sptr"
; width : INT -> "sptr"
; height : INT -> "sptr"
; format : DWORD -> "sptr"
; type : DWORD -> "sptr"
; pixels : void* in/out -> "sptr"
#uselib "OPENGL32.dll"
#func global glReadPixels "glReadPixels" int, int, int, int, int, int, sptr
; glReadPixels x, y, width, height, format, type, pixels
; x : INT -> "int"
; y : INT -> "int"
; width : INT -> "int"
; height : INT -> "int"
; format : DWORD -> "int"
; type : DWORD -> "int"
; pixels : void* in/out -> "sptr"
; void glReadPixels(INT x, INT y, INT width, INT height, DWORD format, DWORD type, void* pixels)
#uselib "OPENGL32.dll"
#func global glReadPixels "glReadPixels" int, int, int, int, int, int, intptr
; glReadPixels x, y, width, height, format, type, pixels
; x : INT -> "int"
; y : INT -> "int"
; width : INT -> "int"
; height : INT -> "int"
; format : DWORD -> "int"
; type : DWORD -> "int"
; pixels : void* in/out -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	opengl32 = windows.NewLazySystemDLL("OPENGL32.dll")
	procglReadPixels = opengl32.NewProc("glReadPixels")
)

// x (INT), y (INT), width (INT), height (INT), format (DWORD), type (DWORD), pixels (void* in/out)
r1, _, err := procglReadPixels.Call(
	uintptr(x),
	uintptr(y),
	uintptr(width),
	uintptr(height),
	uintptr(format),
	uintptr(type),
	uintptr(pixels),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // void
procedure glReadPixels(
  x: Integer;   // INT
  y: Integer;   // INT
  width: Integer;   // INT
  height: Integer;   // INT
  format: DWORD;   // DWORD
  type: DWORD;   // DWORD
  pixels: Pointer   // void* in/out
); stdcall;
  external 'OPENGL32.dll' name 'glReadPixels';
result := DllCall("OPENGL32\glReadPixels"
    , "Int", x   ; INT
    , "Int", y   ; INT
    , "Int", width   ; INT
    , "Int", height   ; INT
    , "UInt", format   ; DWORD
    , "UInt", type   ; DWORD
    , "Ptr", pixels   ; void* in/out
    , "Int")   ; return: void
●glReadPixels(x, y, width, height, format, type, pixels) = DLL("OPENGL32.dll", "int glReadPixels(int, int, int, int, dword, dword, void*)")
# 呼び出し: glReadPixels(x, y, width, height, format, type, pixels)
# x : INT -> "int"
# y : INT -> "int"
# width : INT -> "int"
# height : INT -> "int"
# format : DWORD -> "dword"
# type : DWORD -> "dword"
# pixels : void* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "opengl32" fn glReadPixels(
    x: i32, // INT
    y: i32, // INT
    width: i32, // INT
    height: i32, // INT
    format: u32, // DWORD
    type: u32, // DWORD
    pixels: ?*anyopaque // void* in/out
) callconv(std.os.windows.WINAPI) void;
proc glReadPixels(
    x: int32,  # INT
    y: int32,  # INT
    width: int32,  # INT
    height: int32,  # INT
    format: uint32,  # DWORD
    `type`: uint32,  # DWORD
    pixels: pointer  # void* in/out
) {.importc: "glReadPixels", stdcall, dynlib: "OPENGL32.dll".}
pragma(lib, "opengl32");
extern(Windows)
void glReadPixels(
    int x,   // INT
    int y,   // INT
    int width,   // INT
    int height,   // INT
    uint format,   // DWORD
    uint type,   // DWORD
    void* pixels   // void* in/out
);
ccall((:glReadPixels, "OPENGL32.dll"), stdcall, Cvoid,
      (Int32, Int32, Int32, Int32, UInt32, UInt32, Ptr{Cvoid}),
      x, y, width, height, format, type, pixels)
# x : INT -> Int32
# y : INT -> Int32
# width : INT -> Int32
# height : INT -> Int32
# format : DWORD -> UInt32
# type : DWORD -> UInt32
# pixels : void* in/out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
void glReadPixels(
    int32_t x,
    int32_t y,
    int32_t width,
    int32_t height,
    uint32_t format,
    uint32_t type,
    void* pixels);
]]
local opengl32 = ffi.load("opengl32")
-- opengl32.glReadPixels(x, y, width, height, format, type, pixels)
-- x : INT
-- y : INT
-- width : INT
-- height : INT
-- format : DWORD
-- type : DWORD
-- pixels : void* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('OPENGL32.dll');
const glReadPixels = lib.func('__stdcall', 'glReadPixels', 'void', ['int32_t', 'int32_t', 'int32_t', 'int32_t', 'uint32_t', 'uint32_t', 'void *']);
// glReadPixels(x, y, width, height, format, type, pixels)
// x : INT -> 'int32_t'
// y : INT -> 'int32_t'
// width : INT -> 'int32_t'
// height : INT -> 'int32_t'
// format : DWORD -> 'uint32_t'
// type : DWORD -> 'uint32_t'
// pixels : void* in/out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("OPENGL32.dll", {
  glReadPixels: { parameters: ["i32", "i32", "i32", "i32", "u32", "u32", "pointer"], result: "void" },
});
// lib.symbols.glReadPixels(x, y, width, height, format, type, pixels)
// x : INT -> "i32"
// y : INT -> "i32"
// width : INT -> "i32"
// height : INT -> "i32"
// format : DWORD -> "u32"
// type : DWORD -> "u32"
// pixels : void* in/out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void glReadPixels(
    int32_t x,
    int32_t y,
    int32_t width,
    int32_t height,
    uint32_t format,
    uint32_t type,
    void* pixels);
C, "OPENGL32.dll");
// $ffi->glReadPixels(x, y, width, height, format, type, pixels);
// x : INT
// y : INT
// width : INT
// height : INT
// format : DWORD
// type : DWORD
// pixels : void* in/out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Opengl32 extends StdCallLibrary {
    Opengl32 INSTANCE = Native.load("opengl32", Opengl32.class);
    void glReadPixels(
        int x,   // INT
        int y,   // INT
        int width,   // INT
        int height,   // INT
        int format,   // DWORD
        int type,   // DWORD
        Pointer pixels   // void* in/out
    );
}
@[Link("opengl32")]
lib LibOPENGL32
  fun glReadPixels = glReadPixels(
    x : Int32,   # INT
    y : Int32,   # INT
    width : Int32,   # INT
    height : Int32,   # INT
    format : UInt32,   # DWORD
    type_ : UInt32,   # DWORD
    pixels : Void*   # void* in/out
  ) : Void
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef glReadPixelsNative = Void Function(Int32, Int32, Int32, Int32, Uint32, Uint32, Pointer<Void>);
typedef glReadPixelsDart = void Function(int, int, int, int, int, int, Pointer<Void>);
final glReadPixels = DynamicLibrary.open('OPENGL32.dll')
    .lookupFunction<glReadPixelsNative, glReadPixelsDart>('glReadPixels');
// x : INT -> Int32
// y : INT -> Int32
// width : INT -> Int32
// height : INT -> Int32
// format : DWORD -> Uint32
// type : DWORD -> Uint32
// pixels : void* in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
procedure glReadPixels(
  x: Integer;   // INT
  y: Integer;   // INT
  width: Integer;   // INT
  height: Integer;   // INT
  format: DWORD;   // DWORD
  type: DWORD;   // DWORD
  pixels: Pointer   // void* in/out
); stdcall;
  external 'OPENGL32.dll' name 'glReadPixels';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "glReadPixels"
  c_glReadPixels :: Int32 -> Int32 -> Int32 -> Int32 -> Word32 -> Word32 -> Ptr () -> IO ()
-- x : INT -> Int32
-- y : INT -> Int32
-- width : INT -> Int32
-- height : INT -> Int32
-- format : DWORD -> Word32
-- type : DWORD -> Word32
-- pixels : void* in/out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let glreadpixels =
  foreign "glReadPixels"
    (int32_t @-> int32_t @-> int32_t @-> int32_t @-> uint32_t @-> uint32_t @-> (ptr void) @-> returning void)
(* x : INT -> int32_t *)
(* y : INT -> int32_t *)
(* width : INT -> int32_t *)
(* height : INT -> int32_t *)
(* format : DWORD -> uint32_t *)
(* type : DWORD -> uint32_t *)
(* pixels : void* in/out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library opengl32 (t "OPENGL32.dll"))
(cffi:use-foreign-library opengl32)

(cffi:defcfun ("glReadPixels" gl-read-pixels :convention :stdcall) :void
  (x :int32)   ; INT
  (y :int32)   ; INT
  (width :int32)   ; INT
  (height :int32)   ; INT
  (format :uint32)   ; DWORD
  (type :uint32)   ; DWORD
  (pixels :pointer))   ; void* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $glReadPixels = Win32::API::More->new('OPENGL32',
    'void glReadPixels(int x, int y, int width, int height, DWORD format, DWORD type, LPVOID pixels)');
# my $ret = $glReadPixels->Call($x, $y, $width, $height, $format, $type, $pixels);
# x : INT -> int
# y : INT -> int
# width : INT -> int
# height : INT -> int
# format : DWORD -> DWORD
# type : DWORD -> DWORD
# pixels : void* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。