does anyone have a libxpm example? - xlib

This is the dumbest thing but I have tried and tried and I cannot use libxpm.
I have found some snippets of code but very little and what i have found has been very old code that I cannot compile.
My understanding so far is that I need to:
connect to x windows server (done)
create a window (done)
use libxpm to create a pixmap from data (not done)
copy the pixmap to the window (not done)
If you happen to have a small example lying around or know where to send me that would be great. If you happen to know how to use xcb and libxpm that would be even better. xcb seems to use an integer for it's connection while xlib uses a display struct, I haven't found any examples at all that deal with xcb and libxpm and the connection issue is a deadend for me.
Thanks for reading

This may help. If I use ImageMagick's convert, I can convert a JPEG to an XPM and that may help you see how they are supposed to look.
convert image.jpg image.xpm
And we can look at it like this:
more image.xpm
/* XPM */
static char *background[] = {
/* columns rows colors chars-per-pixel */
"906 603 181 2 ",
" c #040403",
". c #080704",
"X c #070803",
"o c #090906",
"O c #060608",
...
...

The libXpm sources include a simple “show xpm” command, but it still uses Xlib & Xt, not xcb:
https://cgit.freedesktop.org/xorg/lib/libXpm/tree/sxpm/sxpm.c

Related

Converting Shape file from GIS into a PPP using spatstat UK

I am trying to create a PPP in spatstat using my study area (a large polygon made up of individual polygons) from a shape file from GIS.
I have been following: Handling shapeles in the spatstat package
Adrian Baddeley, Rolf Turner and Ege Rubak
2022-11-08
spatstat version 3.0-2
#load packages
install.packages("spatstat")
library(spatstat)
install.packages("maptools")
library(maptools) #will get warning message about rgdal instead, stick with maptools
install.packages(sp)
library(sp)
#import shapefile
UMshape1<-readShapeSpatial('F:/GIS/export_shape/Clipped_urban_matrix.shp')
#check class
class(UMshape1)
#returned: [1] "SpatialPolygonsDataFrame
#following code from guidance to convert Objects of class SpatialPolygonsDataFrame
UM1 <- as(UMshape1, "SpatialPolygons")
UM1regions <- slot(UM1, "polygons") UM1regions <- lapply(UM1regions, function(x) { SpatialPolygons(list(x)) }) UM1windows <- lapply(UM1regions, as.owin)
#checked class of each of these file types
class(UM1)
#"SpatialPolygons"
class(UM1regions)
#"list"
class(UM1windows)
"list"
#from guidance 'The result is a list of objects of class owin. Often it would make sense to convert this to a tessellation object, by typing':
#so I enter the code for my data
teUM1 <-tess(tiles = UM1windows)
This last command (tess) has now been running for 48 hours (red stop box). I did not created a progress bar.
Is this the right thing to do so that I can then created my owin study area? So that I can then create a PPP in spatstat?
If the desired result is a single window of class owin to use as the window for your point pattern, then you don't need a tessellation. Instead of teUM1 <-tess(tiles = UM1windows) you should probably do teWin <- union.owin(as.solist(UM1windows)).
If you do really need a tessellation (which would keep each of the windows separate for further use) then you could call tess(tiles=UM1windows, check=FALSE). The long computation time is caused by the fact that the code is checking whether each window overlaps any of the other windows. This check is disabled if you set check=FALSE.

How can I read and set the paper style (aka papername) in Firemonkey MacOS

Working in Delphi Firemonkey for Mac OS64.
Trying to read and then set the variable Apple calls the "paperName", which is the paper type (letter, legal, envelope, etc.) I know that it is accessed through NSPrinter.PaperName? but I do not understand how to code FMX to access it.
I'm using cookbooked code to get the paper rectangle:
FPrintInfo := TNSPrintInfo.Wrap(TNSPrintInfo.OCClass.sharedPrintInfo);
FPrintInfo.retain;
PMGetAdjustedPaperRect(FPrintInfo.PMPageFormat, #PaperRect);
FPrintInfo.release;
but I'm not experienced at all with Mac code so my attempts to plug-and-play off of this code to get papername have not been successful.
Thanks for your help.
Dave,
Thanks. Sorry, I didn't really give you enough info. The code I provided does work, to get the paper rectangle.
What I am trying to get, in addition, is the paper name, and I can't figure out what function will get me that.
I'm trying to use PMGetPageFormatPaper(FPrintInfo.PMPageFormat, #PaperTypeS); but I think I may not be declaring PaperTypeS correctly.
What I'm trying is:
function getPaperShape: string;
var
FPrintInfo: NSPrintInfo;
PaperRect: PMRect;
paperwidth,paperheight:double;
paperTypeS:string;
begin
FPrintInfo := TNSPrintInfo.Wrap(TNSPrintInfo.OCClass.sharedPrintInfo);
FPrintInfo.retain;
PMGetAdjustedPaperRect(FPrintInfo.PMPageFormat, #PaperRect);
PMGetPageFormatPaper(FPrintInfo.PMPageFormat, #PaperTypeS);
FPrintInfo.release;
paperwidth:= PaperRect.right-PaperRect.left;
paperheight:= PaperRect.bottom-PaperRect.top;
end;
That clearly is not correct, since I get nothing returned in paperTypeS. I've tried declaring paperTypeS as NSPrinter.PaperName, or just as PaperName, or as PMPaperName, but clearly I'm just guessing here and none of those are recognized by FMX as valid types.
Does that make more sense?
Again, thanks.
Scott

How to write an image to the Windows clipboard in Rust

I want to put a local image into the clipboard using Rust. I used the clipboard-win and image crates. My code is as follows, but it doesn't work.
extern crate clipboard_win;
extern crate image;
use clipboard_win::{formats, Clipboard};
use image::GenericImageView;
fn main() {
let img = image::open("C:\\Users\\Crash\\Desktop\\20190405221505.png").unwrap();
Clipboard::new()
.unwrap()
.set(formats::CF_BITMAP, &img.raw_pixels());
}
After execution, there seems to be content in the pasteboard, but there is nothing displayed after Ctrl+V. How can I correct this code?
You have multiple problems.
Format
A PNG format image is not a bitmap format image, even though it is a bitmap.
A thread on MSDN states:
There isn't a standardized clipboard format for PNG.
You can register your own format, but then only you can recognize the clipboard. If you use the standard bitmap or file format then more applications can accept your data.
Error handling
Clipboard::set can fail and it returns a Result. You need to handle this case. The compiler even told you about this:
warning: unused `std::result::Result` that must be used
--> src\main.rs:11:5
|
11 | / Clipboard::new()
12 | | .unwrap()
13 | | .set(formats::CF_BITMAP, &data);
| |________________________________________^
|
= note: #[warn(unused_must_use)] on by default
= note: this `Result` may be an `Err` variant, which should be handled
Don't ignore warnings, especially when trying to debug a problem.
Unfortunately, this is as far as I got:
use clipboard_win::{formats, Clipboard}; // 2.1.2
use image::ImageOutputFormat; // 0.21.0
fn main() {
let img = image::open("unicorn.png").unwrap();
let mut data = Vec::new();
img.write_to(&mut data, ImageOutputFormat::BMP)
.expect("Unable to transform");
Clipboard::new()
.unwrap()
.set(formats::CF_BITMAP, &data)
.expect("Unable to set clipboard");
}
Writing data to a file produces a BMP that Paint can read, but the clipboard data is still invalid. While attempting to debug the differences, I ran into low-level crashes in the library, which suggests it may not be ready for general use, despite the 2.x version number.
I believe that the root problem is that
Windows expects
A handle to a bitmap (HBITMAP).
A BITMAP is a struct with a set of information about the bitmap, such as width and height. This is likely different from the on-disk format of the bitmap.
It seems plausible that fitting the bitmap data into this expected format would go a long way to solving the problem.
Another avenue is to look into using CF_DIB instead of CF_BITMAP. Contrary to the linked forum post above, CF_DIB expects a pointer to a BITMAPINFO which has a BITMAPINFOHEADER field. This makes a reference to a BI_PNG compression, which might allow you to submit a PNG without performing transformations.
See also:
How do I encode a Rust Piston image and get the result in memory?

LibTIFF - Read 16bit image into array?

On the LibTIFF documentation, there is no mention or sample of how to read a 16 bit RGB image. Apparently also, using the scanline functions is required for reading a 16 bit. After a few attempts I couldn't get that to work.
According to this post,
If you want to handle all kind of TIFF image wihout using
TIFFReadRGBAImage then you have to detect the image format and use
low-level interface such as TIFFReadEncodedStrip and
TIFFReadEncodedTile
My end goal is to be able to get 16 bit values for the R,G and B channels, as well as actually know where they are in the image.
In the docs for libtiff they provide this sample code for eading using the strip encoding, however, they don't explain what is in the buf you get or how to make use of it. Can anyone explain how I am supposed to read the 16 bit values from that? Also, is there a way to consistent determine the strip order so I my read image is rendered correct?
TIFF* tif = TIFFOpen("myfile.tif", "r");
if (tif) {
tdata_t buf;
tstrip_t strip;
buf = _TIFFmalloc(TIFFStripSize(tif));
for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
TIFFReadEncodedStrip(tif, strip, buf, (tsize_t) -1);
_TIFFfree(buf);
TIFFClose(tif);
}

Bytes decoding in D

I've got some wrongly decoded text fragment. It was decoded like cp866, but in fact it should be utf-8 ("нажал кабан на баклажан" --> "╨╜╨░╨╢╨░╨╗ ╨║╨░╨▒╨░╨╜ ╨╜╨░ ╨▒╨░╨║╨╗╨░╨╢╨░╨╜"). I'd like to fix it, and I've already written the code in Python which solves the task:
broken = "╨╜╨░╨╢╨░╨╗ ╨║╨░╨▒╨░╨╜ ╨╜╨░ ╨▒╨░╨║╨╗╨░╨╢╨░╨╜"
fixed = bytes(broken, 'cp866').decode('utf-8')
print(fixed) # it will print 'нажал кабан на баклажан'
However, at first I was trying to solve this issue in D, but failed to find an answer. So, how can this task be solved in D?
At the moment, D does not have extensive native facilities for converting text between encodings.
Here are some options:
As ratchet freak mentioned, D does have std.encoding, but it does not cover many encodings at the moment.
On Windows, you could use std.windows.charset.fromMBSz and toMBSz, which wrap MultiByteToWideChar and WideCharToMultiByte.
You could simply embed the encodings that interest you in your program (example).
On POSIX, you could invoke the iconv program (example), or use the libiconv library (D1 binding).

Resources