DateiSystem.cpp 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220
  1. //---Include---
  2. #include "DateiSystem.h"
  3. #include <iostream>
  4. #include "Bild.h"
  5. #include "Text.h"
  6. #ifdef WIN32
  7. # include "Fenster.h"
  8. # include "Fortschritt.h"
  9. # include "Globals.h"
  10. # include "Schrift.h"
  11. #endif
  12. #include "Datei.h"
  13. #include "Logging.h"
  14. using namespace Framework;
  15. // LTDB File format
  16. // Content of the LTDBPixel class from DateiSystem.h
  17. // Constructor
  18. LTDBPixel::LTDBPixel(LTDBPixel* davor)
  19. : ReferenceCounter(),
  20. davor(davor),
  21. index(0),
  22. iR(0),
  23. iG(0),
  24. iB(0),
  25. iA(0),
  26. miR(8),
  27. miG(8),
  28. miB(8),
  29. miA(8),
  30. maxIndex(1),
  31. change(0),
  32. changeR(0),
  33. changeG(0),
  34. changeB(0),
  35. changeA(0),
  36. komp(0),
  37. R(0),
  38. G(0),
  39. B(0),
  40. A(0)
  41. {}
  42. // Destructor
  43. LTDBPixel::~LTDBPixel()
  44. {
  45. if (davor) davor->release();
  46. }
  47. // non-constant
  48. bool LTDBPixel::addBitZuFarbe(
  49. unsigned char bit) // Adds a bit to the color values
  50. {
  51. if (changeR && iR != miR) // The bit belongs to Red
  52. {
  53. R = (unsigned char)(R | ((bit & 0x1) << (7 - komp - iR)));
  54. ++iR;
  55. }
  56. else if (changeG && iG != miG) // The bit belongs to Green
  57. {
  58. G = (unsigned char)(G | ((bit & 0x1) << (7 - komp - iG)));
  59. ++iG;
  60. }
  61. else if (changeB && iB != miB) // The bit belongs to Blue
  62. {
  63. B = (unsigned char)(B | ((bit & 0x1) << (7 - komp - iB)));
  64. ++iB;
  65. }
  66. else if (changeA && iA != miA) // The bit belongs to Alpha
  67. {
  68. A = (unsigned char)(A | ((bit & 0x1) << (7 - komp - iA)));
  69. ++iA;
  70. }
  71. else // The bit belongs to the next pixel
  72. return false;
  73. return true;
  74. }
  75. char LTDBPixel::addByte(
  76. char byte, char begin) // returns end of pixel, -1 if not finished
  77. {
  78. if (begin >= 8 || begin < 0) return -1;
  79. for (int i = begin; i < 8; ++i)
  80. {
  81. switch (index)
  82. {
  83. case 0:
  84. // The first bit of a pixel stores whether the
  85. // compression changed
  86. change = (bool)((byte >> (7 - i)) & 0x1);
  87. if (!change) // If compression doesn't change, the values
  88. // from the previous pixel are inherited
  89. {
  90. if (!davor) // The file is corrupted (the first pixel cannot
  91. // inherit from the previous one)
  92. {
  93. #ifdef WIN32
  94. MessageBox(NULL,
  95. "Error, the image file is corrupted",
  96. "Error",
  97. MB_ICONERROR);
  98. #endif
  99. exit(0);
  100. }
  101. changeR = davor->getChangeR();
  102. changeG = davor->getChangeG();
  103. changeB = davor->getChangeB();
  104. changeA = davor->getChangeA();
  105. komp = davor->getKomp() & 7;
  106. miR = (char)(miR - komp), miG = (char)(miG - komp),
  107. miB = (char)(miB - komp), miA = (char)(miA - komp);
  108. if (!changeR) R = davor->getR();
  109. if (!changeG) G = davor->getG();
  110. if (!changeB) B = davor->getB();
  111. if (!changeA) A = davor->getA();
  112. maxIndex = (char)(maxIndex
  113. + (changeR + changeG + changeB + changeA)
  114. * (8 - komp)); // Determine the length
  115. // of the pixel in bits. Each color has 8 bits by default,
  116. // compression can make it smaller
  117. }
  118. else
  119. maxIndex
  120. = (char)(maxIndex + 7); // Since the pixel doesn't inherit
  121. // values from the previous one,
  122. // it is 7 bits larger
  123. break;
  124. case 1: // The second bit of a pixel stores either the
  125. // compression type or already the color
  126. if (change) // The bit belongs to the 7 compression bits
  127. changeR = (bool)((byte >> (7 - i)) & 0x1);
  128. else
  129. {
  130. if (!addBitZuFarbe(
  131. (unsigned char)(byte >> (7 - i)))) // The bit belongs to
  132. // the color
  133. return (unsigned char)i;
  134. }
  135. break;
  136. case 2: // The third bit of a pixel stores either the
  137. // compression type or already the color
  138. if (change) // The bit belongs to the 7 compression bits
  139. changeG = (bool)((byte >> (7 - i)) & 0x1);
  140. else
  141. {
  142. if (!addBitZuFarbe(
  143. (unsigned char)(byte >> (7 - i)))) // The bit belongs to
  144. // the color
  145. return (unsigned char)i;
  146. }
  147. break;
  148. case 3: // The fourth bit of a pixel stores either the
  149. // compression type or already the color
  150. if (change) // The bit belongs to the 7 compression bits
  151. changeB = (bool)((byte >> (7 - i)) & 0x1);
  152. else
  153. {
  154. if (!addBitZuFarbe(
  155. (unsigned char)(byte >> (7 - i)))) // The bit belongs to
  156. // the color
  157. return (unsigned char)i;
  158. }
  159. break;
  160. case 4: // The fifth bit of a pixel stores either the
  161. // compression type or already the color
  162. if (change) // The bit belongs to the 7 compression bits
  163. changeA = (bool)((byte >> (7 - i)) & 0x1);
  164. else
  165. {
  166. if (!addBitZuFarbe(
  167. (unsigned char)(byte >> (7 - i)))) // The bit belongs to
  168. // the color
  169. return (unsigned char)i;
  170. }
  171. break;
  172. case 5: // The sixth bit of a pixel stores either the
  173. // compression type or already the color
  174. if (change) // The bit belongs to the 7 compression bits
  175. komp = (unsigned char)(komp
  176. | ((unsigned char)((byte >> (7 - i))
  177. & 0x1)
  178. << 2))
  179. & 7;
  180. else
  181. {
  182. if (!addBitZuFarbe(
  183. (unsigned char)(byte >> (7 - i)))) // The bit belongs to
  184. // the color
  185. return (unsigned char)i;
  186. }
  187. break;
  188. case 6: // The seventh bit of a pixel stores either the
  189. // compression type or already the color
  190. if (change) // The bit belongs to the 7 compression bits
  191. komp = (unsigned char)(komp
  192. | ((unsigned char)((byte >> (7 - i))
  193. & 0x1)
  194. << 1))
  195. & 7;
  196. else
  197. {
  198. if (!addBitZuFarbe(
  199. (unsigned char)(byte >> (7 - i)))) // The bit belongs to
  200. // the color
  201. return (unsigned char)i;
  202. }
  203. break;
  204. case 7: // The eighth bit of a pixel stores either the
  205. // compression type or already the color
  206. if (change) // The bit belongs to the 7 compression bits
  207. {
  208. komp = (unsigned char)(komp | ((byte >> (7 - i)) & 0x1)) & 7;
  209. // That was the last compression bit
  210. // Evaluate compression
  211. miR = (char)(miR - komp), miG = (char)(miG - komp),
  212. miB = (char)(miB - komp), miA = (char)(miA - komp);
  213. if (!changeR) R = davor->getR();
  214. if (!changeG) G = davor->getG();
  215. if (!changeB) B = davor->getB();
  216. if (!changeA) A = davor->getA();
  217. maxIndex
  218. = (char)(maxIndex
  219. + (changeR + changeG + changeB + changeA)
  220. * (8 - komp)); // Bit length of the pixel
  221. }
  222. else
  223. {
  224. if (!addBitZuFarbe(
  225. (unsigned char)(byte >> (7 - i)))) // The bit belongs to
  226. // the color
  227. return (unsigned char)i;
  228. }
  229. break;
  230. default: // The remaining bits all store the color values of the pixel
  231. if (!addBitZuFarbe((
  232. unsigned char)(byte
  233. >> (7 - i)))) // The bit belongs to the color
  234. return (unsigned char)i;
  235. break;
  236. }
  237. ++index;
  238. if (index >= maxIndex)
  239. {
  240. if (davor)
  241. {
  242. if (changeR) R = (unsigned char)(davor->getR() + R);
  243. if (changeG) G = (unsigned char)(davor->getG() + G);
  244. if (changeB) B = (unsigned char)(davor->getB() + B);
  245. if (changeA) A = (unsigned char)(davor->getA() + A);
  246. davor = (LTDBPixel*)davor->release();
  247. }
  248. return (char)(i + 1);
  249. }
  250. }
  251. return -1;
  252. }
  253. void LTDBPixel::setFarbe(int f) // sets the color of the pixel
  254. { // Since this function is called, the class is now to be used for saving
  255. // Reset values in case the class was already used for loading or saving
  256. index = 0, maxIndex = 1;
  257. change = 0, changeR = 0, changeG = 0, changeB = 0, changeA = 0, komp = 0;
  258. iR = 0, iG = 0, iB = 0, iA = 0;
  259. miR = 8, miG = 8, miB = 8, miA = 8;
  260. // Set color values
  261. R = (unsigned char)(f >> 16);
  262. G = (unsigned char)(f >> 8);
  263. B = (unsigned char)f;
  264. A = (unsigned char)(f >> 24);
  265. }
  266. void LTDBPixel::komprimieren() // Compresses the pixel
  267. {
  268. maxIndex = 1;
  269. if (!davor)
  270. { // This is the first pixel
  271. change = 1;
  272. changeR = 1;
  273. changeG = 1;
  274. changeB = 1;
  275. changeA = 1;
  276. maxIndex = (char)(maxIndex + 7);
  277. miR = (char)getBits(R);
  278. miG = (char)getBits(G);
  279. miB = (char)getBits(B);
  280. miA = (char)getBits(A);
  281. }
  282. else
  283. { // The difference to the previous pixel is stored
  284. miR = (char)getBits((char)(R - davor->getR()));
  285. miG = (char)getBits((char)(G - davor->getG()));
  286. miB = (char)getBits((char)(B - davor->getB()));
  287. miA = (char)getBits((char)(A - davor->getA()));
  288. changeR = R != davor->getR();
  289. changeG = G != davor->getG();
  290. changeB = B != davor->getB();
  291. changeA = A != davor->getA();
  292. } // Check if something changes
  293. if (!miR && changeR) ++miR;
  294. if (!miG && changeG) ++miG;
  295. if (!miB && changeB) ++miB;
  296. if (!miA && changeA) ++miA;
  297. int k = (miR > miG ? miR : miG);
  298. k = (k > miB ? k : miB);
  299. k = (k > miA ? k : miA);
  300. miR = (char)k, miG = (char)k, miB = (char)k, miA = (char)k;
  301. komp = (unsigned char)(8 - k) & 7;
  302. maxIndex = (char)(maxIndex + (changeR + changeG + changeB + changeA) * k);
  303. if (davor)
  304. {
  305. if (changeR != davor->getChangeR() || changeG != davor->getChangeG()
  306. || changeB != davor->getChangeB() || changeA != davor->getChangeA()
  307. || komp != davor->getKomp())
  308. { // Something changes
  309. change = 1;
  310. maxIndex = (char)(maxIndex + 7);
  311. }
  312. else
  313. { // Nothing changes
  314. change = 0;
  315. }
  316. }
  317. }
  318. bool LTDBPixel::getNextFarbeBit(
  319. char& byte, int i) // Stores the next color bit in byte
  320. {
  321. unsigned char RR = R;
  322. unsigned char GG = G;
  323. unsigned char BB = B;
  324. unsigned char AA = A;
  325. if (davor)
  326. {
  327. RR = (unsigned char)(RR - davor->getR());
  328. GG = (unsigned char)(GG - davor->getG());
  329. BB = (unsigned char)(BB - davor->getB());
  330. AA = (unsigned char)(AA - davor->getA());
  331. }
  332. if (changeR && iR != miR) // The bit belongs to red
  333. {
  334. byte |= (char)(((RR >> (7 - komp - iR)) & 0x1) << (7 - i));
  335. ++iR;
  336. }
  337. else if (changeG && iG != miG) // The bit belongs to green
  338. {
  339. byte |= (char)(((GG >> (7 - komp - iG)) & 0x1) << (7 - i));
  340. ++iG;
  341. }
  342. else if (changeB && iB != miB) // The bit belongs to blue
  343. {
  344. byte |= (char)(((BB >> (7 - komp - iB)) & 0x1) << (7 - i));
  345. ++iB;
  346. }
  347. else if (changeA && iA != miA) // The bit belongs to alpha
  348. {
  349. byte |= (char)(((AA >> (7 - komp - iA)) & 0x1) << (7 - i));
  350. ++iA;
  351. }
  352. else // The pixel is already finished
  353. return false;
  354. return true;
  355. }
  356. char LTDBPixel::getNextByte(
  357. char& byte, int begin) // Returns the next bits, -1 if the pixel
  358. // is not finished
  359. {
  360. // begin specifies where to write in the byte variable
  361. // the function returns the end of the pixel in the byte variable
  362. // -1 means the pixel is not finished
  363. for (int i = begin; i < 8; ++i)
  364. {
  365. switch (index)
  366. {
  367. case 0: // The first bit of the pixel stores whether the
  368. // compression changes
  369. byte |= (char)(((int)change & 0x1) << (7 - i));
  370. break;
  371. case 1: // The second bit of the pixel stores either the color
  372. // or the compression
  373. if (change) // Compression
  374. byte |= (char)(((int)changeR & 0x1) << (7 - i));
  375. else // Color
  376. {
  377. if (!getNextFarbeBit(byte, i)) return (char)i;
  378. }
  379. break;
  380. case 2: // The third bit of the pixel stores either the color
  381. // or the compression
  382. if (change) // Compression
  383. byte |= (char)(((int)changeG & 0x1) << (7 - i));
  384. else // Color
  385. {
  386. if (!getNextFarbeBit(byte, i)) return (char)i;
  387. }
  388. break;
  389. case 3: // The fourth bit of the pixel stores either the color
  390. // or the compression
  391. if (change) // Compression
  392. byte |= (char)(((int)changeB & 0x1) << (7 - i));
  393. else // Color
  394. {
  395. if (!getNextFarbeBit(byte, i)) return (char)i;
  396. }
  397. break;
  398. case 4: // The fifth bit of the pixel stores either the color
  399. // or the compression
  400. if (change) // Compression
  401. byte |= (char)(((int)changeA & 0x1) << (7 - i));
  402. else // Color
  403. {
  404. if (!getNextFarbeBit(byte, i)) return (char)i;
  405. }
  406. break;
  407. case 5: // The sixth bit of the pixel stores either the color
  408. // or the compression
  409. if (change) // Compression
  410. byte |= (char)(((komp >> 2) & 0x1) << (7 - i));
  411. else // Color
  412. {
  413. if (!getNextFarbeBit(byte, i)) return (char)i;
  414. }
  415. break;
  416. case 6: // The seventh bit of the pixel stores either the color
  417. // or the compression
  418. if (change) // Compression
  419. byte |= (char)(((komp >> 1) & 0x1) << (7 - i));
  420. else // Color
  421. {
  422. if (!getNextFarbeBit(byte, i)) return (char)i;
  423. }
  424. break;
  425. case 7: // The eighth bit of the pixel stores either the color
  426. // or the compression
  427. if (change) // Compression
  428. byte |= (char)((komp & 0x1) << (7 - i));
  429. else // Color
  430. {
  431. if (!getNextFarbeBit(byte, i)) return (char)i;
  432. }
  433. break;
  434. default: // The remaining bits store the color of the pixel
  435. if (!getNextFarbeBit(byte, i)) return (char)i;
  436. break;
  437. }
  438. ++index;
  439. if (index >= maxIndex)
  440. {
  441. if (davor) davor = (LTDBPixel*)davor->release();
  442. return (char)(i + 1);
  443. }
  444. }
  445. return -1;
  446. }
  447. // constant
  448. int LTDBPixel::zuFarbe() const // returns the pixel as color
  449. {
  450. return (((int)R << 16) | ((int)G << 8) | (int)B | ((int)A << 24));
  451. }
  452. bool LTDBPixel::getChangeR() const
  453. {
  454. return changeR;
  455. }
  456. bool LTDBPixel::getChangeG() const
  457. {
  458. return changeG;
  459. }
  460. bool LTDBPixel::getChangeB() const
  461. {
  462. return changeB;
  463. }
  464. bool LTDBPixel::getChangeA() const
  465. {
  466. return changeA;
  467. }
  468. unsigned char LTDBPixel::getKomp() const
  469. {
  470. return komp;
  471. }
  472. unsigned char LTDBPixel::getR() const // returns red
  473. {
  474. return R;
  475. }
  476. unsigned char LTDBPixel::getG() const // returns green
  477. {
  478. return G;
  479. }
  480. unsigned char LTDBPixel::getB() const // returns blue
  481. {
  482. return B;
  483. }
  484. unsigned char LTDBPixel::getA() const // returns alpha
  485. {
  486. return A;
  487. }
  488. // Content of the LTDBDateiKopf class from Dateisystem.h
  489. // Constructor
  490. LTDBDateiKopf::LTDBDateiKopf()
  491. : ReferenceCounter(),
  492. bilder(new RCArray<Text>()),
  493. pos(new Array<__int64>()),
  494. bAnzahl(0)
  495. {}
  496. // Destructor
  497. LTDBDateiKopf::~LTDBDateiKopf()
  498. {
  499. bilder->release();
  500. pos->release();
  501. }
  502. // non-constant
  503. void LTDBDateiKopf::removeBild(int i)
  504. {
  505. if (i >= bAnzahl) return;
  506. bilder->remove(i);
  507. pos->remove(i);
  508. --bAnzahl;
  509. }
  510. void LTDBDateiKopf::removeBild(Text* txt)
  511. {
  512. int i = -1;
  513. for (int ii = 0; ii < bAnzahl; ++ii)
  514. {
  515. Text* b = bilder->z(ii);
  516. if (b->istGleich(txt->getText()))
  517. {
  518. i = ii;
  519. break;
  520. }
  521. }
  522. txt->release();
  523. if (i == -1) return;
  524. bilder->remove(i);
  525. pos->remove(i);
  526. --bAnzahl;
  527. }
  528. void LTDBDateiKopf::addBild(Text* txt)
  529. {
  530. bilder->add(txt, bAnzahl);
  531. pos->add(0, bAnzahl);
  532. ++bAnzahl;
  533. }
  534. void LTDBDateiKopf::setBildPos(int i, __int64 pos)
  535. {
  536. this->pos->set(pos, i);
  537. }
  538. void LTDBDateiKopf::setBildPos(Text* txt, __int64 pos)
  539. {
  540. int i = -1;
  541. for (int ii = 0; ii < bAnzahl; ++ii)
  542. {
  543. Text* b = bilder->z(ii);
  544. if (b->istGleich(txt->getText()))
  545. {
  546. i = ii;
  547. break;
  548. }
  549. }
  550. txt->release();
  551. if (i == -1) return;
  552. this->pos->set(pos, i);
  553. }
  554. void LTDBDateiKopf::laden(FBalken* f, std::ifstream* inF)
  555. {
  556. if (inF->is_open() && inF->good())
  557. {
  558. char b = 0;
  559. inF->read(&b, 1);
  560. bAnzahl = b << 8;
  561. inF->read(&b, 1);
  562. bAnzahl |= b & 0xFF;
  563. #ifdef WIN32
  564. if (f)
  565. {
  566. f->reset();
  567. f->setAktionAnzahl(bAnzahl);
  568. }
  569. #endif
  570. bilder->leeren();
  571. pos->leeren();
  572. for (int i = 0; i < bAnzahl; ++i)
  573. {
  574. LTDBKopf* kpf = new LTDBKopf();
  575. kpf->laden(inF); // image name and half file position
  576. bilder->add(kpf->getTitel()); // sets title
  577. Punkt gr = kpf->getSize();
  578. kpf->release();
  579. char p[5];
  580. inF->read((char*)p, 5); // other half of the file position
  581. unsigned __int64 position
  582. = (((__int64)gr.x << 52) & 0xFFF0000000000000)
  583. | (((__int64)gr.y << 40) & 0xFFF0000000000)
  584. | (((__int64)p[0] << 32) & 0xFF00000000)
  585. | (((__int64)p[1] << 24) & 0xFF000000)
  586. | (((__int64)p[2] << 16) & 0xFF0000)
  587. | (((__int64)p[3] << 8) & 0xFF00) | ((__int64)p[4] & 0xFF);
  588. pos->add(position, i); // sets position
  589. #ifdef WIN32
  590. if (f) f->aktionPlus();
  591. #endif
  592. }
  593. }
  594. }
  595. // constant
  596. void LTDBDateiKopf::speichern(std::ofstream* outF) const
  597. {
  598. if (outF->is_open() && outF->good())
  599. {
  600. char b = (char)(bAnzahl >> 8);
  601. outF->write(&b, 1);
  602. b = (char)bAnzahl;
  603. outF->write(&b, 1);
  604. for (int i = 0; i < bAnzahl; ++i)
  605. {
  606. LTDBKopf* kpf = new LTDBKopf();
  607. __int64 position = pos->get(i);
  608. kpf->Init(bilder->get(i),
  609. Punkt((int)(position >> 52), (int)(position >> 40)));
  610. kpf->speichern(outF);
  611. kpf->release();
  612. char p[] = {(char)(position >> 32),
  613. (char)(position >> 24),
  614. (char)(position >> 16),
  615. (char)(position >> 8),
  616. (char)(position)};
  617. outF->write((char*)p, 5);
  618. }
  619. }
  620. }
  621. Text* LTDBDateiKopf::getBild(int i) const
  622. {
  623. return bilder->get(i);
  624. }
  625. Text* LTDBDateiKopf::zBild(int i) const
  626. {
  627. return bilder->z(i);
  628. }
  629. __int64 LTDBDateiKopf::getBildPosition(Text* txt) const
  630. {
  631. int i = -1;
  632. for (int ii = 0; ii < bAnzahl; ++ii)
  633. {
  634. Text* b = bilder->z(ii);
  635. if (b->istGleich(txt->getText()))
  636. {
  637. i = ii;
  638. break;
  639. }
  640. }
  641. txt->release();
  642. if (i == -1) return -1;
  643. return pos->get(i);
  644. }
  645. __int64 LTDBDateiKopf::getBildPosition(int index) const
  646. {
  647. return pos->get(index);
  648. }
  649. int LTDBDateiKopf::getBildIndex(Text* txt) const
  650. {
  651. int i = -1;
  652. for (int ii = 0; ii < bAnzahl; ++ii)
  653. {
  654. Text* b = bilder->z(ii);
  655. if (b->istGleich(txt->getText()))
  656. {
  657. i = ii;
  658. break;
  659. }
  660. }
  661. txt->release();
  662. return i;
  663. }
  664. int LTDBDateiKopf::getbAnzahl() const
  665. {
  666. return bAnzahl;
  667. }
  668. RCArray<Text>* LTDBDateiKopf::zBildListe() const
  669. {
  670. return bilder;
  671. }
  672. // Content of the LTDBKopf class from DateiSystem.h
  673. // Constructor
  674. LTDBKopf::LTDBKopf()
  675. : ReferenceCounter()
  676. {}
  677. // non-constant
  678. void LTDBKopf::laden(std::ifstream* f) // Loads data from a file
  679. {
  680. if (f->is_open())
  681. {
  682. a = 0;
  683. b = 0;
  684. c = 0;
  685. char aa = 0;
  686. f->read(&aa, 1);
  687. int tl = (aa >> 4) & Bits(4);
  688. int BitAnzahl = 4 + tl * 5 + 24;
  689. f->seekg(-1, std::ios::cur);
  690. int Bytes = BitAnzahl / 8;
  691. if (((float)BitAnzahl / 8.0f) != (float)Bytes) ++Bytes;
  692. char byte = 0;
  693. for (int i = 0; i < Bytes; ++i)
  694. {
  695. f->read(&byte, 1);
  696. setBits(i * 8, i * 8 + 8, byte);
  697. }
  698. }
  699. }
  700. int LTDBKopf::Init(Text* t, const Punkt& g) // Fills the data
  701. {
  702. a = 0;
  703. b = 0;
  704. c = 0;
  705. int tl = t->getLength();
  706. if (tl > 15) tl = 15;
  707. char* titel = new char[tl];
  708. int skipped = 0;
  709. for (int i = 0; i < tl; ++i)
  710. {
  711. titel[i - skipped] = t->getText()[i];
  712. if (titel[i - skipped] > 96 && titel[i - skipped] < 123)
  713. titel[i - skipped] = (char)(titel[i - skipped] - 96);
  714. else if (titel[i - skipped] > 64 && titel[i - skipped] < 91)
  715. titel[i - skipped] = (char)(titel[i - skipped] - 64);
  716. else if (titel[i - skipped] == SpecialCharacters::SMALL_UE
  717. || titel[i - skipped] == SpecialCharacters::BIG_UE)
  718. titel[i - skipped] = 27;
  719. else if (titel[i - skipped] == SpecialCharacters::SMALL_OE
  720. || titel[i - skipped] == SpecialCharacters::BIG_OE)
  721. titel[i - skipped] = 28;
  722. else if (titel[i - skipped] == SpecialCharacters::SMALL_AE
  723. || titel[i - skipped] == SpecialCharacters::BIG_AE)
  724. titel[i - skipped] = 29;
  725. else if (titel[i - skipped] == SpecialCharacters::SZ)
  726. titel[i - skipped] = 30;
  727. else if (titel[i - skipped] == '.')
  728. titel[i - skipped] = 31;
  729. else
  730. ++skipped;
  731. }
  732. a = (__int64)((tl - skipped) & Bits(4)) << 60;
  733. int BeginBit = 4;
  734. for (int i = 0; i < tl - skipped; ++i)
  735. {
  736. BeginBit += 5;
  737. switch (i)
  738. {
  739. case 0:
  740. a |= (__int64)(titel[i] & 31) << 55; // store 1st character
  741. break;
  742. case 1:
  743. a |= (__int64)(titel[i] & 31) << 50; // store 2nd character
  744. break;
  745. case 2:
  746. a |= (__int64)(titel[i] & 31) << 45; // store 3rd character
  747. break;
  748. case 3:
  749. a |= (__int64)(titel[i] & 31) << 40; // store 4th character
  750. break;
  751. case 4:
  752. a |= (__int64)(titel[i] & 31)
  753. << 35; // store 5th character
  754. break;
  755. case 5:
  756. a |= (__int64)(titel[i] & 31)
  757. << 30; // store 6th character
  758. break;
  759. case 6:
  760. a |= (__int64)(titel[i] & 31) << 25; // store 7th character
  761. break;
  762. case 7:
  763. a |= (__int64)(titel[i] & 31) << 20; // store 8th character
  764. break;
  765. case 8:
  766. a |= (__int64)(titel[i] & 31) << 15; // store 9th character
  767. break;
  768. case 9:
  769. a |= (__int64)(titel[i] & 31) << 10; // store 10th character
  770. break;
  771. case 10:
  772. a |= (__int64)(titel[i] & 31) << 5; // store 11th character
  773. break;
  774. case 11:
  775. a |= (__int64)(titel[i] & 31); // store 12th character
  776. break;
  777. case 12:
  778. b |= (__int32)(titel[i] & 31)
  779. << 27; // store 13th character
  780. break;
  781. case 13:
  782. b |= (__int32)(titel[i] & 31)
  783. << 22; // store 14th character
  784. break;
  785. case 14:
  786. b |= (__int32)(titel[i] & 31)
  787. << 17; // store 15th character
  788. break;
  789. }
  790. }
  791. __int16 grx = (short)(g.x & Bits(12));
  792. __int16 gry = (short)(g.y & Bits(12));
  793. int EndBit = BeginBit + 24;
  794. setBits(BeginBit, EndBit - 12, grx);
  795. setBits(BeginBit + 12, EndBit, gry);
  796. t->release();
  797. delete[] titel;
  798. return skipped;
  799. }
  800. void LTDBKopf::setBits(int BeginBit, int EndBit, __int16 bits)
  801. {
  802. if (EndBit - BeginBit > 16) EndBit = BeginBit + 16;
  803. if (BeginBit < 64)
  804. {
  805. if (EndBit < 64)
  806. {
  807. a |= ((__int64)bits & Bits(EndBit - BeginBit))
  808. << ((64 - BeginBit) - (EndBit - BeginBit));
  809. }
  810. else
  811. {
  812. a |= (((__int64)bits >> (EndBit - 64)) & Bits(64 - BeginBit));
  813. b |= ((__int32)bits & Bits(EndBit - 64)) << (32 - (EndBit - 64));
  814. }
  815. }
  816. else
  817. {
  818. if (BeginBit < 96)
  819. {
  820. if (EndBit < 96)
  821. {
  822. b |= ((__int32)bits & Bits(EndBit - BeginBit))
  823. << ((96 - BeginBit) - (EndBit - BeginBit));
  824. }
  825. else
  826. {
  827. b |= (((__int32)bits >> (EndBit - 96)) & Bits(96 - BeginBit));
  828. c = (char)(c
  829. | (((__int8)bits & Bits(EndBit - 96))
  830. << (8 - (EndBit - 96))));
  831. }
  832. }
  833. else
  834. {
  835. c = (char)(c
  836. | (((__int8)bits & Bits(EndBit - BeginBit))
  837. << (8 - (EndBit - BeginBit))));
  838. }
  839. }
  840. }
  841. // constant
  842. void LTDBKopf::speichern(
  843. std::ofstream* f) const // Saves data to a file
  844. {
  845. if (f->is_open())
  846. {
  847. int bits = 4 /*title length*/ + getTitelLength() * 5 /*title*/
  848. + 24 /*image size*/;
  849. int bytes = bits / 8; // byte length of the file header
  850. if (((float)bits / 8.0f) != (float)bytes) ++bytes;
  851. char c = 0;
  852. for (int i = 0; i < bytes; ++i)
  853. {
  854. c = (char)getBits(i * 8, i * 8 + 8);
  855. f->write(&c, 1);
  856. }
  857. }
  858. }
  859. int LTDBKopf::getTitelLength() const // returns the length of the image name
  860. {
  861. return (int)(a >> 60 & Bits(4)); // The title length is stored in the first
  862. // 4 bits of the file
  863. }
  864. Text* LTDBKopf::getTitel() const // returns the name of the image
  865. {
  866. Text* ret = new Text("");
  867. char c[2];
  868. c[1] = '\0';
  869. int l = getTitelLength();
  870. for (int i = 0; i < l; ++i)
  871. {
  872. c[0] = 0;
  873. switch (i)
  874. {
  875. case 0:
  876. c[0] = (a >> 55) & 31; // get 1st character
  877. break;
  878. case 1:
  879. c[0] = (a >> 50) & 31; // get 2nd character
  880. break;
  881. case 2:
  882. c[0] = (a >> 45) & 31; // get 3rd character
  883. break;
  884. case 3:
  885. c[0] = (a >> 40) & 31; // get 4th character
  886. break;
  887. case 4:
  888. c[0] = (a >> 35) & 31; // get 5th character
  889. break;
  890. case 5:
  891. c[0] = (a >> 30) & 31; // get 6th character
  892. break;
  893. case 6:
  894. c[0] = (a >> 25) & 31; // get 7th character
  895. break;
  896. case 7:
  897. c[0] = (a >> 20) & 31; // get 8th character
  898. break;
  899. case 8:
  900. c[0] = (a >> 15) & 31; // get 9th character
  901. break;
  902. case 9:
  903. c[0] = (a >> 10) & 31; // get 10th character
  904. break;
  905. case 10:
  906. c[0] = (a >> 5) & 31; // get 11th character
  907. break;
  908. case 11:
  909. c[0] = a & 31; // get 12th character
  910. break;
  911. case 12:
  912. c[0] = (char)((b >> 27) & 31); // get 13th character
  913. break;
  914. case 13:
  915. c[0] = (char)((b >> 22) & 31); // get 14th character
  916. break;
  917. case 14:
  918. c[0] = (char)((b >> 17) & 31); // get 15th character
  919. break;
  920. }
  921. if (c[0] == 27)
  922. c[0] = SpecialCharacters::SMALL_UE;
  923. else if (c[0] == 28)
  924. c[0] = SpecialCharacters::SMALL_OE;
  925. else if (c[0] == 29)
  926. c[0] = SpecialCharacters::SMALL_AE;
  927. else if (c[0] == 30)
  928. c[0] = SpecialCharacters::SZ;
  929. else if (c[0] == 31)
  930. c[0] = '.';
  931. else
  932. c[0] = (char)(c[0] + 96);
  933. ret->append(c);
  934. }
  935. return ret;
  936. }
  937. Punkt LTDBKopf::getSize() const // returns the size of the image
  938. {
  939. int BeginBit = 4 /*title length*/ + getTitelLength() * 5 /*title*/;
  940. int EndBit = BeginBit + 24;
  941. __int16 grx = getBits(BeginBit, EndBit - 12);
  942. __int16 gry = getBits(BeginBit + 12, EndBit);
  943. return Punkt((int)(grx & Bits(12)), (int)(gry & Bits(12)));
  944. }
  945. __int16 LTDBKopf::getBits(int begin,
  946. int ende) const // returns the bits from begin to ende (excluding ende)
  947. {
  948. if (ende < begin) return 0;
  949. if (ende - begin > 16) ende = begin + 16;
  950. __int16 ret = 0;
  951. if (begin < 64)
  952. {
  953. if (ende < 64)
  954. {
  955. ret = (__int16)(a >> ((64 - begin) - (ende - begin))
  956. & Bits(ende - begin));
  957. }
  958. else
  959. {
  960. ret = (__int16)((a & Bits(64 - begin)) << (ende - 64));
  961. ret = (__int16)(ret
  962. | ((b >> (32 - (ende - 64))) & Bits(ende - 64)));
  963. }
  964. }
  965. else
  966. {
  967. if (begin < 96)
  968. {
  969. if (ende < 96)
  970. {
  971. ret = (__int16)(b >> ((96 - begin) - (ende - begin))
  972. & Bits(ende - begin));
  973. }
  974. else
  975. {
  976. ret = (__int16)((b & Bits(96 - begin)) << (ende - 96));
  977. ret = (__int16)(ret
  978. | ((c >> (8 - (ende - 96))) & Bits(ende - 96)));
  979. }
  980. }
  981. else
  982. {
  983. ret = (__int16)(c >> ((104 - begin) - (ende - begin))
  984. & Bits(ende - begin));
  985. }
  986. }
  987. return ret;
  988. }
  989. // Content of the LTDBBody class from Dateisystem.h
  990. // Constructor
  991. LTDBBody::LTDBBody()
  992. : ReferenceCounter(),
  993. gr(0, 0),
  994. b(new Bild())
  995. {}
  996. LTDBBody::LTDBBody(LTDBKopf* k) // calls init
  997. : ReferenceCounter(),
  998. gr(0, 0),
  999. b(new Bild())
  1000. {
  1001. init(k);
  1002. }
  1003. // Destructor
  1004. LTDBBody::~LTDBBody()
  1005. {
  1006. b->release();
  1007. }
  1008. // non-constant
  1009. void LTDBBody::init(LTDBKopf k) // Initializes, required before loading
  1010. {
  1011. gr = k.getSize();
  1012. int l = k.getTitelLength();
  1013. l = 4 + l * 5 + 24;
  1014. dateiSize = (l / 8.0 == l) ? (l / 8) : (l / 8 + 1);
  1015. }
  1016. void LTDBBody::init(LTDBKopf* k) // Initializes, required before loading
  1017. {
  1018. gr = k->getSize();
  1019. int l = k->getTitelLength();
  1020. l = 4 + l * 5 + 24;
  1021. dateiSize = (l / 8.0 == l) ? (l / 8) : (l / 8 + 1);
  1022. k->release();
  1023. }
  1024. void LTDBBody::laden(FBalken* zF, std::ifstream* inF) // loads the image
  1025. {
  1026. b->neuBild(gr.x, gr.y, 0xFF000000); // create new image
  1027. int* buff = b->getBuffer();
  1028. int breite = b->getBreite();
  1029. char byte = 0;
  1030. int index = 0;
  1031. LTDBPixel* davor = 0; // last loaded pixel
  1032. LTDBPixel* dieser = new LTDBPixel(0); // pixel currently being loaded
  1033. int begin = 0; // pixel start, end position in the byte variable
  1034. #ifdef WIN32
  1035. if (zF)
  1036. {
  1037. zF->reset();
  1038. zF->setAktionAnzahl(gr.x * gr.y);
  1039. }
  1040. #endif
  1041. while (index < gr.x * gr.y) // for each pixel
  1042. {
  1043. if (!dieser) // if it is not the first pixel
  1044. dieser = new LTDBPixel(dynamic_cast<LTDBPixel*>(davor->getThis()));
  1045. int ende = -1;
  1046. while (ende < 0) // load pixel
  1047. {
  1048. if (begin == 0) inF->read(&byte, 1);
  1049. ende = dieser->addByte(byte, (char)begin); // evaluate byte
  1050. begin = 0;
  1051. }
  1052. begin = ende;
  1053. if (begin == 8) begin = 0;
  1054. buff[(index % gr.x) + (index / gr.x) * breite] = dieser->zuFarbe();
  1055. if (davor) davor = (LTDBPixel*)davor->release();
  1056. davor = dynamic_cast<LTDBPixel*>(dieser->getThis());
  1057. dieser = (LTDBPixel*)dieser->release();
  1058. ++index;
  1059. #ifdef WIN32
  1060. if (zF) zF->aktionPlus();
  1061. #endif
  1062. }
  1063. if (davor) davor = (LTDBPixel*)davor->release();
  1064. }
  1065. void LTDBBody::setBild(Bild* b) // sets the image to be saved
  1066. {
  1067. this->b->release();
  1068. this->b = b;
  1069. }
  1070. // constant
  1071. void LTDBBody::speichern(
  1072. FBalken* zF, std::ofstream* outF) const // saves image
  1073. {
  1074. if (outF->is_open())
  1075. {
  1076. LTDBPixel* letzter = 0; // Last saved pixel
  1077. LTDBPixel* dieser
  1078. = new LTDBPixel(0); // The pixel currently being saved
  1079. int begin = 0,
  1080. ende = 0; // pixel start, end position in the byte variable
  1081. char byte = 0; // The next byte of the file
  1082. bool w = 0;
  1083. #ifdef WIN32
  1084. if (zF)
  1085. {
  1086. zF->reset();
  1087. zF->setAktionAnzahl(gr.x * gr.y);
  1088. }
  1089. #endif
  1090. int* pBuff = b->getBuffer();
  1091. for (int i = 0; i < gr.x * gr.y; ++i) // for each pixel
  1092. {
  1093. if (!dieser) // if it is not the first pixel
  1094. dieser = new LTDBPixel(
  1095. dynamic_cast<LTDBPixel*>(letzter->getThis()));
  1096. dieser->setFarbe(pBuff[i]); // set pixel color
  1097. dieser->komprimieren(); // compress pixel
  1098. ende = -1;
  1099. while (ende < 0) // fill byte
  1100. {
  1101. ende = dieser->getNextByte(byte, begin);
  1102. begin = 0;
  1103. w = 0;
  1104. if (ende == -1 || ende == 8) // save byte
  1105. {
  1106. outF->write(&byte, 1);
  1107. w = 1;
  1108. byte = 0;
  1109. }
  1110. } // pixel finished
  1111. begin = ende;
  1112. if (begin == 8) begin = 0;
  1113. if (letzter) letzter->release();
  1114. letzter = dynamic_cast<LTDBPixel*>(
  1115. dieser->getThis()); // dieser becomes letzter
  1116. dieser = (LTDBPixel*)dieser->release();
  1117. #ifdef WIN32
  1118. if (zF) zF->aktionPlus();
  1119. #endif
  1120. }
  1121. if (letzter) letzter = (LTDBPixel*)letzter->release();
  1122. if (!w)
  1123. {
  1124. outF->write(&byte, 1); // save the last byte
  1125. }
  1126. outF->flush(); // flush file stream
  1127. }
  1128. }
  1129. Bild* LTDBBody::getBild() const // returns the loaded image
  1130. {
  1131. return dynamic_cast<Bild*>(b->getThis());
  1132. }
  1133. const Punkt& LTDBBody::getSize() const // returns the size of the image
  1134. {
  1135. return gr;
  1136. }
  1137. // Content of the LTDBDatei class from Dateisystem.h
  1138. // Constructor
  1139. LTDBDatei::LTDBDatei()
  1140. : ReferenceCounter(),
  1141. pfad(new Text()),
  1142. datKpf(0)
  1143. {}
  1144. // Destructor
  1145. LTDBDatei::~LTDBDatei()
  1146. {
  1147. if (pfad) pfad->release();
  1148. if (datKpf) datKpf->release();
  1149. }
  1150. // non-constant
  1151. void LTDBDatei::setDatei(Text* pfad) // Sets the path to the file
  1152. { // Delete values of any previous file
  1153. if (datKpf) datKpf = (LTDBDateiKopf*)datKpf->release();
  1154. // Set path
  1155. this->pfad->setText(*pfad);
  1156. pfad->release();
  1157. }
  1158. void LTDBDatei::erstellen() // Creates the file
  1159. {
  1160. DateiPfadErstellen(dynamic_cast<Text*>(pfad->getThis()));
  1161. std::ofstream* outF = new std::ofstream(pfad->getText(), std::ios::binary);
  1162. int i = 0;
  1163. outF->write((char*)&i, 2);
  1164. delete outF;
  1165. }
  1166. void LTDBDatei::leseDaten(
  1167. FBalken* f) // The class briefly reads all images and remembers
  1168. // where in the file each one is located
  1169. { // This function will take some time, but will make
  1170. // the other functions faster afterwards
  1171. if (DateiExistiert(dynamic_cast<Text*>(pfad->getThis())))
  1172. {
  1173. if (datKpf) datKpf->release();
  1174. datKpf = new LTDBDateiKopf();
  1175. std::ifstream* inF
  1176. = new std::ifstream(pfad->getText(), std::ios::binary);
  1177. datKpf->laden(f, inF);
  1178. delete inF;
  1179. }
  1180. }
  1181. void LTDBDatei::remove() // Deletes the file
  1182. {
  1183. if (DateiExistiert(dynamic_cast<Text*>(pfad->getThis())))
  1184. {
  1185. DateiRemove(dynamic_cast<Text*>(pfad->getThis()));
  1186. if (datKpf) datKpf->release();
  1187. }
  1188. }
  1189. void LTDBDatei::remove(FBalken* f, Text* name) // Deletes an image from the file
  1190. {
  1191. if (DateiExistiert(dynamic_cast<Text*>(pfad->getThis())) && name)
  1192. {
  1193. if (!datKpf) leseDaten(0); // read data
  1194. // Check if file is not present
  1195. if (!datKpf)
  1196. {
  1197. name->release();
  1198. return;
  1199. }
  1200. int index = datKpf->getBildIndex(dynamic_cast<Text*>(name->getThis()));
  1201. if (index == -1) // the image does not exist
  1202. {
  1203. name->release();
  1204. return;
  1205. }
  1206. // Determine temporary file path
  1207. Text* pf_tmp = new Text(pfad->getText());
  1208. char c = '0';
  1209. pf_tmp->append("0");
  1210. for (int i = 0; DateiExistiert(dynamic_cast<Text*>(pf_tmp->getThis()));
  1211. ++i)
  1212. {
  1213. c = (char)('0' + (i % 10));
  1214. if ((i % 10) == 0) pf_tmp->append("$");
  1215. pf_tmp->ersetzen(
  1216. pf_tmp->anzahlVon((char)('0' - ((i - 1) % 10))) - 1,
  1217. (char)('0' - ((i - 1) % 10)),
  1218. c);
  1219. }
  1220. std::ifstream* inF = new std::ifstream(
  1221. pfad->getText(), std::ios::binary); // open old file
  1222. inF->seekg(0, std::ios::end);
  1223. __int64 datlen = inF->tellg();
  1224. inF->seekg(0, std::ios::beg);
  1225. std::ofstream* outF = new std::ofstream(
  1226. pf_tmp->getText(), std::ios::binary); // open new file
  1227. if (inF->is_open() && outF->is_open())
  1228. {
  1229. __int64 position = datKpf->getBildPosition(index);
  1230. datKpf->removeBild(index);
  1231. datKpf->speichern(outF);
  1232. LTDBDateiKopf* kpf_tmp = new LTDBDateiKopf();
  1233. kpf_tmp->laden(0, inF);
  1234. kpf_tmp->release();
  1235. char byte = 0;
  1236. __int64 pos_minus = inF->tellg() - outF->tellp();
  1237. for (int i = 0; i < index; ++i)
  1238. datKpf->setBildPos(i, datKpf->getBildPosition(i) - pos_minus);
  1239. // Copy bytes up to the file
  1240. for (__int64 i = inF->tellg(); i < position; ++i)
  1241. {
  1242. inF->read(&byte, 1);
  1243. outF->write(&byte, 1);
  1244. } // skip image to be deleted
  1245. LTDBKopf* delkpf = new LTDBKopf();
  1246. delkpf->laden(inF);
  1247. LTDBBody* delkpr
  1248. = new LTDBBody(dynamic_cast<LTDBKopf*>(delkpf->getThis()));
  1249. delkpr->laden(f, inF);
  1250. delkpf = (LTDBKopf*)delkpf->release();
  1251. delkpr = (LTDBBody*)delkpr->release(); // copy remaining bytes
  1252. pos_minus = inF->tellg() - outF->tellp();
  1253. for (__int64 i = (__int64)inF->tellg(); i < datlen; ++i)
  1254. {
  1255. inF->read(&byte, 1);
  1256. outF->write(&byte, 1);
  1257. }
  1258. for (int i = index; i < datKpf->getbAnzahl(); ++i)
  1259. datKpf->setBildPos(i, datKpf->getBildPosition(i) - pos_minus);
  1260. outF->seekp(0, std::ios::beg);
  1261. datKpf->speichern(outF);
  1262. inF->close();
  1263. outF->close();
  1264. DateiRemove(dynamic_cast<Text*>(pfad->getThis()));
  1265. DateiUmbenennen(dynamic_cast<Text*>(pf_tmp->getThis()),
  1266. dynamic_cast<Text*>(pfad->getThis()));
  1267. }
  1268. delete inF;
  1269. delete outF;
  1270. pf_tmp = (Text*)pf_tmp->release();
  1271. }
  1272. if (name) name = (Text*)name->release();
  1273. }
  1274. Bild* LTDBDatei::laden(FBalken* f, Text* name) // Loads an image from the file
  1275. {
  1276. if (name)
  1277. {
  1278. if (!DateiExistiert(dynamic_cast<Text*>(pfad->getThis())))
  1279. {
  1280. name->release();
  1281. return 0;
  1282. }
  1283. if (!datKpf) leseDaten(0);
  1284. LTDBKopf* k_tmp = new LTDBKopf();
  1285. k_tmp->Init(dynamic_cast<Text*>(name->getThis()), Punkt(0, 0));
  1286. int index = datKpf->getBildIndex(k_tmp->getTitel());
  1287. k_tmp->release();
  1288. if (index == -1)
  1289. { // Error message
  1290. Text* fehler = new Text("The image ");
  1291. fehler->append(*name);
  1292. fehler->append(" was not found in the file\n");
  1293. fehler->append(*pfad);
  1294. fehler->append("!");
  1295. Logging::error() << fehler << "\n";
  1296. name->release();
  1297. return 0;
  1298. }
  1299. std::ifstream* inF
  1300. = new std::ifstream(pfad->getText(), std::ios::binary);
  1301. // Determine start of the file to load
  1302. __int64 position = datKpf->getBildPosition(index);
  1303. inF->seekg(position, std::ios::beg);
  1304. LTDBKopf* kpf = new LTDBKopf();
  1305. kpf->laden(inF);
  1306. Text* t = kpf->getTitel();
  1307. if (!t->istGleich(*name))
  1308. { // Error message
  1309. t->release();
  1310. kpf->release();
  1311. inF->close();
  1312. delete inF;
  1313. Text* fehler = new Text("The file ");
  1314. fehler->append(*pfad);
  1315. fehler->append(" is not a valid LTDB file!");
  1316. #ifdef WIN32
  1317. WMessageBox(0, new Text("Error"), fehler, MB_ICONERROR);
  1318. #endif
  1319. name->release();
  1320. return 0;
  1321. }
  1322. t->release();
  1323. LTDBBody* kpr = new LTDBBody(dynamic_cast<LTDBKopf*>(kpf->getThis()));
  1324. kpr->laden(f, inF); // load image
  1325. Bild* ret = kpr->getBild();
  1326. kpr->release();
  1327. kpf->release();
  1328. inF->close();
  1329. delete inF;
  1330. name->release();
  1331. return ret;
  1332. }
  1333. return 0;
  1334. }
  1335. int LTDBDatei::speichern(
  1336. FBalken* f, Bild* bild, Text* name) // Saves an image to the file
  1337. {
  1338. int warn = -1;
  1339. if (name && bild)
  1340. {
  1341. if (DateiExistiert(dynamic_cast<Text*>(pfad->getThis())))
  1342. {
  1343. if (!datKpf) leseDaten(0);
  1344. int index
  1345. = datKpf->getBildIndex(dynamic_cast<Text*>(name->getThis()));
  1346. if (index == -1)
  1347. {
  1348. warn = 0;
  1349. LTDBKopf* kpf = new LTDBKopf();
  1350. warn = kpf->Init(
  1351. dynamic_cast<Text*>(name->getThis()), bild->getSize());
  1352. if (datKpf->getBildIndex(kpf->getTitel()) != -1)
  1353. {
  1354. Logging::error()
  1355. << "An image with this name already exists!\n";
  1356. bild->release();
  1357. name->release();
  1358. kpf->release();
  1359. return -1;
  1360. }
  1361. // find temporary file path
  1362. Text* pf_tmp = new Text(pfad->getText());
  1363. char c = '0';
  1364. pf_tmp->append("0");
  1365. for (int i = 0;
  1366. DateiExistiert(dynamic_cast<Text*>(pf_tmp->getThis()));
  1367. ++i)
  1368. {
  1369. c = (char)('0' + (i % 10));
  1370. if ((i % 10) == 0) pf_tmp->append("$");
  1371. pf_tmp->ersetzen(
  1372. pf_tmp->anzahlVon((char)('0' - ((i - 1) % 10))) - 1,
  1373. (char)('0' - ((i - 1) % 10)),
  1374. c);
  1375. }
  1376. std::ifstream* inF
  1377. = new std::ifstream(pfad->getText(), std::ios::binary);
  1378. inF->seekg(0, std::ios::end);
  1379. __int64 datLen = inF->tellg();
  1380. inF->seekg(0, std::ios::beg);
  1381. std::ofstream* outF
  1382. = new std::ofstream(pf_tmp->getText(), std::ios::binary);
  1383. datKpf->addBild(kpf->getTitel());
  1384. index = datKpf->getBildIndex(kpf->getTitel());
  1385. datKpf->speichern(outF);
  1386. LTDBDateiKopf* kpf_tmp = new LTDBDateiKopf();
  1387. kpf_tmp->laden(0, inF);
  1388. kpf_tmp->release();
  1389. __int64 pos_plus = outF->tellp() - inF->tellg();
  1390. for (int i = 0; i < index; ++i)
  1391. datKpf->setBildPos(
  1392. i, datKpf->getBildPosition(i) + pos_plus);
  1393. datKpf->setBildPos(index, datLen + pos_plus);
  1394. outF->seekp(0, std::ios::beg);
  1395. datKpf->speichern(outF);
  1396. char byte = 0;
  1397. for (__int64 i = inF->tellg(); i < datLen; ++i)
  1398. {
  1399. inF->read(&byte, 1);
  1400. outF->write(&byte, 1);
  1401. }
  1402. kpf->speichern(outF); // save image header
  1403. LTDBBody* kpr
  1404. = new LTDBBody(dynamic_cast<LTDBKopf*>(kpf->getThis()));
  1405. kpr->setBild(dynamic_cast<Bild*>(bild->getThis()));
  1406. kpr->speichern(f, outF); // save image
  1407. kpf->release();
  1408. kpr->release();
  1409. inF->close();
  1410. outF->close();
  1411. delete inF;
  1412. delete outF;
  1413. DateiRemove(dynamic_cast<Text*>(pfad->getThis()));
  1414. DateiUmbenennen(
  1415. (Text*)pf_tmp, dynamic_cast<Text*>(pfad->getThis()));
  1416. }
  1417. }
  1418. }
  1419. if (name) name->release();
  1420. if (bild) bild->release();
  1421. return warn;
  1422. }
  1423. RCArray<Text>* LTDBDatei::zBildListe() // Lists all images in the file
  1424. {
  1425. if (!datKpf) leseDaten(0);
  1426. if (datKpf) return datKpf->zBildListe();
  1427. return 0;
  1428. }
  1429. // constant
  1430. Text* LTDBDatei::getPfad() const // Returns the path to the file
  1431. {
  1432. return dynamic_cast<Text*>(pfad->getThis());
  1433. }
  1434. int LTDBDatei::getBildAnzahl() const
  1435. {
  1436. if (!datKpf) return 0;
  1437. return datKpf->getbAnzahl();
  1438. }
  1439. bool LTDBDatei::istOffen() const // Checks if the file is open
  1440. {
  1441. if (!pfad) return 0;
  1442. return DateiExistiert(dynamic_cast<Text*>(pfad->getThis()));
  1443. }
  1444. #ifdef WIN32
  1445. // LTDS File format
  1446. // Content of the LTDSPixel class from DateiSystem.h
  1447. // Constructor
  1448. LTDSPixel::LTDSPixel(LTDSPixel* davor)
  1449. : ReferenceCounter(),
  1450. index(0),
  1451. iA(0),
  1452. miA(8),
  1453. maxIndex(1),
  1454. aender(0),
  1455. aenderA(0),
  1456. komp(0),
  1457. alpha(0),
  1458. davor(davor)
  1459. {}
  1460. // Destructor
  1461. LTDSPixel::~LTDSPixel()
  1462. {
  1463. if (davor) davor->release();
  1464. }
  1465. // non-constant
  1466. // intended for loading
  1467. bool LTDSPixel::addBitZuFarbe(unsigned char bit)
  1468. {
  1469. if (aenderA && iA != miA) // The bit belongs to alpha
  1470. {
  1471. alpha |= ((bit & Bits(1))) << (7 - komp - iA);
  1472. ++iA;
  1473. }
  1474. else // The bit belongs to the next pixel
  1475. return false;
  1476. return true;
  1477. }
  1478. char LTDSPixel::addByte(char byte,
  1479. char begin) // returns end of pixel, -1 if not finished
  1480. {
  1481. if (begin >= 8 || begin < 0) return -1;
  1482. for (int i = begin; i < 8; ++i)
  1483. {
  1484. switch (index)
  1485. {
  1486. case 0:
  1487. // The first bit of a pixel stores whether the
  1488. // compression changed
  1489. aender = ((byte >> (7 - i)) & Bits(1)) == 1;
  1490. if (!aender) // If compression doesn't change, the values
  1491. // from the previous pixel are inherited
  1492. {
  1493. if (!davor) // The file is corrupted (the first pixel cannot
  1494. // inherit from the previous one)
  1495. {
  1496. MessageBox(NULL,
  1497. "Error, the image file is corrupted",
  1498. "Error",
  1499. MB_ICONERROR);
  1500. exit(0);
  1501. }
  1502. aenderA = davor->getAEnderA();
  1503. komp = davor->getKomp();
  1504. miA -= komp;
  1505. if (!aenderA) alpha = davor->getA();
  1506. maxIndex += aenderA * (8 - komp); // Determine the length
  1507. // of the pixel in bits. Each color has 8 bits by default,
  1508. // compression can make it smaller
  1509. }
  1510. else
  1511. maxIndex += 4; // Since the pixel doesn't inherit values from
  1512. // the previous one, it is 4 bits larger
  1513. break;
  1514. case 1: // The second bit of a pixel stores either the
  1515. // compression type or already the color
  1516. if (aender) // The bit belongs to the 4 compression bits
  1517. aenderA = ((byte >> (7 - i)) & Bits(1)) == 1;
  1518. else
  1519. {
  1520. if (!addBitZuFarbe(
  1521. byte >> (7 - i))) // The bit belongs to the color
  1522. return i;
  1523. }
  1524. break;
  1525. case 2: // The sixth bit of a pixel stores either the
  1526. // compression type or already the color
  1527. if (aender) // The bit belongs to the 4 compression bits
  1528. komp |= (unsigned char)((byte >> (7 - i)) & Bits(1)) << 2;
  1529. else
  1530. {
  1531. if (!addBitZuFarbe(
  1532. byte >> (7 - i))) // The bit belongs to the color
  1533. return i;
  1534. }
  1535. break;
  1536. case 3: // The seventh bit of a pixel stores either the
  1537. // compression type or already the color
  1538. if (aender) // The bit belongs to the 4 compression bits
  1539. komp |= (unsigned char)((byte >> (7 - i)) & Bits(1)) << 1;
  1540. else
  1541. {
  1542. if (!addBitZuFarbe(
  1543. byte >> (7 - i))) // The bit belongs to the color
  1544. return i;
  1545. }
  1546. break;
  1547. case 4: // The eighth bit of a pixel stores either the
  1548. // compression type or already the color
  1549. if (aender) // The bit belongs to the 4 compression bits
  1550. {
  1551. komp |= (unsigned char)((byte >> (7 - i)) & Bits(1));
  1552. // That was the last compression bit
  1553. // Evaluate compression
  1554. miA -= komp;
  1555. if (!aenderA) alpha = davor->getA();
  1556. maxIndex += aenderA * (8 - komp); // bit length of the pixel
  1557. }
  1558. else
  1559. {
  1560. if (!addBitZuFarbe(
  1561. byte >> (7 - i))) // The bit belongs to the color
  1562. return i;
  1563. }
  1564. break;
  1565. default: // The remaining bits all store the color values of the pixel
  1566. if (!addBitZuFarbe(byte >> (7 - i))) // The bit belongs to the color
  1567. return i;
  1568. break;
  1569. }
  1570. ++index;
  1571. if (index >= maxIndex)
  1572. {
  1573. if (davor)
  1574. {
  1575. if (aenderA) alpha = davor->getA() + alpha;
  1576. davor = (LTDSPixel*)davor->release();
  1577. }
  1578. return i + 1;
  1579. }
  1580. }
  1581. return -1;
  1582. }
  1583. // intended for saving
  1584. void LTDSPixel::setAlpha(unsigned char alpha) // sets the color of the pixel
  1585. {
  1586. this->alpha = alpha;
  1587. }
  1588. void LTDSPixel::Komp() // Compresses the pixel
  1589. {
  1590. maxIndex = 1;
  1591. if (!davor)
  1592. { // This is the first pixel
  1593. aender = 1;
  1594. aenderA = 1;
  1595. maxIndex += 4;
  1596. miA = getBits(alpha);
  1597. }
  1598. else
  1599. { // The difference to the previous pixel is stored
  1600. miA = getBits(alpha - davor->getA());
  1601. if (alpha != davor->getA())
  1602. aenderA = 1;
  1603. else
  1604. aenderA = 0;
  1605. } // Check if something changes
  1606. if (!miA && aenderA) ++miA;
  1607. komp = 8 - miA;
  1608. maxIndex += aenderA * miA;
  1609. if (davor)
  1610. {
  1611. if (aenderA != davor->getAEnderA() || komp != davor->getKomp())
  1612. { // Something changes
  1613. aender = 1;
  1614. maxIndex += 4;
  1615. }
  1616. else
  1617. { // Nothing changes
  1618. aender = 0;
  1619. }
  1620. }
  1621. }
  1622. bool LTDSPixel::getNextFarbeBit(char& byte, int i)
  1623. {
  1624. unsigned char AA = alpha;
  1625. if (davor)
  1626. {
  1627. AA -= davor->getA();
  1628. }
  1629. if (aenderA && iA != miA) // The bit belongs to alpha
  1630. {
  1631. byte |= ((AA >> (7 - komp - iA)) & Bits(1)) << (7 - i);
  1632. ++iA;
  1633. }
  1634. else // The pixel is already finished
  1635. return false;
  1636. return true;
  1637. }
  1638. char LTDSPixel::getNextByte(
  1639. char& byte, int bbegin) // Returns the next bits, -1 if the pixel
  1640. // is not finished
  1641. {
  1642. // bbegin specifies where to write in the byte variable
  1643. // the function returns the end of the pixel in the byte variable
  1644. // -1 means the pixel is not finished
  1645. for (int i = bbegin; i < 8; ++i)
  1646. {
  1647. switch (index)
  1648. {
  1649. case 0: // The first bit of the pixel stores whether the
  1650. // compression changes
  1651. byte |= ((int)aender & Bits(1)) << (7 - i);
  1652. break;
  1653. case 1: // The second bit of the pixel stores either the color
  1654. // or the compression
  1655. if (aender) // Compression
  1656. byte |= ((int)aenderA & Bits(1)) << (7 - i);
  1657. else // Color
  1658. {
  1659. if (!getNextFarbeBit(byte, i)) return i;
  1660. }
  1661. break;
  1662. case 2: // The second bit of the pixel stores either the color
  1663. // or the compression
  1664. if (aender) // Compression
  1665. byte |= ((komp >> 2) & Bits(1)) << (7 - i);
  1666. else // Color
  1667. {
  1668. if (!getNextFarbeBit(byte, i)) return i;
  1669. }
  1670. break;
  1671. case 3: // The third bit of the pixel stores either the color
  1672. // or the compression
  1673. if (aender) // Compression
  1674. byte |= ((komp >> 1) & Bits(1)) << (7 - i);
  1675. else // Color
  1676. {
  1677. if (!getNextFarbeBit(byte, i)) return i;
  1678. }
  1679. break;
  1680. case 4: // The fourth bit of the pixel stores either the color
  1681. // or the compression
  1682. if (aender) // Compression
  1683. byte |= (komp & Bits(1)) << (7 - i);
  1684. else // Color
  1685. {
  1686. if (!getNextFarbeBit(byte, i)) return i;
  1687. }
  1688. break;
  1689. default: // The remaining bits store the color of the pixel
  1690. if (!getNextFarbeBit(byte, i)) return i;
  1691. break;
  1692. }
  1693. ++index;
  1694. if (index >= maxIndex)
  1695. {
  1696. if (davor) davor = (LTDSPixel*)davor->release();
  1697. return i + 1;
  1698. }
  1699. }
  1700. return -1;
  1701. }
  1702. // constant
  1703. unsigned char LTDSPixel::getKomp() const // has the compression changed
  1704. {
  1705. return komp;
  1706. }
  1707. bool LTDSPixel::getAEnderA()
  1708. const // returns whether the alpha value changes
  1709. {
  1710. return aenderA;
  1711. }
  1712. unsigned char LTDSPixel::getA() const // returns alpha
  1713. {
  1714. return alpha;
  1715. }
  1716. // Content of the LTDSDateiKopf class from Dateisystem.h
  1717. // Constructor
  1718. LTDSDateiKopf::LTDSDateiKopf()
  1719. : ReferenceCounter(),
  1720. sganzahl(0),
  1721. gr(0),
  1722. pos(0)
  1723. {}
  1724. // Destructor
  1725. LTDSDateiKopf::~LTDSDateiKopf()
  1726. {
  1727. delete[] gr;
  1728. delete[] pos;
  1729. }
  1730. // non-constant
  1731. void LTDSDateiKopf::laden(std::ifstream* inF) // Loads from inF
  1732. {
  1733. if (inF->good() && inF->is_open())
  1734. {
  1735. inF->read((char*)&sganzahl, 1);
  1736. delete[] gr;
  1737. delete[] pos;
  1738. gr = new unsigned char[sganzahl + 1];
  1739. pos = new int[sganzahl + 1];
  1740. gr[sganzahl] = 0;
  1741. pos[sganzahl] = 0;
  1742. for (int i = 0; i < sganzahl; ++i)
  1743. {
  1744. inF->read((char*)&gr[i], 1);
  1745. inF->read((char*)&pos[i], 4);
  1746. }
  1747. }
  1748. }
  1749. void LTDSDateiKopf::addSG(char sg) // Add font size
  1750. {
  1751. ++sganzahl;
  1752. unsigned char* gr_tmp = gr;
  1753. int* pos_tmp = pos;
  1754. gr = new unsigned char[sganzahl + 1];
  1755. pos = new int[sganzahl + 1];
  1756. gr[sganzahl] = 0;
  1757. pos[sganzahl] = 0;
  1758. if (sganzahl - 1 > 0)
  1759. {
  1760. memcpy(gr, gr_tmp, 1 * (sganzahl - 1));
  1761. memcpy(pos, pos_tmp, 4 * (sganzahl - 1));
  1762. }
  1763. delete[] gr_tmp;
  1764. delete[] pos_tmp;
  1765. pos[sganzahl - 1] = 0;
  1766. gr[sganzahl - 1] = sg;
  1767. }
  1768. void LTDSDateiKopf::removeSG(char sg) // Remove font size
  1769. {
  1770. bool hatsg = 0;
  1771. int sgpos = 0;
  1772. for (int i = 0; i < sganzahl; ++i)
  1773. {
  1774. hatsg = gr[i] == sg;
  1775. sgpos = i;
  1776. if (hatsg) break;
  1777. }
  1778. if (hatsg)
  1779. {
  1780. --sganzahl;
  1781. unsigned char* gr_tmp = gr;
  1782. int* pos_tmp = pos;
  1783. gr = new unsigned char[sganzahl + 1];
  1784. pos = new int[sganzahl + 1];
  1785. gr[sganzahl] = 0;
  1786. pos[sganzahl] = 0;
  1787. for (int i = 0; i < sgpos; ++i)
  1788. {
  1789. gr[i] = gr_tmp[i];
  1790. pos[i] = pos_tmp[i];
  1791. }
  1792. for (int i = sgpos + 1; i < sganzahl; ++i)
  1793. {
  1794. gr[i - 1] = gr_tmp[i];
  1795. pos[i - 1] = pos_tmp[i];
  1796. }
  1797. delete[] gr_tmp;
  1798. delete[] pos_tmp;
  1799. }
  1800. }
  1801. // constant
  1802. void LTDSDateiKopf::speichern(std::ofstream* outF) const // Saves to outF
  1803. {
  1804. if (outF->is_open() && outF->good())
  1805. {
  1806. outF->write((char*)&sganzahl, 1);
  1807. for (int i = 0; i < sganzahl; ++i)
  1808. {
  1809. outF->write((char*)&gr[i], 1);
  1810. outF->write((char*)&pos[i], 4);
  1811. }
  1812. }
  1813. }
  1814. unsigned char* LTDSDateiKopf::getSchriftGroesseList()
  1815. const // returns a list of stored font sizes
  1816. {
  1817. return gr;
  1818. }
  1819. int* LTDSDateiKopf::getPositionList()
  1820. const // returns a position list of stored font sizes
  1821. {
  1822. return pos;
  1823. }
  1824. int LTDSDateiKopf::getSchriftGroesseAnzahl()
  1825. const // returns the number of stored font sizes
  1826. {
  1827. return sganzahl;
  1828. }
  1829. // Content of the LTDSSchriftKopf class from Dateisystem.h
  1830. // Constructor
  1831. LTDSSchriftKopf::LTDSSchriftKopf()
  1832. : ReferenceCounter(),
  1833. schriftSize(0),
  1834. zeichen(0),
  1835. pos(0),
  1836. zeichenAnzahl(0)
  1837. {}
  1838. // Destructor
  1839. LTDSSchriftKopf::~LTDSSchriftKopf()
  1840. {
  1841. delete[] pos;
  1842. delete[] zeichen;
  1843. }
  1844. // non-constant
  1845. void LTDSSchriftKopf::laden(std::ifstream* inF) // loads from inF
  1846. {
  1847. if (inF->good() && inF->is_open())
  1848. {
  1849. inF->read((char*)&schriftSize, 1);
  1850. inF->read((char*)&zeichenAnzahl, 1);
  1851. delete[] pos;
  1852. delete[] zeichen;
  1853. zeichen = new unsigned char[zeichenAnzahl];
  1854. pos = new int[zeichenAnzahl + 1];
  1855. for (int i = 0; i < zeichenAnzahl; ++i)
  1856. {
  1857. inF->read((char*)&zeichen[i], 1);
  1858. inF->read((char*)&pos[i], 4);
  1859. }
  1860. pos[zeichenAnzahl] = 0;
  1861. }
  1862. }
  1863. void LTDSSchriftKopf::setSchriftgroesse(
  1864. unsigned char gr) // set font size
  1865. {
  1866. schriftSize = gr;
  1867. }
  1868. void LTDSSchriftKopf::setZeichenAlphabet(
  1869. Alphabet* alphabet) // sets the characters from alphabet
  1870. {
  1871. int count = 0;
  1872. for (int i = 0; i < 256; ++i)
  1873. {
  1874. Buchstabe* zeich = alphabet->zBuchstabe(i);
  1875. if (zeich) ++count;
  1876. }
  1877. delete[] zeichen;
  1878. delete[] pos;
  1879. zeichen = new unsigned char[count];
  1880. pos = new int[count + 1];
  1881. pos[count] = 0;
  1882. zeichenAnzahl = count;
  1883. count = 0;
  1884. for (int i = 0; i < 256; ++i)
  1885. {
  1886. Buchstabe* zeich = alphabet->getBuchstabe(i);
  1887. if (zeich)
  1888. {
  1889. zeichen[count] = i;
  1890. pos[count] = 0;
  1891. ++count;
  1892. zeich->release();
  1893. }
  1894. }
  1895. schriftSize = alphabet->getSchriftSize();
  1896. alphabet->release();
  1897. }
  1898. void LTDSSchriftKopf::addZeichen(unsigned char zeichen) // Add character
  1899. {
  1900. ++zeichenAnzahl;
  1901. unsigned char* zeichen_tmp = this->zeichen;
  1902. int* pos_tmp = pos;
  1903. this->zeichen = new unsigned char[zeichenAnzahl];
  1904. pos = new int[zeichenAnzahl + 1];
  1905. if (zeichenAnzahl - 1 > 0)
  1906. {
  1907. memcpy(this->zeichen, zeichen_tmp, 1 * (zeichenAnzahl - 1));
  1908. memcpy(pos, pos_tmp, 4 * (zeichenAnzahl - 1));
  1909. }
  1910. delete[] zeichen_tmp;
  1911. delete[] pos_tmp;
  1912. this->zeichen[zeichenAnzahl - 1] = zeichen;
  1913. pos[zeichenAnzahl - 1] = 0;
  1914. pos[zeichenAnzahl] = 0;
  1915. }
  1916. void LTDSSchriftKopf::removeZeichen(unsigned char zeich) // Remove character
  1917. {
  1918. bool hatZ = 0;
  1919. int zPos = 0;
  1920. for (int i = 0; i < zeichenAnzahl; ++i)
  1921. {
  1922. hatZ = zeichen[i] == zeich;
  1923. zPos = i;
  1924. if (hatZ) break;
  1925. }
  1926. if (hatZ)
  1927. {
  1928. --zeichenAnzahl;
  1929. unsigned char* zeichen_tmp = zeichen;
  1930. int* pos_tmp = pos;
  1931. zeichen = new unsigned char[zeichenAnzahl];
  1932. pos = new int[zeichenAnzahl + 1];
  1933. for (int i = 0; i < zPos; ++i)
  1934. {
  1935. zeichen[i] = zeichen_tmp[i];
  1936. pos[i] = pos_tmp[i];
  1937. }
  1938. for (int i = zPos + 1; i <= zeichenAnzahl; ++i)
  1939. {
  1940. zeichen[i - 1] = zeichen_tmp[i];
  1941. pos[i - 1] = pos_tmp[i];
  1942. }
  1943. pos[zeichenAnzahl] = 0;
  1944. delete[] zeichen_tmp;
  1945. delete[] pos_tmp;
  1946. }
  1947. }
  1948. // constant
  1949. void LTDSSchriftKopf::speichern(
  1950. std::ofstream* outF) const // saves to outF
  1951. {
  1952. if (outF->good() && outF->is_open())
  1953. {
  1954. outF->write((char*)&schriftSize, 1);
  1955. outF->write((char*)&zeichenAnzahl, 1);
  1956. for (int i = 0; i < zeichenAnzahl; ++i)
  1957. {
  1958. outF->write((char*)&zeichen[i], 1);
  1959. outF->write((char*)&pos[i], 4);
  1960. }
  1961. }
  1962. }
  1963. unsigned char
  1964. LTDSSchriftKopf::getSchriftGroesse() const // returns the font size
  1965. {
  1966. return schriftSize;
  1967. }
  1968. unsigned char
  1969. LTDSSchriftKopf::getZeichenAnzahl() const // returns the character count
  1970. {
  1971. return zeichenAnzahl;
  1972. }
  1973. int* LTDSSchriftKopf::getPositionen()
  1974. const // returns the character positions
  1975. {
  1976. return pos;
  1977. }
  1978. unsigned char* LTDSSchriftKopf::getZeichen() const // returns the characters
  1979. {
  1980. return zeichen;
  1981. }
  1982. // Content of the LTDSBuchstabenKopf class from Dateisystem.h
  1983. // Constructor
  1984. LTDSBuchstabenKopf::LTDSBuchstabenKopf()
  1985. : ReferenceCounter(),
  1986. zeichen(0),
  1987. size(0, 0)
  1988. {}
  1989. // non-constant
  1990. void LTDSBuchstabenKopf::laden(std::ifstream* inF) // loads from inF
  1991. {
  1992. if (inF->good() && inF->is_open())
  1993. {
  1994. inF->read((char*)&zeichen, 1);
  1995. inF->read((char*)&size.x, 1);
  1996. inF->read((char*)&size.y, 1);
  1997. }
  1998. }
  1999. void LTDSBuchstabenKopf::init(unsigned char zeichen,
  2000. const Punkt& groesse) // initialization (for saving)
  2001. {
  2002. this->zeichen = zeichen;
  2003. this->size = groesse;
  2004. }
  2005. void LTDSBuchstabenKopf::init(unsigned char zeichen, int br, int hoe)
  2006. {
  2007. this->zeichen = zeichen;
  2008. size.x = br, size.y = hoe;
  2009. }
  2010. // constant
  2011. void LTDSBuchstabenKopf::speichern(
  2012. std::ofstream* outF) const // saves to outF
  2013. {
  2014. if (outF->good() && outF->is_open())
  2015. {
  2016. outF->write((char*)&zeichen, 1);
  2017. outF->write((char*)&size.x, 1);
  2018. outF->write((char*)&size.y, 1);
  2019. }
  2020. }
  2021. unsigned char LTDSBuchstabenKopf::getZeichen() const // returns the character
  2022. {
  2023. return zeichen;
  2024. }
  2025. int LTDSBuchstabenKopf::getBreite() const // returns the width
  2026. {
  2027. return size.x;
  2028. }
  2029. int LTDSBuchstabenKopf::getHoehe() const // returns the height
  2030. {
  2031. return size.y;
  2032. }
  2033. const Punkt& LTDSBuchstabenKopf::getGroesse() const // returns the size
  2034. {
  2035. return size;
  2036. }
  2037. // Content of the LTDSBuchstabenKoerper class from Dateisystem.h
  2038. // Constructor
  2039. LTDSBuchstabenKoerper::LTDSBuchstabenKoerper(LTDSBuchstabenKopf* kopf)
  2040. : ReferenceCounter(),
  2041. size(kopf->getGroesse()),
  2042. zeichen(kopf->getZeichen()),
  2043. buchstabe(new Buchstabe())
  2044. {
  2045. buchstabe->NeuBuchstabe(size);
  2046. kopf->release();
  2047. }
  2048. // Destructor
  2049. LTDSBuchstabenKoerper::~LTDSBuchstabenKoerper()
  2050. {
  2051. if (buchstabe) buchstabe->release();
  2052. }
  2053. // non-constant
  2054. void LTDSBuchstabenKoerper::setBuchstabe(
  2055. Buchstabe* zeichen) // sets the character
  2056. {
  2057. if (buchstabe) buchstabe->release();
  2058. buchstabe = zeichen;
  2059. }
  2060. void LTDSBuchstabenKoerper::laden(std::ifstream* inF) // Loads from inF
  2061. {
  2062. if (inF->good() && inF->is_open())
  2063. {
  2064. LTDSPixel* vorher = 0;
  2065. LTDSPixel* jetzt = new LTDSPixel(vorher);
  2066. char byte = 0;
  2067. int beg = 0;
  2068. int ende = -1;
  2069. for (int i = 0; i < size.x * size.y; ++i)
  2070. {
  2071. if (!jetzt) // if it is not the first pixel
  2072. jetzt = new LTDSPixel(
  2073. dynamic_cast<LTDSPixel*>(vorher->getThis()));
  2074. int ende = -1;
  2075. while (ende < 0) // load pixel
  2076. {
  2077. if (beg == 0) inF->read(&byte, 1);
  2078. ende = jetzt->addByte(byte, beg); // evaluate byte
  2079. beg = 0;
  2080. }
  2081. beg = ende;
  2082. if (beg == 8) beg = 0;
  2083. if (buchstabe) buchstabe->setPixel(i, jetzt->getA());
  2084. if (vorher) vorher = (LTDSPixel*)vorher->release();
  2085. vorher = dynamic_cast<LTDSPixel*>(jetzt->getThis());
  2086. jetzt = (LTDSPixel*)jetzt->release();
  2087. }
  2088. if (vorher) vorher->release();
  2089. if (jetzt) jetzt->release();
  2090. }
  2091. }
  2092. // constant
  2093. void LTDSBuchstabenKoerper::speichern(
  2094. std::ofstream* outF) const // saves to outF
  2095. {
  2096. if (outF->good() && outF->is_open())
  2097. {
  2098. LTDSPixel* vorher = 0; // Last saved pixel
  2099. LTDSPixel* jetzt
  2100. = new LTDSPixel(0); // The pixel currently being saved
  2101. int begin = 0,
  2102. ende = 0; // pixel start, end position in the byte variable
  2103. char byte = 0; // The next byte of the file
  2104. bool w = 0;
  2105. unsigned char* alphaBuff = buchstabe->getBuff();
  2106. for (int i = 0; i < size.x * size.y; ++i) // for each pixel
  2107. {
  2108. if (!jetzt) // if it is not the first pixel
  2109. jetzt = new LTDSPixel(
  2110. dynamic_cast<LTDSPixel*>(vorher->getThis()));
  2111. jetzt->setAlpha(alphaBuff[i]); // set pixel color
  2112. jetzt->Komp(); // compress pixel
  2113. ende = -1;
  2114. while (ende < 0) // fill byte
  2115. {
  2116. ende = jetzt->getNextByte(byte, begin);
  2117. begin = 0;
  2118. w = 0;
  2119. if (ende == -1 || ende == 8) // save byte
  2120. {
  2121. outF->write(&byte, 1);
  2122. w = 1;
  2123. byte = 0;
  2124. }
  2125. } // pixel finished
  2126. begin = ende;
  2127. if (begin == 8) begin = 0;
  2128. if (vorher) vorher->release();
  2129. vorher = dynamic_cast<LTDSPixel*>(
  2130. jetzt->getThis()); // jetzt becomes vorher
  2131. jetzt = (LTDSPixel*)jetzt->release();
  2132. }
  2133. if (vorher) vorher = (LTDSPixel*)vorher->release();
  2134. if (!w)
  2135. {
  2136. outF->write(&byte, 1); // save the last byte
  2137. }
  2138. outF->flush(); // flush file stream
  2139. }
  2140. }
  2141. Buchstabe*
  2142. LTDSBuchstabenKoerper::getBuchstabe() const // returns the character
  2143. {
  2144. return dynamic_cast<Buchstabe*>(buchstabe->getThis());
  2145. }
  2146. unsigned char
  2147. LTDSBuchstabenKoerper::getZeichen() const // returns the character
  2148. {
  2149. return zeichen;
  2150. }
  2151. // Content of the LTDSDatei class from Dateisystem.h
  2152. // Constructor
  2153. LTDSDatei::LTDSDatei()
  2154. : ReferenceCounter(),
  2155. pfad(new Text()),
  2156. dateiKopf(0)
  2157. {}
  2158. // Destructor
  2159. LTDSDatei::~LTDSDatei()
  2160. {
  2161. if (dateiKopf) dateiKopf->release();
  2162. pfad->release();
  2163. }
  2164. // non-constant
  2165. void LTDSDatei::setPfad(Text* txt) // sets the path to the file
  2166. {
  2167. if (dateiKopf) dateiKopf = (LTDSDateiKopf*)dateiKopf->release();
  2168. pfad->setText(txt->getText());
  2169. txt->release();
  2170. }
  2171. void LTDSDatei::leseDaten() // reads the file header
  2172. {
  2173. if (!DateiExistiert(dynamic_cast<Text*>(pfad->getThis()))) return;
  2174. if (dateiKopf) dateiKopf->release();
  2175. dateiKopf = new LTDSDateiKopf();
  2176. std::ifstream* inF = new std::ifstream(pfad->getText(), std::ios::binary);
  2177. dateiKopf->laden(inF);
  2178. inF->close();
  2179. delete inF;
  2180. }
  2181. void LTDSDatei::addSchriftgroesse(
  2182. Alphabet* alphabet) // adds a font size
  2183. {
  2184. if (!DateiExistiert(dynamic_cast<Text*>(
  2185. pfad->getThis()))) // check if the file exists
  2186. return;
  2187. if (!dateiKopf) // check if the file header has been read
  2188. leseDaten();
  2189. int sgroesse
  2190. = alphabet
  2191. ->getSchriftSize(); // font size to be added
  2192. unsigned char* sglist
  2193. = dateiKopf->getSchriftGroesseList(); // list of already existing
  2194. // font sizes
  2195. unsigned char sganzahl
  2196. = dateiKopf->getSchriftGroesseAnzahl(); // number of already
  2197. // existing font sizes
  2198. for (int i = 0; i < sganzahl;
  2199. ++i) // check if the font size already exists
  2200. {
  2201. if (sglist[i] == sgroesse)
  2202. {
  2203. alphabet->release();
  2204. return;
  2205. }
  2206. }
  2207. dateiKopf->addSG(sgroesse); // add font size to file header
  2208. int* sgPosList
  2209. = dateiKopf
  2210. ->getPositionList(); // list of font size positions
  2211. sglist = dateiKopf->getSchriftGroesseList(); // list of already
  2212. // existing font sizes
  2213. std::ifstream* inF
  2214. = new std::ifstream(pfad->getText(), std::ios::binary); // old file
  2215. pfad->append("0");
  2216. std::ofstream* outF
  2217. = new std::ofstream(pfad->getText(), std::ios::binary); // new file
  2218. dateiKopf->speichern(outF); // save file header to new file
  2219. inF->seekg(1 + 5 * sganzahl,
  2220. std::ios::beg); // position of the first font size in the old file
  2221. for (int i = 0; i < sganzahl;
  2222. ++i) // update character positions of all font sizes
  2223. {
  2224. LTDSSchriftKopf* sgKpf_tmp
  2225. = new LTDSSchriftKopf(); // font size header
  2226. sgKpf_tmp->laden(inF); // load from old file
  2227. int* zeichP_tmp
  2228. = sgKpf_tmp
  2229. ->getPositionen(); // character positions of the font size
  2230. unsigned char zeichA_tmp
  2231. = sgKpf_tmp
  2232. ->getZeichenAnzahl(); // number of characters in the font size
  2233. for (int i1 = 0; i1 < zeichA_tmp; ++i1)
  2234. zeichP_tmp[i1] += 5; // update character positions
  2235. sgKpf_tmp->speichern(
  2236. outF); // save font size header to new file
  2237. int beginByte
  2238. = (int)inF->tellg(); // copy remaining bytes until the next
  2239. // font size to new file
  2240. int endByte = sgPosList[i + 1];
  2241. if (!endByte)
  2242. {
  2243. inF->seekg(0, std::ios::end);
  2244. endByte = (int)inF->tellg();
  2245. inF->seekg(beginByte, std::ios::beg);
  2246. }
  2247. char byte;
  2248. for (int i1 = beginByte; i1 < endByte; ++i1) // copy operation
  2249. {
  2250. inF->read(&byte, 1);
  2251. outF->write(&byte, 1);
  2252. }
  2253. sgKpf_tmp->release();
  2254. }
  2255. inF->close(); // close old file
  2256. sgPosList[sganzahl] = (int)outF->tellp();
  2257. outF->seekp(0, std::ios::beg);
  2258. for (int i = 0; i < sganzahl; ++i) // update positions in file header
  2259. sgPosList[i] += 5;
  2260. dateiKopf->speichern(outF); // save updated file header
  2261. outF->seekp(sgPosList[sganzahl], std::ios::beg);
  2262. LTDSSchriftKopf* sgkopf
  2263. = new LTDSSchriftKopf(); // header of the new font size
  2264. sgkopf->setZeichenAlphabet(dynamic_cast<Alphabet*>(
  2265. alphabet->getThis())); // initialize font size header
  2266. sgkopf->speichern(outF); // save font size header
  2267. int* BuchstabenPosList
  2268. = sgkopf->getPositionen(); // positions of the different characters
  2269. // in the file (still 0)
  2270. int count = 0;
  2271. for (int i = 0; i < 256; ++i)
  2272. {
  2273. Buchstabe* zeich = alphabet->getBuchstabe(i);
  2274. if (zeich)
  2275. {
  2276. BuchstabenPosList[count]
  2277. = (int)outF->tellp(); // set character position
  2278. LTDSBuchstabenKopf* zeichKpf
  2279. = new LTDSBuchstabenKopf(); // character header
  2280. zeichKpf->init(i, zeich->getBreite(), zeich->getHeight());
  2281. zeichKpf->speichern(outF); // save character header
  2282. LTDSBuchstabenKoerper* zeichKoerp
  2283. = new LTDSBuchstabenKoerper(dynamic_cast<LTDSBuchstabenKopf*>(
  2284. zeichKpf->getThis())); // character body
  2285. zeichKpf->release();
  2286. zeichKoerp->setBuchstabe(
  2287. dynamic_cast<Buchstabe*>(zeich->getThis()));
  2288. zeichKoerp->speichern(outF); // save character body
  2289. zeich->release();
  2290. ++count;
  2291. }
  2292. }
  2293. outF->seekp(sgPosList[sganzahl], std::ios::beg);
  2294. sgkopf->speichern(outF); // save updated font size header
  2295. outF->close();
  2296. Text* pfad2 = new Text();
  2297. pfad2->setText(pfad->getText());
  2298. pfad->remove(pfad->getLength() - 1, pfad->getLength());
  2299. DateiRemove(dynamic_cast<Text*>(pfad->getThis())); // delete old file
  2300. DateiUmbenennen(dynamic_cast<Text*>(pfad2->getThis()),
  2301. dynamic_cast<Text*>(
  2302. pfad->getThis())); // rename new file to old
  2303. pfad2->release(); // free memory
  2304. sgkopf->release();
  2305. delete inF;
  2306. delete outF;
  2307. alphabet->release();
  2308. }
  2309. void LTDSDatei::addBuchstabe(int gr,
  2310. Buchstabe* zeich,
  2311. unsigned char zeichen) // Adds a character to a font size
  2312. {
  2313. if (!DateiExistiert(
  2314. dynamic_cast<Text*>(pfad->getThis()))) // check if file exists
  2315. {
  2316. zeich->release();
  2317. return;
  2318. }
  2319. if (!dateiKopf) // check if the file header was loaded
  2320. leseDaten();
  2321. unsigned char* sgList
  2322. = dateiKopf->getSchriftGroesseList(); // list of font sizes from
  2323. // the file
  2324. int* sgPosList = dateiKopf->getPositionList(); // list of font size
  2325. // positions from file
  2326. unsigned char sgAnzahl
  2327. = dateiKopf->getSchriftGroesseAnzahl(); // number of font sizes
  2328. // from the file
  2329. int sgNum = -1;
  2330. for (int i = 0; i < sgAnzahl;
  2331. ++i) // find the position of the correct font size
  2332. {
  2333. if (sgList[i] == gr)
  2334. {
  2335. sgNum = i;
  2336. break;
  2337. }
  2338. }
  2339. std::ifstream* inF
  2340. = new std::ifstream(pfad->getText(), std::ios::binary); // old file
  2341. pfad->append("0");
  2342. std::ofstream* outF
  2343. = new std::ofstream(pfad->getText(), std::ios::binary); // new file
  2344. inF->seekg(1 + 5 * sgAnzahl, std::ios::beg);
  2345. if (sgNum
  2346. == -1) // The font size does not exist yet and will be created
  2347. {
  2348. dateiKopf->addSG(gr); // add font size to file header
  2349. sgPosList = dateiKopf->getPositionList();
  2350. sgList = dateiKopf->getSchriftGroesseList();
  2351. dateiKopf->speichern(outF); // save file header
  2352. inF->seekg(1 + 5 * sgAnzahl,
  2353. std::ios::beg); // position of the first
  2354. // font size in the old file
  2355. for (int i = 0; i < sgAnzahl;
  2356. ++i) // update character positions of all font sizes
  2357. {
  2358. LTDSSchriftKopf* sgKpf_tmp
  2359. = new LTDSSchriftKopf(); // font size header
  2360. sgKpf_tmp->laden(inF); // load from old file
  2361. int* zeichP_tmp
  2362. = sgKpf_tmp
  2363. ->getPositionen(); // character positions of the font size
  2364. unsigned char zeichA_tmp
  2365. = sgKpf_tmp->getZeichenAnzahl(); // number of characters in
  2366. // the font size
  2367. for (int i1 = 0; i1 < zeichA_tmp; ++i1)
  2368. zeichP_tmp[i1] += 5; // update character positions
  2369. sgKpf_tmp->speichern(
  2370. outF); // save font size header to new file
  2371. int beginByte
  2372. = (int)inF->tellg(); // copy remaining bytes until the next
  2373. // font size to new file
  2374. int endByte = sgPosList[i + 1];
  2375. if (i + 1 >= sgAnzahl)
  2376. {
  2377. inF->seekg(0, std::ios::end);
  2378. endByte = (int)inF->tellg();
  2379. inF->seekg(beginByte, std::ios::beg);
  2380. }
  2381. char byte;
  2382. for (int i1 = beginByte; i1 < endByte; ++i1) // copy operation
  2383. {
  2384. inF->read(&byte, 1);
  2385. outF->write(&byte, 1);
  2386. }
  2387. sgKpf_tmp->release();
  2388. }
  2389. sgPosList[sgAnzahl] = (int)outF->tellp();
  2390. outF->seekp(0, std::ios::beg);
  2391. for (int i = 0; i < sgAnzahl;
  2392. ++i) // update positions in file header
  2393. sgPosList[i] += 5;
  2394. dateiKopf->speichern(outF); // save updated file header
  2395. outF->seekp(sgPosList[sgAnzahl], std::ios::beg);
  2396. LTDSSchriftKopf* sgKpf
  2397. = new LTDSSchriftKopf(); // initialize font size header
  2398. sgKpf->setSchriftgroesse(gr);
  2399. sgKpf->addZeichen(zeichen);
  2400. sgKpf->getPositionen()[0] = (int)outF->tellp() + 7;
  2401. sgKpf->speichern(outF); // save font size header
  2402. sgKpf->release();
  2403. LTDSBuchstabenKopf* zeichKpf
  2404. = new LTDSBuchstabenKopf(); // character header
  2405. zeichKpf->init(zeichen, zeich->getSize());
  2406. zeichKpf->speichern(outF); // save character header
  2407. LTDSBuchstabenKoerper* zeichKoerp
  2408. = new LTDSBuchstabenKoerper(dynamic_cast<LTDSBuchstabenKopf*>(
  2409. zeichKpf->getThis())); // character body
  2410. zeichKpf->release();
  2411. zeichKoerp->setBuchstabe(dynamic_cast<Buchstabe*>(zeich->getThis()));
  2412. zeichKoerp->speichern(outF); // save character body
  2413. zeichKoerp->release();
  2414. }
  2415. else
  2416. {
  2417. dateiKopf->speichern(outF); // save file header
  2418. int beginByte
  2419. = 1 + 5 * sgAnzahl; // copy remaining bytes until the next
  2420. // font size to new file
  2421. int endByte = sgPosList[sgNum];
  2422. char byte;
  2423. for (int i1 = beginByte; i1 < endByte;
  2424. ++i1) // copy operation up to the specified size
  2425. {
  2426. inF->read(&byte, 1);
  2427. outF->write(&byte, 1);
  2428. }
  2429. LTDSSchriftKopf* sgKpf = new LTDSSchriftKopf(); // font size header
  2430. sgKpf->laden(inF);
  2431. for (int i = 0; i < sgKpf->getZeichenAnzahl(); ++i)
  2432. sgKpf->getPositionen()[i] += 5;
  2433. sgKpf->addZeichen(zeichen);
  2434. int indexPlus = 5;
  2435. int zeichenPos = sgPosList[sgNum + 1]; // position of the new character
  2436. if (sgNum + 1 >= sgAnzahl)
  2437. {
  2438. int tmp = (int)inF->tellg();
  2439. inF->seekg(0, std::ios::end);
  2440. zeichenPos = (int)inF->tellg();
  2441. inF->seekg(tmp, std::ios::beg);
  2442. }
  2443. zeichenPos += indexPlus;
  2444. sgKpf->getPositionen()[sgKpf->getZeichenAnzahl() - 1] = zeichenPos;
  2445. sgKpf->speichern(outF); // save font size header
  2446. sgKpf->release();
  2447. for (int i = (int)inF->tellg() + indexPlus; i < zeichenPos;
  2448. ++i) // copy operation up to the character start
  2449. {
  2450. inF->read(&byte, 1);
  2451. outF->write(&byte, 1);
  2452. }
  2453. LTDSBuchstabenKopf* zeichKpf = new LTDSBuchstabenKopf(); // character header
  2454. zeichKpf->init(zeichen, zeich->getSize());
  2455. zeichKpf->speichern(outF); // save character header
  2456. LTDSBuchstabenKoerper* zeichKoerp
  2457. = new LTDSBuchstabenKoerper(dynamic_cast<LTDSBuchstabenKopf*>(
  2458. zeichKpf->getThis())); // character body
  2459. zeichKpf->release();
  2460. zeichKoerp->setBuchstabe(dynamic_cast<Buchstabe*>(zeich->getThis()));
  2461. zeichKoerp->speichern(outF); // save character body
  2462. zeichKoerp->release();
  2463. int nowPos = (int)outF->tellp();
  2464. indexPlus += nowPos - zeichenPos;
  2465. for (int i = sgNum + 1; i < sgAnzahl; ++i) // update file header
  2466. sgPosList[i] += indexPlus;
  2467. outF->seekp(0, std::ios::beg);
  2468. dateiKopf->speichern(outF); // save file header
  2469. outF->seekp(nowPos, std::ios::beg);
  2470. for (int i = sgNum + 1; i < sgAnzahl;
  2471. ++i) // update remaining font sizes
  2472. {
  2473. LTDSSchriftKopf* sgKpf_tmp
  2474. = new LTDSSchriftKopf(); // font size header
  2475. sgKpf_tmp->laden(inF); // load from old file
  2476. int* zeichP_tmp
  2477. = sgKpf_tmp
  2478. ->getPositionen(); // character positions of the font size
  2479. unsigned char zeichA_tmp
  2480. = sgKpf_tmp->getZeichenAnzahl(); // number of characters in
  2481. // the font size
  2482. for (int i1 = 0; i1 < zeichA_tmp; ++i1)
  2483. zeichP_tmp[i1]
  2484. += indexPlus; // update character positions
  2485. sgKpf_tmp->speichern(
  2486. outF); // save font size header to new file
  2487. int beginByte
  2488. = (int)inF->tellg(); // copy remaining bytes until the next
  2489. // font size to new file
  2490. int endByte = sgPosList[i + 1];
  2491. if (i + 1 >= sgAnzahl)
  2492. {
  2493. inF->seekg(0, std::ios::end);
  2494. endByte = (int)inF->tellg();
  2495. inF->seekg(beginByte, std::ios::beg);
  2496. }
  2497. char byte;
  2498. for (int i1 = beginByte; i1 < endByte; ++i1) // copy operation
  2499. {
  2500. inF->read(&byte, 1);
  2501. outF->write(&byte, 1);
  2502. }
  2503. sgKpf_tmp->release();
  2504. }
  2505. }
  2506. inF->close();
  2507. outF->close();
  2508. Text* pfad2 = new Text(pfad->getText());
  2509. pfad->remove(pfad->getLength() - 1, pfad->getLength());
  2510. DateiRemove(dynamic_cast<Text*>(pfad->getThis())); // delete old file
  2511. DateiUmbenennen(dynamic_cast<Text*>(pfad2->getThis()),
  2512. dynamic_cast<Text*>(pfad->getThis())); // rename new file to old
  2513. pfad2->release(); // free memory
  2514. delete inF;
  2515. delete outF;
  2516. zeich->release();
  2517. }
  2518. void LTDSDatei::loescheSchrifrGroesse(
  2519. int gr) // Deletes a font size from the file
  2520. {
  2521. if (!DateiExistiert(dynamic_cast<Text*>(
  2522. pfad->getThis()))) // check if file exists
  2523. return;
  2524. if (!dateiKopf) // check if the file header was loaded
  2525. leseDaten();
  2526. unsigned char* sgList
  2527. = dateiKopf->getSchriftGroesseList(); // list of font sizes
  2528. unsigned char sgAnzahl
  2529. = dateiKopf->getSchriftGroesseAnzahl(); // number of font sizes
  2530. int sgNum = -1;
  2531. for (int i = 0; i < sgAnzahl; ++i) // search for font size to delete
  2532. {
  2533. if (sgList[i] == gr)
  2534. {
  2535. sgNum = i;
  2536. break;
  2537. }
  2538. }
  2539. if (sgNum == -1) // the font size is not present
  2540. return;
  2541. int* sgPosList
  2542. = dateiKopf
  2543. ->getPositionList(); // list of font size positions
  2544. std::ifstream* inF
  2545. = new std::ifstream(pfad->getText(), std::ios::binary); // old file
  2546. pfad->append("0");
  2547. std::ofstream* outF
  2548. = new std::ofstream(pfad->getText(), std::ios::binary); // new file
  2549. outF->seekp(1 + 5 * (sgAnzahl - 1), std::ios::beg);
  2550. inF->seekg(1 + 5 * sgAnzahl, std::ios::beg);
  2551. for (int i = 0; i < sgNum; ++i) // update font sizes before the one
  2552. // being deleted
  2553. {
  2554. LTDSSchriftKopf* sgKpf_tmp
  2555. = new LTDSSchriftKopf(); // font size header
  2556. sgKpf_tmp->laden(inF); // load font size header
  2557. int* zeichPosLTmp = sgKpf_tmp->getPositionen(); // character positions
  2558. unsigned char zeichATmp
  2559. = sgKpf_tmp->getZeichenAnzahl(); // character count
  2560. for (int i1 = 0; i1 < zeichATmp;
  2561. ++i1) // move character positions back by 5 bytes
  2562. zeichPosLTmp[i1] -= 5;
  2563. sgKpf_tmp->speichern(outF); // save to new file
  2564. char byte = 0;
  2565. for (int i1 = (int)inF->tellg(); i1 < sgPosList[i + 1];
  2566. ++i1) // copy the character body
  2567. {
  2568. inF->read(&byte, 1);
  2569. outF->write(&byte, 1);
  2570. }
  2571. sgKpf_tmp->release();
  2572. }
  2573. int indexMinus = 5 + sgPosList[sgNum + 1] - (int)inF->tellg();
  2574. inF->seekg(sgPosList[sgNum + 1], std::ios::beg);
  2575. for (int i = sgNum + 1; i < sgAnzahl;
  2576. ++i) // font sizes after the one being deleted
  2577. {
  2578. LTDSSchriftKopf* sgKpf_tmp
  2579. = new LTDSSchriftKopf(); // font size header
  2580. sgKpf_tmp->laden(inF); // load font size header
  2581. int* zeichPosLTmp = sgKpf_tmp->getPositionen(); // character positions
  2582. unsigned char zeichATmp
  2583. = sgKpf_tmp->getZeichenAnzahl(); // character count
  2584. for (int i1 = 0; i1 < zeichATmp;
  2585. ++i1) // update character positions
  2586. zeichPosLTmp[i1] -= indexMinus;
  2587. sgKpf_tmp->speichern(outF); // save to new file
  2588. char byte = 0;
  2589. int BeginByte = (int)inF->tellg();
  2590. int EndByte = sgPosList[i + 1];
  2591. if (!EndByte)
  2592. {
  2593. inF->seekg(0, std::ios::end);
  2594. EndByte = (int)inF->tellg();
  2595. inF->seekg(BeginByte, std::ios::beg);
  2596. }
  2597. for (int i1 = BeginByte; i1 < EndByte;
  2598. ++i1) // copy the character body
  2599. {
  2600. inF->read(&byte, 1);
  2601. outF->write(&byte, 1);
  2602. }
  2603. }
  2604. for (int i = 0; i < sgNum; ++i) // update file header
  2605. sgPosList[i] -= 5;
  2606. for (int i = sgNum + 1; i < sgAnzahl; ++i)
  2607. sgPosList[i] -= indexMinus;
  2608. dateiKopf->removeSG(gr);
  2609. outF->seekp(0, std::ios::beg);
  2610. dateiKopf->speichern(outF); // save file header
  2611. inF->close();
  2612. outF->close();
  2613. Text* pfad2 = new Text(pfad->getText());
  2614. pfad->remove(pfad->getLength() - 1, pfad->getLength());
  2615. DateiRemove(dynamic_cast<Text*>(pfad->getThis())); // delete old file
  2616. DateiUmbenennen(dynamic_cast<Text*>(pfad2->getThis()),
  2617. dynamic_cast<Text*>(pfad->getThis())); // rename new file to old
  2618. pfad2->release();
  2619. delete inF;
  2620. delete outF;
  2621. }
  2622. void LTDSDatei::loescheBuchstabe(
  2623. int gr, unsigned char zeichen) // Deletes a character from the file
  2624. {
  2625. if (!DateiExistiert(dynamic_cast<Text*>(
  2626. pfad->getThis()))) // check if the file exists
  2627. return;
  2628. if (!dateiKopf) // check if the file header was read
  2629. leseDaten();
  2630. unsigned char* sgList
  2631. = dateiKopf->getSchriftGroesseList(); // list of font sizes
  2632. unsigned char sgAnzahl
  2633. = dateiKopf->getSchriftGroesseAnzahl(); // number of font sizes
  2634. int* sgPosList
  2635. = dateiKopf
  2636. ->getPositionList(); // list of font size positions
  2637. int sgNum = -1;
  2638. for (int i = 0; i < sgAnzahl; ++i) // search for font size
  2639. {
  2640. if (sgList[i] == gr)
  2641. {
  2642. sgNum = i;
  2643. break;
  2644. }
  2645. }
  2646. if (sgNum == -1) // font size not found
  2647. return;
  2648. std::ifstream* inF
  2649. = new std::ifstream(pfad->getText(), std::ios::binary); // old file
  2650. pfad->append("0");
  2651. std::ofstream* outF
  2652. = new std::ofstream(pfad->getText(), std::ios::binary); // new file
  2653. int indexMinus = 0;
  2654. inF->seekg(1 + 5 * sgAnzahl, std::ios::beg);
  2655. dateiKopf->speichern(outF); // save file header
  2656. for (int i = 0; i < sgAnzahl; ++i)
  2657. {
  2658. LTDSSchriftKopf* sgKopf = new LTDSSchriftKopf(); // font header
  2659. sgKopf->laden(inF); // load font header
  2660. unsigned char sgZeichAnzahl
  2661. = sgKopf->getZeichenAnzahl(); // character count
  2662. unsigned char* sgZeichenList = sgKopf->getZeichen(); // character list
  2663. int* sgZPosList = sgKopf->getPositionen(); // position list
  2664. if (i == sgNum) // character is in this font size
  2665. {
  2666. int sgZNum = -1;
  2667. for (int i1 = 0; i1 < sgZeichAnzahl; ++i1) // search for character
  2668. {
  2669. if (sgZeichenList[i1] == zeichen)
  2670. {
  2671. sgZNum = i1;
  2672. break;
  2673. }
  2674. }
  2675. if (sgZNum == -1) // character not found
  2676. {
  2677. sgKopf->release();
  2678. inF->close();
  2679. outF->close();
  2680. delete inF;
  2681. delete outF;
  2682. DateiRemove(dynamic_cast<Text*>(pfad->getThis()));
  2683. pfad->remove(pfad->getLength() - 1, pfad->getLength());
  2684. return; // abort
  2685. }
  2686. outF->seekp(2 + 5 * (sgZeichAnzahl - 1), std::ios::cur);
  2687. indexMinus += 5;
  2688. for (int i1 = 0; i1 < sgZNum;
  2689. ++i1) // characters before the one being deleted
  2690. {
  2691. char byte = 0;
  2692. for (int i2 = sgZPosList[i1]; i2 < sgZPosList[i1 + 1];
  2693. ++i2) // copy
  2694. {
  2695. inF->read(&byte, 1);
  2696. outF->write(&byte, 1);
  2697. }
  2698. sgZPosList[i1]
  2699. -= indexMinus; // update font size header
  2700. }
  2701. if (!sgZPosList[sgZNum + 1])
  2702. {
  2703. int endByte = sgPosList[i + 1];
  2704. if (!endByte)
  2705. {
  2706. int beginByte = (int)inF->tellg();
  2707. inF->seekg(0, std::ios::end);
  2708. endByte = (int)inF->tellg();
  2709. inF->seekg(beginByte, std::ios::beg);
  2710. }
  2711. indexMinus += endByte - sgZPosList[sgZNum];
  2712. }
  2713. else
  2714. indexMinus += sgZPosList[sgZNum + 1] - sgZPosList[sgZNum];
  2715. if (sgZNum + 1 < sgZeichAnzahl)
  2716. inF->seekg(sgZPosList[sgZNum + 1], std::ios::beg);
  2717. for (int i1 = sgZNum + 1; i1 < sgZeichAnzahl;
  2718. ++i1) // characters after the deleted character
  2719. {
  2720. int beginByte = (int)inF->tellg();
  2721. int endByte = sgZPosList[i1 + 1];
  2722. if (!endByte)
  2723. {
  2724. inF->seekg(0, std::ios::end);
  2725. endByte = (int)inF->tellg();
  2726. inF->seekg(beginByte, std::ios::beg);
  2727. }
  2728. char byte = 0;
  2729. for (int i2 = beginByte; i2 < endByte; ++i2) // copy
  2730. {
  2731. inF->read(&byte, 1);
  2732. outF->write(&byte, 1);
  2733. }
  2734. sgZPosList[i1]
  2735. -= indexMinus; // update font size header
  2736. }
  2737. sgKopf->removeZeichen(zeichen);
  2738. }
  2739. else
  2740. {
  2741. for (int i1 = 0; i1 < sgZeichAnzahl;
  2742. ++i1) // update font size header
  2743. sgZPosList[i] -= indexMinus;
  2744. sgKopf->speichern(outF); // save font size header
  2745. int beginByte = (int)inF->tellg();
  2746. int endByte = sgPosList[i + 1];
  2747. if (!endByte)
  2748. {
  2749. inF->seekg(0, std::ios::end);
  2750. endByte = (int)inF->tellg();
  2751. inF->seekg(beginByte, std::ios::beg);
  2752. }
  2753. char byte;
  2754. for (int i1 = beginByte; i1 < endByte; ++i1) // copy
  2755. {
  2756. inF->read(&byte, 1);
  2757. outF->write(&byte, 1);
  2758. }
  2759. sgPosList[i] -= indexMinus;
  2760. }
  2761. outF->seekp(sgPosList[i], std::ios::beg);
  2762. sgKopf->speichern(outF); // save font size header
  2763. outF->seekp(sgPosList[i + 1], std::ios::beg);
  2764. sgKopf->release();
  2765. }
  2766. inF->close();
  2767. outF->close();
  2768. Text* pfad2 = new Text(pfad->getText());
  2769. pfad->remove(pfad->getLength() - 1, pfad->getLength());
  2770. DateiRemove(dynamic_cast<Text*>(pfad->getThis())); // delete old file
  2771. DateiUmbenennen(dynamic_cast<Text*>(pfad2->getThis()),
  2772. dynamic_cast<Text*>(
  2773. pfad->getThis())); // rename new file to old
  2774. pfad2->release(); // free memory
  2775. delete inF;
  2776. delete outF;
  2777. }
  2778. void LTDSDatei::loescheDatei() // Deletes the entire file
  2779. {
  2780. if (!DateiExistiert(dynamic_cast<Text*>(pfad->getThis()))) return;
  2781. if (!dateiKopf) leseDaten();
  2782. DateiRemove(dynamic_cast<Text*>(pfad->getThis()));
  2783. }
  2784. void LTDSDatei::erstelleDatei() // creates the file
  2785. {
  2786. DateiPfadErstellen(dynamic_cast<Text*>(pfad->getThis()));
  2787. if (dateiKopf) dateiKopf->release();
  2788. dateiKopf = new LTDSDateiKopf();
  2789. std::ofstream* outF = new std::ofstream(pfad->getText(), std::ios::binary);
  2790. dateiKopf->speichern(outF);
  2791. outF->close();
  2792. delete outF;
  2793. }
  2794. void LTDSDatei::speicherSchrift(
  2795. Schrift* schrift) // Saves the provided font
  2796. {
  2797. if (!DateiExistiert(dynamic_cast<Text*>(pfad->getThis())))
  2798. {
  2799. schrift->release();
  2800. return;
  2801. }
  2802. loescheDatei();
  2803. if (dateiKopf) dateiKopf->release();
  2804. dateiKopf = new LTDSDateiKopf();
  2805. for (int i = 0; i < 256; ++i)
  2806. {
  2807. Alphabet* alp = schrift->getAlphabet((unsigned char)i);
  2808. if (alp)
  2809. {
  2810. dateiKopf->addSG((char)alp->getSchriftSize());
  2811. alp->release();
  2812. }
  2813. }
  2814. std::ofstream* outF = new std::ofstream(pfad->getText(), std::ios::binary);
  2815. dateiKopf->speichern(outF);
  2816. for (int i = 0; i < 256; ++i)
  2817. {
  2818. Alphabet* alp = schrift->getAlphabet((unsigned char)i);
  2819. if (alp)
  2820. {
  2821. dateiKopf->getPositionList()[i] = (int)outF->tellp();
  2822. LTDSSchriftKopf* sgKpf = new LTDSSchriftKopf();
  2823. sgKpf->setZeichenAlphabet(dynamic_cast<Alphabet*>(alp->getThis()));
  2824. sgKpf->speichern(outF);
  2825. for (int i1 = 0; i1 < sgKpf->getZeichenAnzahl(); ++i1)
  2826. {
  2827. sgKpf->getPositionen()[i1] = (int)outF->tellp();
  2828. LTDSBuchstabenKopf* zeichKpf = new LTDSBuchstabenKopf();
  2829. Buchstabe* zeichen = alp->getBuchstabe(sgKpf->getZeichen()[i1]);
  2830. zeichKpf->init(sgKpf->getZeichen()[i1],
  2831. zeichen->getBreite(),
  2832. zeichen->getHeight());
  2833. zeichKpf->speichern(outF);
  2834. LTDSBuchstabenKoerper* zeichKoerp = new LTDSBuchstabenKoerper(
  2835. dynamic_cast<LTDSBuchstabenKopf*>(zeichKpf->getThis()));
  2836. zeichKoerp->setBuchstabe(
  2837. dynamic_cast<Buchstabe*>(zeichen->getThis()));
  2838. zeichKoerp->speichern(outF);
  2839. zeichKoerp->release();
  2840. zeichen->release();
  2841. zeichKpf->release();
  2842. }
  2843. alp->release();
  2844. int p = (int)outF->tellp();
  2845. outF->seekp(dateiKopf->getPositionList()[i], std::ios::beg);
  2846. sgKpf->speichern(outF);
  2847. outF->seekp(p, std::ios::beg);
  2848. sgKpf->release();
  2849. }
  2850. }
  2851. outF->seekp(0, std::ios::beg);
  2852. dateiKopf->speichern(outF);
  2853. outF->close();
  2854. delete outF;
  2855. schrift->release();
  2856. }
  2857. // constant
  2858. Schrift* LTDSDatei::ladeSchrift() // returns the loaded font
  2859. {
  2860. if (!DateiExistiert(dynamic_cast<Text*>(pfad->getThis()))) return 0;
  2861. if (!dateiKopf) return 0;
  2862. Schrift* ret = new Schrift();
  2863. std::ifstream* inF = new std::ifstream(pfad->getText(), std::ios::binary);
  2864. inF->seekg(dateiKopf->getPositionList()[0], std::ios::beg);
  2865. for (int i = 0; i < dateiKopf->getSchriftGroesseAnzahl(); ++i)
  2866. {
  2867. LTDSSchriftKopf* sgKpf = new LTDSSchriftKopf();
  2868. sgKpf->laden(inF);
  2869. Alphabet* alphabet = new Alphabet();
  2870. alphabet->setSchriftSize(sgKpf->getSchriftGroesse());
  2871. for (int i1 = 0; i1 < sgKpf->getZeichenAnzahl(); ++i1)
  2872. {
  2873. LTDSBuchstabenKopf* zeichKpf = new LTDSBuchstabenKopf();
  2874. zeichKpf->laden(inF);
  2875. LTDSBuchstabenKoerper* zeichKoerp = new LTDSBuchstabenKoerper(
  2876. dynamic_cast<LTDSBuchstabenKopf*>(zeichKpf->getThis()));
  2877. zeichKoerp->laden(inF);
  2878. alphabet->setBuchstabe(
  2879. zeichKpf->getZeichen(), zeichKoerp->getBuchstabe());
  2880. zeichKoerp->release();
  2881. zeichKpf->release();
  2882. }
  2883. ret->addAlphabet(dynamic_cast<Alphabet*>(alphabet->getThis()));
  2884. alphabet->release();
  2885. sgKpf->release();
  2886. }
  2887. inF->close();
  2888. delete inF;
  2889. return ret;
  2890. }
  2891. Alphabet* LTDSDatei::ladeAlphabet(
  2892. int schriftgroesse) // returns a loaded font with only the specified
  2893. // font size
  2894. {
  2895. if (!DateiExistiert(dynamic_cast<Text*>(pfad->getThis()))) return 0;
  2896. if (!dateiKopf) return 0;
  2897. Alphabet* ret = 0;
  2898. int sgNum = -1;
  2899. for (int i = 0; i < dateiKopf->getSchriftGroesseAnzahl(); ++i)
  2900. {
  2901. if (dateiKopf->getSchriftGroesseList()[i] == schriftgroesse)
  2902. {
  2903. sgNum = i;
  2904. break;
  2905. }
  2906. }
  2907. if (sgNum == -1) return 0;
  2908. ret = new Alphabet();
  2909. ret->NeuAlphabet();
  2910. ret->setSchriftSize(schriftgroesse);
  2911. std::ifstream* inF = new std::ifstream(pfad->getText(), std::ios::binary);
  2912. inF->seekg(dateiKopf->getPositionList()[sgNum], std::ios::beg);
  2913. LTDSSchriftKopf* sgKpf = new LTDSSchriftKopf();
  2914. sgKpf->laden(inF);
  2915. for (int i = 0; i < sgKpf->getZeichenAnzahl(); ++i)
  2916. {
  2917. LTDSBuchstabenKopf* sgZKpf = new LTDSBuchstabenKopf();
  2918. sgZKpf->laden(inF);
  2919. LTDSBuchstabenKoerper* sgZKoerp = new LTDSBuchstabenKoerper(
  2920. dynamic_cast<LTDSBuchstabenKopf*>(sgZKpf->getThis()));
  2921. sgZKoerp->laden(inF);
  2922. ret->setBuchstabe(sgZKpf->getZeichen(), sgZKoerp->getBuchstabe());
  2923. sgZKoerp->release();
  2924. sgZKpf->release();
  2925. }
  2926. sgKpf->release();
  2927. inF->close();
  2928. delete inF;
  2929. return ret;
  2930. }
  2931. Buchstabe* LTDSDatei::ladeBuchstabe(int schriftgroesse,
  2932. unsigned char zeichen) // Loads a specific character
  2933. {
  2934. if (!DateiExistiert(dynamic_cast<Text*>(pfad->getThis()))) return 0;
  2935. if (!dateiKopf) return 0;
  2936. Buchstabe* ret = 0;
  2937. int sgNum = -1;
  2938. for (int i = 0; i < dateiKopf->getSchriftGroesseAnzahl(); ++i)
  2939. {
  2940. if (dateiKopf->getSchriftGroesseList()[i] == schriftgroesse)
  2941. {
  2942. sgNum = i;
  2943. break;
  2944. }
  2945. }
  2946. if (sgNum == -1) return 0;
  2947. std::ifstream* inF = new std::ifstream(pfad->getText(), std::ios::binary);
  2948. inF->seekg(dateiKopf->getPositionList()[sgNum], std::ios::beg);
  2949. LTDSSchriftKopf* sgKpf = new LTDSSchriftKopf();
  2950. sgKpf->laden(inF);
  2951. int sgZNum = -1;
  2952. for (int i = 0; i < sgKpf->getZeichenAnzahl(); ++i)
  2953. {
  2954. if (sgKpf->getZeichen()[i] == zeichen)
  2955. {
  2956. sgZNum = i;
  2957. break;
  2958. }
  2959. }
  2960. if (sgZNum != -1)
  2961. {
  2962. inF->seekg(sgKpf->getPositionen()[sgZNum], std::ios::beg);
  2963. LTDSBuchstabenKopf* sgZKpf = new LTDSBuchstabenKopf();
  2964. sgZKpf->laden(inF);
  2965. LTDSBuchstabenKoerper* sgZKoerp = new LTDSBuchstabenKoerper(
  2966. dynamic_cast<LTDSBuchstabenKopf*>(sgZKpf->getThis()));
  2967. sgZKoerp->laden(inF);
  2968. ret = sgZKoerp->getBuchstabe();
  2969. sgZKoerp->release();
  2970. sgZKpf->release();
  2971. }
  2972. sgKpf->release();
  2973. inF->close();
  2974. delete inF;
  2975. if (ret)
  2976. {
  2977. ret->setSchriftSize(schriftgroesse);
  2978. }
  2979. return ret;
  2980. }
  2981. Text* LTDSDatei::getPfad() const // returns the file path
  2982. {
  2983. return dynamic_cast<Text*>(pfad->getThis());
  2984. }
  2985. int LTDSDatei::getAnzahlSchriftgroessen()
  2986. const // returns the number of font sizes from the file
  2987. {
  2988. if (!DateiExistiert(dynamic_cast<Text*>(pfad->getThis()))) return 0;
  2989. if (!dateiKopf) return 0;
  2990. return dateiKopf->getSchriftGroesseAnzahl();
  2991. }
  2992. unsigned char* LTDSDatei::getSchriftGroessen()
  2993. const // returns an array of font sizes
  2994. {
  2995. if (!DateiExistiert(dynamic_cast<Text*>(pfad->getThis()))) return 0;
  2996. if (!dateiKopf) return 0;
  2997. return dateiKopf->getSchriftGroesseList();
  2998. }
  2999. unsigned char LTDSDatei::getAnzahlBuchstaben(
  3000. int sg) // returns the number of stored characters for a font size
  3001. {
  3002. if (!DateiExistiert(dynamic_cast<Text*>(pfad->getThis()))) return 0;
  3003. if (!dateiKopf) return 0;
  3004. int ret = 0;
  3005. unsigned char* groessen = dateiKopf->getSchriftGroesseList();
  3006. unsigned char granzahl = dateiKopf->getSchriftGroesseAnzahl();
  3007. int grpos = -1;
  3008. for (int i = 0; i < granzahl; ++i)
  3009. {
  3010. if (groessen[i] == sg)
  3011. {
  3012. grpos = i;
  3013. break;
  3014. }
  3015. }
  3016. if (grpos != -1)
  3017. {
  3018. int* grposlist = dateiKopf->getPositionList();
  3019. LTDSSchriftKopf* sgkpf = new LTDSSchriftKopf();
  3020. std::ifstream* inF
  3021. = new std::ifstream(pfad->getText(), std::ios::binary);
  3022. inF->seekg(grposlist[grpos], std::ios::beg);
  3023. sgkpf->laden(inF);
  3024. ret = sgkpf->getZeichenAnzahl();
  3025. sgkpf->release();
  3026. inF->close();
  3027. delete inF;
  3028. }
  3029. return ret;
  3030. }
  3031. unsigned char* LTDSDatei::getBuchstaben(
  3032. int sg) // returns an array of characters for a font size
  3033. {
  3034. if (!DateiExistiert(dynamic_cast<Text*>(pfad->getThis()))) return 0;
  3035. if (!dateiKopf) return 0;
  3036. unsigned char* ret = 0;
  3037. unsigned char* groessen = dateiKopf->getSchriftGroesseList();
  3038. unsigned char granzahl = dateiKopf->getSchriftGroesseAnzahl();
  3039. int grpos = -1;
  3040. for (int i = 0; i < granzahl; ++i)
  3041. {
  3042. if (groessen[i] == sg)
  3043. {
  3044. grpos = i;
  3045. break;
  3046. }
  3047. }
  3048. if (grpos != -1)
  3049. {
  3050. int* grposlist = dateiKopf->getPositionList();
  3051. LTDSSchriftKopf* sgkpf = new LTDSSchriftKopf();
  3052. std::ifstream* inF
  3053. = new std::ifstream(pfad->getText(), std::ios::binary);
  3054. inF->seekg(grposlist[grpos], std::ios::beg);
  3055. sgkpf->laden(inF);
  3056. int anz = sgkpf->getZeichenAnzahl();
  3057. ret = new unsigned char[anz];
  3058. for (int i = 0; i < anz; ++i)
  3059. ret[i] = sgkpf->getZeichen()[i];
  3060. sgkpf->release();
  3061. inF->close();
  3062. delete inF;
  3063. }
  3064. return ret;
  3065. }
  3066. #endif
  3067. // Bit functions
  3068. int Framework::Bits(int a) // returns 1-bits in the desired count
  3069. {
  3070. int ret = 0;
  3071. for (int i = 0; i < a; ++i)
  3072. {
  3073. ret <<= 1;
  3074. ++ret;
  3075. }
  3076. return ret;
  3077. }
  3078. int Framework::getBits(char c) // returns how many bits c requires
  3079. {
  3080. int ret = 0;
  3081. for (int i = 0; (c & (char)Bits(i)) != c; ++i)
  3082. ++ret;
  3083. return ret;
  3084. }