9 changed files with 237 additions and 12 deletions
-
2CMakeLists.txt
-
3src/CMakeLists.txt
-
37src/Image.cc
-
77src/Image.h
-
20src/Raster.cc
-
2src/Raster.h
-
10src/Rgba.cc
-
9src/Rgba.h
-
71src/draw_image.cc
@ -0,0 +1,37 @@ |
|||||
|
/*
|
||||
|
* To change this license header, choose License Headers in Project Properties. |
||||
|
* To change this template file, choose Tools | Templates |
||||
|
* and open the template in the editor. |
||||
|
*/ |
||||
|
|
||||
|
/*
|
||||
|
* File: Image.cc |
||||
|
* Author: Blobt |
||||
|
* |
||||
|
* Created on February 10, 2020, 5:55 PM |
||||
|
*/ |
||||
|
|
||||
|
#include "Image.h"
|
||||
|
#include <string.h>
|
||||
|
|
||||
|
Image::Image(int width, int height, void* data) { |
||||
|
if (width == 0 || height == 0 || data == 0) { |
||||
|
_width = 0; |
||||
|
_height = 0; |
||||
|
_pixel = 0; |
||||
|
} else { |
||||
|
_width = width; |
||||
|
_height = height; |
||||
|
_pixel = new unsigned int[width * height]; |
||||
|
memcpy(_pixel, data, width * height * sizeof (unsigned int)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
Image::~Image() { |
||||
|
delete [] _pixel; |
||||
|
} |
||||
|
|
||||
|
Rgba Image::pixelAt(int x, int y) const { |
||||
|
return Rgba(_pixel[y * _width + x]); |
||||
|
} |
||||
|
|
@ -0,0 +1,77 @@ |
|||||
|
/* |
||||
|
* To change this license header, choose License Headers in Project Properties. |
||||
|
* To change this template file, choose Tools | Templates |
||||
|
* and open the template in the editor. |
||||
|
*/ |
||||
|
|
||||
|
/* |
||||
|
* File: Image.h |
||||
|
* Author: Blobt |
||||
|
* |
||||
|
* Created on February 10, 2020, 5:55 PM |
||||
|
*/ |
||||
|
|
||||
|
#ifndef IMAGE_H |
||||
|
#define IMAGE_H |
||||
|
#include "Rgba.h" |
||||
|
#include "FreeImage.h" |
||||
|
#include "stdio.h" |
||||
|
|
||||
|
class Image { |
||||
|
public: |
||||
|
Image(int width, int height, void* data); |
||||
|
virtual ~Image(); |
||||
|
|
||||
|
int width() const { |
||||
|
return _width; |
||||
|
} |
||||
|
|
||||
|
int height() const { |
||||
|
return _height; |
||||
|
} |
||||
|
|
||||
|
Rgba pixelAt(int x, int y) const; |
||||
|
|
||||
|
static Image* loadFromFile(const char* fileName) { |
||||
|
|
||||
|
//检查文件类型 |
||||
|
FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(fileName, 0); |
||||
|
if (fifmt == FIF_UNKNOWN) { |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
//获取 |
||||
|
FIBITMAP* dib = FreeImage_Load(fifmt, fileName, 0); |
||||
|
|
||||
|
//转换 |
||||
|
FIBITMAP* temp = dib; |
||||
|
dib = FreeImage_ConvertTo32Bits(dib); |
||||
|
FreeImage_Unload(temp); |
||||
|
|
||||
|
|
||||
|
BYTE* pixels = (BYTE*) FreeImage_GetBits(dib); |
||||
|
int width = FreeImage_GetWidth(dib); |
||||
|
int height = FreeImage_GetHeight(dib); |
||||
|
|
||||
|
//颜色为数调换 |
||||
|
for (int i = 0; i < width * height * 4; i += 4) { |
||||
|
BYTE temp = pixels[i]; |
||||
|
pixels[i] = pixels[i + 2]; |
||||
|
pixels[i + 2] = temp; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
Image* image = new Image(width, height, (void*) pixels); |
||||
|
FreeImage_Unload(dib); |
||||
|
|
||||
|
return image; |
||||
|
|
||||
|
} |
||||
|
private: |
||||
|
int _width; |
||||
|
int _height; |
||||
|
unsigned int* _pixel; |
||||
|
}; |
||||
|
#endif /* IMAGE_H */ |
||||
|
|
@ -0,0 +1,71 @@ |
|||||
|
#include <iostream>
|
||||
|
#include <math.h>
|
||||
|
#include <cairo.h>
|
||||
|
#include <gtk/gtk.h>
|
||||
|
#include "Rgba.h"
|
||||
|
#include "Raster.h"
|
||||
|
#include "common.h"
|
||||
|
#include "Image.h"
|
||||
|
|
||||
|
using namespace std; |
||||
|
|
||||
|
|
||||
|
gint height = 500; |
||||
|
gint width = 500; |
||||
|
|
||||
|
Raster raster(width, height); |
||||
|
|
||||
|
unsigned char* makeBitmap() { |
||||
|
raster.clean(); |
||||
|
|
||||
|
Image* image = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/build/bin/12.jpg"); |
||||
|
|
||||
|
raster.drawImage(25, 25, image); |
||||
|
|
||||
|
return (unsigned char*) raster.buffer; |
||||
|
} |
||||
|
|
||||
|
void render(GtkWidget *widget) { |
||||
|
//允许窗口可以绘图
|
||||
|
gtk_widget_set_app_paintable(widget, TRUE); |
||||
|
gtk_widget_realize(widget); |
||||
|
gtk_widget_queue_draw(widget); |
||||
|
|
||||
|
//模拟一张图片
|
||||
|
unsigned char* data = makeBitmap(); |
||||
|
|
||||
|
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE, 8, width, height, width * 4, NULL, NULL); |
||||
|
|
||||
|
GdkPixmap *pixmap = NULL; |
||||
|
|
||||
|
gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, NULL, 128); |
||||
|
|
||||
|
gdk_window_set_back_pixmap(widget->window, pixmap, FALSE); |
||||
|
|
||||
|
g_object_unref(pixbuf); |
||||
|
g_object_unref(pixmap); |
||||
|
//delete data;
|
||||
|
} |
||||
|
|
||||
|
int main(int argc, char* argv[]) { |
||||
|
|
||||
|
gtk_init(&argc, &argv); |
||||
|
|
||||
|
//创建窗口
|
||||
|
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
||||
|
gtk_widget_set_size_request(window, width, height); |
||||
|
|
||||
|
gtk_window_set_title(GTK_WINDOW(window), "Gtk testing"); |
||||
|
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS); |
||||
|
|
||||
|
//窗口关闭时候,关闭程序
|
||||
|
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL); |
||||
|
//设置渲染
|
||||
|
g_signal_connect(window, "expose-event", G_CALLBACK(render), window); |
||||
|
|
||||
|
|
||||
|
gtk_widget_show_all(window); |
||||
|
gtk_main(); |
||||
|
|
||||
|
return 0; |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue