Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are bitmap (.bmp) files supported ? #286

Open
devilsclaw opened this issue Oct 1, 2024 · 14 comments
Open

Are bitmap (.bmp) files supported ? #286

devilsclaw opened this issue Oct 1, 2024 · 14 comments

Comments

@devilsclaw
Copy link

Currently I am using Zint to generate code 39 barcode and the direct to buffer. The only format currently supported for direct to buffer is bitmap.

I was wondering if this project supports the file format to be embedded or a simple way to convert it to any of the other formats that it does support ?

@galkahana
Copy link
Owner

The library does not provide embedding for BMP file formats. only tiff, jpg, png and the embedding of PDF files are supported natively (so if you got something quick to use to convert to any of those...this could be sufficient). You can still, however, embed any image data provided that you can translate the source data in bmp to raw color data, or to any of the PDF supported encoders. This goes through creating a PDF Image object, which is something you can do with the library.

I have an example of how to do something like that with TIFF images. I use libtiff to decode the image to color data (also alpha mask, for transparency effect) and then build a PDF image object directly into the PDF.
here is the example:
https://github.com/galkahana/pdf-transparent-tiff

specifically https://github.com/galkahana/pdf-transparent-tiff/blob/master/src/lib/ModernTiffImageHandler.cpp#L88 shows how to create the image object and write the raw color data.
I wrap it with a form xobject to provide the image "physical" dimensions, as image objects are drawn 1X1.

scanning a few libs it might be quite simple to translate bmp code to raw data...as it is pretty much there to begin with. so it might not be overly difficult to find something read it or write something yrself along the lines of existing libs/samples/attempts.

Gal.

@devilsclaw
Copy link
Author

Thanks. I was able to adapt that code to work with bitmap.

Now my only problem is, is that I can't seem to figure out how to make it modify a preexisting PDF page and add a image to it.

@galkahana
Copy link
Owner

well, if it's just an overlay on an existing page you can essentially add a new content stream to a page, and place drawing commands to put the image form.

there's a helper implementation with the library that makes this fairly simple. refer to the PDFModifiedPage Class section of the modification page in the library wiki.

@devilsclaw
Copy link
Author

devilsclaw commented Oct 4, 2024

EDITED: Removed due to creating a example in a repo

@devilsclaw
Copy link
Author

devilsclaw commented Oct 4, 2024

I did find PDFModifiedPage. I tried it out and maybe I am using it wrong. It does not complain. The output does not have the barcode. The original style adds a new page that has the barcode.

https://github.com/devilsclaw/pdf_barcode

output/output1.pdf is where it adds a new page and has the barcode at the bottom
output/output2.pdf is where it is supposed to add it to an existing pdf page but does not seem to.

@devilsclaw
Copy link
Author

On my own build I also make it add text to the pdf with WriteText. The text wont show up after

ObjectIDType imageXObjectObjectId = objCxt.GetInDirectObjectsRegistry().AllocateNewObjectID();

is called.

@galkahana
Copy link
Owner

reviewed the project. very nice :)
here's a PR to resolve the problem - devilsclaw/pdf_barcode#1

when looking at output2 you can see that there's a form starting and then while in it there's another form starting. the former is the implementation of the content context that's just started, and the latter is the barcode form xobject. this is a corrupted pdf.
reason is that the context context is started and then the form is created. you will want to keep both separate.

In this case is just moved the barcode form creation to be prior to the content context to resolve the issue and have the barcode show up. This is the right thing to do for both modification case, and adding new page.

Depending on how your application flow goes there's several other strategies you can use for the case of realizing you want to create an object in the midst of a content context session:

  • you can create the image beforehand and use the resultant object id (b.t.w no need to allocate it yourself in this case. the apis normally allow getting a resultant object id)
  • you can pause the content context (there's an api for that on content context) write the other object and start the content context again (i like this option least, normally. it creates a verbose pdf). this options is discussed here
  • you can forward reference an object, in which case you allocate an object id during writing in a content context session, and later use that id to create an object with, see a discussion here.

in any case, a wonderful example of using BMP image :).

@devilsclaw
Copy link
Author

First off thank you very much. Second this was very much almost a whole sale rip from the tiff project you pointed to. So it should have the same problem.

Again thanks. I will try it out.

@devilsclaw
Copy link
Author

Merged the pull request and tested again thanks. I wanted to point out that out of all the project managers I have had to deal with. Your one of the best and most helpful.

@devilsclaw
Copy link
Author

Oh one other thing, since the project is based off your project how would I give attribution ? I have not used many apache 2 license projects.

@galkahana
Copy link
Owner

No attribution required, unless you include source code in your project in which case what source code you take should retain the notice at the top (meaning you shouldnt jsut take the source code without the copyright notice i left there)...and if you make modifications to them do document those changes there. In the example project you provided, given yr just referencing the project via cmake there's no need to do any of that. however, in a project such as this one where i have copies of the code, i have to include them as a whole.
The intent is to avoid cases where someone will claim that my work is theirs, basically.

Also - 0 liability on my part and no warranty is provided. This is code that i provided for the benefit of the software community, not for it to come back and haunt me. it is provided as open source for a reason - so you can change if it doesn't work for you, and/or debug what issues you see. while i do provide assistance on occasion this is by no means guaranteed.

Gal.

@devilsclaw
Copy link
Author

So the project https://github.com/galkahana/pdf-transparent-tiff which is yours has a license file but nothing in the source files that would look like attribution. My project copies some of the functions and then modifies them to work with the bitmap buffer from zint. So I was trying to figure out how to attribute. Since to me its mostly your code with just a small adaptation.

Would i just add something to the top of the source file that point to the original project that it was based off of and your name ?

@devilsclaw
Copy link
Author

Maybe something like this at the top of the main.cpp ?

/*
  This project is based off https://github.com/galkahana/pdf-transparent-tiff 
  functions were copied a modified to work with the zint bitmap buffer

  Thanks: galkahana (https://github.com/galkahana)
  License: Apache-2.0 license
*/

@galkahana
Copy link
Owner

i think you can avoid even that. it's different enough to not be considered a derived work.
like any of the samples i provide.
if you don't want to still put something i reckon this works very well. but really...i don't think you have to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants